@webiny/app-security 5.27.0 → 5.28.0-beta.0
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/Security.js +21 -8
- package/Security.js.map +1 -1
- package/components/HasPermission.js +29 -11
- package/components/HasPermission.js.map +1 -1
- package/components/SecureRoute.js +17 -6
- package/components/SecureRoute.js.map +1 -1
- package/components/SecureView.js +11 -3
- package/components/SecureView.js.map +1 -1
- package/components/index.js +31 -3
- package/components/index.js.map +1 -1
- package/contexts/Security.js +34 -14
- package/contexts/Security.js.map +1 -1
- package/hooks/usePermission.js +11 -3
- package/hooks/usePermission.js.map +1 -1
- package/hooks/useSecurity.js +13 -4
- package/hooks/useSecurity.js.map +1 -1
- package/index.js +84 -6
- package/index.js.map +1 -1
- package/package.json +8 -8
- package/types.js +5 -1
- package/types.js.map +1 -1
package/Security.js
CHANGED
|
@@ -1,25 +1,38 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
|
|
4
|
+
|
|
5
|
+
Object.defineProperty(exports, "__esModule", {
|
|
6
|
+
value: true
|
|
7
|
+
});
|
|
8
|
+
exports.Security = void 0;
|
|
9
|
+
|
|
10
|
+
var _react = _interopRequireDefault(require("react"));
|
|
11
|
+
|
|
12
|
+
var _appAdminCore = require("@webiny/app-admin-core");
|
|
13
|
+
|
|
14
|
+
var _Security = require("./contexts/Security");
|
|
15
|
+
|
|
1
16
|
// Importing from `app-admin-core` and NOT from `app-admin`, to avoid circular dependency.
|
|
2
17
|
// This can be resolved in a different way, by changing the location of `AppInstaller` component (currently in `app-admin`).
|
|
3
18
|
// But this is a faster solution, as I'm really short on time :)
|
|
4
|
-
import React from "react";
|
|
5
|
-
import { Provider } from "@webiny/app-admin-core";
|
|
6
|
-
import { SecurityProvider as ContextProvider } from "./contexts/Security";
|
|
7
|
-
|
|
8
19
|
var SecurityProviderHOC = function SecurityProviderHOC(Component) {
|
|
9
20
|
return function SecurityProvider(_ref) {
|
|
10
21
|
var children = _ref.children;
|
|
11
|
-
return /*#__PURE__*/
|
|
22
|
+
return /*#__PURE__*/_react.default.createElement(_Security.SecurityProvider, null, /*#__PURE__*/_react.default.createElement(Component, null, children));
|
|
12
23
|
};
|
|
13
24
|
};
|
|
14
25
|
|
|
15
|
-
|
|
26
|
+
var Security = function Security() {
|
|
16
27
|
/**
|
|
17
28
|
* TODO @ts-refactor somewhere down the line in Provider.
|
|
18
29
|
* Provider expects ComponentType and we are giving it the React.FC.
|
|
19
30
|
* It works, just the types are wrong.
|
|
20
31
|
*/
|
|
21
32
|
// @ts-ignore
|
|
22
|
-
return /*#__PURE__*/
|
|
33
|
+
return /*#__PURE__*/_react.default.createElement(_appAdminCore.Provider, {
|
|
23
34
|
hoc: SecurityProviderHOC
|
|
24
35
|
});
|
|
25
|
-
};
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
exports.Security = Security;
|
package/Security.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"
|
|
1
|
+
{"version":3,"names":["SecurityProviderHOC","Component","SecurityProvider","children","Security"],"sources":["Security.tsx"],"sourcesContent":["// Importing from `app-admin-core` and NOT from `app-admin`, to avoid circular dependency.\n// This can be resolved in a different way, by changing the location of `AppInstaller` component (currently in `app-admin`).\n// But this is a faster solution, as I'm really short on time :)\nimport React from \"react\";\nimport { Provider } from \"@webiny/app-admin-core\";\nimport { SecurityProvider as ContextProvider } from \"./contexts/Security\";\n\nconst SecurityProviderHOC = (Component: React.FC<any>): React.FC<any> => {\n return function SecurityProvider({ children }) {\n return (\n <ContextProvider>\n <Component>{children}</Component>\n </ContextProvider>\n );\n };\n};\n\nexport const Security: React.FC = () => {\n /**\n * TODO @ts-refactor somewhere down the line in Provider.\n * Provider expects ComponentType and we are giving it the React.FC.\n * It works, just the types are wrong.\n */\n // @ts-ignore\n return <Provider hoc={SecurityProviderHOC} />;\n};\n"],"mappings":";;;;;;;;;AAGA;;AACA;;AACA;;AALA;AACA;AACA;AAKA,IAAMA,mBAAmB,GAAG,SAAtBA,mBAAsB,CAACC,SAAD,EAA6C;EACrE,OAAO,SAASC,gBAAT,OAAwC;IAAA,IAAZC,QAAY,QAAZA,QAAY;IAC3C,oBACI,6BAAC,0BAAD,qBACI,6BAAC,SAAD,QAAYA,QAAZ,CADJ,CADJ;EAKH,CAND;AAOH,CARD;;AAUO,IAAMC,QAAkB,GAAG,SAArBA,QAAqB,GAAM;EACpC;AACJ;AACA;AACA;AACA;EACI;EACA,oBAAO,6BAAC,sBAAD;IAAU,GAAG,EAAEJ;EAAf,EAAP;AACH,CARM"}
|
|
@@ -1,13 +1,29 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
|
|
4
|
+
|
|
5
|
+
var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard").default;
|
|
6
|
+
|
|
7
|
+
Object.defineProperty(exports, "__esModule", {
|
|
8
|
+
value: true
|
|
9
|
+
});
|
|
10
|
+
exports.HasPermission = void 0;
|
|
11
|
+
|
|
12
|
+
var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
|
|
13
|
+
|
|
14
|
+
var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));
|
|
15
|
+
|
|
16
|
+
var _react = _interopRequireWildcard(require("react"));
|
|
17
|
+
|
|
18
|
+
var _useSecurity2 = require("../hooks/useSecurity");
|
|
19
|
+
|
|
3
20
|
var _excluded = ["children"];
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
export var HasPermission = function HasPermission(_ref) {
|
|
21
|
+
|
|
22
|
+
var HasPermission = function HasPermission(_ref) {
|
|
7
23
|
var children = _ref.children,
|
|
8
|
-
props =
|
|
24
|
+
props = (0, _objectWithoutProperties2.default)(_ref, _excluded);
|
|
9
25
|
|
|
10
|
-
var _useSecurity = useSecurity(),
|
|
26
|
+
var _useSecurity = (0, _useSecurity2.useSecurity)(),
|
|
11
27
|
getPermission = _useSecurity.getPermission;
|
|
12
28
|
|
|
13
29
|
if (props.any && props.all) {
|
|
@@ -15,10 +31,10 @@ export var HasPermission = function HasPermission(_ref) {
|
|
|
15
31
|
}
|
|
16
32
|
|
|
17
33
|
if (props.name) {
|
|
18
|
-
return getPermission(props.name) ? /*#__PURE__*/
|
|
34
|
+
return getPermission(props.name) ? /*#__PURE__*/_react.default.createElement(_react.Fragment, null, children) : null;
|
|
19
35
|
}
|
|
20
36
|
|
|
21
|
-
var permissions = [].concat(
|
|
37
|
+
var permissions = [].concat((0, _toConsumableArray2.default)(props.any || []), (0, _toConsumableArray2.default)(props.all || [])).map(function (name) {
|
|
22
38
|
return getPermission(name);
|
|
23
39
|
});
|
|
24
40
|
var hasPermission = props.any ? permissions.some(function (p) {
|
|
@@ -26,5 +42,7 @@ export var HasPermission = function HasPermission(_ref) {
|
|
|
26
42
|
}) : permissions.every(function (p) {
|
|
27
43
|
return Boolean(p);
|
|
28
44
|
});
|
|
29
|
-
return hasPermission ? /*#__PURE__*/
|
|
30
|
-
};
|
|
45
|
+
return hasPermission ? /*#__PURE__*/_react.default.createElement(_react.Fragment, null, children) : null;
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
exports.HasPermission = HasPermission;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"
|
|
1
|
+
{"version":3,"names":["HasPermission","children","props","useSecurity","getPermission","any","all","Error","name","permissions","map","hasPermission","some","p","Boolean","every"],"sources":["HasPermission.tsx"],"sourcesContent":["import React, { Fragment } from \"react\";\nimport { useSecurity } from \"~/hooks/useSecurity\";\n\ninterface HasPermissionProps {\n any?: string[];\n all?: string[];\n name?: string;\n children: React.ReactNode;\n}\n\nexport const HasPermission = ({ children, ...props }: HasPermissionProps) => {\n const { getPermission } = useSecurity();\n\n if (props.any && props.all) {\n throw new Error(`You can use either \"any\" or \"all\", but not both at the same time.`);\n }\n\n if (props.name) {\n return getPermission(props.name) ? <Fragment>{children}</Fragment> : null;\n }\n\n const permissions = [...(props.any || []), ...(props.all || [])].map(name =>\n getPermission(name)\n );\n\n const hasPermission = props.any\n ? permissions.some(p => Boolean(p))\n : permissions.every(p => Boolean(p));\n\n return hasPermission ? <Fragment>{children}</Fragment> : null;\n};\n"],"mappings":";;;;;;;;;;;;;;;AAAA;;AACA;;;;AASO,IAAMA,aAAa,GAAG,SAAhBA,aAAgB,OAAgD;EAAA,IAA7CC,QAA6C,QAA7CA,QAA6C;EAAA,IAAhCC,KAAgC;;EACzE,mBAA0B,IAAAC,yBAAA,GAA1B;EAAA,IAAQC,aAAR,gBAAQA,aAAR;;EAEA,IAAIF,KAAK,CAACG,GAAN,IAAaH,KAAK,CAACI,GAAvB,EAA4B;IACxB,MAAM,IAAIC,KAAJ,yEAAN;EACH;;EAED,IAAIL,KAAK,CAACM,IAAV,EAAgB;IACZ,OAAOJ,aAAa,CAACF,KAAK,CAACM,IAAP,CAAb,gBAA4B,6BAAC,eAAD,QAAWP,QAAX,CAA5B,GAA8D,IAArE;EACH;;EAED,IAAMQ,WAAW,GAAG,2CAAKP,KAAK,CAACG,GAAN,IAAa,EAAlB,oCAA2BH,KAAK,CAACI,GAAN,IAAa,EAAxC,GAA6CI,GAA7C,CAAiD,UAAAF,IAAI;IAAA,OACrEJ,aAAa,CAACI,IAAD,CADwD;EAAA,CAArD,CAApB;EAIA,IAAMG,aAAa,GAAGT,KAAK,CAACG,GAAN,GAChBI,WAAW,CAACG,IAAZ,CAAiB,UAAAC,CAAC;IAAA,OAAIC,OAAO,CAACD,CAAD,CAAX;EAAA,CAAlB,CADgB,GAEhBJ,WAAW,CAACM,KAAZ,CAAkB,UAAAF,CAAC;IAAA,OAAIC,OAAO,CAACD,CAAD,CAAX;EAAA,CAAnB,CAFN;EAIA,OAAOF,aAAa,gBAAG,6BAAC,eAAD,QAAWV,QAAX,CAAH,GAAqC,IAAzD;AACH,CApBM"}
|
|
@@ -1,9 +1,18 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = void 0;
|
|
7
|
+
|
|
8
|
+
var _useSecurity = require("../hooks/useSecurity");
|
|
9
|
+
|
|
10
|
+
var _plugins = require("@webiny/plugins");
|
|
11
|
+
|
|
12
|
+
var _default = function _default(_ref) {
|
|
4
13
|
var children = _ref.children,
|
|
5
14
|
permission = _ref.permission;
|
|
6
|
-
var security = useSecurity();
|
|
15
|
+
var security = (0, _useSecurity.useSecurity)();
|
|
7
16
|
|
|
8
17
|
if (!security) {
|
|
9
18
|
return null;
|
|
@@ -26,11 +35,13 @@ export default (function (_ref) {
|
|
|
26
35
|
return children;
|
|
27
36
|
}
|
|
28
37
|
|
|
29
|
-
var plugin = plugins.byName("secure-route-error");
|
|
38
|
+
var plugin = _plugins.plugins.byName("secure-route-error");
|
|
30
39
|
|
|
31
40
|
if (!plugin) {
|
|
32
41
|
return null;
|
|
33
42
|
}
|
|
34
43
|
|
|
35
44
|
return plugin.render();
|
|
36
|
-
}
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
exports.default = _default;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"
|
|
1
|
+
{"version":3,"names":["children","permission","security","useSecurity","identity","getPermission","hasPermission","Boolean","plugin","plugins","byName","render"],"sources":["SecureRoute.tsx"],"sourcesContent":["import React from \"react\";\nimport { useSecurity } from \"~/hooks/useSecurity\";\nimport { SecureRouteErrorPlugin } from \"~/types\";\nimport { plugins } from \"@webiny/plugins\";\n\ninterface SecureRouteProps {\n children: React.ReactNode;\n permission?: string;\n}\nexport default ({ children, permission }: SecureRouteProps): React.ReactElement | null => {\n const security = useSecurity();\n\n if (!security) {\n return null;\n }\n\n const { identity, getPermission } = security;\n\n if (!identity) {\n return null;\n }\n\n let hasPermission = false;\n if (identity) {\n hasPermission = permission ? Boolean(getPermission(permission)) : true;\n }\n\n if (hasPermission) {\n return children as unknown as React.ReactElement;\n }\n\n const plugin = plugins.byName<SecureRouteErrorPlugin>(\"secure-route-error\");\n if (!plugin) {\n return null;\n }\n\n return plugin.render();\n};\n"],"mappings":";;;;;;;AACA;;AAEA;;eAMe,wBAA2E;EAAA,IAAxEA,QAAwE,QAAxEA,QAAwE;EAAA,IAA9DC,UAA8D,QAA9DA,UAA8D;EACtF,IAAMC,QAAQ,GAAG,IAAAC,wBAAA,GAAjB;;EAEA,IAAI,CAACD,QAAL,EAAe;IACX,OAAO,IAAP;EACH;;EAED,IAAQE,QAAR,GAAoCF,QAApC,CAAQE,QAAR;EAAA,IAAkBC,aAAlB,GAAoCH,QAApC,CAAkBG,aAAlB;;EAEA,IAAI,CAACD,QAAL,EAAe;IACX,OAAO,IAAP;EACH;;EAED,IAAIE,aAAa,GAAG,KAApB;;EACA,IAAIF,QAAJ,EAAc;IACVE,aAAa,GAAGL,UAAU,GAAGM,OAAO,CAACF,aAAa,CAACJ,UAAD,CAAd,CAAV,GAAwC,IAAlE;EACH;;EAED,IAAIK,aAAJ,EAAmB;IACf,OAAON,QAAP;EACH;;EAED,IAAMQ,MAAM,GAAGC,gBAAA,CAAQC,MAAR,CAAuC,oBAAvC,CAAf;;EACA,IAAI,CAACF,MAAL,EAAa;IACT,OAAO,IAAP;EACH;;EAED,OAAOA,MAAM,CAACG,MAAP,EAAP;AACH,C"}
|
package/components/SecureView.js
CHANGED
|
@@ -1,10 +1,17 @@
|
|
|
1
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = void 0;
|
|
7
|
+
|
|
8
|
+
var _useSecurity2 = require("../hooks/useSecurity");
|
|
2
9
|
|
|
3
10
|
function SecureView(_ref) {
|
|
4
11
|
var children = _ref.children,
|
|
5
12
|
permission = _ref.permission;
|
|
6
13
|
|
|
7
|
-
var _useSecurity = useSecurity(),
|
|
14
|
+
var _useSecurity = (0, _useSecurity2.useSecurity)(),
|
|
8
15
|
getPermission = _useSecurity.getPermission;
|
|
9
16
|
|
|
10
17
|
var hasPermission = false;
|
|
@@ -25,4 +32,5 @@ function SecureView(_ref) {
|
|
|
25
32
|
return hasPermission ? children : null;
|
|
26
33
|
}
|
|
27
34
|
|
|
28
|
-
|
|
35
|
+
var _default = SecureView;
|
|
36
|
+
exports.default = _default;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"
|
|
1
|
+
{"version":3,"names":["SecureView","children","permission","useSecurity","getPermission","hasPermission","matchedPermission","Boolean"],"sources":["SecureView.tsx"],"sourcesContent":["import * as React from \"react\";\nimport { useSecurity } from \"~/hooks/useSecurity\";\nimport { SecurityPermission } from \"~/types\";\n\ninterface ChildrenRenderFunctionArgs<T extends SecurityPermission> {\n hasPermission: boolean;\n permission: T | null;\n}\n\ninterface Props<T extends SecurityPermission> {\n children: ((args: ChildrenRenderFunctionArgs<T>) => React.ReactElement) | React.ReactElement;\n permission?: string;\n}\n\nfunction SecureView<T extends SecurityPermission>({\n children,\n permission\n}: Props<T>): React.ReactElement | null {\n const { getPermission } = useSecurity();\n\n let hasPermission = false;\n let matchedPermission: T | null = null;\n if (permission) {\n matchedPermission = getPermission<T>(permission);\n hasPermission = Boolean(matchedPermission);\n }\n\n if (typeof children === \"function\") {\n return children({\n hasPermission,\n permission: matchedPermission\n });\n }\n\n return hasPermission ? children : null;\n}\n\nexport default SecureView;\n"],"mappings":";;;;;;;AACA;;AAaA,SAASA,UAAT,OAGwC;EAAA,IAFpCC,QAEoC,QAFpCA,QAEoC;EAAA,IADpCC,UACoC,QADpCA,UACoC;;EACpC,mBAA0B,IAAAC,yBAAA,GAA1B;EAAA,IAAQC,aAAR,gBAAQA,aAAR;;EAEA,IAAIC,aAAa,GAAG,KAApB;EACA,IAAIC,iBAA2B,GAAG,IAAlC;;EACA,IAAIJ,UAAJ,EAAgB;IACZI,iBAAiB,GAAGF,aAAa,CAAIF,UAAJ,CAAjC;IACAG,aAAa,GAAGE,OAAO,CAACD,iBAAD,CAAvB;EACH;;EAED,IAAI,OAAOL,QAAP,KAAoB,UAAxB,EAAoC;IAChC,OAAOA,QAAQ,CAAC;MACZI,aAAa,EAAbA,aADY;MAEZH,UAAU,EAAEI;IAFA,CAAD,CAAf;EAIH;;EAED,OAAOD,aAAa,GAAGJ,QAAH,GAAc,IAAlC;AACH;;eAEcD,U"}
|
package/components/index.js
CHANGED
|
@@ -1,3 +1,31 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
|
|
4
|
+
|
|
5
|
+
Object.defineProperty(exports, "__esModule", {
|
|
6
|
+
value: true
|
|
7
|
+
});
|
|
8
|
+
Object.defineProperty(exports, "HasPermission", {
|
|
9
|
+
enumerable: true,
|
|
10
|
+
get: function get() {
|
|
11
|
+
return _HasPermission.HasPermission;
|
|
12
|
+
}
|
|
13
|
+
});
|
|
14
|
+
Object.defineProperty(exports, "SecureRoute", {
|
|
15
|
+
enumerable: true,
|
|
16
|
+
get: function get() {
|
|
17
|
+
return _SecureRoute.default;
|
|
18
|
+
}
|
|
19
|
+
});
|
|
20
|
+
Object.defineProperty(exports, "SecureView", {
|
|
21
|
+
enumerable: true,
|
|
22
|
+
get: function get() {
|
|
23
|
+
return _SecureView.default;
|
|
24
|
+
}
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
var _SecureView = _interopRequireDefault(require("./SecureView"));
|
|
28
|
+
|
|
29
|
+
var _SecureRoute = _interopRequireDefault(require("./SecureRoute"));
|
|
30
|
+
|
|
31
|
+
var _HasPermission = require("./HasPermission");
|
package/components/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"
|
|
1
|
+
{"version":3,"names":[],"sources":["index.ts"],"sourcesContent":["export { default as SecureView } from \"./SecureView\";\nexport { default as SecureRoute } from \"./SecureRoute\";\nexport { HasPermission } from \"./HasPermission\";\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;;AACA;;AACA"}
|
package/contexts/Security.js
CHANGED
|
@@ -1,8 +1,23 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard").default;
|
|
4
|
+
|
|
5
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
|
|
6
|
+
|
|
7
|
+
Object.defineProperty(exports, "__esModule", {
|
|
8
|
+
value: true
|
|
9
|
+
});
|
|
10
|
+
exports.SecurityProvider = exports.SecurityContext = void 0;
|
|
11
|
+
|
|
12
|
+
var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));
|
|
13
|
+
|
|
14
|
+
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
|
|
15
|
+
|
|
16
|
+
var _minimatch = _interopRequireDefault(require("minimatch"));
|
|
17
|
+
|
|
18
|
+
var _react = _interopRequireWildcard(require("react"));
|
|
19
|
+
|
|
20
|
+
var SecurityContext = /*#__PURE__*/_react.default.createContext({
|
|
6
21
|
identity: null,
|
|
7
22
|
setIdentity: function setIdentity() {
|
|
8
23
|
return void 0;
|
|
@@ -11,13 +26,16 @@ export var SecurityContext = /*#__PURE__*/React.createContext({
|
|
|
11
26
|
return null;
|
|
12
27
|
}
|
|
13
28
|
});
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
29
|
+
|
|
30
|
+
exports.SecurityContext = SecurityContext;
|
|
31
|
+
|
|
32
|
+
var SecurityProvider = function SecurityProvider(props) {
|
|
33
|
+
var _useState = (0, _react.useState)(null),
|
|
34
|
+
_useState2 = (0, _slicedToArray2.default)(_useState, 2),
|
|
17
35
|
identity = _useState2[0],
|
|
18
36
|
setIdentity = _useState2[1];
|
|
19
37
|
|
|
20
|
-
var getPermission = useCallback(function (name) {
|
|
38
|
+
var getPermission = (0, _react.useCallback)(function (name) {
|
|
21
39
|
if (!identity) {
|
|
22
40
|
return null;
|
|
23
41
|
}
|
|
@@ -33,12 +51,12 @@ export var SecurityProvider = function SecurityProvider(props) {
|
|
|
33
51
|
|
|
34
52
|
|
|
35
53
|
return perms.find(function (p) {
|
|
36
|
-
return
|
|
54
|
+
return (0, _minimatch.default)(name, p.name);
|
|
37
55
|
});
|
|
38
56
|
}, [identity]);
|
|
39
|
-
var value = useMemo(function () {
|
|
57
|
+
var value = (0, _react.useMemo)(function () {
|
|
40
58
|
return {
|
|
41
|
-
identity: identity ?
|
|
59
|
+
identity: identity ? (0, _objectSpread2.default)((0, _objectSpread2.default)({}, identity), {}, {
|
|
42
60
|
// For backwards compatibility, expose the `getPermission` method on the `identity` object.
|
|
43
61
|
getPermission: getPermission
|
|
44
62
|
}) : null,
|
|
@@ -46,7 +64,9 @@ export var SecurityProvider = function SecurityProvider(props) {
|
|
|
46
64
|
getPermission: getPermission
|
|
47
65
|
};
|
|
48
66
|
}, [identity]);
|
|
49
|
-
return /*#__PURE__*/
|
|
67
|
+
return /*#__PURE__*/_react.default.createElement(SecurityContext.Provider, {
|
|
50
68
|
value: value
|
|
51
69
|
}, props.children);
|
|
52
|
-
};
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
exports.SecurityProvider = SecurityProvider;
|
package/contexts/Security.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"
|
|
1
|
+
{"version":3,"names":["SecurityContext","React","createContext","identity","setIdentity","getPermission","SecurityProvider","props","useState","useCallback","name","perms","permissions","exactMatch","find","p","minimatch","value","useMemo","children"],"sources":["Security.tsx"],"sourcesContent":["import minimatch from \"minimatch\";\nimport React, { useState, useMemo, Dispatch, SetStateAction, useCallback } from \"react\";\nimport { SecurityIdentity, SecurityPermission } from \"~/types\";\n\nexport interface SecurityContext {\n identity: SecurityIdentity | null;\n setIdentity: Dispatch<SetStateAction<SecurityIdentity | null>>;\n getPermission<T extends SecurityPermission = SecurityPermission>(name: string): T | null;\n}\n\nexport const SecurityContext = React.createContext<SecurityContext>({\n identity: null,\n setIdentity: () => {\n return void 0;\n },\n getPermission: () => {\n return null;\n }\n});\n\nexport const SecurityProvider: React.FC = props => {\n const [identity, setIdentity] = useState<SecurityIdentity | null>(null);\n\n const getPermission = useCallback(\n <T extends SecurityPermission = SecurityPermission>(name: string): T | null => {\n if (!identity) {\n return null;\n }\n\n const perms = identity.permissions || [];\n const exactMatch = perms.find(p => p.name === name);\n if (exactMatch) {\n return exactMatch as T;\n }\n\n // Try matching using patterns\n return perms.find(p => minimatch(name, p.name)) as any;\n },\n [identity]\n );\n\n const value = useMemo(() => {\n return {\n identity: identity\n ? {\n ...identity,\n // For backwards compatibility, expose the `getPermission` method on the `identity` object.\n getPermission\n }\n : null,\n setIdentity,\n getPermission\n };\n }, [identity]);\n\n return <SecurityContext.Provider value={value}>{props.children}</SecurityContext.Provider>;\n};\n"],"mappings":";;;;;;;;;;;;;;;AAAA;;AACA;;AASO,IAAMA,eAAe,gBAAGC,cAAA,CAAMC,aAAN,CAAqC;EAChEC,QAAQ,EAAE,IADsD;EAEhEC,WAAW,EAAE,uBAAM;IACf,OAAO,KAAK,CAAZ;EACH,CAJ+D;EAKhEC,aAAa,EAAE,yBAAM;IACjB,OAAO,IAAP;EACH;AAP+D,CAArC,CAAxB;;;;AAUA,IAAMC,gBAA0B,GAAG,SAA7BA,gBAA6B,CAAAC,KAAK,EAAI;EAC/C,gBAAgC,IAAAC,eAAA,EAAkC,IAAlC,CAAhC;EAAA;EAAA,IAAOL,QAAP;EAAA,IAAiBC,WAAjB;;EAEA,IAAMC,aAAa,GAAG,IAAAI,kBAAA,EAClB,UAAoDC,IAApD,EAA+E;IAC3E,IAAI,CAACP,QAAL,EAAe;MACX,OAAO,IAAP;IACH;;IAED,IAAMQ,KAAK,GAAGR,QAAQ,CAACS,WAAT,IAAwB,EAAtC;IACA,IAAMC,UAAU,GAAGF,KAAK,CAACG,IAAN,CAAW,UAAAC,CAAC;MAAA,OAAIA,CAAC,CAACL,IAAF,KAAWA,IAAf;IAAA,CAAZ,CAAnB;;IACA,IAAIG,UAAJ,EAAgB;MACZ,OAAOA,UAAP;IACH,CAT0E,CAW3E;;;IACA,OAAOF,KAAK,CAACG,IAAN,CAAW,UAAAC,CAAC;MAAA,OAAI,IAAAC,kBAAA,EAAUN,IAAV,EAAgBK,CAAC,CAACL,IAAlB,CAAJ;IAAA,CAAZ,CAAP;EACH,CAdiB,EAelB,CAACP,QAAD,CAfkB,CAAtB;EAkBA,IAAMc,KAAK,GAAG,IAAAC,cAAA,EAAQ,YAAM;IACxB,OAAO;MACHf,QAAQ,EAAEA,QAAQ,+DAELA,QAFK;QAGR;QACAE,aAAa,EAAbA;MAJQ,KAMZ,IAPH;MAQHD,WAAW,EAAXA,WARG;MASHC,aAAa,EAAbA;IATG,CAAP;EAWH,CAZa,EAYX,CAACF,QAAD,CAZW,CAAd;EAcA,oBAAO,6BAAC,eAAD,CAAiB,QAAjB;IAA0B,KAAK,EAAEc;EAAjC,GAAyCV,KAAK,CAACY,QAA/C,CAAP;AACH,CApCM"}
|
package/hooks/usePermission.js
CHANGED
|
@@ -1,6 +1,14 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.usePermission = usePermission;
|
|
7
|
+
|
|
8
|
+
var _useSecurity2 = require("./useSecurity");
|
|
9
|
+
|
|
10
|
+
function usePermission(name) {
|
|
11
|
+
var _useSecurity = (0, _useSecurity2.useSecurity)(),
|
|
4
12
|
getPermission = _useSecurity.getPermission;
|
|
5
13
|
|
|
6
14
|
return getPermission(name);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"
|
|
1
|
+
{"version":3,"names":["usePermission","name","useSecurity","getPermission"],"sources":["usePermission.ts"],"sourcesContent":["import { useSecurity } from \"~/hooks/useSecurity\";\nimport { SecurityPermission } from \"~/types\";\n\nexport function usePermission<T extends SecurityPermission = SecurityPermission>(name: string) {\n const { getPermission } = useSecurity();\n return getPermission<T>(name);\n}\n"],"mappings":";;;;;;;AAAA;;AAGO,SAASA,aAAT,CAA0EC,IAA1E,EAAwF;EAC3F,mBAA0B,IAAAC,yBAAA,GAA1B;EAAA,IAAQC,aAAR,gBAAQA,aAAR;;EACA,OAAOA,aAAa,CAAIF,IAAJ,CAApB;AACH"}
|
package/hooks/useSecurity.js
CHANGED
|
@@ -1,5 +1,14 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.useSecurity = useSecurity;
|
|
7
|
+
|
|
8
|
+
var _react = require("react");
|
|
9
|
+
|
|
10
|
+
var _Security = require("../contexts/Security");
|
|
11
|
+
|
|
12
|
+
function useSecurity() {
|
|
13
|
+
return (0, _react.useContext)(_Security.SecurityContext);
|
|
5
14
|
}
|
package/hooks/useSecurity.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"
|
|
1
|
+
{"version":3,"names":["useSecurity","useContext","SecurityContext"],"sources":["useSecurity.ts"],"sourcesContent":["import { useContext } from \"react\";\nimport { SecurityContext } from \"~/contexts/Security\";\n\nexport function useSecurity() {\n return useContext(SecurityContext);\n}\n"],"mappings":";;;;;;;AAAA;;AACA;;AAEO,SAASA,WAAT,GAAuB;EAC1B,OAAO,IAAAC,iBAAA,EAAWC,yBAAX,CAAP;AACH"}
|
package/index.js
CHANGED
|
@@ -1,6 +1,84 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
var _exportNames = {
|
|
7
|
+
SecurityContext: true
|
|
8
|
+
};
|
|
9
|
+
Object.defineProperty(exports, "SecurityContext", {
|
|
10
|
+
enumerable: true,
|
|
11
|
+
get: function get() {
|
|
12
|
+
return _Security.SecurityContext;
|
|
13
|
+
}
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
var _components = require("./components");
|
|
17
|
+
|
|
18
|
+
Object.keys(_components).forEach(function (key) {
|
|
19
|
+
if (key === "default" || key === "__esModule") return;
|
|
20
|
+
if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
|
|
21
|
+
if (key in exports && exports[key] === _components[key]) return;
|
|
22
|
+
Object.defineProperty(exports, key, {
|
|
23
|
+
enumerable: true,
|
|
24
|
+
get: function get() {
|
|
25
|
+
return _components[key];
|
|
26
|
+
}
|
|
27
|
+
});
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
var _Security = require("./contexts/Security");
|
|
31
|
+
|
|
32
|
+
Object.keys(_Security).forEach(function (key) {
|
|
33
|
+
if (key === "default" || key === "__esModule") return;
|
|
34
|
+
if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
|
|
35
|
+
if (key in exports && exports[key] === _Security[key]) return;
|
|
36
|
+
Object.defineProperty(exports, key, {
|
|
37
|
+
enumerable: true,
|
|
38
|
+
get: function get() {
|
|
39
|
+
return _Security[key];
|
|
40
|
+
}
|
|
41
|
+
});
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
var _useSecurity = require("./hooks/useSecurity");
|
|
45
|
+
|
|
46
|
+
Object.keys(_useSecurity).forEach(function (key) {
|
|
47
|
+
if (key === "default" || key === "__esModule") return;
|
|
48
|
+
if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
|
|
49
|
+
if (key in exports && exports[key] === _useSecurity[key]) return;
|
|
50
|
+
Object.defineProperty(exports, key, {
|
|
51
|
+
enumerable: true,
|
|
52
|
+
get: function get() {
|
|
53
|
+
return _useSecurity[key];
|
|
54
|
+
}
|
|
55
|
+
});
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
var _usePermission = require("./hooks/usePermission");
|
|
59
|
+
|
|
60
|
+
Object.keys(_usePermission).forEach(function (key) {
|
|
61
|
+
if (key === "default" || key === "__esModule") return;
|
|
62
|
+
if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
|
|
63
|
+
if (key in exports && exports[key] === _usePermission[key]) return;
|
|
64
|
+
Object.defineProperty(exports, key, {
|
|
65
|
+
enumerable: true,
|
|
66
|
+
get: function get() {
|
|
67
|
+
return _usePermission[key];
|
|
68
|
+
}
|
|
69
|
+
});
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
var _Security2 = require("./Security");
|
|
73
|
+
|
|
74
|
+
Object.keys(_Security2).forEach(function (key) {
|
|
75
|
+
if (key === "default" || key === "__esModule") return;
|
|
76
|
+
if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
|
|
77
|
+
if (key in exports && exports[key] === _Security2[key]) return;
|
|
78
|
+
Object.defineProperty(exports, key, {
|
|
79
|
+
enumerable: true,
|
|
80
|
+
get: function get() {
|
|
81
|
+
return _Security2[key];
|
|
82
|
+
}
|
|
83
|
+
});
|
|
84
|
+
});
|
package/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"
|
|
1
|
+
{"version":3,"names":[],"sources":["index.ts"],"sourcesContent":["export * from \"./components\";\nexport * from \"./contexts/Security\";\nexport { SecurityContext } from \"./contexts/Security\";\nexport * from \"./hooks/useSecurity\";\nexport * from \"./hooks/usePermission\";\nexport * from \"./Security\";\n"],"mappings":";;;;;;;;;;;;;;;AAAA;;AAAA;EAAA;EAAA;EAAA;EAAA;IAAA;IAAA;MAAA;IAAA;EAAA;AAAA;;AACA;;AAAA;EAAA;EAAA;EAAA;EAAA;IAAA;IAAA;MAAA;IAAA;EAAA;AAAA;;AAEA;;AAAA;EAAA;EAAA;EAAA;EAAA;IAAA;IAAA;MAAA;IAAA;EAAA;AAAA;;AACA;;AAAA;EAAA;EAAA;EAAA;EAAA;IAAA;IAAA;MAAA;IAAA;EAAA;AAAA;;AACA;;AAAA;EAAA;EAAA;EAAA;EAAA;IAAA;IAAA;MAAA;IAAA;EAAA;AAAA"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@webiny/app-security",
|
|
3
|
-
"version": "5.
|
|
3
|
+
"version": "5.28.0-beta.0",
|
|
4
4
|
"main": "index.js",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -13,10 +13,10 @@
|
|
|
13
13
|
],
|
|
14
14
|
"license": "MIT",
|
|
15
15
|
"dependencies": {
|
|
16
|
-
"@webiny/app": "5.
|
|
17
|
-
"@webiny/app-admin-core": "5.
|
|
18
|
-
"@webiny/plugins": "5.
|
|
19
|
-
"minimatch": "3.
|
|
16
|
+
"@webiny/app": "5.28.0-beta.0",
|
|
17
|
+
"@webiny/app-admin-core": "5.28.0-beta.0",
|
|
18
|
+
"@webiny/plugins": "5.28.0-beta.0",
|
|
19
|
+
"minimatch": "3.1.2",
|
|
20
20
|
"react": "16.14.0",
|
|
21
21
|
"react-dom": "16.14.0"
|
|
22
22
|
},
|
|
@@ -27,8 +27,8 @@
|
|
|
27
27
|
"@babel/preset-env": "^7.16.4",
|
|
28
28
|
"@babel/preset-react": "^7.16.0",
|
|
29
29
|
"@babel/preset-typescript": "^7.16.0",
|
|
30
|
-
"@webiny/cli": "^5.
|
|
31
|
-
"@webiny/project-utils": "^5.
|
|
30
|
+
"@webiny/cli": "^5.28.0-beta.0",
|
|
31
|
+
"@webiny/project-utils": "^5.28.0-beta.0",
|
|
32
32
|
"babel-plugin-emotion": "^9.2.8",
|
|
33
33
|
"babel-plugin-lodash": "^3.3.4",
|
|
34
34
|
"rimraf": "^3.0.2",
|
|
@@ -50,5 +50,5 @@
|
|
|
50
50
|
]
|
|
51
51
|
}
|
|
52
52
|
},
|
|
53
|
-
"gitHead": "
|
|
53
|
+
"gitHead": "6503c7f86cfc90660f005acf72049505bfd4fb76"
|
|
54
54
|
}
|
package/types.js
CHANGED
package/types.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"
|
|
1
|
+
{"version":3,"names":[],"sources":["types.ts"],"sourcesContent":["import { Plugin } from \"@webiny/app/types\";\n\nexport type SecureRouteErrorPlugin = Plugin & {\n render(): React.ReactElement;\n};\n\nexport interface FullAccessPermission {\n name: \"*\";\n}\n\nexport interface SecurityPermission {\n name: string;\n [key: string]: any;\n}\n\nexport interface SecurityIdentity {\n id: string;\n type: string;\n displayName: string;\n permissions?: SecurityPermission[];\n /**\n * TODO @ts-refactor @pavel\n * Verify that login can be present in here.\n */\n login?: string;\n /**\n * TODO @ts-refactor @pavel\n * Verify that profile can be present in here.\n */\n profile?: {\n email?: string;\n firstName?: string;\n lastName?: string;\n avatar?: {\n src?: string;\n };\n gravatar?: string;\n };\n logout(): void;\n // For backwards compatibility, expose the `getPermission` method on the `identity` object.\n getPermission?<T extends SecurityPermission = SecurityPermission>(name: string): T | null;\n [key: string]: any;\n}\n"],"mappings":""}
|