Skip to content

Commit

Permalink
[#202] Add new types and roles
Browse files Browse the repository at this point in the history
  • Loading branch information
palagdan committed Sep 3, 2024
1 parent c298508 commit 9ff161e
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 3 deletions.
31 changes: 30 additions & 1 deletion src/constants/DefaultConstants.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import Routes from "./RoutesConstants";
import { APP_TITLE } from "../../config";

import * as Vocabulary from "./Vocabulary.js";
export const WEB_LANG = "en";

export const APP_NAME = APP_TITLE;
Expand Down Expand Up @@ -81,8 +82,36 @@ export const ACTION_STATUS = {

export const ROLE = {
ADMIN: "Admin",
DOCTOR: "Regular User",
DOCTOR: "Doctor",
COMPLETE_RECORDS: "rm_complete_records",
REJECT_RECORDS: "rm_reject_records",
PUBLISH_RECORDS: "rm_publish_records",
DELETE_ALL_RECORDS: "rm_delete_all_records",
EDIT_ALL_RECORDS: "rm_edit_all_records",
VIEW_ALL_RECORDS: "rm_view_all_records",
DELETE_ORGANIZATION_RECORDS: "rm_delete_organization_records",
EDIT_ORGANIZATION_RECORDS: "rm_edit_organization_records",
VIEW_ORGANIZATION_RECORDS: "rm_view_organization_records",
EDIT_USERS: "rm_edit_users",
IMPORT_CODELISTS: "rm_import_codelists",
};

export const TYPE_ROLE = {
[Vocabulary.ADMIN_TYPE]: ROLE.ADMIN,
[Vocabulary.DOCTOR_TYPE]: ROLE.DOCTOR,
[Vocabulary.COMPLETE_RECORDS_TYPE]: ROLE.COMPLETE_RECORDS,
[Vocabulary.REJECT_RECORDS_TYPE]: ROLE.REJECT_RECORDS,
[Vocabulary.PUBLISH_RECORDS_TYPE]: ROLE.PUBLISH_RECORDS,
[Vocabulary.DELETE_ALL_RECORDS_TYPE]: ROLE.DELETE_ALL_RECORDS,
[Vocabulary.EDIT_ALL_RECORDS_TYPE]: ROLE.EDIT_ALL_RECORDS,
[Vocabulary.VIEW_ALL_RECORDS_TYPE]: ROLE.VIEW_ALL_RECORDS,
[Vocabulary.DELETE_ORGANIZATION_RECORDS_TYPE]: ROLE.DELETE_ORGANIZATION_RECORDS,
[Vocabulary.EDIT_ORGANIZATION_RECORDS_TYPE]: ROLE.EDIT_ORGANIZATION_RECORDS,
[Vocabulary.VIEW_ORGANIZATION_RECORDS_TYPE]: ROLE.VIEW_ORGANIZATION_RECORDS,
[Vocabulary.EDIT_USERS_TYPE]: ROLE.EDIT_USERS,
[Vocabulary.IMPORT_CODELISTS_TYPE]: ROLE.IMPORT_CODELISTS,
};

// Default number of table elements per page.
export const DEFAULT_PAGE_SIZE = 10;

Expand Down
14 changes: 14 additions & 0 deletions src/constants/Vocabulary.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,17 @@ export const RDFS_COMMENT = "http://www.w3.org/2000/01/rdf-schema#comment";
export const ADMIN_TYPE = "http://onto.fel.cvut.cz/ontologies/record-manager/administrator";
export const DOCTOR_TYPE = "http://onto.fel.cvut.cz/ontologies/record-manager/doctor";
export const IMPERSONATOR_TYPE = "http://onto.fel.cvut.cz/ontologies/record-manager/impersonator";
export const COMPLETE_RECORDS_TYPE = "http://onto.fel.cvut.cz/ontologies/record-manager/record-complete";
export const REJECT_RECORDS_TYPE = "http://onto.fel.cvut.cz/ontologies/record-manager/reject-records";
export const PUBLISH_RECORDS_TYPE = "http://onto.fel.cvut.cz/ontologies/record-manager/publish-records";
export const DELETE_ALL_RECORDS_TYPE = "http://onto.fel.cvut.cz/ontologies/record-manager/delete-all-records";
export const EDIT_ALL_RECORDS_TYPE = "http://onto.fel.cvut.cz/ontologies/record-manager/edit-all-records";
export const VIEW_ALL_RECORDS_TYPE = "http://onto.fel.cvut.cz/ontologies/record-manager/view-all-records";
export const DELETE_ORGANIZATION_RECORDS_TYPE =
"http://onto.fel.cvut.cz/ontologies/record-manager/delete-organization-records";
export const EDIT_ORGANIZATION_RECORDS_TYPE =
"http://onto.fel.cvut.cz/ontologies/record-manager/edit-organization-records";
export const VIEW_ORGANIZATION_RECORDS_TYPE =
"http://onto.fel.cvut.cz/ontologies/record-manager/view-organization-records";
export const EDIT_USERS_TYPE = "http://onto.fel.cvut.cz/ontologies/record-manager/edit-users";
export const IMPORT_CODELISTS_TYPE = "http://onto.fel.cvut.cz/ontologies/record-manager/import-codelists";
19 changes: 17 additions & 2 deletions src/utils/SecurityUtils.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { getOidcIdentityStorageKey, isUsingOidcAuth } from "./OidcUtils";
import { sanitizeArray } from "./Utils";
import { IMPERSONATOR_TYPE } from "../constants/Vocabulary";
import { ROLE } from "../constants/DefaultConstants";
import { ROLE, TYPE_ROLE } from "../constants/DefaultConstants";

export function getOidcToken() {
const identityData = sessionStorage.getItem(getOidcIdentityStorageKey());
Expand All @@ -17,10 +17,25 @@ export function clearToken() {
}

export function isAdmin(currentUser) {
return currentUser.role === ROLE.ADMIN;
return currentUser.roles ? currentUser.roles.includes(ROLE.ADMIN) : false;
}

export function hasRole(currentUser, role) {
return currentUser.roles ? currentUser.roles.includes(role) : false;
}

export function isImpersonator(currentUser) {
// When using OIDC, the access token does not contain any info that the current user is being impersonated
return !isUsingOidcAuth() && sanitizeArray(currentUser.types).indexOf(IMPERSONATOR_TYPE) !== -1;
}

export function getRoles(user) {
if (!user) {
return undefined;
}
let roles = [];
user.types.map((type) => {
TYPE_ROLE[type] && roles.push(TYPE_ROLE[type]);
});
return roles;
}

0 comments on commit 9ff161e

Please sign in to comment.