@onehat/ui 0.3.366 → 0.3.369
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/package.json
CHANGED
|
@@ -13,6 +13,73 @@ import {
|
|
|
13
13
|
import UiGlobals from '../../UiGlobals.js';
|
|
14
14
|
import _ from 'lodash';
|
|
15
15
|
|
|
16
|
+
/**
|
|
17
|
+
* checkPermission
|
|
18
|
+
* @param {string} permission like 'view_pm_events'
|
|
19
|
+
* @returns {boolean} - Whether permission is permitted
|
|
20
|
+
*/
|
|
21
|
+
export function checkPermission(permission) {
|
|
22
|
+
const
|
|
23
|
+
reduxState = UiGlobals.redux?.getState(),
|
|
24
|
+
permissions = reduxState?.app?.permissions;
|
|
25
|
+
if (!permissions) {
|
|
26
|
+
return false;
|
|
27
|
+
}
|
|
28
|
+
return inArray(permission, permissions);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Check if user has permission to perform an action
|
|
33
|
+
*
|
|
34
|
+
* Example usages:
|
|
35
|
+
* canUser('view') // check if user can perform 'view' action on the default model
|
|
36
|
+
* canUser('add', 'PmEvents') // check if user can perform 'add' action on a specific model
|
|
37
|
+
* canUser('do_something_else) // check if user has a custom permission
|
|
38
|
+
*
|
|
39
|
+
* @param {string} permission - The permission to check for.
|
|
40
|
+
* @param {string} modelToCheck - The model to check for the permission on.
|
|
41
|
+
* @returns {boolean} - Whether user has permission
|
|
42
|
+
*/
|
|
43
|
+
export function canUser(permission, modelToCheck = null) {
|
|
44
|
+
|
|
45
|
+
// deal with special cases that refer to other permissions
|
|
46
|
+
switch(permission) {
|
|
47
|
+
case PRINT:
|
|
48
|
+
permission = VIEW;
|
|
49
|
+
break;
|
|
50
|
+
case COPY:
|
|
51
|
+
case DUPLICATE: {
|
|
52
|
+
// user must have ADD _and_ EDIT permissions, so check both
|
|
53
|
+
const
|
|
54
|
+
hasAddPermission = canUser(ADD, modelToCheck),
|
|
55
|
+
hasEditPermission = canUser(EDIT, modelToCheck);
|
|
56
|
+
return hasAddPermission && hasEditPermission;
|
|
57
|
+
}
|
|
58
|
+
case UPLOAD_DOWNLOAD: {
|
|
59
|
+
// user must have VIEW, ADD, EDIT, and DELETE permissions, so check all of them
|
|
60
|
+
const
|
|
61
|
+
hasViewPermission = canUser(VIEW, modelToCheck),
|
|
62
|
+
hasAddPermission = canUser(ADD, modelToCheck),
|
|
63
|
+
hasEditPermission = canUser(EDIT, modelToCheck),
|
|
64
|
+
hasDeletePermission = canUser(DELETE, modelToCheck);
|
|
65
|
+
return hasViewPermission && hasAddPermission && hasEditPermission && hasDeletePermission;
|
|
66
|
+
}
|
|
67
|
+
default:
|
|
68
|
+
// do nothing
|
|
69
|
+
break;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// standard CRUD permissions
|
|
73
|
+
if (inArray(permission, [VIEW, ADD, EDIT, DELETE])) {
|
|
74
|
+
if (!modelToCheck) {
|
|
75
|
+
modelToCheck = model; // use default model if none supplied
|
|
76
|
+
}
|
|
77
|
+
modelToCheck = Inflector.underscore(modelToCheck); // 'PmEvents' -> 'pm_events'
|
|
78
|
+
permission += '_' + modelToCheck; // e.g. 'view_pm_events'
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
return checkPermission(permission);
|
|
82
|
+
}
|
|
16
83
|
|
|
17
84
|
export default function withPermissions(WrappedComponent, forceUsePermissions = false) {
|
|
18
85
|
return (props) => {
|
|
@@ -29,16 +96,6 @@ export default function withPermissions(WrappedComponent, forceUsePermissions =
|
|
|
29
96
|
Repository,
|
|
30
97
|
} = props,
|
|
31
98
|
model = Repository?.schema?.permissionsModel || Repository?.schema?.name, // so we can use an alternate model for permissions if needed
|
|
32
|
-
checkPermission = (permission) => {
|
|
33
|
-
const
|
|
34
|
-
reduxState = UiGlobals.redux?.getState(),
|
|
35
|
-
permissions = reduxState?.app?.permissions;
|
|
36
|
-
if (!permissions) {
|
|
37
|
-
return false;
|
|
38
|
-
}
|
|
39
|
-
return inArray(permission, permissions);
|
|
40
|
-
},
|
|
41
|
-
|
|
42
99
|
showPermissionsError = (permission, modelForAlert = null) => {
|
|
43
100
|
if (!modelForAlert) {
|
|
44
101
|
modelForAlert = model; // use default model if none supplied
|
|
@@ -46,59 +103,6 @@ export default function withPermissions(WrappedComponent, forceUsePermissions =
|
|
|
46
103
|
modelForAlert = Inflector.humanize(Inflector.underscore(modelForAlert)); // 'PmEvents' -> 'pm events'
|
|
47
104
|
|
|
48
105
|
alert(`You are not authorized to ${permission} ${modelForAlert}.`);
|
|
49
|
-
},
|
|
50
|
-
|
|
51
|
-
/**
|
|
52
|
-
* Check if user has permission to perform an action
|
|
53
|
-
*
|
|
54
|
-
* Example usages:
|
|
55
|
-
* canUser('view') // check if user can perform 'view' action on the default model
|
|
56
|
-
* canUser('add', 'PmEvents') // check if user can perform 'add' action on a specific model
|
|
57
|
-
* canUser('do_something_else) // check if user has a custom permission
|
|
58
|
-
*
|
|
59
|
-
* @param {string} permission - The permission to check for.
|
|
60
|
-
* @param {string} modelToCheck - The model to check for the permission on.
|
|
61
|
-
* @returns {boolean} - Whether user has permission
|
|
62
|
-
*/
|
|
63
|
-
canUser = (permission, modelToCheck = null) => {
|
|
64
|
-
|
|
65
|
-
// deal with special cases that refer to other permissions
|
|
66
|
-
switch(permission) {
|
|
67
|
-
case PRINT:
|
|
68
|
-
permission = VIEW;
|
|
69
|
-
break;
|
|
70
|
-
case COPY:
|
|
71
|
-
case DUPLICATE: {
|
|
72
|
-
// user must have ADD _and_ EDIT permissions, so check both
|
|
73
|
-
const
|
|
74
|
-
hasAddPermission = canUser(ADD, modelToCheck),
|
|
75
|
-
hasEditPermission = canUser(EDIT, modelToCheck);
|
|
76
|
-
return hasAddPermission && hasEditPermission;
|
|
77
|
-
}
|
|
78
|
-
case UPLOAD_DOWNLOAD: {
|
|
79
|
-
// user must have VIEW, ADD, EDIT, and DELETE permissions, so check all of them
|
|
80
|
-
const
|
|
81
|
-
hasViewPermission = canUser(VIEW, modelToCheck),
|
|
82
|
-
hasAddPermission = canUser(ADD, modelToCheck),
|
|
83
|
-
hasEditPermission = canUser(EDIT, modelToCheck),
|
|
84
|
-
hasDeletePermission = canUser(DELETE, modelToCheck);
|
|
85
|
-
return hasViewPermission && hasAddPermission && hasEditPermission && hasDeletePermission;
|
|
86
|
-
}
|
|
87
|
-
default:
|
|
88
|
-
// do nothing
|
|
89
|
-
break;
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
// standard CRUD permissions
|
|
93
|
-
if (inArray(permission, [VIEW, ADD, EDIT, DELETE])) {
|
|
94
|
-
if (!modelToCheck) {
|
|
95
|
-
modelToCheck = model; // use default model if none supplied
|
|
96
|
-
}
|
|
97
|
-
modelToCheck = Inflector.underscore(modelToCheck); // 'PmEvents' -> 'pm_events'
|
|
98
|
-
permission += '_' + modelToCheck; // e.g. 'view_pm_events'
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
return checkPermission(permission);
|
|
102
106
|
};
|
|
103
107
|
|
|
104
108
|
return <WrappedComponent
|