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

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 RedirectionMap = {
            'wait scheduled': 1,
            'wait processed': 2,
            'qa reporting to': 3,
            'qa reporting sos':4,
            'pi verification':5,
            'decide acceptance':6,
            'ingesting':7,
            'unpin data':8,
            'end':9
         }
       // const modules = ['project', 'scheduleunit_draft', 'scheduleunit_blueprint', 'cycle', 'task_draft', 'task_blueprint', 'reservation' ];
        const module_url = {
            project: 'project',
            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'
        }
        const modules = Object.keys(module_url);
        for(const module of modules) {
            const url = module_url[module];
            const allowedPermission = await AuthService.getAccessControlMethod(url);
            if (allowedPermission) {

                if (module === 'project') {
                    permissionStack[module] ={
                        list: allowedPermission?(_.includes(allowedPermission, 'GET')):false,
                        create: allowedPermission?(_.includes(allowedPermission, 'PUT')):false,
                        edit: allowedPermission?(_.includes(allowedPermission, 'PATCH')):false,
                        delete: allowedPermission?(_.includes(allowedPermission, 'DELETE')):false};
                }  
                else if(module ===  'scheduleunit_draft' || module === 'schedulingunit_blueprint') {
                    let getAccesss = allowedPermission?(_.includes(allowedPermission, 'GET')):false;
                    let putAccesss = allowedPermission?(_.includes(allowedPermission, 'PUT')):false;
                    let patchAccess = allowedPermission?(_.includes(allowedPermission, 'PATCH')):false;
                    let deleteAccess = allowedPermission?(_.includes(allowedPermission, 'DELETE')):false;
                    let postAccess = allowedPermission?(_.includes(allowedPermission, 'POST')):false;
                    permissionStack['scheduleunit'] ={
                        create: putAccesss, edit: patchAccess, delete: deleteAccess, list: getAccesss,
                        createsub: putAccesss, autodeletion:patchAccess, copysu:putAccesss, excelview:putAccesss,
                        cleanuptask:true, cancelsu:true, viewworkflow:true,dataproduct: true };
                    permissionStack[module] = {
                        create: putAccesss, edit: patchAccess, delete: deleteAccess, list: getAccesss, add: postAccess
                    }
                }
                else if (module === ' scheduling_set') {
                    permissionStack['scheduleunit_draft'][' scheduling_set'] = allowedPermission?(_.includes(allowedPermission, 'POST')):false;
                }
                else if(module === 'cycle') {
                    permissionStack[module] ={
                        list: allowedPermission?(_.includes(allowedPermission, 'GET')):false,
                        create: allowedPermission?(_.includes(allowedPermission, 'PUT')):false,
                        edit: allowedPermission?(_.includes(allowedPermission, 'PATCH')):false,
                        delete: allowedPermission?(_.includes(allowedPermission, 'DELETE')):false};
                } else if(module === 'task_draft' || module === 'task_blueprint'){
                    permissionStack['task'] ={
                        list: allowedPermission?(_.includes(allowedPermission, 'GET')):false,
                        edit: allowedPermission?(_.includes(allowedPermission, 'PATCH')):false,
                        delete: allowedPermission?(_.includes(allowedPermission, 'DELETE')):false
                    }
                } else if(module === 'reservation') {
                    let getAccesss = allowedPermission?(_.includes(allowedPermission, 'GET')):false;
                    let postAccess = allowedPermission?(_.includes(allowedPermission, 'POST')):false;
                    permissionStack['timeline'] = {
                        addreservation: postAccess,
                        listreservation: getAccesss
                    };
                    permissionStack['weekoverview'] = {
                        addreservation: postAccess,
                        listreservation: getAccesss 
                    };
                    permissionStack['reservation'] = {
                        create: postAccess,
                        list: getAccesss,
                        edit: allowedPermission?(_.includes(allowedPermission, 'PATCH')):false,
                    };
                }    
                
            }
            //  else {
            //     permissionStack['project'] = {create: false, edit: false, delete: false};
            //     permissionStack['cycle'] = {create: false, edit: false, delete: false}
            //     permissionStack['scheduleunit'] =  {create: false, edit: false, delete: false, createsub: false, autodeletion:false, 
            //         copysu:false, excelview:false, cleanuptask:true, cancelsu:true, viewworkflow:true,dataproduct: true,};
            // }
        }
        permissionStack['workflow'] = {
                'qa_reporting_to': true,
                'qa_reporting_sos':true,
                'pi_verification':true,
                'decide_acceptance':true,
                'unpin_data':true,
        };
        return permissionStack;
    },

    async getUserAccessModulebyId(module, id) {
        let permissionStack = {};
        const url = module+'/'+id;
            const allowedPermission = await AuthService.getAccessControlMethod(url);
            permissionStack[module] = {}
            if(allowedPermission) {
                permissionStack[module][id] ={ 
                    view : allowedPermission?(_.includes(allowedPermission, 'GET')):false,
                    edit : allowedPermission?(_.includes(allowedPermission, 'PUT')):false,
                    delete : allowedPermission?(_.includes(allowedPermission, 'DELETE')):false
                }
            }
            AuthStore.dispatch({ type: 'loadpermissionbyid', payload: permissionStack, id: id, module: module});
        return permissionStack[module];
    }
}

export default PermissionStackUtil;