Permission System (#47)
* feat(api): permissions system Adds a permission system for isAuthenticated middleware. Also adds user CRUD.pull/48/head
parent
5d46f8d76d
commit
cfc84ce2f3
@ -0,0 +1,38 @@
|
||||
export enum Permission {
|
||||
NONE = 0,
|
||||
ADMIN = 2,
|
||||
MANAGE_SETTINGS = 4,
|
||||
MANAGE_USERS = 8,
|
||||
MANAGE_REQUESTS = 16,
|
||||
REQUEST = 32,
|
||||
VOTE = 64,
|
||||
}
|
||||
|
||||
/**
|
||||
* Takes a Permission and the users permission value and determines
|
||||
* if the user has access to the permission provided. If the user has
|
||||
* the admin permission, true will always be returned from this check!
|
||||
*
|
||||
* @param permissions Single permission or array of permissions
|
||||
* @param value users current permission value
|
||||
*/
|
||||
export const hasPermission = (
|
||||
permissions: Permission | Permission[],
|
||||
value: number
|
||||
): boolean => {
|
||||
let total = 0;
|
||||
|
||||
// If we are not checking any permissions, bail out and return true
|
||||
if (permissions === 0) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (Array.isArray(permissions)) {
|
||||
// Combine all permission values into one
|
||||
total = permissions.reduce((a, v) => a + v, 0);
|
||||
} else {
|
||||
total = permissions;
|
||||
}
|
||||
|
||||
return !!(value & Permission.ADMIN) || !!(value & total);
|
||||
};
|
Loading…
Reference in new issue