@capillarytech/cap-ui-utils 1.2.0 → 1.2.2

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.
@@ -0,0 +1,14 @@
1
+ import warn from '../utils/console/warn';
2
+
3
+ /**
4
+ * Function to check if the user has access to the permission
5
+ * @param {*} feature
6
+ */
7
+ export default function hasFeatureAccess(feature) {
8
+ if(!window.capAuth) {
9
+ warn("Cap auth not initialized. Please initialize cap auth to use this method");
10
+ return;
11
+ }
12
+ const { accessibleFeatures } = window.capAuth;
13
+ return accessibleFeatures.includes(feature);
14
+ }
package/auth/index.js CHANGED
@@ -12,31 +12,42 @@ import hasAccess from './hasAccess';
12
12
  import authHoc from './authHoc';
13
13
  import hasRole from './hasRole';
14
14
  import isCapUser from './isCapUser';
15
+ import hasFeatureAccess from './hasFeatureAccess';
15
16
 
17
+ const KEYS_EXEMPTED_FROM_VALIDATION = ['isCapUser']
16
18
  /**
17
19
  * Function to initialize the capAuth configurations to the document
18
20
  * @param {*} authConfigs
19
21
  */
20
22
  function _initialize(authConfigs) {
21
23
  const capAuth = {};
22
- if (!authConfigs.permissions) {
23
- warn("Cannot initialize auth without permissions");
24
- return;
25
- }
26
- if(authConfigs.permissions) {
27
- capAuth.permissions = Object.freeze(authConfigs.permissions);
28
- }
29
- if(authConfigs.orgFeatures) {
30
- capAuth.orgFeatures = Object.freeze(authConfigs.orgFeatures);
31
- }
32
- if(authConfigs.roles) {
33
- capAuth.roles = Object.freeze(authConfigs.roles);
24
+ const { permissions, orgFeatures, roles, isCapUser, accessibleFeatures } = authConfigs;
25
+ try {
26
+ if (!permissions) {
27
+ warn("Cannot initialize auth without permissions");
28
+ return;
29
+ }
30
+ if(permissions) {
31
+ capAuth.permissions = Object.freeze(permissions);
32
+ }
33
+ if(orgFeatures) {
34
+ capAuth.orgFeatures = Object.freeze(orgFeatures);
35
+ }
36
+ if(roles) {
37
+ capAuth.roles = Object.freeze(roles);
38
+ }
39
+ if(isCapUser) {
40
+ capAuth.isCapUser = true;
41
+ }
42
+ if(accessibleFeatures) {
43
+ capAuth.accessibleFeatures = Object.freeze(accessibleFeatures);
44
+ }
45
+ log("Cap auth initialized");
46
+ window.capAuth = Object.freeze(capAuth);
34
47
  }
35
- if(authConfigs.isCapUser) {
36
- capAuth.isCapUser = true;
48
+ catch(error) {
49
+ warn(error);
37
50
  }
38
- log("Cap auth initialized");
39
- window.capAuth = Object.freeze(capAuth);
40
51
  }
41
52
 
42
53
  /**
@@ -53,12 +64,12 @@ function initialize(authConfigs, authOptions) {
53
64
  return;
54
65
  }
55
66
  for ( const config in authConfigs) {
56
- if(typeof authConfigs[config] !== "object") {
57
- warn("Config must be an object");
58
- return;
67
+ if(KEYS_EXEMPTED_FROM_VALIDATION.includes(config)) {
68
+ //Skip validation
69
+ continue;
59
70
  }
60
- if (isEmpty(authConfigs[config])) {
61
- warn("Config is empty");
71
+ if(!["object"].includes(typeof authConfigs[config])) {
72
+ warn("Config must be an object");
62
73
  return;
63
74
  }
64
75
  }
@@ -72,5 +83,6 @@ export default {
72
83
  authHoc,
73
84
  hasRole,
74
85
  isCapUser,
86
+ hasFeatureAccess,
75
87
  }
76
88
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@capillarytech/cap-ui-utils",
3
- "version": "1.2.0",
3
+ "version": "1.2.2",
4
4
  "description": "Utility functions shared accross all the modules",
5
5
  "main": "index.js",
6
6
  "scripts": {