Browse Source

adding cleaner for uuid

master
HerrHase 3 years ago
parent
commit
792fe6e8e9
12 changed files with 75 additions and 42 deletions
  1. +5
    -11
      src/middleware/bucket.ts
  2. +31
    -0
      src/middleware/permissions/admin.ts
  3. +7
    -13
      src/middleware/users.ts
  4. +6
    -1
      src/repositories/bucket.ts
  5. +9
    -2
      src/repositories/note.ts
  6. +1
    -1
      src/repositories/user.ts
  7. +0
    -0
      src/schemas/bucket.ts
  8. +0
    -0
      src/schemas/note.ts
  9. +0
    -0
      src/schemas/tag.ts
  10. +0
    -0
      src/schemas/user.ts
  11. +16
    -0
      src/serializers/uuid.ts
  12. +0
    -14
      src/stores/baseStore.ts

+ 5
- 11
src/middleware/bucket.ts View File

@ -3,6 +3,7 @@ import { validate, required } from 'https://deno.land/x/validasaur@v0.15.0/mod.t
import { uuid } from '../rules/uuid.ts' import { uuid } from '../rules/uuid.ts'
import BucketRepository from '../repositories/bucket.ts' import BucketRepository from '../repositories/bucket.ts'
import uuidSerialize from '../serializers/uuid.ts'
const router = Router() const router = Router()
@ -16,11 +17,8 @@ const router = Router()
*/ */
async function bucketMiddleware(request: any, response: any, next: any) async function bucketMiddleware(request: any, response: any, next: any)
{ {
// max for id
request.params.bucket_id = request.params.bucket_id.slice(0, 128)
// only dash, numbers & letters are allowed
request.params.bucket_id = request.params.bucket_id.replace(/[^a-z0-9-]/gi, '')
// clean id
request.params.bucket_id = uuidSerialize(request.params.bucket_id)
const [ valid, errors ] = await validate(request.params, { const [ valid, errors ] = await validate(request.params, {
bucket_id: [ uuid ] bucket_id: [ uuid ]
@ -28,9 +26,7 @@ async function bucketMiddleware(request: any, response: any, next: any)
// if invalid send 404 // if invalid send 404
if (!valid) { if (!valid) {
response
.setStatus(404)
.send()
response.send(422)
} }
// getting // getting
@ -39,9 +35,7 @@ async function bucketMiddleware(request: any, response: any, next: any)
// if not exists send 404 // if not exists send 404
if (!bucket) { if (!bucket) {
response
.setStatus(404)
.send()
response.send(404)
} }
response.locals.bucket = bucket response.locals.bucket = bucket


+ 31
- 0
src/middleware/permissions/admin.ts View File

@ -0,0 +1,31 @@
import { Router } from 'https://deno.land/x/opine@1.5.3/mod.ts'
import { validate, required } from 'https://deno.land/x/validasaur@v0.15.0/mod.ts'
import { uuid } from '../rules/uuid.ts'
import UserRepository from '../repositories/user.ts'
const router = Router()
/**
* check route for user if parameter is
*
* @param request
* @param response
* @param next
* @return
*
*/
async function adminAllowedMiddleware(request: any, response: any, next: any)
{
// if no session
if (!response.locals.current) {
response.send(401)
}
// if role is wrong
if (response.locals.current.roles.indexOf('admin') === -1) {
response.send(403)
}
next()
}

+ 7
- 13
src/middleware/users.ts View File

@ -3,6 +3,7 @@ import { validate, required } from 'https://deno.land/x/validasaur@v0.15.0/mod.t
import { uuid } from '../rules/uuid.ts' import { uuid } from '../rules/uuid.ts'
import UserRepository from '../repositories/user.ts' import UserRepository from '../repositories/user.ts'
import uuidSerialize from '../serializers/uuid.ts'
const router = Router() const router = Router()
@ -13,25 +14,20 @@ const router = Router()
* @param response * @param response
* @param next * @param next
* @return * @return
*
*
*/ */
async function usersMiddleware(request: any, response: any, next: any) async function usersMiddleware(request: any, response: any, next: any)
{ {
// max for id
request.params.id = request.params.id.slice(0, 128)
// only dash, numbers & letters are allowed
request.params.id = request.params.id.replace(/[^a-z0-9-]/gi, '')
// clean id
request.params.id = uuidSerialize(request.params.id)
const [ valid, errors ] = await validate(request.params, { const [ valid, errors ] = await validate(request.params, {
id: [ uuid ] id: [ uuid ]
}) })
// if invalid send 404
// if invalid send 422
if (!valid) { if (!valid) {
response
.setStatus(404)
.send()
response.send(422)
} }
// getting // getting
@ -40,9 +36,7 @@ async function usersMiddleware(request: any, response: any, next: any)
// if not exists send 404 // if not exists send 404
if (!user) { if (!user) {
response
.setStatus(404)
.send()
response.send(404)
} }
response.locals.user = user response.locals.user = user


+ 6
- 1
src/repositories/bucket.ts View File

@ -8,8 +8,13 @@ import { BucketSchema } from '../stores/bucket.ts'
*/ */
class BucketRepository class BucketRepository
{ {
/** */
db: any db: any
/**
*
*
*/
constructor() constructor()
{ {
this.db = new Database<BucketSchema>('./storage/database/buckets.json') this.db = new Database<BucketSchema>('./storage/database/buckets.json')
@ -17,7 +22,7 @@ class BucketRepository
/** /**
* *
*
*
*/ */
async create(data: any) async create(data: any)
{ {


+ 9
- 2
src/repositories/note.ts View File

@ -6,10 +6,17 @@ import { BucketSchema } from '../stores/bucket.ts'
* *
* *
*/ */
class BucketRepository
class NoteRepository
{ {
/** */
db: any db: any
/**
*
* @param user_id
* @param bucket_id
*
*/
constructor(user_id, bucket_id) constructor(user_id, bucket_id)
{ {
this.db = new Database<BucketSchema>('./storage/database/' + user_id + '/' + bucket_id + '.json') this.db = new Database<BucketSchema>('./storage/database/' + user_id + '/' + bucket_id + '.json')
@ -27,7 +34,7 @@ class BucketRepository
/** /**
* *
*
*
*/ */
async update(data: any) async update(data: any)
{ {


+ 1
- 1
src/repositories/user.ts View File

@ -2,7 +2,7 @@ import * as bcrypt from 'https://deno.land/x/bcrypt@v0.2.4/mod.ts'
import { v4 } from 'https://deno.land/std@0.99.0/uuid/mod.ts' import { v4 } from 'https://deno.land/std@0.99.0/uuid/mod.ts'
import { Database } from 'https://deno.land/x/aloedb@0.9.0/mod.ts' import { Database } from 'https://deno.land/x/aloedb@0.9.0/mod.ts'
import { UserSchema } from './../stores/user.ts'
import { UserSchema } from './../schemas/user.ts'
/** /**
* *


src/stores/bucket.ts → src/schemas/bucket.ts View File


src/stores/note.ts → src/schemas/note.ts View File


src/stores/tag.ts → src/schemas/tag.ts View File


src/stores/user.ts → src/schemas/user.ts View File


+ 16
- 0
src/serializers/uuid.ts View File

@ -0,0 +1,16 @@
/**
*
* @param value
* @return
*/
function uuidSerialize(value: string)
{
// max for id
value = value.slice(0, 128)
// only dash, numbers & letters are allowed
value = value.replace(/[^a-z0-9-]/gi, '')
return value
}

+ 0
- 14
src/stores/baseStore.ts View File

@ -1,14 +0,0 @@
import { Database } from 'https://deno.land/x/aloedb/mod.ts';
class BaseStore
{
construct()
{
this.db = new Database<('./storage/database/' + this.name + '.json')
}
uuid()
{
}
}

Loading…
Cancel
Save