@bigbinary/neeto-commons-frontend 2.0.70 → 2.0.71

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/react-utils.d.ts CHANGED
@@ -51,13 +51,21 @@ export const HoneybadgerErrorBoundary: React.FC<{
51
51
  }>;
52
52
  /**
53
53
  *
54
- * A route that will restrict access to it based on a specified condition.
54
+ * A route that will restrict access to it based on a specified condition and
55
55
  *
56
- * If the given condition is true, it acts identical to a normal route. Otherwise
56
+ * permissions.
57
57
  *
58
- * we have two options: it can either redirect user to a different path or it can
58
+ * If the given condition is true and the user has the required permissions, it
59
59
  *
60
- * render a supplied error page instead of the actual component in that route.
60
+ * acts identical to a normal route. If the condition is true and the user does not
61
+ *
62
+ * have the required permissions, it will show 403 error page or it will render a
63
+ *
64
+ * supplied error page. If the condition is false, it will redirect user to a
65
+ *
66
+ * different path if redirectRoute is specified or it will render a supplied error
67
+ *
68
+ * page.
61
69
  *
62
70
  * If condition is not specified, it will assume the value of
63
71
  *
@@ -70,6 +78,7 @@ export function PrivateRoute(props: {
70
78
  condition?: boolean;
71
79
  redirectRoute?: string;
72
80
  errorPage?: React.ReactNode;
81
+ permissions?: string[];
73
82
  } & RouteProps): JSX.Element;
74
83
  type OptionsType = {
75
84
  root?: Element | null;
package/react-utils.js CHANGED
@@ -2,7 +2,7 @@ import * as React from 'react';
2
2
  import React__default, { useEffect, useState, useDebugValue, useRef, useMemo } from 'react';
3
3
  import { Honeybadger, HoneybadgerErrorBoundary as HoneybadgerErrorBoundary$1 } from '@honeybadger-io/react';
4
4
  import { Typography, Button } from '@bigbinary/neetoui';
5
- import { isNil, prop, toPairs, mergeLeft, isEmpty, omit, curry } from 'ramda';
5
+ import { isNil, includes, __, prop, toPairs, mergeLeft, isEmpty, omit, curry } from 'ramda';
6
6
  import { useTranslation, Trans } from 'react-i18next';
7
7
  import ErrorPage from '@bigbinary/neeto-molecules/ErrorPage';
8
8
  import { Route, Redirect } from 'react-router-dom';
@@ -152,7 +152,7 @@ function _objectWithoutProperties(source, excluded) {
152
152
  return target;
153
153
  }
154
154
 
155
- var _excluded = ["condition", "redirectRoute", "errorPage"];
155
+ var _excluded = ["condition", "redirectRoute", "errorPage", "permissions"];
156
156
  var PrivateRoute = function PrivateRoute(_ref) {
157
157
  var _ref$condition = _ref.condition,
158
158
  condition = _ref$condition === void 0 ? globalProps.authenticated : _ref$condition,
@@ -160,17 +160,27 @@ var PrivateRoute = function PrivateRoute(_ref) {
160
160
  redirectRoute = _ref$redirectRoute === void 0 ? undefined : _ref$redirectRoute,
161
161
  _ref$errorPage = _ref.errorPage,
162
162
  errorPage = _ref$errorPage === void 0 ? undefined : _ref$errorPage,
163
+ _ref$permissions = _ref.permissions,
164
+ permissions = _ref$permissions === void 0 ? undefined : _ref$permissions,
163
165
  props = _objectWithoutProperties(_ref, _excluded);
164
- return (
165
- // eslint-disable-next-line no-nested-ternary
166
- condition ? /*#__PURE__*/React__default.createElement(Route, props) : redirectRoute ? /*#__PURE__*/React__default.createElement(Redirect, {
166
+ if (condition) {
167
+ if (permissions) {
168
+ return globalProps.permissions.some(includes(__, permissions)) ? /*#__PURE__*/React__default.createElement(Route, props) : errorPage || /*#__PURE__*/React__default.createElement(ErrorPage, {
169
+ status: 403
170
+ });
171
+ }
172
+ return /*#__PURE__*/React__default.createElement(Route, props);
173
+ }
174
+ if (redirectRoute) {
175
+ return /*#__PURE__*/React__default.createElement(Redirect, {
167
176
  to: {
168
177
  pathname: redirectRoute
169
178
  }
170
- }) : errorPage || /*#__PURE__*/React__default.createElement(ErrorPage, {
171
- status: "403"
172
- })
173
- );
179
+ });
180
+ }
181
+ return errorPage || /*#__PURE__*/React__default.createElement(ErrorPage, {
182
+ status: 403
183
+ });
174
184
  };
175
185
 
176
186
  function _arrayWithHoles(arr) {