first commit
This commit is contained in:
50
src/utils/authorization/authorization.js
Normal file
50
src/utils/authorization/authorization.js
Normal file
@@ -0,0 +1,50 @@
|
||||
/* global window */
|
||||
|
||||
import { createFactory } from 'react';
|
||||
import { contains } from 'ramda';
|
||||
|
||||
const checkAuthorization = (user, roles) => {
|
||||
const userRole = user.profiles[0];
|
||||
const userOrgType = userRole.organization.type.key;
|
||||
|
||||
if (!roles) {
|
||||
return true;
|
||||
}
|
||||
|
||||
const role = roles[userRole.key];
|
||||
|
||||
if (role && contains(userOrgType, role)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
};
|
||||
|
||||
const authorization = roles => ({
|
||||
roles,
|
||||
authorize(WrappedComponent) {
|
||||
const loggedUser = JSON.parse(window.localStorage.getItem('loggedUser'));
|
||||
|
||||
if (!loggedUser) {
|
||||
return (props) => {
|
||||
props.router.push('/login');
|
||||
return null;
|
||||
};
|
||||
}
|
||||
|
||||
const factory = createFactory(WrappedComponent);
|
||||
const isAuthorized = checkAuthorization(loggedUser, roles);
|
||||
|
||||
if (!isAuthorized) {
|
||||
return (props) => {
|
||||
props.router.push('/404');
|
||||
return null;
|
||||
};
|
||||
}
|
||||
|
||||
const Component = props => factory(props);
|
||||
return Component;
|
||||
},
|
||||
});
|
||||
|
||||
export default authorization;
|
||||
9
src/utils/authorization/authorize.js
Normal file
9
src/utils/authorization/authorize.js
Normal file
@@ -0,0 +1,9 @@
|
||||
import authorization from './authorization';
|
||||
import normalizeRoles from './normalizeRoles';
|
||||
|
||||
const authorize = (...profiles) => {
|
||||
const roles = normalizeRoles(profiles);
|
||||
return authorization(roles).authorize;
|
||||
};
|
||||
|
||||
export default authorize;
|
||||
18
src/utils/authorization/index.js
Normal file
18
src/utils/authorization/index.js
Normal file
@@ -0,0 +1,18 @@
|
||||
import authorization from './authorization';
|
||||
|
||||
export { default as authorize } from './authorize';
|
||||
export { default as loggedUser } from './loggedUser';
|
||||
|
||||
export { default as bcbsaScheduler } from './profiles/bcbsaScheduler';
|
||||
export { default as bcbsiAdmin } from './profiles/bcbsiAdmin';
|
||||
export { default as bcbsiScheduler } from './profiles/bcbsiScheduler';
|
||||
export { default as planAdmin } from './profiles/planAdmin';
|
||||
export { default as planScheduler } from './profiles/planScheduler';
|
||||
export { default as programsupportSupport } from './profiles/programsupportSupport';
|
||||
export { default as providerAdmin } from './profiles/providerAdmin';
|
||||
export { default as providerScheduler } from './profiles/providerScheduler';
|
||||
export { default as superAdmin } from './profiles/superAdmin';
|
||||
export { default as support } from './profiles/support';
|
||||
export { default as techsupportAdmin } from './profiles/techsupportAdmin';
|
||||
|
||||
export default authorization;
|
||||
28
src/utils/authorization/loggedUser.js
Normal file
28
src/utils/authorization/loggedUser.js
Normal file
@@ -0,0 +1,28 @@
|
||||
/* global window */
|
||||
import { contains } from 'ramda';
|
||||
|
||||
import normalizeRoles from './normalizeRoles';
|
||||
|
||||
const loggedUser = JSON.parse(window.localStorage.getItem('loggedUser'));
|
||||
|
||||
if (loggedUser) {
|
||||
loggedUser.anyOf = (...profiles) => {
|
||||
const userRole = loggedUser.profiles[0];
|
||||
const userOrgType = userRole.organization.type.key;
|
||||
const roles = normalizeRoles(profiles);
|
||||
|
||||
if (!roles) {
|
||||
return true;
|
||||
}
|
||||
|
||||
const role = roles[userRole.key];
|
||||
|
||||
if (role && contains(userOrgType, role)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
};
|
||||
}
|
||||
|
||||
export default loggedUser;
|
||||
40
src/utils/authorization/normalizeRoles.js
Normal file
40
src/utils/authorization/normalizeRoles.js
Normal file
@@ -0,0 +1,40 @@
|
||||
/* eslint no-console: "off" */
|
||||
|
||||
const { assign, keys } = Object;
|
||||
|
||||
const normalizeRoles = (profiles) => {
|
||||
const roleKeys = keys(profiles);
|
||||
|
||||
if (roleKeys.length === 0) {
|
||||
console.warn('[Authorize Warning] You need to specify at least one profile');
|
||||
return arg => arg;
|
||||
}
|
||||
|
||||
if (roleKeys.length === 1) {
|
||||
return profiles[roleKeys[0]].authorize;
|
||||
}
|
||||
|
||||
const roles = profiles.reduce((result, profile) => {
|
||||
const profileRoles = keys(profile.roles).reduce((r, key) => {
|
||||
const role = r;
|
||||
|
||||
if (result[key]) {
|
||||
role[key] = result[key].concat(profile.roles[key]);
|
||||
} else {
|
||||
role[key] = profile.roles[key];
|
||||
}
|
||||
|
||||
return role;
|
||||
}, {});
|
||||
|
||||
return assign(
|
||||
{},
|
||||
result,
|
||||
profileRoles,
|
||||
);
|
||||
}, {});
|
||||
|
||||
return roles;
|
||||
};
|
||||
|
||||
export default normalizeRoles;
|
||||
7
src/utils/authorization/profiles/bcbsaScheduler.js
Normal file
7
src/utils/authorization/profiles/bcbsaScheduler.js
Normal file
@@ -0,0 +1,7 @@
|
||||
import authorization from '../authorization';
|
||||
|
||||
const bcbsaScheduler = authorization({
|
||||
SP: ['bcbsa'],
|
||||
});
|
||||
|
||||
export default bcbsaScheduler;
|
||||
7
src/utils/authorization/profiles/bcbsiAdmin.js
Normal file
7
src/utils/authorization/profiles/bcbsiAdmin.js
Normal file
@@ -0,0 +1,7 @@
|
||||
import authorization from '../authorization';
|
||||
|
||||
const bcbsiAdmin = authorization({
|
||||
BCBSIAD: ['bcbsi'],
|
||||
});
|
||||
|
||||
export default bcbsiAdmin;
|
||||
7
src/utils/authorization/profiles/bcbsiScheduler.js
Normal file
7
src/utils/authorization/profiles/bcbsiScheduler.js
Normal file
@@ -0,0 +1,7 @@
|
||||
import authorization from '../authorization';
|
||||
|
||||
const bcbsiScheduler = authorization({
|
||||
SP: ['bcbsi'],
|
||||
});
|
||||
|
||||
export default bcbsiScheduler;
|
||||
7
src/utils/authorization/profiles/planAdmin.js
Normal file
7
src/utils/authorization/profiles/planAdmin.js
Normal file
@@ -0,0 +1,7 @@
|
||||
import authorization from '../authorization';
|
||||
|
||||
const planAdmin = authorization({
|
||||
PLANAD: ['plan'],
|
||||
});
|
||||
|
||||
export default planAdmin;
|
||||
7
src/utils/authorization/profiles/planScheduler.js
Normal file
7
src/utils/authorization/profiles/planScheduler.js
Normal file
@@ -0,0 +1,7 @@
|
||||
import authorization from '../authorization';
|
||||
|
||||
const planScheduler = authorization({
|
||||
SP: ['plan'],
|
||||
});
|
||||
|
||||
export default planScheduler;
|
||||
@@ -0,0 +1,7 @@
|
||||
import authorization from '../authorization';
|
||||
|
||||
const programsupportSupport = authorization({
|
||||
SPT: ['programsupport'],
|
||||
});
|
||||
|
||||
export default programsupportSupport;
|
||||
7
src/utils/authorization/profiles/providerAdmin.js
Normal file
7
src/utils/authorization/profiles/providerAdmin.js
Normal file
@@ -0,0 +1,7 @@
|
||||
import authorization from '../authorization';
|
||||
|
||||
const providerAdmin = authorization({
|
||||
AD: ['provider'],
|
||||
});
|
||||
|
||||
export default providerAdmin;
|
||||
7
src/utils/authorization/profiles/providerScheduler.js
Normal file
7
src/utils/authorization/profiles/providerScheduler.js
Normal file
@@ -0,0 +1,7 @@
|
||||
import authorization from '../authorization';
|
||||
|
||||
const providerScheduler = authorization({
|
||||
SP: ['provider'],
|
||||
});
|
||||
|
||||
export default providerScheduler;
|
||||
7
src/utils/authorization/profiles/superAdmin.js
Normal file
7
src/utils/authorization/profiles/superAdmin.js
Normal file
@@ -0,0 +1,7 @@
|
||||
import authorization from '../authorization';
|
||||
|
||||
const superAdmin = authorization({
|
||||
AD: ['techsupport'],
|
||||
});
|
||||
|
||||
export default superAdmin;
|
||||
7
src/utils/authorization/profiles/support.js
Normal file
7
src/utils/authorization/profiles/support.js
Normal file
@@ -0,0 +1,7 @@
|
||||
import authorization from '../authorization';
|
||||
|
||||
const support = authorization({
|
||||
SPT: ['programsupport'],
|
||||
});
|
||||
|
||||
export default support;
|
||||
7
src/utils/authorization/profiles/techsupportAdmin.js
Normal file
7
src/utils/authorization/profiles/techsupportAdmin.js
Normal file
@@ -0,0 +1,7 @@
|
||||
import authorization from '../authorization';
|
||||
|
||||
const techsupportAdmin = authorization({
|
||||
AD: ['techsupport'],
|
||||
});
|
||||
|
||||
export default techsupportAdmin;
|
||||
Reference in New Issue
Block a user