From 9ff161eadf880167c0fc329f8901223bf0547654 Mon Sep 17 00:00:00 2001 From: Daniil Palagin Date: Tue, 3 Sep 2024 20:56:44 +0200 Subject: [PATCH] [#202] Add new types and roles --- src/constants/DefaultConstants.js | 31 ++++++++++++++++++++++++++++++- src/constants/Vocabulary.js | 14 ++++++++++++++ src/utils/SecurityUtils.js | 19 +++++++++++++++++-- 3 files changed, 61 insertions(+), 3 deletions(-) diff --git a/src/constants/DefaultConstants.js b/src/constants/DefaultConstants.js index de26bf85..e80232db 100644 --- a/src/constants/DefaultConstants.js +++ b/src/constants/DefaultConstants.js @@ -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; @@ -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; diff --git a/src/constants/Vocabulary.js b/src/constants/Vocabulary.js index 170763a5..a4dff40c 100644 --- a/src/constants/Vocabulary.js +++ b/src/constants/Vocabulary.js @@ -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"; diff --git a/src/utils/SecurityUtils.js b/src/utils/SecurityUtils.js index 119a2e43..83267969 100644 --- a/src/utils/SecurityUtils.js +++ b/src/utils/SecurityUtils.js @@ -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()); @@ -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; +}