@capillarytech/cap-ui-utils 1.0.8 → 1.0.12
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/hasRole.js +14 -0
- package/auth/index.js +31 -11
- package/auth/isCapUser.js +13 -0
- package/package.json +1 -1
package/auth/hasRole.js
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import warn from '../utils/console/warn';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Function to check if the user has access to the role
|
|
5
|
+
* @param {*} role
|
|
6
|
+
*/
|
|
7
|
+
export default function hasRole(role) {
|
|
8
|
+
if(!window.capAuth) {
|
|
9
|
+
warn("Cap auth not initialized. Please initialize cap auth to use this method");
|
|
10
|
+
return;
|
|
11
|
+
}
|
|
12
|
+
const { roles } = window.capAuth;
|
|
13
|
+
return roles.includes(role);
|
|
14
|
+
}
|
package/auth/index.js
CHANGED
|
@@ -10,25 +10,39 @@ import log from '../utils/console/log';
|
|
|
10
10
|
import isEmpty from '../utils/isEmpty';
|
|
11
11
|
import hasAccess from './hasAccess';
|
|
12
12
|
import authHoc from './authHoc';
|
|
13
|
+
import hasRole from './hasRole';
|
|
14
|
+
import isCapUser from './isCapUser';
|
|
13
15
|
|
|
16
|
+
const KEYS_EXEMPTED_FROM_VALIDATION = ['isCapUser']
|
|
14
17
|
/**
|
|
15
18
|
* Function to initialize the capAuth configurations to the document
|
|
16
19
|
* @param {*} authConfigs
|
|
17
20
|
*/
|
|
18
21
|
function _initialize(authConfigs) {
|
|
19
22
|
const capAuth = {};
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
23
|
+
try {
|
|
24
|
+
if (!authConfigs.permissions) {
|
|
25
|
+
warn("Cannot initialize auth without permissions");
|
|
26
|
+
return;
|
|
27
|
+
}
|
|
28
|
+
if(authConfigs.permissions) {
|
|
29
|
+
capAuth.permissions = Object.freeze(authConfigs.permissions);
|
|
30
|
+
}
|
|
31
|
+
if(authConfigs.orgFeatures) {
|
|
32
|
+
capAuth.orgFeatures = Object.freeze(authConfigs.orgFeatures);
|
|
33
|
+
}
|
|
34
|
+
if(authConfigs.roles) {
|
|
35
|
+
capAuth.roles = Object.freeze(authConfigs.roles);
|
|
36
|
+
}
|
|
37
|
+
if(authConfigs.isCapUser) {
|
|
38
|
+
capAuth.isCapUser = true;
|
|
39
|
+
}
|
|
40
|
+
log("Cap auth initialized");
|
|
41
|
+
window.capAuth = Object.freeze(capAuth);
|
|
26
42
|
}
|
|
27
|
-
|
|
28
|
-
|
|
43
|
+
catch(error) {
|
|
44
|
+
warn(error);
|
|
29
45
|
}
|
|
30
|
-
log("Cap auth initialized");
|
|
31
|
-
window.capAuth = Object.freeze(capAuth);
|
|
32
46
|
}
|
|
33
47
|
|
|
34
48
|
/**
|
|
@@ -45,7 +59,11 @@ function initialize(authConfigs, authOptions) {
|
|
|
45
59
|
return;
|
|
46
60
|
}
|
|
47
61
|
for ( const config in authConfigs) {
|
|
48
|
-
if(
|
|
62
|
+
if(KEYS_EXEMPTED_FROM_VALIDATION.includes(config)) {
|
|
63
|
+
//Skip validation
|
|
64
|
+
continue;
|
|
65
|
+
}
|
|
66
|
+
if(!["object"].includes(typeof authConfigs[config])) {
|
|
49
67
|
warn("Config must be an object");
|
|
50
68
|
return;
|
|
51
69
|
}
|
|
@@ -62,5 +80,7 @@ export default {
|
|
|
62
80
|
initialize,
|
|
63
81
|
hasAccess,
|
|
64
82
|
authHoc,
|
|
83
|
+
hasRole,
|
|
84
|
+
isCapUser,
|
|
65
85
|
}
|
|
66
86
|
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import warn from '../utils/console/warn';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Function to check if the user is a capUser
|
|
5
|
+
*/
|
|
6
|
+
export default function isCapUser() {
|
|
7
|
+
if(!window.capAuth) {
|
|
8
|
+
warn("Cap auth not initialized. Please initialize cap auth to use this method");
|
|
9
|
+
return;
|
|
10
|
+
}
|
|
11
|
+
const { isCapUser = false } = window.capAuth;
|
|
12
|
+
return isCapUser;
|
|
13
|
+
}
|