@onehat/ui 0.3.369 → 0.3.371

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.371",
4
4
  "description": "Base UI for OneHat apps",
5
5
  "main": "src/index.js",
6
6
  "type": "module",
@@ -1,3 +1,4 @@
1
+ import { forwardRef } from 'react';
1
2
  import Inflector from 'inflector-js';
2
3
  import inArray from '../../Functions/inArray.js';
3
4
  import {
@@ -42,47 +43,46 @@ export function checkPermission(permission) {
42
43
  */
43
44
  export function canUser(permission, modelToCheck = null) {
44
45
 
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;
46
+ if (modelToCheck) {
47
+ // deal with special cases that refer to other permissions
48
+ switch(permission) {
49
+ case PRINT:
50
+ permission = VIEW; // correct; doesn't recursively call canUser(), just continues on with this permission
51
+ break;
52
+ case COPY:
53
+ case DUPLICATE: {
54
+ // user must have ADD _and_ EDIT permissions, so check both
55
+ const
56
+ hasAddPermission = canUser(ADD, modelToCheck),
57
+ hasEditPermission = canUser(EDIT, modelToCheck);
58
+ return hasAddPermission && hasEditPermission;
59
+ }
60
+ case UPLOAD_DOWNLOAD: {
61
+ // user must have VIEW, ADD, EDIT, and DELETE permissions, so check all of them
62
+ const
63
+ hasViewPermission = canUser(VIEW, modelToCheck),
64
+ hasAddPermission = canUser(ADD, modelToCheck),
65
+ hasEditPermission = canUser(EDIT, modelToCheck),
66
+ hasDeletePermission = canUser(DELETE, modelToCheck);
67
+ return hasViewPermission && hasAddPermission && hasEditPermission && hasDeletePermission;
68
+ }
69
+ default:
70
+ // do nothing
71
+ break;
57
72
  }
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
73
 
72
- // standard CRUD permissions
73
- if (inArray(permission, [VIEW, ADD, EDIT, DELETE])) {
74
- if (!modelToCheck) {
75
- modelToCheck = model; // use default model if none supplied
74
+ // standard CRUD permissions
75
+ if (inArray(permission, [VIEW, ADD, EDIT, DELETE])) {
76
+ modelToCheck = Inflector.underscore(modelToCheck); // 'PmEvents' -> 'pm_events'
77
+ permission += '_' + modelToCheck; // e.g. 'view_pm_events'
76
78
  }
77
- modelToCheck = Inflector.underscore(modelToCheck); // 'PmEvents' -> 'pm_events'
78
- permission += '_' + modelToCheck; // e.g. 'view_pm_events'
79
79
  }
80
80
 
81
81
  return checkPermission(permission);
82
82
  }
83
83
 
84
84
  export default function withPermissions(WrappedComponent, forceUsePermissions = false) {
85
- return (props) => {
85
+ return forwardRef((props, ref) => {
86
86
 
87
87
  if (!props.usePermissions && !forceUsePermissions) {
88
88
  return <WrappedComponent {...props} />;
@@ -98,17 +98,23 @@ export default function withPermissions(WrappedComponent, forceUsePermissions =
98
98
  model = Repository?.schema?.permissionsModel || Repository?.schema?.name, // so we can use an alternate model for permissions if needed
99
99
  showPermissionsError = (permission, modelForAlert = null) => {
100
100
  if (!modelForAlert) {
101
- modelForAlert = model; // use default model if none supplied
101
+ modelForAlert = model;
102
102
  }
103
103
  modelForAlert = Inflector.humanize(Inflector.underscore(modelForAlert)); // 'PmEvents' -> 'pm events'
104
104
 
105
105
  alert(`You are not authorized to ${permission} ${modelForAlert}.`);
106
+ },
107
+ canUserDecorator = (permission, modelToCheck = null) => {
108
+ if (!modelToCheck) {
109
+ modelToCheck = model; // fallback to the model of the Repository
110
+ }
111
+ return canUser(permission, modelToCheck);
106
112
  };
107
113
 
108
114
  return <WrappedComponent
109
115
  {...props}
110
- canUser={canUser}
116
+ canUser={canUserDecorator}
111
117
  showPermissionsError={showPermissionsError}
112
118
  />;
113
- };
119
+ });
114
120
  }