@strapi/plugin-users-permissions 0.0.0-next.e9bb5ccdc459f4c6b6717a2d5d86359b7a47d47d → 0.0.0-next.f7babb775ed9a7e18d8351cb7f74c63e016323c4

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.
Files changed (48) hide show
  1. package/admin/src/components/BoundRoute/index.js +5 -3
  2. package/admin/src/components/FormModal/Input/index.js +6 -3
  3. package/admin/src/components/FormModal/index.js +13 -10
  4. package/admin/src/components/Permissions/PermissionRow/CheckboxWrapper.js +1 -1
  5. package/admin/src/components/Permissions/PermissionRow/SubCategory.js +26 -5
  6. package/admin/src/components/Permissions/PermissionRow/index.js +4 -2
  7. package/admin/src/components/Permissions/index.js +5 -2
  8. package/admin/src/components/Policies/index.js +3 -2
  9. package/admin/src/components/UsersPermissions/index.js +8 -5
  10. package/admin/src/{permissions.js → constants.js} +1 -3
  11. package/admin/src/contexts/UsersPermissionsContext/index.js +1 -0
  12. package/admin/src/hooks/index.js +1 -1
  13. package/admin/src/hooks/useFetchRole/index.js +6 -3
  14. package/admin/src/hooks/useForm/index.js +4 -3
  15. package/admin/src/hooks/usePlugins.js +71 -0
  16. package/admin/src/hooks/useRolesList/index.js +4 -1
  17. package/admin/src/index.js +7 -5
  18. package/admin/src/pages/AdvancedSettings/index.js +27 -28
  19. package/admin/src/pages/AdvancedSettings/utils/api.js +2 -3
  20. package/admin/src/pages/AdvancedSettings/utils/schema.js +1 -1
  21. package/admin/src/pages/EmailTemplates/components/EmailForm.js +14 -13
  22. package/admin/src/pages/EmailTemplates/components/EmailTable.js +9 -7
  23. package/admin/src/pages/EmailTemplates/index.js +16 -17
  24. package/admin/src/pages/EmailTemplates/utils/api.js +2 -3
  25. package/admin/src/pages/EmailTemplates/utils/schema.js +1 -1
  26. package/admin/src/pages/Providers/index.js +31 -32
  27. package/admin/src/pages/Providers/utils/api.js +2 -3
  28. package/admin/src/pages/Providers/utils/forms.js +1 -1
  29. package/admin/src/pages/Roles/{CreatePage/index.js → CreatePage.js} +26 -23
  30. package/admin/src/pages/Roles/{EditPage/index.js → EditPage.js} +23 -20
  31. package/admin/src/pages/Roles/ListPage/components/TableBody.js +4 -3
  32. package/admin/src/pages/Roles/ListPage/index.js +31 -34
  33. package/admin/src/pages/Roles/ListPage/utils/api.js +3 -4
  34. package/admin/src/pages/Roles/{ProtectedCreatePage/index.js → ProtectedCreatePage.js} +6 -3
  35. package/admin/src/pages/Roles/{ProtectedEditPage/index.js → ProtectedEditPage.js} +6 -3
  36. package/admin/src/pages/Roles/{ProtectedListPage/index.js → ProtectedListPage.js} +5 -3
  37. package/admin/src/pages/Roles/{CreatePage/utils/schema.js → constants.js} +2 -4
  38. package/admin/src/pages/Roles/index.js +9 -6
  39. package/admin/src/utils/index.js +1 -2
  40. package/jest.config.front.js +1 -0
  41. package/package.json +21 -24
  42. package/server/middlewares/rateLimit.js +41 -21
  43. package/server/strategies/users-permissions.js +1 -8
  44. package/admin/src/hooks/usePlugins/index.js +0 -67
  45. package/admin/src/hooks/usePlugins/init.js +0 -5
  46. package/admin/src/hooks/usePlugins/reducer.js +0 -34
  47. package/admin/src/pages/Roles/EditPage/utils/schema.js +0 -9
  48. package/admin/src/utils/getRequestURL.js +0 -5
@@ -1,27 +1,47 @@
1
1
  'use strict';
2
2
 
3
+ const utils = require('@strapi/utils');
4
+ const { isString, has, toLower } = require('lodash/fp');
5
+ const path = require('path');
6
+
7
+ const { RateLimitError } = utils.errors;
8
+
3
9
  module.exports =
4
10
  (config, { strapi }) =>
5
11
  async (ctx, next) => {
6
- const ratelimit = require('koa2-ratelimit').RateLimit;
7
-
8
- const message = [
9
- {
10
- messages: [
11
- {
12
- id: 'Auth.form.error.ratelimit',
13
- message: 'Too many attempts, please try again in a minute.',
14
- },
15
- ],
16
- },
17
- ];
18
-
19
- return ratelimit.middleware({
20
- interval: 1 * 60 * 1000,
21
- max: 5,
22
- prefixKey: `${ctx.request.path}:${ctx.request.ip}`,
23
- message,
24
- ...strapi.config.get('plugin.users-permissions.ratelimit'),
25
- ...config,
26
- })(ctx, next);
12
+ let rateLimitConfig = strapi.config.get('plugin.users-permissions.ratelimit');
13
+
14
+ if (!rateLimitConfig) {
15
+ rateLimitConfig = {
16
+ enabled: true,
17
+ };
18
+ }
19
+
20
+ if (!has('enabled', rateLimitConfig)) {
21
+ rateLimitConfig.enabled = true;
22
+ }
23
+
24
+ if (rateLimitConfig.enabled === true) {
25
+ const rateLimit = require('koa2-ratelimit').RateLimit;
26
+
27
+ const userIdentifier = toLower(ctx.request.body.email) || 'unknownIdentifier';
28
+ const requestPath = isString(ctx.request.path)
29
+ ? toLower(path.normalize(ctx.request.path))
30
+ : 'invalidPath';
31
+
32
+ const loadConfig = {
33
+ interval: { min: 5 },
34
+ max: 5,
35
+ prefixKey: `${userIdentifier}:${requestPath}:${ctx.request.ip}`,
36
+ handler() {
37
+ throw new RateLimitError();
38
+ },
39
+ ...rateLimitConfig,
40
+ ...config,
41
+ };
42
+
43
+ return rateLimit.middleware(loadConfig)(ctx, next);
44
+ }
45
+
46
+ return next();
27
47
  };
