@onehat/ui 0.3.369 → 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
|
@@ -42,40 +42,39 @@ export function checkPermission(permission) {
|
|
|
42
42
|
*/
|
|
43
43
|
export function canUser(permission, modelToCheck = null) {
|
|
44
44
|
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
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;
|
|
57
71
|
}
|
|
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
|
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
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'
|
|
76
77
|
}
|
|
77
|
-
modelToCheck = Inflector.underscore(modelToCheck); // 'PmEvents' -> 'pm_events'
|
|
78
|
-
permission += '_' + modelToCheck; // e.g. 'view_pm_events'
|
|
79
78
|
}
|
|
80
79
|
|
|
81
80
|
return checkPermission(permission);
|
|
@@ -98,16 +97,22 @@ export default function withPermissions(WrappedComponent, forceUsePermissions =
|
|
|
98
97
|
model = Repository?.schema?.permissionsModel || Repository?.schema?.name, // so we can use an alternate model for permissions if needed
|
|
99
98
|
showPermissionsError = (permission, modelForAlert = null) => {
|
|
100
99
|
if (!modelForAlert) {
|
|
101
|
-
modelForAlert = model;
|
|
100
|
+
modelForAlert = model;
|
|
102
101
|
}
|
|
103
102
|
modelForAlert = Inflector.humanize(Inflector.underscore(modelForAlert)); // 'PmEvents' -> 'pm events'
|
|
104
103
|
|
|
105
104
|
alert(`You are not authorized to ${permission} ${modelForAlert}.`);
|
|
105
|
+
},
|
|
106
|
+
canUserDecorator = (permission, modelToCheck = null) => {
|
|
107
|
+
if (!modelToCheck) {
|
|
108
|
+
modelToCheck = model; // fallback to the model of the Repository
|
|
109
|
+
}
|
|
110
|
+
return canUser(permission, modelToCheck);
|
|
106
111
|
};
|
|
107
112
|
|
|
108
113
|
return <WrappedComponent
|
|
109
114
|
{...props}
|
|
110
|
-
canUser={
|
|
115
|
+
canUser={canUserDecorator}
|
|
111
116
|
showPermissionsError={showPermissionsError}
|
|
112
117
|
/>;
|
|
113
118
|
};
|