@capillarytech/cap-ui-utils 0.0.1 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/auth/AccessForbidden.js +9 -0
- package/auth/authHoc.js +54 -0
- package/auth/hasAccess.js +18 -0
- package/auth/index.js +68 -0
- package/index.js +1 -3
- package/package.json +5 -2
- package/utils/console/log.js +3 -0
- package/utils/console/warn.js +3 -0
- package/utils/isEmpty.js +7 -0
package/auth/authHoc.js
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import AccessForbidden from './AccessForbidden'
|
|
3
|
+
import hasAccess from './hasAccess';
|
|
4
|
+
import log from '../utils/console/log'
|
|
5
|
+
|
|
6
|
+
const AUTHORIZE_OVERRIDE = 'capAuthAuthorized';
|
|
7
|
+
|
|
8
|
+
const getPermissionToBeVerified = (options, props) => {
|
|
9
|
+
const { permission, permissionParamMatch, paramToBeAssessed } = options;
|
|
10
|
+
const { match = {} } = props;
|
|
11
|
+
const { params = {}} = match;
|
|
12
|
+
const param = params[paramToBeAssessed];
|
|
13
|
+
if (permission) {
|
|
14
|
+
return permission;
|
|
15
|
+
}
|
|
16
|
+
if (permissionParamMatch) {
|
|
17
|
+
return permissionParamMatch[param] || AUTHORIZE_OVERRIDE;
|
|
18
|
+
}
|
|
19
|
+
return undefined;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* HOC to verify the authorize the component
|
|
23
|
+
* Authorization of a component can happen in two ways
|
|
24
|
+
* 1) By passing the permission option. If this value is passed, the entired component is accessed irrespective of location params
|
|
25
|
+
* 2) By passing permissionParamMatch and paramToBeAssessed, here you can authorize the component based on specific param value
|
|
26
|
+
* ex. Auth.authHoc(CampaignMessage, {
|
|
27
|
+
* RedirectComponent: CampaignHomePage,
|
|
28
|
+
* redirectPath: `${rootPath}/list`,
|
|
29
|
+
* paramToBeAssessed: 'mode',
|
|
30
|
+
* permissionParamMatch: {
|
|
31
|
+
* create: 'somethig',
|
|
32
|
+
* },
|
|
33
|
+
* })
|
|
34
|
+
* @param {*} ComponentToBeVerified
|
|
35
|
+
* @param {*} options
|
|
36
|
+
*/
|
|
37
|
+
const withAuth = (ComponentToBeVerified, options = {}) => {
|
|
38
|
+
const getComponent = (permissionToBeVerified) => {
|
|
39
|
+
const { RedirectComponent } = options;
|
|
40
|
+
return (hasAccess(permissionToBeVerified)
|
|
41
|
+
|| permissionToBeVerified === AUTHORIZE_OVERRIDE) ?
|
|
42
|
+
ComponentToBeVerified
|
|
43
|
+
:
|
|
44
|
+
(RedirectComponent || AccessForbidden);
|
|
45
|
+
}
|
|
46
|
+
const HOC = props => {
|
|
47
|
+
const permissionToBeVerified = getPermissionToBeVerified(options, props);
|
|
48
|
+
const VerifiedComponent = getComponent(permissionToBeVerified);
|
|
49
|
+
return <VerifiedComponent {...props} />;
|
|
50
|
+
};
|
|
51
|
+
return HOC;
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
export default withAuth;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import warn from '../utils/console/warn';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Function to check if the user has access to the permission
|
|
5
|
+
* @param {*} permission
|
|
6
|
+
*/
|
|
7
|
+
export default function hasAccess(permission) {
|
|
8
|
+
if(!window.capAuth) {
|
|
9
|
+
warn("Cap auth not initialized. Please initialize cap auth to use this method");
|
|
10
|
+
return;
|
|
11
|
+
}
|
|
12
|
+
const { permissions } = window.capAuth;
|
|
13
|
+
//AOM is a universal action permission.
|
|
14
|
+
// if (permissions.includes('AOM')) {
|
|
15
|
+
// return true;
|
|
16
|
+
// }
|
|
17
|
+
return permissions.includes(permission);
|
|
18
|
+
}
|
package/auth/index.js
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* UI Auth Module
|
|
3
|
+
*
|
|
4
|
+
* @package @capillarytech/cap-ui-utils
|
|
5
|
+
* @author Rajshekar <rajshekar.venkatesh@capillarytech.com>
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import warn from '../utils/console/warn';
|
|
9
|
+
import log from '../utils/console/log';
|
|
10
|
+
import isEmpty from '../utils/isEmpty';
|
|
11
|
+
import hasAccess from './hasAccess';
|
|
12
|
+
import authHoc from './authHoc';
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Function to initialize the capAuth configurations to the document
|
|
16
|
+
* @param {*} authConfigs
|
|
17
|
+
*/
|
|
18
|
+
function _initialize(authConfigs) {
|
|
19
|
+
const capAuth = {};
|
|
20
|
+
if (!authConfigs.permissions) {
|
|
21
|
+
warn("Cannot initialize auth without permissions");
|
|
22
|
+
return;
|
|
23
|
+
}
|
|
24
|
+
if(authConfigs.permissions) {
|
|
25
|
+
capAuth.permissions = Object.freeze(authConfigs.permissions);
|
|
26
|
+
}
|
|
27
|
+
if(authConfigs.orgFeatures) {
|
|
28
|
+
capAuth.orgFeatures = Object.freeze(authConfigs.orgFeatures);
|
|
29
|
+
}
|
|
30
|
+
log("Cap auth initialized");
|
|
31
|
+
window.capAuth = Object.freeze(capAuth);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Function to initialize Auth
|
|
36
|
+
* @param {
|
|
37
|
+
* permissions, // Array of user permission
|
|
38
|
+
* orgFeatures, // Array of org Features
|
|
39
|
+
* } authConfigs
|
|
40
|
+
* @param authOptions
|
|
41
|
+
*/
|
|
42
|
+
function initialize(authConfigs, authOptions) {
|
|
43
|
+
log("Before initialize 1")
|
|
44
|
+
if (isEmpty(authConfigs)) {
|
|
45
|
+
warn("Cannot initialize empty object");
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
for ( const config in authConfigs) {
|
|
49
|
+
if(typeof authConfigs[config] !== "object") {
|
|
50
|
+
warn("Config must be an object");
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
if (isEmpty(authConfigs[config])) {
|
|
54
|
+
warn("Config is empty");
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
log("Before initialize")
|
|
59
|
+
_initialize(authConfigs);
|
|
60
|
+
return true;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export default {
|
|
64
|
+
initialize,
|
|
65
|
+
hasAccess,
|
|
66
|
+
authHoc,
|
|
67
|
+
}
|
|
68
|
+
|
package/index.js
CHANGED
package/package.json
CHANGED
|
@@ -1,10 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@capillarytech/cap-ui-utils",
|
|
3
|
-
"version": "0.0
|
|
3
|
+
"version": "1.0.0",
|
|
4
4
|
"description": "Utility functions shared accross all the modules",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
7
7
|
"test": "echo \"Error: no test specified\" && exit 1"
|
|
8
8
|
},
|
|
9
|
-
"author": "Rajshekar"
|
|
9
|
+
"author": "Rajshekar",
|
|
10
|
+
"dependencies": {
|
|
11
|
+
"react": "^16.13.0"
|
|
12
|
+
}
|
|
10
13
|
}
|