@webiny/app-security 5.20.0-beta.1 → 5.21.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.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ /// <reference types="react" />
2
+ export declare const Security: () => JSX.Element;
package/Security.js ADDED
@@ -0,0 +1,19 @@
1
+ // Importing from `app-admin-core` and NOT from `app-admin`, to avoid circular dependency.
2
+ // This can be resolved in a different way, by changing the location of `AppInstaller` component (currently in `app-admin`).
3
+ // But this is a faster solution, as I'm really short on time :)
4
+ import { Provider } from "@webiny/app-admin-core";
5
+ import React from "react";
6
+ import { SecurityProvider as ContextProvider } from "./contexts/Security";
7
+
8
+ var SecurityProviderHOC = function SecurityProviderHOC(Component) {
9
+ return function SecurityProvider(_ref) {
10
+ var children = _ref.children;
11
+ return /*#__PURE__*/React.createElement(ContextProvider, null, /*#__PURE__*/React.createElement(Component, null, children));
12
+ };
13
+ };
14
+
15
+ export var Security = function Security() {
16
+ return /*#__PURE__*/React.createElement(Provider, {
17
+ hoc: SecurityProviderHOC
18
+ });
19
+ };
@@ -0,0 +1,9 @@
1
+ import React from "react";
2
+ interface HasPermissionProps {
3
+ any?: string[];
4
+ all?: string[];
5
+ name?: string;
6
+ children: React.ReactNode;
7
+ }
8
+ export declare const HasPermission: ({ children, ...props }: HasPermissionProps) => JSX.Element;
9
+ export {};
@@ -0,0 +1,30 @@
1
+ import _toConsumableArray from "@babel/runtime/helpers/toConsumableArray";
2
+ import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
3
+ var _excluded = ["children"];
4
+ import React, { Fragment } from "react";
5
+ import { useSecurity } from "../hooks/useSecurity";
6
+ export var HasPermission = function HasPermission(_ref) {
7
+ var children = _ref.children,
8
+ props = _objectWithoutProperties(_ref, _excluded);
9
+
10
+ var _useSecurity = useSecurity(),
11
+ getPermission = _useSecurity.getPermission;
12
+
13
+ if (props.any && props.all) {
14
+ throw new Error("You can use either \"any\" or \"all\", but not both at the same time.");
15
+ }
16
+
17
+ if (props.name) {
18
+ return getPermission(props.name) ? /*#__PURE__*/React.createElement(Fragment, null, children) : null;
19
+ }
20
+
21
+ var permissions = [].concat(_toConsumableArray(props.any || []), _toConsumableArray(props.all || [])).map(function (name) {
22
+ return getPermission(name);
23
+ });
24
+ var hasPermission = props.any ? permissions.some(function (p) {
25
+ return Boolean(p);
26
+ }) : permissions.every(function (p) {
27
+ return Boolean(p);
28
+ });
29
+ return hasPermission ? /*#__PURE__*/React.createElement(Fragment, null, children) : null;
30
+ };
@@ -1,11 +1,19 @@
1
- import { useSecurity } from "..";
1
+ import { useSecurity } from "../hooks/useSecurity";
2
2
  import { plugins } from "@webiny/plugins";
3
3
  export default (function (_ref) {
4
4
  var children = _ref.children,
5
5
  permission = _ref.permission;
6
+ var security = useSecurity();
6
7
 
7
- var _useSecurity = useSecurity(),
8
- identity = _useSecurity.identity;
8
+ if (!security) {
9
+ return null;
10
+ }
11
+
12
+ var identity = security.identity;
13
+
14
+ if (!identity) {
15
+ return null;
16
+ }
9
17
 
10
18
  var hasPermission = false;
11
19
 
@@ -1,4 +1,4 @@
1
- import { useSecurity } from "..";
1
+ import { useSecurity } from "../hooks/useSecurity";
2
2
 
3
3
  function SecureView(_ref) {
4
4
  var children = _ref.children,
@@ -1,2 +1,3 @@
1
1
  export { default as SecureView } from "./SecureView";
2
2
  export { default as SecureRoute } from "./SecureRoute";
3
+ export { HasPermission } from "./HasPermission";
@@ -1,2 +1,3 @@
1
1
  export { default as SecureView } from "./SecureView";
2
- export { default as SecureRoute } from "./SecureRoute";
2
+ export { default as SecureRoute } from "./SecureRoute";
3
+ export { HasPermission } from "./HasPermission";
@@ -0,0 +1,2 @@
1
+ import { SecurityPermission } from "../types";
2
+ export declare function usePermission<T extends SecurityPermission = SecurityPermission>(name: string): T;
@@ -0,0 +1,7 @@
1
+ import { useSecurity } from "./useSecurity";
2
+ export function usePermission(name) {
3
+ var _useSecurity = useSecurity(),
4
+ getPermission = _useSecurity.getPermission;
5
+
6
+ return getPermission(name);
7
+ }
package/index.d.ts CHANGED
@@ -1,3 +1,5 @@
1
1
  export * from "./components";
2
2
  export * from "./contexts/Security";
3
3
  export * from "./hooks/useSecurity";
4
+ export * from "./hooks/usePermission";
5
+ export * from "./Security";
package/index.js CHANGED
@@ -1,3 +1,5 @@
1
1
  export * from "./components";
2
2
  export * from "./contexts/Security";
3
- export * from "./hooks/useSecurity";
3
+ export * from "./hooks/useSecurity";
4
+ export * from "./hooks/usePermission";
5
+ export * from "./Security";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@webiny/app-security",
3
- "version": "5.20.0-beta.1",
3
+ "version": "5.21.0",
4
4
  "main": "index.js",
5
5
  "repository": {
6
6
  "type": "git",
@@ -13,8 +13,9 @@
13
13
  ],
14
14
  "license": "MIT",
15
15
  "dependencies": {
16
- "@webiny/app": "5.20.0-beta.1",
17
- "@webiny/plugins": "5.20.0-beta.1",
16
+ "@webiny/app": "5.21.0",
17
+ "@webiny/app-admin-core": "5.21.0",
18
+ "@webiny/plugins": "5.21.0",
18
19
  "minimatch": "3.0.4",
19
20
  "react": "16.14.0",
20
21
  "react-dom": "16.14.0"
@@ -26,8 +27,8 @@
26
27
  "@babel/preset-env": "^7.5.5",
27
28
  "@babel/preset-react": "^7.0.0",
28
29
  "@babel/preset-typescript": "^7.8.3",
29
- "@webiny/cli": "^5.20.0-beta.1",
30
- "@webiny/project-utils": "^5.20.0-beta.1",
30
+ "@webiny/cli": "^5.21.0",
31
+ "@webiny/project-utils": "^5.21.0",
31
32
  "babel-plugin-emotion": "^9.2.8",
32
33
  "babel-plugin-lodash": "^3.3.4",
33
34
  "babel-plugin-named-asset-import": "^1.0.0-next.3e165448",
@@ -55,5 +56,5 @@
55
56
  ]
56
57
  }
57
58
  },
58
- "gitHead": "40e30fb4c778d0bcc773b3b8231ce8e0861b6d52"
59
+ "gitHead": "c3d4955bf74e7ffdb9628867e3b23cdfe64ea8dc"
59
60
  }