Skip to content

Commit

Permalink
feat: add support for public role
Browse files Browse the repository at this point in the history
  • Loading branch information
iacoshoria committed Oct 29, 2020
1 parent 616c152 commit 91b5bfd
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions packages/earth-shared/src/authorization/roles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@
*/

enum RoleEnum {
OWNER = 'Owner',
ADMIN = 'Admin',
EDITOR = 'Editor',
PUBLIC = 'Public',
VIEWER = 'Viewer',
EDITOR = 'Editor',
ADMIN = 'Admin',
OWNER = 'Owner',
SUPER_ADMIN = 'SuperAdmin',
}

Expand Down Expand Up @@ -50,7 +51,9 @@ export const isAuthz = (roles: string[]): boolean => {
* @param sep
*/
export const isAdminAuthz = (roles: string[], sep: string = ':'): boolean => {
return !!roles.find((r: string) => r.split(sep)[1] !== RoleEnum.VIEWER);
return !!roles.find(
(r: string) => ![RoleEnum.PUBLIC, RoleEnum.VIEWER].includes(<any>r.split(sep)[1])
);
};

/**
Expand Down Expand Up @@ -83,7 +86,7 @@ export const mapAuthorizedRoleGroups = (
sep: string = ':'
): string[] => {
const groups = roles
.filter((r: string) => r.split(sep)[1] !== RoleEnum.VIEWER)
.filter((r: string) => ![RoleEnum.PUBLIC, RoleEnum.VIEWER].includes(<any>r.split(sep)[1]))
.map((r: string) => r.split(sep)[0])
.filter((g: string) => !excludeGroups.includes(g));
return Array.from(new Set(groups));
Expand All @@ -94,7 +97,7 @@ export const mapAuthorizedRoleGroups = (
*/
export const checkRole = (roles: string[]): boolean => {
const exceptViewer: string[] = Object.values(RoleEnum).filter(
(r: string) => r !== RoleEnum.VIEWER
(r) => ![RoleEnum.PUBLIC, RoleEnum.VIEWER].includes(r)
);
return roles.some((r: string) => exceptViewer.includes(r));
};

0 comments on commit 91b5bfd

Please sign in to comment.