@@ -1,6 +1,6 @@
1
1
  'use strict';
2
2
 
3
- const { castArray, map, every, pipe, isEmpty } = require('lodash/fp');
3
+ const { castArray, map, every, pipe } = require('lodash/fp');
4
4
  const { ForbiddenError, UnauthorizedError } = require('@strapi/utils').errors;
5
5
 
6
6
  const { getService } = require('../utils');
@@ -80,13 +80,6 @@ const authenticate = async (ctx) => {
80
80
  const verify = async (auth, config) => {
81
81
  const { credentials: user, ability } = auth;
82
82
 
83
- strapi.telemetry.send('didReceiveAPIRequest', {
84
- eventProperties: {
85
- authenticationMethod: auth?.strategy?.name,
86
- isAuthenticated: !isEmpty(user),
87
- },
88
- });
89
-
90
83
  if (!config.scope) {
91
84
  if (!user) {
92
85
  // A non authenticated user cannot access routes that do not have a scope
@@ -1,67 +0,0 @@
1
- import { useCallback, useEffect, useReducer } from 'react';
2
- import { useNotification, useFetchClient } from '@strapi/helper-plugin';
3
- import get from 'lodash/get';
4
- import init from './init';
5
- import pluginId from '../../pluginId';
6
- import { cleanPermissions } from '../../utils';
7
- import reducer, { initialState } from './reducer';
8
-
9
- const usePlugins = (shouldFetchData = true) => {
10
- const toggleNotification = useNotification();
11
- const [{ permissions, routes, isLoading }, dispatch] = useReducer(reducer, initialState, () =>
12
- init(initialState, shouldFetchData)
13
- );
14
- const fetchClient = useFetchClient();
15
-
16
- const fetchPlugins = useCallback(async () => {
17
- try {
18
- dispatch({
19
- type: 'GET_DATA',
20
- });
21
-
22
- const [{ permissions }, { routes }] = await Promise.all(
23
- [`/${pluginId}/permissions`, `/${pluginId}/routes`].map(async (endpoint) => {
24
- const res = await fetchClient.get(endpoint);
25
-
26
- return res.data;
27
- })
28
- );
29
-
30
- dispatch({
31
- type: 'GET_DATA_SUCCEEDED',
32
- permissions: cleanPermissions(permissions),
33
- routes,
34
- });
35
- } catch (err) {
36
- const message = get(err, ['response', 'payload', 'message'], 'An error occured');
37
-
38
- dispatch({
39
- type: 'GET_DATA_ERROR',
40
- });
41
-
42
- if (message !== 'Forbidden') {
43
- toggleNotification({
44
- type: 'warning',
45
- message,
46
- });
47
- }
48
- }
49
-
50
- // eslint-disable-next-line react-hooks/exhaustive-deps
51
- }, [toggleNotification]);
52
-
53
- useEffect(() => {
54
- if (shouldFetchData) {
55
- fetchPlugins();
56
- }
57
- }, [fetchPlugins, shouldFetchData]);
58
-
59
- return {
60
- permissions,
61
- routes,
62
- getData: fetchPlugins,
63
- isLoading,
64
- };
65
- };
66
-
67
- export default usePlugins;
@@ -1,5 +0,0 @@
1
- const init = (initialState, shouldFetchData) => {
2
- return { ...initialState, isLoading: shouldFetchData };
3
- };
4
-
5
- export default init;
@@ -1,34 +0,0 @@
1
- /* eslint-disable consistent-return */
2
- import produce from 'immer';
3
-
4
- export const initialState = {
5
- permissions: {},
6
- routes: {},
7
- isLoading: true,
8
- };
9
-
10
- const reducer = (state, action) =>
11
- produce(state, (draftState) => {
12
- switch (action.type) {
13
- case 'GET_DATA': {
14
- draftState.isLoading = true;
15
- draftState.permissions = {};
16
- draftState.routes = {};
17
- break;
18
- }
19
- case 'GET_DATA_SUCCEEDED': {
20
- draftState.permissions = action.permissions;
21
- draftState.routes = action.routes;
22
- draftState.isLoading = false;
23
- break;
24
- }
25
- case 'GET_DATA_ERROR': {
26
- draftState.isLoading = false;
27
- break;
28
- }
29
- default:
30
- return draftState;
31
- }
32
- });
33
-
34
- export default reducer;
@@ -1,9 +0,0 @@
1
- import * as yup from 'yup';
2
- import { translatedErrors } from '@strapi/helper-plugin';
3
-
4
- const schema = yup.object().shape({
5
- name: yup.string().required(translatedErrors.required),
6
- description: yup.string().required(translatedErrors.required),
7
- });
8
-
9
- export default schema;
@@ -1,5 +0,0 @@
1
- import pluginId from '../pluginId';
2
-
3
- const getRequestURL = (endPoint) => `/${pluginId}/${endPoint}`;
4
-
5
- export default getRequestURL;