diff --git a/src/middleware/bucket.ts b/src/middleware/bucket.ts index 9e8f6df..283756f 100644 --- a/src/middleware/bucket.ts +++ b/src/middleware/bucket.ts @@ -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 BucketRepository from '../repositories/bucket.ts' +import uuidSerialize from '../serializers/uuid.ts' const router = Router() @@ -16,11 +17,8 @@ const router = Router() */ 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, { bucket_id: [ uuid ] @@ -28,9 +26,7 @@ async function bucketMiddleware(request: any, response: any, next: any) // if invalid send 404 if (!valid) { - response - .setStatus(404) - .send() + response.send(422) } // getting @@ -39,9 +35,7 @@ async function bucketMiddleware(request: any, response: any, next: any) // if not exists send 404 if (!bucket) { - response - .setStatus(404) - .send() + response.send(404) } response.locals.bucket = bucket diff --git a/src/middleware/permissions/admin.ts b/src/middleware/permissions/admin.ts new file mode 100644 index 0000000..c9bc874 --- /dev/null +++ b/src/middleware/permissions/admin.ts @@ -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() +} \ No newline at end of file diff --git a/src/middleware/users.ts b/src/middleware/users.ts index 97bfb4f..b616d9d 100644 --- a/src/middleware/users.ts +++ b/src/middleware/users.ts @@ -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 UserRepository from '../repositories/user.ts' +import uuidSerialize from '../serializers/uuid.ts' const router = Router() @@ -13,25 +14,20 @@ const router = Router() * @param response * @param next * @return - * + * */ 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, { id: [ uuid ] }) - // if invalid send 404 + // if invalid send 422 if (!valid) { - response - .setStatus(404) - .send() + response.send(422) } // getting @@ -40,9 +36,7 @@ async function usersMiddleware(request: any, response: any, next: any) // if not exists send 404 if (!user) { - response - .setStatus(404) - .send() + response.send(404) } response.locals.user = user diff --git a/src/repositories/bucket.ts b/src/repositories/bucket.ts index a703960..7e84c21 100644 --- a/src/repositories/bucket.ts +++ b/src/repositories/bucket.ts @@ -8,8 +8,13 @@ import { BucketSchema } from '../stores/bucket.ts' */ class BucketRepository { + /** */ db: any + /** + * + * + */ constructor() { this.db = new Database('./storage/database/buckets.json') @@ -17,7 +22,7 @@ class BucketRepository /** * - * + * */ async create(data: any) { diff --git a/src/repositories/note.ts b/src/repositories/note.ts index 13c4660..a71f1e3 100644 --- a/src/repositories/note.ts +++ b/src/repositories/note.ts @@ -6,10 +6,17 @@ import { BucketSchema } from '../stores/bucket.ts' * * */ -class BucketRepository +class NoteRepository { + /** */ db: any + /** + * + * @param user_id + * @param bucket_id + * + */ constructor(user_id, bucket_id) { this.db = new Database('./storage/database/' + user_id + '/' + bucket_id + '.json') @@ -27,7 +34,7 @@ class BucketRepository /** * - * + * */ async update(data: any) { diff --git a/src/repositories/user.ts b/src/repositories/user.ts index c7225ad..11b04bf 100644 --- a/src/repositories/user.ts +++ b/src/repositories/user.ts @@ -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 { Database } from 'https://deno.land/x/aloedb@0.9.0/mod.ts' -import { UserSchema } from './../stores/user.ts' +import { UserSchema } from './../schemas/user.ts' /** * diff --git a/src/stores/bucket.ts b/src/schemas/bucket.ts similarity index 100% rename from src/stores/bucket.ts rename to src/schemas/bucket.ts diff --git a/src/stores/note.ts b/src/schemas/note.ts similarity index 100% rename from src/stores/note.ts rename to src/schemas/note.ts diff --git a/src/stores/tag.ts b/src/schemas/tag.ts similarity index 100% rename from src/stores/tag.ts rename to src/schemas/tag.ts diff --git a/src/stores/user.ts b/src/schemas/user.ts similarity index 100% rename from src/stores/user.ts rename to src/schemas/user.ts diff --git a/src/serializers/uuid.ts b/src/serializers/uuid.ts new file mode 100644 index 0000000..c93d90b --- /dev/null +++ b/src/serializers/uuid.ts @@ -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 +} \ No newline at end of file diff --git a/src/stores/baseStore.ts b/src/stores/baseStore.ts deleted file mode 100644 index dafcbf6..0000000 --- a/src/stores/baseStore.ts +++ /dev/null @@ -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() - { - - } -} \ No newline at end of file