@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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@onehat/ui",
3
- "version": "0.3.369",
3
+ "version": "0.3.370",
4
4
  "description": "Base UI for OneHat apps",
5
5
  "main": "src/index.js",
6
6
  "type": "module",
@@ -42,40 +42,39 @@ export function checkPermission(permission) {
42
42
  */
43
43
  export function canUser(permission, modelToCheck = null) {
44
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;
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
- // standard CRUD permissions
73
- if (inArray(permission, [VIEW, ADD, EDIT, DELETE])) {
74
- if (!modelToCheck) {
75
- modelToCheck = model; // use default model if none supplied
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; // use default model if none supplied
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={canUser}
115
+ canUser={canUserDecorator}
111
116
  showPermissionsError={showPermissionsError}
112
117
  />;
113
118
  };