From da8b76dedc6dbeae4bef5e94809e51d8ecc47cbe Mon Sep 17 00:00:00 2001 From: James Chang Date: Thu, 8 Apr 2021 12:36:21 -0400 Subject: [PATCH] Re-organize backend core helpers --- backend/functions/src/helpers/auth.ts | 2 +- .../src/schema/core/generators/edge.ts | 4 +- .../schema/core/generators/enumPaginator.ts | 2 +- .../src/schema/core/generators/link.ts | 2 +- .../src/schema/core/generators/paginator.ts | 2 +- .../schema/core/generators/paginatorInfo.ts | 4 +- .../src/schema/{ => core}/helpers/enum.ts | 0 .../src/schema/{ => core}/helpers/error.ts | 0 .../src/schema/core/helpers/permissions.ts | 151 ++++++++++++++++++ .../src/schema/{ => core}/helpers/resolver.ts | 2 +- .../schema/{ => core}/helpers/rootResolver.ts | 2 +- .../src/schema/{ => core}/helpers/scalar.ts | 0 .../src/schema/{ => core}/helpers/shared.ts | 2 +- .../src/schema/{ => core}/helpers/sql.ts | 6 +- .../schema/{ => core}/helpers/subscription.ts | 4 +- .../src/schema/{ => core}/helpers/typeDef.ts | 11 +- .../src/schema/core/services/enum.ts | 2 +- .../src/schema/core/services/normal.ts | 12 +- .../src/schema/core/services/paginator.ts | 4 +- .../src/schema/core/services/paginatorInfo.ts | 4 +- .../src/schema/core/services/simple.ts | 4 +- .../functions/src/schema/enums/scoreMethod.ts | 2 +- .../src/schema/enums/userPermission.ts | 2 +- .../functions/src/schema/enums/userRole.ts | 2 +- .../src/schema/helpers/permissions.ts | 150 ----------------- .../links/userUserFollowLink/rootResolver.ts | 2 +- .../links/userUserFollowLink/service.ts | 4 +- .../src/schema/models/auth/service.ts | 6 +- .../src/schema/models/auth/typeDef.ts | 2 +- .../src/schema/models/event/rootResolver.ts | 2 +- .../src/schema/models/event/typeDef.ts | 2 +- .../src/schema/models/github/service.ts | 2 +- .../models/personalBest/rootResolver.ts | 2 +- .../src/schema/models/personalBest/service.ts | 6 +- .../src/schema/models/personalBest/typeDef.ts | 2 +- .../models/personalBestClass/rootResolver.ts | 2 +- .../models/personalBestClass/typeDef.ts | 2 +- .../src/schema/models/product/rootResolver.ts | 2 +- .../src/schema/models/product/typeDef.ts | 2 +- .../src/schema/models/user/rootResolver.ts | 2 +- .../src/schema/models/user/service.ts | 2 +- .../src/schema/models/user/typeDef.ts | 2 +- backend/functions/src/schema/scalars/index.ts | 2 +- 43 files changed, 210 insertions(+), 212 deletions(-) rename backend/functions/src/schema/{ => core}/helpers/enum.ts (100%) rename backend/functions/src/schema/{ => core}/helpers/error.ts (100%) create mode 100644 backend/functions/src/schema/core/helpers/permissions.ts rename backend/functions/src/schema/{ => core}/helpers/resolver.ts (99%) rename backend/functions/src/schema/{ => core}/helpers/rootResolver.ts (99%) rename backend/functions/src/schema/{ => core}/helpers/scalar.ts (100%) rename backend/functions/src/schema/{ => core}/helpers/shared.ts (97%) rename backend/functions/src/schema/{ => core}/helpers/sql.ts (99%) rename backend/functions/src/schema/{ => core}/helpers/subscription.ts (97%) rename backend/functions/src/schema/{ => core}/helpers/typeDef.ts (98%) diff --git a/backend/functions/src/helpers/auth.ts b/backend/functions/src/helpers/auth.ts index fb884dd..66f36a7 100644 --- a/backend/functions/src/helpers/auth.ts +++ b/backend/functions/src/helpers/auth.ts @@ -4,7 +4,7 @@ import { User } from "../schema/services"; import { userRoleKenum, userPermissionEnum } from "../schema/enums"; import { userRoleToPermissionsMap } from "../schema/helpers/permissions"; import type { ContextUser } from "../types"; -import * as sqlHelper from "../schema/helpers/sql"; +import * as sqlHelper from "../schema/core/helpers/sql"; export async function validateToken(auth: string): Promise { if (auth.split(" ")[0] !== "Bearer") { diff --git a/backend/functions/src/schema/core/generators/edge.ts b/backend/functions/src/schema/core/generators/edge.ts index 667e934..c5d5114 100644 --- a/backend/functions/src/schema/core/generators/edge.ts +++ b/backend/functions/src/schema/core/generators/edge.ts @@ -1,8 +1,8 @@ import { BaseService, NormalService } from "../services"; import * as Scalars from "../../scalars"; -import { generateTypenameField } from "../../helpers/typeDef"; -import { atob } from "../../helpers/shared"; +import { generateTypenameField } from "../helpers/typeDef"; +import { atob } from "../helpers/shared"; import type { ObjectTypeDefinition } from "giraffeql"; export function generateEdgeTypeDef( diff --git a/backend/functions/src/schema/core/generators/enumPaginator.ts b/backend/functions/src/schema/core/generators/enumPaginator.ts index 6a94d3e..878604e 100644 --- a/backend/functions/src/schema/core/generators/enumPaginator.ts +++ b/backend/functions/src/schema/core/generators/enumPaginator.ts @@ -1,7 +1,7 @@ import { BaseService, EnumService } from "../services"; import * as Scalars from "../../scalars"; import type { ObjectTypeDefinition } from "giraffeql"; -import { generateTypenameField } from "../../helpers/typeDef"; +import { generateTypenameField } from "../helpers/typeDef"; export function generateEnumPaginatorTypeDef( service: EnumService, diff --git a/backend/functions/src/schema/core/generators/link.ts b/backend/functions/src/schema/core/generators/link.ts index ad0094d..bad0b6f 100644 --- a/backend/functions/src/schema/core/generators/link.ts +++ b/backend/functions/src/schema/core/generators/link.ts @@ -7,7 +7,7 @@ import { generateCreatedByField, generateJoinableField, generateTypenameField, -} from "../../helpers/typeDef"; +} from "../helpers/typeDef"; import { ObjectTypeDefinition, ObjectTypeDefinitionField } from "giraffeql"; type ServicesObjectMap = { diff --git a/backend/functions/src/schema/core/generators/paginator.ts b/backend/functions/src/schema/core/generators/paginator.ts index f75039b..e013e68 100644 --- a/backend/functions/src/schema/core/generators/paginator.ts +++ b/backend/functions/src/schema/core/generators/paginator.ts @@ -5,7 +5,7 @@ import { BaseService, } from "../services"; -import { generateTypenameField } from "../../helpers/typeDef"; +import { generateTypenameField } from "../helpers/typeDef"; import type { ObjectTypeDefinition } from "giraffeql"; import { PaginatorData } from "../../../types"; diff --git a/backend/functions/src/schema/core/generators/paginatorInfo.ts b/backend/functions/src/schema/core/generators/paginatorInfo.ts index a66dd49..d2e48f1 100644 --- a/backend/functions/src/schema/core/generators/paginatorInfo.ts +++ b/backend/functions/src/schema/core/generators/paginatorInfo.ts @@ -1,9 +1,9 @@ import { BaseService, NormalService } from "../services"; import * as Scalars from "../../scalars"; -import { atob } from "../../helpers/shared"; +import { atob } from "../helpers/shared"; import type { ObjectTypeDefinition } from "giraffeql"; -import { generateTypenameField } from "../../helpers/typeDef"; +import { generateTypenameField } from "../helpers/typeDef"; import { PaginatorData } from "../../../types"; export function generatePaginatorInfoTypeDef( diff --git a/backend/functions/src/schema/helpers/enum.ts b/backend/functions/src/schema/core/helpers/enum.ts similarity index 100% rename from backend/functions/src/schema/helpers/enum.ts rename to backend/functions/src/schema/core/helpers/enum.ts diff --git a/backend/functions/src/schema/helpers/error.ts b/backend/functions/src/schema/core/helpers/error.ts similarity index 100% rename from backend/functions/src/schema/helpers/error.ts rename to backend/functions/src/schema/core/helpers/error.ts diff --git a/backend/functions/src/schema/core/helpers/permissions.ts b/backend/functions/src/schema/core/helpers/permissions.ts new file mode 100644 index 0000000..ff3f78c --- /dev/null +++ b/backend/functions/src/schema/core/helpers/permissions.ts @@ -0,0 +1,151 @@ +import { userRoleKenum } from "../../enums"; +import { BaseService, NormalService } from "../services"; +import * as errorHelper from "./error"; +import { ServiceFunctionInputs, AccessControlFunction } from "../../../types"; +import { StringKeyObject } from "giraffeql"; + +export function generateItemCreatedByUserGuard( + service: NormalService +): AccessControlFunction { + return async function ({ req, args, fieldPath }) { + // args should be validated already + const validatedArgs = args; + //check if logged in + if (!req.user) return false; + + try { + const itemRecord = await service.lookupRecord( + [{ field: "createdBy" }], + validatedArgs.item ?? validatedArgs, + fieldPath + ); + + return itemRecord?.createdBy === req.user.id; + } catch (err) { + return false; + } + }; +} + +export function generateUserAdminGuard(): AccessControlFunction { + return generateUserRoleGuard([userRoleKenum.ADMIN]); +} + +export function generateUserRoleGuard( + allowedRoles: userRoleKenum[] +): AccessControlFunction { + return async function ({ req }) { + //check if logged in + if (!req.user) return false; + + try { + // role is loaded in helpers/auth on token decode + /* + const userRecords = await sqlHelper.fetchTableRows({ + select: [{ field: "role" }], + from: User.typename, + where: { + fields: [{ field: "id", value: req.user.id }], + }, + }); + */ + + if (!req.user.role) return false; + return allowedRoles.includes(req.user.role); + } catch (err) { + return false; + } + }; +} + +/* +export function userRoleGuard(allowedRoles: userRoleKenum[]) { + return function ( + target: BaseService, + propertyName: string, + propertyDescriptor: PropertyDescriptor + ): PropertyDescriptor { + // target === Employee.prototype + // propertyName === "greet" + // propertyDesciptor === Object.getOwnPropertyDescriptor(Employee.prototype, "greet") + const method = propertyDescriptor.value; + + propertyDescriptor.value = async function (req, args, query) { + // convert list of greet arguments to string + //const params = args.map((a) => JSON.stringify(a)).join(); + const params = "bar"; + //if it does not pass the access control, throw an error + if (!(await target.testPermissions("get", req, args, query))) { + throw errorHelper.badPermissionsError(); + } + + // invoke greet() and get its return value + const result = await method.apply(this, [req, args, query]); + + // convert result to string + const r = JSON.stringify(result); + + // display in console the function call details + console.log(`Call: ${propertyName}(${params}) => ${r}`); + + // return the result of invoking the method + return result; + }; + return propertyDescriptor; + }; +} +*/ + +export function permissionsCheck(methodKey: string) { + return function ( + target: BaseService, + propertyName: string, + propertyDescriptor: PropertyDescriptor + ): PropertyDescriptor { + // target === Employee.prototype + // propertyName === "greet" + // propertyDesciptor === Object.getOwnPropertyDescriptor(Employee.prototype, "greet") + const method = propertyDescriptor.value; + + propertyDescriptor.value = async function ({ + req, + fieldPath, + args, + query, + data, + isAdmin = false, + }: ServiceFunctionInputs) { + //if it does not pass the access control, throw an error + if ( + !(await target.testPermissions.apply(this, [ + methodKey, + { + req, + fieldPath, + args, + query, + data, + isAdmin, + }, + ])) + ) { + throw errorHelper.badPermissionsError(fieldPath); + } + // invoke greet() and get its return value + const result = await method.apply(this, [ + { + req, + fieldPath, + args, + query, + data, + isAdmin, + }, + ]); + + // return the result of invoking the method + return result; + }; + return propertyDescriptor; + }; +} diff --git a/backend/functions/src/schema/helpers/resolver.ts b/backend/functions/src/schema/core/helpers/resolver.ts similarity index 99% rename from backend/functions/src/schema/helpers/resolver.ts rename to backend/functions/src/schema/core/helpers/resolver.ts index 115cfec..a5c29e4 100644 --- a/backend/functions/src/schema/helpers/resolver.ts +++ b/backend/functions/src/schema/core/helpers/resolver.ts @@ -21,7 +21,7 @@ import { SqlSelectQueryObject, SqlWhereObject, } from "./sql"; -import { CustomResolverFunction } from "../../types"; +import { CustomResolverFunction } from "../../../types"; import { isObject } from "../helpers/shared"; import type { Request } from "express"; diff --git a/backend/functions/src/schema/helpers/rootResolver.ts b/backend/functions/src/schema/core/helpers/rootResolver.ts similarity index 99% rename from backend/functions/src/schema/helpers/rootResolver.ts rename to backend/functions/src/schema/core/helpers/rootResolver.ts index 80591cc..4780dfd 100644 --- a/backend/functions/src/schema/helpers/rootResolver.ts +++ b/backend/functions/src/schema/core/helpers/rootResolver.ts @@ -9,7 +9,7 @@ import { GiraffeqlObjectType, GiraffeqlInputFieldType, } from "giraffeql"; -import { NormalService, PaginatedService, EnumService } from "../core/services"; +import { NormalService, PaginatedService, EnumService } from "../services"; import { generatePaginatorPivotResolverObject } from "../helpers/typeDef"; import { capitalizeString, isObject } from "../helpers/shared"; type BaseRootResolverTypes = diff --git a/backend/functions/src/schema/helpers/scalar.ts b/backend/functions/src/schema/core/helpers/scalar.ts similarity index 100% rename from backend/functions/src/schema/helpers/scalar.ts rename to backend/functions/src/schema/core/helpers/scalar.ts diff --git a/backend/functions/src/schema/helpers/shared.ts b/backend/functions/src/schema/core/helpers/shared.ts similarity index 97% rename from backend/functions/src/schema/helpers/shared.ts rename to backend/functions/src/schema/core/helpers/shared.ts index eb69f33..f814e4a 100644 --- a/backend/functions/src/schema/helpers/shared.ts +++ b/backend/functions/src/schema/core/helpers/shared.ts @@ -1,4 +1,4 @@ -import { StringKeyObject } from "../../types"; +import { StringKeyObject } from "../../../types"; export function getUnixTimestamp(): number { return new Date().getTime(); } diff --git a/backend/functions/src/schema/helpers/sql.ts b/backend/functions/src/schema/core/helpers/sql.ts similarity index 99% rename from backend/functions/src/schema/helpers/sql.ts rename to backend/functions/src/schema/core/helpers/sql.ts index 00663dd..a7b768a 100644 --- a/backend/functions/src/schema/helpers/sql.ts +++ b/backend/functions/src/schema/core/helpers/sql.ts @@ -8,9 +8,9 @@ import { objectTypeDefs, } from "giraffeql"; import Knex = require("knex"); -import { isDev } from "../../config"; -import { executeDBQuery, knex } from "../../utils/knex"; -import { linkDefs } from "../links"; +import { isDev } from "../../../config"; +import { executeDBQuery, knex } from "../../../utils/knex"; +import { linkDefs } from "../../links"; type FieldInfo = { alias: string; diff --git a/backend/functions/src/schema/helpers/subscription.ts b/backend/functions/src/schema/core/helpers/subscription.ts similarity index 97% rename from backend/functions/src/schema/helpers/subscription.ts rename to backend/functions/src/schema/core/helpers/subscription.ts index 0f3d436..4553b50 100644 --- a/backend/functions/src/schema/helpers/subscription.ts +++ b/backend/functions/src/schema/core/helpers/subscription.ts @@ -1,5 +1,5 @@ -import { getPusher } from "../../utils/pusher"; -import { NormalService } from "../core/services"; +import { getPusher } from "../../../utils/pusher"; +import { NormalService } from "../services"; import * as randomstring from "randomstring"; diff --git a/backend/functions/src/schema/helpers/typeDef.ts b/backend/functions/src/schema/core/helpers/typeDef.ts similarity index 98% rename from backend/functions/src/schema/helpers/typeDef.ts rename to backend/functions/src/schema/core/helpers/typeDef.ts index 3d1c369..fe1decd 100644 --- a/backend/functions/src/schema/helpers/typeDef.ts +++ b/backend/functions/src/schema/core/helpers/typeDef.ts @@ -14,16 +14,13 @@ import { GiraffeqlInputFieldType, ArrayOptions, inputTypeDefs, - ObjectTypeDefinition, } from "giraffeql"; -import { knex } from "../../utils/knex"; +import { knex } from "../../../utils/knex"; import * as Resolver from "./resolver"; import { deepAssign, isObject, snakeToCamel } from "./shared"; -import { BaseService, NormalService, PaginatedService } from "../core/services"; -import { linkDefs } from "../links"; -import * as Scalars from "../scalars"; -import type { ObjectTypeDefSqlOptions, SqlType } from "../../types"; -import { FieldObject } from "../core/services/normal"; +import { BaseService, NormalService, PaginatedService } from "../services"; +import * as Scalars from "../../scalars"; +import type { ObjectTypeDefSqlOptions, SqlType } from "../../../types"; type GenerateFieldParams = { name?: string; diff --git a/backend/functions/src/schema/core/services/enum.ts b/backend/functions/src/schema/core/services/enum.ts index 54557ae..3ed3db7 100644 --- a/backend/functions/src/schema/core/services/enum.ts +++ b/backend/functions/src/schema/core/services/enum.ts @@ -1,6 +1,6 @@ import { BaseService, EnumPaginatorService } from "."; import { ServiceFunctionInputs } from "../../../types"; -import { generateEnumRootResolver } from "../../helpers/rootResolver"; +import { generateEnumRootResolver } from "../helpers/rootResolver"; import { GiraffeqlRootResolverType } from "giraffeql"; export class EnumService extends BaseService { diff --git a/backend/functions/src/schema/core/services/normal.ts b/backend/functions/src/schema/core/services/normal.ts index da68c1f..7391c80 100644 --- a/backend/functions/src/schema/core/services/normal.ts +++ b/backend/functions/src/schema/core/services/normal.ts @@ -1,4 +1,4 @@ -import * as errorHelper from "../../helpers/error"; +import * as errorHelper from "../helpers/error"; import { BaseService } from "."; import { fetchTableRows, @@ -6,11 +6,11 @@ import { SqlSelectQueryObject, SqlWhereFieldOperator, SqlWhereObject, -} from "../../helpers/sql"; -import { permissionsCheck } from "../../helpers/permissions"; -import { handleJqlSubscription } from "../../helpers/subscription"; +} from "../helpers/sql"; +import { permissionsCheck } from "../helpers/permissions"; +import { handleJqlSubscription } from "../helpers/subscription"; -import * as Resolver from "../../helpers/resolver"; +import * as Resolver from "../helpers/resolver"; import { GiraffeqlObjectType, @@ -28,7 +28,7 @@ import { import { ServiceFunctionInputs } from "../../../types"; -import { btoa, isObject } from "../../helpers/shared"; +import { btoa, isObject } from "../helpers/shared"; export type FieldObject = { field?: string; diff --git a/backend/functions/src/schema/core/services/paginator.ts b/backend/functions/src/schema/core/services/paginator.ts index 5b4694f..4b32907 100644 --- a/backend/functions/src/schema/core/services/paginator.ts +++ b/backend/functions/src/schema/core/services/paginator.ts @@ -1,7 +1,7 @@ import { SimpleService, PaginatedService } from "."; -import * as Resolver from "../../helpers/resolver"; -import { itemNotFoundError } from "../../helpers/error"; +import * as Resolver from "../helpers/resolver"; +import { itemNotFoundError } from "../helpers/error"; import { generatePaginatorTypeDef } from "../generators"; import { PaginatorData, ServiceFunctionInputs } from "../../../types"; diff --git a/backend/functions/src/schema/core/services/paginatorInfo.ts b/backend/functions/src/schema/core/services/paginatorInfo.ts index 6dfda8a..942c879 100644 --- a/backend/functions/src/schema/core/services/paginatorInfo.ts +++ b/backend/functions/src/schema/core/services/paginatorInfo.ts @@ -1,6 +1,6 @@ import { NormalService, SimpleService } from "."; -import * as Resolver from "../../helpers/resolver"; -import { itemNotFoundError } from "../../helpers/error"; +import * as Resolver from "../helpers/resolver"; +import { itemNotFoundError } from "../helpers/error"; import { generatePaginatorInfoTypeDef } from "../generators"; import { ServiceFunctionInputs } from "../../../types"; import { lookupSymbol, GiraffeqlObjectType } from "giraffeql"; diff --git a/backend/functions/src/schema/core/services/simple.ts b/backend/functions/src/schema/core/services/simple.ts index 6743a56..582f927 100644 --- a/backend/functions/src/schema/core/services/simple.ts +++ b/backend/functions/src/schema/core/services/simple.ts @@ -1,7 +1,7 @@ import { BaseService } from "."; import { GiraffeqlObjectType } from "giraffeql"; -import * as Resolver from "../../helpers/resolver"; -import * as errorHelper from "../../helpers/error"; +import * as Resolver from "../helpers/resolver"; +import * as errorHelper from "../helpers/error"; import { ServiceFunctionInputs } from "../../../types"; export class SimpleService extends BaseService { diff --git a/backend/functions/src/schema/enums/scoreMethod.ts b/backend/functions/src/schema/enums/scoreMethod.ts index 11a58bc..1e3e0f3 100644 --- a/backend/functions/src/schema/enums/scoreMethod.ts +++ b/backend/functions/src/schema/enums/scoreMethod.ts @@ -1,4 +1,4 @@ -import { Enum } from "../helpers/enum"; +import { Enum } from "../core/helpers/enum"; export class scoreMethodEnum extends Enum { static readonly STANDARD = new scoreMethodEnum("STANDARD"); diff --git a/backend/functions/src/schema/enums/userPermission.ts b/backend/functions/src/schema/enums/userPermission.ts index 104da73..5380f50 100644 --- a/backend/functions/src/schema/enums/userPermission.ts +++ b/backend/functions/src/schema/enums/userPermission.ts @@ -1,4 +1,4 @@ -import { Enum } from "../helpers/enum"; +import { Enum } from "../core/helpers/enum"; export class userPermissionEnum extends Enum { static readonly A_A = new userPermissionEnum("A_A"); diff --git a/backend/functions/src/schema/enums/userRole.ts b/backend/functions/src/schema/enums/userRole.ts index 35a4fed..9ac1ae9 100644 --- a/backend/functions/src/schema/enums/userRole.ts +++ b/backend/functions/src/schema/enums/userRole.ts @@ -1,4 +1,4 @@ -import { Kenum } from "../helpers/enum"; +import { Kenum } from "../core/helpers/enum"; export class userRoleKenum extends Kenum { static readonly NORMAL = new userRoleKenum("NORMAL", 1); diff --git a/backend/functions/src/schema/helpers/permissions.ts b/backend/functions/src/schema/helpers/permissions.ts index b174a25..5cb22b2 100644 --- a/backend/functions/src/schema/helpers/permissions.ts +++ b/backend/functions/src/schema/helpers/permissions.ts @@ -1,8 +1,4 @@ import { userRoleKenum, userPermissionEnum } from "../enums"; -import { BaseService, NormalService } from "../core/services"; -import * as errorHelper from "./error"; -import { ServiceFunctionInputs, AccessControlFunction } from "../../types"; -import { StringKeyObject } from "giraffeql"; export const userRoleToPermissionsMap = { [userRoleKenum.ADMIN.name]: [userPermissionEnum.A_A], @@ -12,149 +8,3 @@ export const userRoleToPermissionsMap = { userPermissionEnum.userUserFollowLink_get, ], }; - -export function generateItemCreatedByUserGuard( - service: NormalService -): AccessControlFunction { - return async function ({ req, args, fieldPath }) { - // args should be validated already - const validatedArgs = args; - //check if logged in - if (!req.user) return false; - - try { - const itemRecord = await service.lookupRecord( - [{ field: "createdBy" }], - validatedArgs.item ?? validatedArgs, - fieldPath - ); - - return itemRecord?.createdBy === req.user.id; - } catch (err) { - return false; - } - }; -} - -export function generateUserAdminGuard(): AccessControlFunction { - return generateUserRoleGuard([userRoleKenum.ADMIN]); -} - -export function generateUserRoleGuard( - allowedRoles: userRoleKenum[] -): AccessControlFunction { - return async function ({ req }) { - //check if logged in - if (!req.user) return false; - - try { - // role is loaded in helpers/auth on token decode - /* - const userRecords = await sqlHelper.fetchTableRows({ - select: [{ field: "role" }], - from: User.typename, - where: { - fields: [{ field: "id", value: req.user.id }], - }, - }); - */ - - if (!req.user.role) return false; - return allowedRoles.includes(req.user.role); - } catch (err) { - return false; - } - }; -} - -/* -export function userRoleGuard(allowedRoles: userRoleKenum[]) { - return function ( - target: BaseService, - propertyName: string, - propertyDescriptor: PropertyDescriptor - ): PropertyDescriptor { - // target === Employee.prototype - // propertyName === "greet" - // propertyDesciptor === Object.getOwnPropertyDescriptor(Employee.prototype, "greet") - const method = propertyDescriptor.value; - - propertyDescriptor.value = async function (req, args, query) { - // convert list of greet arguments to string - //const params = args.map((a) => JSON.stringify(a)).join(); - const params = "bar"; - //if it does not pass the access control, throw an error - if (!(await target.testPermissions("get", req, args, query))) { - throw errorHelper.badPermissionsError(); - } - - // invoke greet() and get its return value - const result = await method.apply(this, [req, args, query]); - - // convert result to string - const r = JSON.stringify(result); - - // display in console the function call details - console.log(`Call: ${propertyName}(${params}) => ${r}`); - - // return the result of invoking the method - return result; - }; - return propertyDescriptor; - }; -} -*/ - -export function permissionsCheck(methodKey: string) { - return function ( - target: BaseService, - propertyName: string, - propertyDescriptor: PropertyDescriptor - ): PropertyDescriptor { - // target === Employee.prototype - // propertyName === "greet" - // propertyDesciptor === Object.getOwnPropertyDescriptor(Employee.prototype, "greet") - const method = propertyDescriptor.value; - - propertyDescriptor.value = async function ({ - req, - fieldPath, - args, - query, - data, - isAdmin = false, - }: ServiceFunctionInputs) { - //if it does not pass the access control, throw an error - if ( - !(await target.testPermissions.apply(this, [ - methodKey, - { - req, - fieldPath, - args, - query, - data, - isAdmin, - }, - ])) - ) { - throw errorHelper.badPermissionsError(fieldPath); - } - // invoke greet() and get its return value - const result = await method.apply(this, [ - { - req, - fieldPath, - args, - query, - data, - isAdmin, - }, - ]); - - // return the result of invoking the method - return result; - }; - return propertyDescriptor; - }; -} diff --git a/backend/functions/src/schema/links/userUserFollowLink/rootResolver.ts b/backend/functions/src/schema/links/userUserFollowLink/rootResolver.ts index 0087c84..97771cb 100644 --- a/backend/functions/src/schema/links/userUserFollowLink/rootResolver.ts +++ b/backend/functions/src/schema/links/userUserFollowLink/rootResolver.ts @@ -1,5 +1,5 @@ import { UserUserFollowLink } from "../../services"; -import { generateBaseRootResolvers } from "../../helpers/rootResolver"; +import { generateBaseRootResolvers } from "../../core/helpers/rootResolver"; export default { ...generateBaseRootResolvers(UserUserFollowLink, [ diff --git a/backend/functions/src/schema/links/userUserFollowLink/service.ts b/backend/functions/src/schema/links/userUserFollowLink/service.ts index 153b7a1..31a22c8 100644 --- a/backend/functions/src/schema/links/userUserFollowLink/service.ts +++ b/backend/functions/src/schema/links/userUserFollowLink/service.ts @@ -2,10 +2,10 @@ import { LinkService } from "../../core/services"; import { generateUserRoleGuard, permissionsCheck, -} from "../../helpers/permissions"; +} from "../../core/helpers/permissions"; import { userRoleKenum } from "../../enums"; import { ServiceFunctionInputs, AccessControlMap } from "../../../types"; -import * as Resolver from "../../helpers/resolver"; +import * as Resolver from "../../core/helpers/resolver"; import { User } from "../../services"; export class UserUserFollowLinkService extends LinkService { diff --git a/backend/functions/src/schema/models/auth/service.ts b/backend/functions/src/schema/models/auth/service.ts index fb1497d..c2e8f72 100644 --- a/backend/functions/src/schema/models/auth/service.ts +++ b/backend/functions/src/schema/models/auth/service.ts @@ -1,12 +1,12 @@ import { SimpleService } from "../../core/services"; // import * as bcrypt from "bcryptjs"; -import * as errorHelper from "../../helpers/error"; +import * as errorHelper from "../../core/helpers/error"; import { User } from "../../services"; import { env } from "../../../config"; import axios from "axios"; import { ServiceFunctionInputs } from "../../../types"; -import * as sqlHelper from "../../helpers/sql"; -import * as Resolver from "../../helpers/resolver"; +import * as sqlHelper from "../../core/helpers/sql"; +import * as Resolver from "../../core/helpers/resolver"; import { userRoleKenum } from "../../enums"; export class AuthService extends SimpleService { diff --git a/backend/functions/src/schema/models/auth/typeDef.ts b/backend/functions/src/schema/models/auth/typeDef.ts index a245b0b..7005e35 100644 --- a/backend/functions/src/schema/models/auth/typeDef.ts +++ b/backend/functions/src/schema/models/auth/typeDef.ts @@ -1,7 +1,7 @@ import { User, Auth } from "../../services"; import * as jwt from "jsonwebtoken"; -import { generateTypenameField } from "../../helpers/typeDef"; +import { generateTypenameField } from "../../core/helpers/typeDef"; import { env } from "../../../config"; import { ObjectTypeDefinition, GiraffeqlObjectType } from "giraffeql"; import * as Scalars from "../../scalars"; diff --git a/backend/functions/src/schema/models/event/rootResolver.ts b/backend/functions/src/schema/models/event/rootResolver.ts index 134456f..cd5841d 100644 --- a/backend/functions/src/schema/models/event/rootResolver.ts +++ b/backend/functions/src/schema/models/event/rootResolver.ts @@ -1,5 +1,5 @@ import { Event } from "../../services"; -import { generateBaseRootResolvers } from "../../helpers/rootResolver"; +import { generateBaseRootResolvers } from "../../core/helpers/rootResolver"; export default { ...generateBaseRootResolvers(Event, [ diff --git a/backend/functions/src/schema/models/event/typeDef.ts b/backend/functions/src/schema/models/event/typeDef.ts index 0e9d9f9..58d8733 100644 --- a/backend/functions/src/schema/models/event/typeDef.ts +++ b/backend/functions/src/schema/models/event/typeDef.ts @@ -8,7 +8,7 @@ import { generateStringField, generateTypenameField, generateEnumField, -} from "../../helpers/typeDef"; +} from "../../core/helpers/typeDef"; import * as Scalars from "../../scalars"; export default new GiraffeqlObjectType({ diff --git a/backend/functions/src/schema/models/github/service.ts b/backend/functions/src/schema/models/github/service.ts index 8880057..d1d7fa1 100644 --- a/backend/functions/src/schema/models/github/service.ts +++ b/backend/functions/src/schema/models/github/service.ts @@ -1,5 +1,5 @@ import { BaseService } from "../../core/services"; -import { permissionsCheck } from "../../helpers/permissions"; +import { permissionsCheck } from "../../core/helpers/permissions"; import { ServiceFunctionInputs } from "../../../types"; import { env } from "../../../config"; import axios from "axios"; diff --git a/backend/functions/src/schema/models/personalBest/rootResolver.ts b/backend/functions/src/schema/models/personalBest/rootResolver.ts index a81b9c8..94bc63b 100644 --- a/backend/functions/src/schema/models/personalBest/rootResolver.ts +++ b/backend/functions/src/schema/models/personalBest/rootResolver.ts @@ -1,5 +1,5 @@ import { PersonalBest } from "../../services"; -import { generateBaseRootResolvers } from "../../helpers/rootResolver"; +import { generateBaseRootResolvers } from "../../core/helpers/rootResolver"; export default { ...generateBaseRootResolvers(PersonalBest, [ diff --git a/backend/functions/src/schema/models/personalBest/service.ts b/backend/functions/src/schema/models/personalBest/service.ts index 63e2077..554d03e 100644 --- a/backend/functions/src/schema/models/personalBest/service.ts +++ b/backend/functions/src/schema/models/personalBest/service.ts @@ -1,7 +1,7 @@ import { PaginatedService } from "../../core/services"; -import { permissionsCheck } from "../../helpers/permissions"; -import * as Resolver from "../../helpers/resolver"; -import * as sqlHelper from "../../helpers/sql"; +import { permissionsCheck } from "../../core/helpers/permissions"; +import * as Resolver from "../../core/helpers/resolver"; +import * as sqlHelper from "../../core/helpers/sql"; import { ServiceFunctionInputs } from "../../../types"; import { GiraffeqlBaseError } from "giraffeql"; import { scoreMethodEnum } from "../../enums"; diff --git a/backend/functions/src/schema/models/personalBest/typeDef.ts b/backend/functions/src/schema/models/personalBest/typeDef.ts index e57e53e..3daad62 100644 --- a/backend/functions/src/schema/models/personalBest/typeDef.ts +++ b/backend/functions/src/schema/models/personalBest/typeDef.ts @@ -17,7 +17,7 @@ import { generateBooleanField, generateCreatedByField, generateDecimalField, -} from "../../helpers/typeDef"; +} from "../../core/helpers/typeDef"; export default new GiraffeqlObjectType({ name: PersonalBest.typename, diff --git a/backend/functions/src/schema/models/personalBestClass/rootResolver.ts b/backend/functions/src/schema/models/personalBestClass/rootResolver.ts index 528e4a2..466245a 100644 --- a/backend/functions/src/schema/models/personalBestClass/rootResolver.ts +++ b/backend/functions/src/schema/models/personalBestClass/rootResolver.ts @@ -1,5 +1,5 @@ import { PersonalBestClass } from "../../services"; -import { generateBaseRootResolvers } from "../../helpers/rootResolver"; +import { generateBaseRootResolvers } from "../../core/helpers/rootResolver"; export default { ...generateBaseRootResolvers(PersonalBestClass, [ diff --git a/backend/functions/src/schema/models/personalBestClass/typeDef.ts b/backend/functions/src/schema/models/personalBestClass/typeDef.ts index ff4dd09..3a0229b 100644 --- a/backend/functions/src/schema/models/personalBestClass/typeDef.ts +++ b/backend/functions/src/schema/models/personalBestClass/typeDef.ts @@ -9,7 +9,7 @@ import { generateTypenameField, generateTextField, generateIntegerField, -} from "../../helpers/typeDef"; +} from "../../core/helpers/typeDef"; export default new GiraffeqlObjectType({ name: PersonalBestClass.typename, diff --git a/backend/functions/src/schema/models/product/rootResolver.ts b/backend/functions/src/schema/models/product/rootResolver.ts index e7a87aa..081cd5a 100644 --- a/backend/functions/src/schema/models/product/rootResolver.ts +++ b/backend/functions/src/schema/models/product/rootResolver.ts @@ -1,5 +1,5 @@ import { Product } from "../../services"; -import { generateBaseRootResolvers } from "../../helpers/rootResolver"; +import { generateBaseRootResolvers } from "../../core/helpers/rootResolver"; export default { ...generateBaseRootResolvers(Product, [ diff --git a/backend/functions/src/schema/models/product/typeDef.ts b/backend/functions/src/schema/models/product/typeDef.ts index 73627f5..f64c68d 100644 --- a/backend/functions/src/schema/models/product/typeDef.ts +++ b/backend/functions/src/schema/models/product/typeDef.ts @@ -7,7 +7,7 @@ import { generateCreatedByField, generateStringField, generateTypenameField, -} from "../../helpers/typeDef"; +} from "../../core/helpers/typeDef"; export default new GiraffeqlObjectType({ name: Product.typename, diff --git a/backend/functions/src/schema/models/user/rootResolver.ts b/backend/functions/src/schema/models/user/rootResolver.ts index d82b2b3..35a3416 100644 --- a/backend/functions/src/schema/models/user/rootResolver.ts +++ b/backend/functions/src/schema/models/user/rootResolver.ts @@ -1,5 +1,5 @@ import { User } from "../../services"; -import { generateBaseRootResolvers } from "../../helpers/rootResolver"; +import { generateBaseRootResolvers } from "../../core/helpers/rootResolver"; import { GiraffeqlRootResolverType } from "giraffeql"; export default { diff --git a/backend/functions/src/schema/models/user/service.ts b/backend/functions/src/schema/models/user/service.ts index b133d54..bd8adfb 100644 --- a/backend/functions/src/schema/models/user/service.ts +++ b/backend/functions/src/schema/models/user/service.ts @@ -2,7 +2,7 @@ import { AccessControlMap } from "../../../types"; import { PaginatedService } from "../../core/services"; import { userRoleKenum } from "../../enums"; -import { generateItemCreatedByUserGuard } from "../../helpers/permissions"; +import { generateItemCreatedByUserGuard } from "../../core/helpers/permissions"; export class UserService extends PaginatedService { defaultTypename = "user"; diff --git a/backend/functions/src/schema/models/user/typeDef.ts b/backend/functions/src/schema/models/user/typeDef.ts index 716c924..ad57dad 100644 --- a/backend/functions/src/schema/models/user/typeDef.ts +++ b/backend/functions/src/schema/models/user/typeDef.ts @@ -13,7 +13,7 @@ import { generateBooleanField, generateArrayField, generateTypenameField, -} from "../../helpers/typeDef"; +} from "../../core/helpers/typeDef"; import * as Scalars from "../../scalars"; import { userRoleToPermissionsMap } from "../../helpers/permissions"; import { userRoleKenum } from "../../enums"; diff --git a/backend/functions/src/schema/scalars/index.ts b/backend/functions/src/schema/scalars/index.ts index 87e331a..d874eee 100644 --- a/backend/functions/src/schema/scalars/index.ts +++ b/backend/functions/src/schema/scalars/index.ts @@ -1,7 +1,7 @@ import { generateKenumScalarDefinition, generateEnumScalarDefinition, -} from "../helpers/scalar"; +} from "../core/helpers/scalar"; import { userRoleKenum, userPermissionEnum, scoreMethodEnum } from "../enums"; import { BaseScalars, GiraffeqlScalarType } from "giraffeql";