Skip to content
Snippets Groups Projects
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
permission.stack.handler.js 11.70 KiB
import AuthStore, { persistor } from './../authenticate/auth.store';
import AuthService from '../services/auth.service';
import _ from 'lodash';
import axios from 'axios';

const PermissionStackUtil = {
    /**
     * Get current user permission from API
     * @param {*} loadPermission 
     * @returns 
     */
    getPermissions: async(loadPermission) => {
        let permissionStack = {};
        if (loadPermission) {
            permissionStack =  await PermissionStackUtil.getAPIBasedPermission();
            //Use this code if API role permission is not ready for demo
            /* let user = Auth.getUser();
            user = user?user.name:"";
            if (user === 'scientist' ) {
                permissionStack['project'] = {create: true, edit: false, delete: true};
                permissionStack['scheduleunit'] =  {create: true, edit: false, delete: true, createsub: false, autodeletion:false, 
                    copysu:false, excelview:true, cleanuptask:false, cancelsu:false, viewworkflow:true,dataproduct: true,};
            }   else if (user === 'operator' ) {
                permissionStack['project'] = {create: true, edit: false, delete: false};
                permissionStack['scheduleunit'] =  {create: true, edit: false, delete: false, createsub: false, autodeletion:true, 
                    copysu:true, excelview:false, cleanuptask:true, cancelsu:false, viewworkflow:true,dataproduct: false,};
            }   else {
                permissionStack['project'] = {create: true, edit: false, delete: true};
                permissionStack['scheduleunit'] =  {create: true, edit: false, delete: true, createsub: false, autodeletion:false, 
                    copysu:false, excelview:true, cleanuptask:false, cancelsu:false, viewworkflow:true,dataproduct: true,};
            } */
        }
        AuthStore.dispatch({ type: 'loadpermission', payload: permissionStack});
        return permissionStack;
    },
    async getAPIBasedPermission() {
        let permissionStack = {};
        const module_url = {
            project: 'project',
            observationStrategy: 'scheduling_unit_observing_strategy_template',
            scheduleunit_draft: 'scheduling_unit_draft',
            scheduleunit_blueprint: 'scheduling_unit_blueprint',
            scheduling_set: 'scheduling_set',
            cycle: 'cycle',
            task_draft: 'task_draft',
            task_blueprint: 'task_blueprint',
            reservation: 'reservation',
            task_relation_draft: 'task_relation_draft',
            task_relation_blueprint: 'task_relation_blueprint',
            dynamicScheduler: 'setting/dynamic_scheduling_enabled',
            fixedTimeScheduler: 'setting/fixed_time_scheduling_enabled',
            systemevent: 'system_event',
        }
        const modules = Object.keys(module_url);
        const source = axios.CancelToken.source();
        for(const module of modules) {
            const url = module_url[module];
            let allowedPermission = [];
            const response = await AuthService.getAccessControlMethod(url, source.token);
            if(response && response.status === 401){
                source.cancel();
            }
            else {
                allowedPermission = response
            }
            if (allowedPermission) {
                if (module === 'project') {
                    permissionStack[module] ={
                        list: _.includes(allowedPermission, 'GET'),
                        create: _.includes(allowedPermission, 'PUT'),
                        edit: _.includes(allowedPermission, 'PATCH'),
                        delete: _.includes(allowedPermission, 'DELETE')};
                }  
                else if(module ===  'scheduleunit_draft' || module === 'scheduleunit_blueprint') {
                    let getAccesss = _.includes(allowedPermission, 'GET');
                    let putAccesss = _.includes(allowedPermission, 'PUT');
                    let patchAccess = _.includes(allowedPermission, 'PATCH');
                    let deleteAccess = _.includes(allowedPermission, 'DELETE');
                    let postAccess = _.includes(allowedPermission, 'POST');
                    let createAccess = _.includes(permissionStack['observationStrategy'], "create_scheduling_unit")
                    permissionStack['scheduleunit'] ={
                        create: createAccess, edit: patchAccess, delete: deleteAccess, list: getAccesss,
                        createsub: putAccesss, autodeletion:patchAccess, copysu:putAccesss, excelview:getAccesss,
                        cleanuptask:true, cancelsu:true, viewworkflow:true, create_rerun: true, update_sub: true };
                    permissionStack[module] = {
                        create: createAccess, edit: patchAccess, delete: deleteAccess, list: getAccesss, add: postAccess
                    }  
                }
                else if(module === 'task_relation_draft') {
                    permissionStack['scheduleunit'].dataproduct = _.includes(allowedPermission, 'POST');
                }
                else if(module === 'task_relation_blueprint') {
                    permissionStack['scheduleunit_blueprint'].dataproduct = _.includes(allowedPermission, 'POST');
                }
                else if (module === 'scheduling_set') {
                    permissionStack['scheduleunit_draft']['scheduling_set'] = _.includes(allowedPermission, 'POST');
                    permissionStack['scheduleunit']['create'] = _.includes(allowedPermission, 'POST');
                }
                else if(module === 'cycle') {
                    permissionStack[module] ={
                        list: _.includes(allowedPermission, 'GET'),
                        create: _.includes(allowedPermission, 'PUT'),
                        edit: _.includes(allowedPermission, 'PATCH'),
                        delete: _.includes(allowedPermission, 'DELETE')};
                } 
                else if(module === 'task_draft' || module === 'task_blueprint'){
                    permissionStack['task'] ={
                        list: _.includes(allowedPermission, 'GET'),
                        edit: _.includes(allowedPermission, 'PATCH'),
                        delete: _.includes(allowedPermission, 'DELETE'),
                    }
                    permissionStack[module] = {
                        canceltask: _.includes(allowedPermission, 'POST')
                    };
                } 
                else if(module === 'reservation') {
                    let getAccesss = _.includes(allowedPermission, 'GET');
                    let postAccess = _.includes(allowedPermission, 'POST');
                    permissionStack['timeline'] = {
                        addreservation: postAccess,
                        listreservation: getAccesss
                    };
                    permissionStack['weekoverview'] = {
                        addreservation: postAccess,
                        listreservation: getAccesss 
                    };
                    permissionStack['reservation'] = {
                        create: postAccess,
                        list: getAccesss,
                        edit: _.includes(allowedPermission, 'PATCH'),
                        delete: _.includes(allowedPermission, 'DELETE')
                    };
                }
                else if(module === 'systemevent') {
                    let getAccesss = _.includes(allowedPermission, 'GET');
                    let postAccess = _.includes(allowedPermission, 'POST');
                    permissionStack['timeline']['addsystemevent'] = postAccess;
                    permissionStack['timeline']['listsystemevent'] = getAccesss;
                    permissionStack['weekoverview']['addsystemevent'] = postAccess;
                    permissionStack['weekoverview']['listsystemevent'] = getAccesss;
                   
                    permissionStack['systemevent'] = {
                        create: postAccess,
                        list: getAccesss,
                        edit: _.includes(allowedPermission, 'PATCH'),
                        delete: _.includes(allowedPermission, 'DELETE')
                    };
                }
                else if (module === 'dynamicScheduler' || module === 'fixedTimeScheduler') {
                    permissionStack[module] = {
                        setting: _.includes(allowedPermission, 'PATCH'),
                    };
                }   else {
                    permissionStack[module] = allowedPermission;
                    
                }
            }
        }
        permissionStack['workflow'] = {
                'qa_reporting_to': true,
                'qa_reporting_sos':true,
                'pi_verification':true,
                'decide_acceptance':true,
                'unpin_data':true,
        };
        return permissionStack;
    },
    async getAccessByModuleAndId(module, id) {
        let permissionStack = {};
        const url = module+'/'+id;
        const allowedPermission = await AuthService.getAccessControlMethod(url);
        permissionStack[module] = {}
        if(allowedPermission) {
            if(module === 'task_draft' || module === 'task_blueprint' || module === 'subtask') {
                permissionStack[module][id] ={ 
                    view : _.includes(allowedPermission, 'GET'),
                    edit : _.includes(allowedPermission, 'PUT'),
                    delete : _.includes(allowedPermission, 'DELETE'),
                    cancel : _.includes(allowedPermission, 'cancel'),
                    obsolete : _.includes(allowedPermission, 'mark_as_obsolete')
                }
            }
            else if(module ===  'scheduling_unit_draft' || module === 'scheduling_unit_blueprint') {
                let getAccesss = _.includes(allowedPermission, 'GET');
                let putAccesss = _.includes(allowedPermission, 'PUT');
                let patchAccess = _.includes(allowedPermission, 'PATCH');
                let deleteAccess = _.includes(allowedPermission, 'DELETE');
                let copySU = _.includes(allowedPermission, module === 'scheduling_unit_blueprint'? 'copy_to_draft': 'copy');
                let cleanupTask = _.includes(allowedPermission, 'create_cleanuptask_for_scheduling_unit_blueprint');
                let copyFailedTasks = _.includes(allowedPermission, 'create_copies_of_failed_tasks_via_draft');
                let updateSub = _.includes(allowedPermission, 'update_task_blueprints_and_subtasks_graph_from_draft');
                let createSub = _.includes(allowedPermission, 'create_scheduling_unit_blueprint_and_tasks_and_subtasks_tree');
                let cancel = _.includes(allowedPermission, 'cancel');
                let createRerun = _.includes(allowedPermission, 'create_copies_of_failed_tasks_via_draft');
                permissionStack[module][id] ={
                    create: putAccesss, edit: patchAccess, delete: deleteAccess, list: getAccesss,
                    createSub: createSub, autoDeletion:patchAccess, copySU:copySU, copyFailedTasks: copyFailedTasks,
                    cleanupTask:cleanupTask, cancelSU:cancel, viewWorkflow:true, createRerun: createRerun, updateSub: updateSub };
            }
            else {
                permissionStack[module][id] = { 
                    view : _.includes(allowedPermission, 'GET'),
                    edit : _.includes(allowedPermission, 'PUT'),
                    delete : _.includes(allowedPermission, 'DELETE'),
                }
            }
            
        }
        AuthStore.dispatch({ type: 'loadpermissionbyid', payload: permissionStack, id: id, module: module});
        return permissionStack[module];
    },
    async deleteStore() {
        await AuthStore.dispatch({type: 'deletestore', payload: { rolePermission: {}}});
        return
    }
}

export default PermissionStackUtil;