@onehat/ui 0.3.367 → 0.3.370
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,72 @@ 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
|
+
if (modelToCheck) {
|
|
46
|
+
// deal with special cases that refer to other permissions
|
|
47
|
+
switch(permission) {
|
|
48
|
+
case PRINT:
|
|
49
|
+
permission = VIEW; // correct; doesn't recursively call canUser(), just continues on with this permission
|
|
50
|
+
break;
|
|
51
|
+
case COPY:
|
|
52
|
+
case DUPLICATE: {
|
|
53
|
+
// user must have ADD _and_ EDIT permissions, so check both
|
|
54
|
+
const
|
|
55
|
+
hasAddPermission = canUser(ADD, modelToCheck),
|
|
56
|
+
hasEditPermission = canUser(EDIT, modelToCheck);
|
|
57
|
+
return hasAddPermission && hasEditPermission;
|
|
58
|
+
}
|
|
59
|
+
case UPLOAD_DOWNLOAD: {
|
|
60
|
+
// user must have VIEW, ADD, EDIT, and DELETE permissions, so check all of them
|
|
61
|
+
const
|
|
62
|
+
hasViewPermission = canUser(VIEW, modelToCheck),
|
|
63
|
+
hasAddPermission = canUser(ADD, modelToCheck),
|
|
64
|
+
hasEditPermission = canUser(EDIT, modelToCheck),
|
|
65
|
+
hasDeletePermission = canUser(DELETE, modelToCheck);
|
|
66
|
+
return hasViewPermission && hasAddPermission && hasEditPermission && hasDeletePermission;
|
|
67
|
+
}
|
|
68
|
+
default:
|
|
69
|
+
// do nothing
|
|
70
|
+
break;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
// standard CRUD permissions
|
|
74
|
+
if (inArray(permission, [VIEW, ADD, EDIT, DELETE])) {
|
|
75
|
+
modelToCheck = Inflector.underscore(modelToCheck); // 'PmEvents' -> 'pm_events'
|
|
76
|
+
permission += '_' + modelToCheck; // e.g. 'view_pm_events'
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
return checkPermission(permission);
|
|
81
|
+
}
|
|
16
82
|
|
|
17
83
|
export default function withPermissions(WrappedComponent, forceUsePermissions = false) {
|
|
18
84
|
return (props) => {
|
|
@@ -29,81 +95,24 @@ export default function withPermissions(WrappedComponent, forceUsePermissions =
|
|
|
29
95
|
Repository,
|
|
30
96
|
} = props,
|
|
31
97
|
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
98
|
showPermissionsError = (permission, modelForAlert = null) => {
|
|
43
99
|
if (!modelForAlert) {
|
|
44
|
-
modelForAlert = model;
|
|
100
|
+
modelForAlert = model;
|
|
45
101
|
}
|
|
46
102
|
modelForAlert = Inflector.humanize(Inflector.underscore(modelForAlert)); // 'PmEvents' -> 'pm events'
|
|
47
103
|
|
|
48
104
|
alert(`You are not authorized to ${permission} ${modelForAlert}.`);
|
|
49
105
|
},
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
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'
|
|
106
|
+
canUserDecorator = (permission, modelToCheck = null) => {
|
|
107
|
+
if (!modelToCheck) {
|
|
108
|
+
modelToCheck = model; // fallback to the model of the Repository
|
|
99
109
|
}
|
|
100
|
-
|
|
101
|
-
return checkPermission(permission);
|
|
110
|
+
return canUser(permission, modelToCheck);
|
|
102
111
|
};
|
|
103
112
|
|
|
104
113
|
return <WrappedComponent
|
|
105
114
|
{...props}
|
|
106
|
-
canUser={
|
|
115
|
+
canUser={canUserDecorator}
|
|
107
116
|
showPermissionsError={showPermissionsError}
|
|
108
117
|
/>;
|
|
109
118
|
};
|