-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
43 changed files
with
210 additions
and
212 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
backend/functions/src/schema/core/generators/paginatorInfo.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
151 changes: 151 additions & 0 deletions
151
backend/functions/src/schema/core/helpers/permissions.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 = <StringKeyObject>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; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
2 changes: 1 addition & 1 deletion
2
...nd/functions/src/schema/helpers/shared.ts → ...nctions/src/schema/core/helpers/shared.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
...ctions/src/schema/helpers/subscription.ts → ...s/src/schema/core/helpers/subscription.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.