@strapi/admin 4.1.9 → 4.1.10-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.
Files changed (47) hide show
  1. package/admin/src/components/ConfigurationsProvider/index.js +51 -0
  2. package/admin/src/components/ConfigurationsProvider/reducer.js +28 -0
  3. package/admin/src/components/LeftMenu/index.js +4 -2
  4. package/admin/src/components/Providers/index.js +8 -4
  5. package/admin/src/components/UnauthenticatedLogo/index.js +4 -2
  6. package/admin/src/pages/App/index.js +7 -2
  7. package/admin/src/pages/SettingsPage/pages/ApplicationInfosPage/components/Form/index.js +85 -0
  8. package/admin/src/pages/SettingsPage/pages/ApplicationInfosPage/components/Form/init.js +13 -0
  9. package/admin/src/pages/SettingsPage/pages/ApplicationInfosPage/components/Form/reducer.js +43 -0
  10. package/admin/src/pages/SettingsPage/pages/ApplicationInfosPage/components/LogoInput/index.js +116 -0
  11. package/admin/src/pages/SettingsPage/pages/ApplicationInfosPage/components/LogoInput/reducer.js +28 -0
  12. package/admin/src/pages/SettingsPage/pages/ApplicationInfosPage/components/LogoInput/stepper.js +25 -0
  13. package/admin/src/pages/SettingsPage/pages/ApplicationInfosPage/components/LogoModalStepper/AddLogoDialog.js +67 -0
  14. package/admin/src/pages/SettingsPage/pages/ApplicationInfosPage/components/LogoModalStepper/FromComputerForm.js +176 -0
  15. package/admin/src/pages/SettingsPage/pages/ApplicationInfosPage/components/LogoModalStepper/FromUrlForm.js +82 -0
  16. package/admin/src/pages/SettingsPage/pages/ApplicationInfosPage/components/LogoModalStepper/ImageCardAsset.js +51 -0
  17. package/admin/src/pages/SettingsPage/pages/ApplicationInfosPage/components/LogoModalStepper/PendingLogoDialog.js +97 -0
  18. package/admin/src/pages/SettingsPage/pages/ApplicationInfosPage/components/LogoModalStepper/index.js +85 -0
  19. package/admin/src/pages/SettingsPage/pages/ApplicationInfosPage/components/LogoModalStepper/reducer.js +28 -0
  20. package/admin/src/pages/SettingsPage/pages/ApplicationInfosPage/index.js +149 -87
  21. package/admin/src/pages/SettingsPage/pages/ApplicationInfosPage/utils/api.js +16 -0
  22. package/admin/src/pages/SettingsPage/pages/ApplicationInfosPage/utils/constants.js +3 -0
  23. package/admin/src/pages/SettingsPage/pages/ApplicationInfosPage/utils/getFormData.js +17 -0
  24. package/admin/src/pages/SettingsPage/pages/ApplicationInfosPage/utils/parseFileMetadatas.js +76 -0
  25. package/admin/src/pages/SettingsPage/pages/ApplicationInfosPage/utils/prefixAllUrls.js +17 -0
  26. package/admin/src/pages/SettingsPage/pages/ApplicationInfosPage/utils/urlToFile.js +21 -0
  27. package/admin/src/translations/en.json +25 -0
  28. package/build/7197.959bbe97.chunk.js +113 -0
  29. package/build/{9298.5b5c6ea1.chunk.js → 9298.f97fcef0.chunk.js} +2 -2
  30. package/build/Admin-authenticatedApp.d8767873.chunk.js +80 -0
  31. package/build/Admin_settingsPage.0d94a598.chunk.js +180 -0
  32. package/build/en-json.ca572384.chunk.js +1 -0
  33. package/build/index.html +1 -1
  34. package/build/{main.25315363.js → main.06f66609.js} +1 -1
  35. package/build/{runtime~main.1ecd9f7d.js → runtime~main.dc1c7ef6.js} +1 -1
  36. package/package.json +5 -5
  37. package/server/config/admin-actions.js +14 -0
  38. package/server/controllers/admin.js +33 -1
  39. package/server/routes/admin.js +28 -0
  40. package/server/services/index.js +1 -0
  41. package/server/services/project-settings.js +173 -0
  42. package/server/utils/index.d.ts +2 -0
  43. package/server/validation/project-settings.js +39 -0
  44. package/build/6706.b0b5124d.chunk.js +0 -113
  45. package/build/Admin-authenticatedApp.72cf6aa3.chunk.js +0 -80
  46. package/build/Admin_settingsPage.f83a7c21.chunk.js +0 -172
  47. package/build/en-json.3e1a222e.chunk.js +0 -1
@@ -8,6 +8,11 @@ const { ValidationError } = require('@strapi/utils').errors;
8
8
  // eslint-disable-next-line node/no-extraneous-require
9
9
  const ee = require('@strapi/strapi/lib/utils/ee');
10
10
 
11
+ const {
12
+ validateUpdateProjectSettings,
13
+ validateUpdateProjectSettingsFiles,
14
+ validateUpdateProjectSettingsImagesDimensions,
15
+ } = require('../validation/project-settings');
11
16
  const { getService } = require('../utils');
12
17
 
13
18
  const PLUGIN_NAME_REGEX = /^[A-Za-z][A-Za-z0-9-_]+$/;
@@ -39,6 +44,7 @@ module.exports = {
39
44
  async init() {
40
45
  let uuid = strapi.config.get('uuid', false);
41
46
  const hasAdmin = await getService('user').exists();
47
+ const { menuLogo } = await getService('project-settings').getProjectSettings();
42
48
  // set to null if telemetryDisabled flag not avaialble in package.json
43
49
  const telemetryDisabled = strapi.config.get('packageJsonStrapi.telemetryDisabled', null);
44
50
 
@@ -46,7 +52,33 @@ module.exports = {
46
52
  uuid = false;
47
53
  }
48
54
 
49
- return { data: { uuid, hasAdmin } };
55
+ return {
56
+ data: {
57
+ uuid,
58
+ hasAdmin,
59
+ menuLogo: menuLogo ? menuLogo.url : null,
60
+ },
61
+ };
62
+ },
63
+
64
+ async getProjectSettings() {
65
+ return getService('project-settings').getProjectSettings();
66
+ },
67
+
68
+ async updateProjectSettings(ctx) {
69
+ const projectSettingsService = getService('project-settings');
70
+
71
+ const {
72
+ request: { files, body },
73
+ } = ctx;
74
+
75
+ await validateUpdateProjectSettings(body);
76
+ await validateUpdateProjectSettingsFiles(files);
77
+
78
+ const formatedFiles = await projectSettingsService.parseFilesData(files);
79
+ await validateUpdateProjectSettingsImagesDimensions(formatedFiles);
80
+
81
+ return projectSettingsService.updateProjectSettings({ ...body, ...formatedFiles });
50
82
  },
51
83
 
52
84
  async information() {
@@ -7,6 +7,34 @@ module.exports = [
7
7
  handler: 'admin.init',
8
8
  config: { auth: false },
9
9
  },
10
+ {
11
+ method: 'GET',
12
+ path: '/project-settings',
13
+ handler: 'admin.getProjectSettings',
14
+ config: {
15
+ policies: [
16
+ 'admin::isAuthenticatedAdmin',
17
+ {
18
+ name: 'admin::hasPermissions',
19
+ config: { actions: ['admin::project-settings.read'] },
20
+ },
21
+ ],
22
+ },
23
+ },
24
+ {
25
+ method: 'POST',
26
+ path: '/project-settings',
27
+ handler: 'admin.updateProjectSettings',
28
+ config: {
29
+ policies: [
30
+ 'admin::isAuthenticatedAdmin',
31
+ {
32
+ name: 'admin::hasPermissions',
33
+ config: { actions: ['admin::project-settings.update'] },
34
+ },
35
+ ],
36
+ },
37
+ },
10
38
  {
11
39
  method: 'GET',
12
40
  path: '/project-type',
@@ -13,4 +13,5 @@ module.exports = {
13
13
  auth: require('./auth'),
14
14
  action: require('./action'),
15
15
  'api-token': require('./api-token'),
16
+ 'project-settings': require('./project-settings'),
16
17
  };
@@ -0,0 +1,173 @@
1
+ 'use strict';
2
+
3
+ const fs = require('fs');
4
+ const { pick } = require('lodash');
5
+
6
+ const PROJECT_SETTINGS_FILE_INPUTS = ['menuLogo'];
7
+ const DEFAULT_PROJECT_SETTINGS = {
8
+ menuLogo: null,
9
+ };
10
+
11
+ const parseFilesData = async files => {
12
+ const formatedFilesData = {};
13
+
14
+ await Promise.all(
15
+ PROJECT_SETTINGS_FILE_INPUTS.map(async inputName => {
16
+ const file = files[inputName];
17
+
18
+ // Skip empty file inputs
19
+ if (!file) {
20
+ return;
21
+ }
22
+
23
+ const getStream = () => fs.createReadStream(file.path);
24
+
25
+ // Add formated data for the upload provider
26
+ formatedFilesData[inputName] = strapi
27
+ .plugin('upload')
28
+ .service('upload')
29
+ .formatFileInfo({
30
+ filename: file.name,
31
+ type: file.type,
32
+ size: file.size,
33
+ });
34
+
35
+ // Add image dimensions
36
+ Object.assign(
37
+ formatedFilesData[inputName],
38
+ await strapi
39
+ .plugin('upload')
40
+ .service('image-manipulation')
41
+ .getDimensions({ getStream })
42
+ );
43
+
44
+ // Add file path, and stream
45
+ Object.assign(formatedFilesData[inputName], {
46
+ stream: getStream(),
47
+ tmpPath: file.path,
48
+ provider: strapi.config.get('plugin.upload').provider,
49
+ });
50
+ })
51
+ );
52
+
53
+ return formatedFilesData;
54
+ };
55
+
56
+ const getProjectSettings = async () => {
57
+ const store = strapi.store({ type: 'core', name: 'admin' });
58
+ const projectSettings = {
59
+ ...DEFAULT_PROJECT_SETTINGS,
60
+ ...(await store.get({ key: 'project-settings' })),
61
+ };
62
+
63
+ // Filter file input fields
64
+ PROJECT_SETTINGS_FILE_INPUTS.forEach(inputName => {
65
+ if (!projectSettings[inputName]) {
66
+ return;
67
+ }
68
+
69
+ projectSettings[inputName] = pick(projectSettings[inputName], [
70
+ 'name',
71
+ 'url',
72
+ 'width',
73
+ 'height',
74
+ 'ext',
75
+ 'size',
76
+ ]);
77
+ });
78
+
79
+ return projectSettings;
80
+ };
81
+
82
+ const uploadFiles = async (files = {}) => {
83
+ // Call the provider upload function for each file
84
+ return Promise.all(
85
+ Object.values(files)
86
+ .filter(file => file.stream instanceof fs.ReadStream)
87
+ .map(file => strapi.plugin('upload').provider.uploadStream(file))
88
+ );
89
+ };
90
+
91
+ const deleteOldFiles = async ({ previousSettings, newSettings }) => {
92
+ return Promise.all(
93
+ PROJECT_SETTINGS_FILE_INPUTS.map(async inputName => {
94
+ // Skip if the store doesn't contain project settings
95
+ if (!previousSettings) {
96
+ return;
97
+ }
98
+
99
+ // Skip if there was no previous file
100
+ if (!previousSettings[inputName]) {
101
+ return;
102
+ }
103
+
104
+ // Skip if the file was not changed
105
+ if (
106
+ newSettings[inputName] &&
107
+ previousSettings[inputName].hash === newSettings[inputName].hash
108
+ ) {
109
+ return;
110
+ }
111
+
112
+ // Skip if the file was not uploaded with the current provider
113
+ if (strapi.config.get('plugin.upload').provider !== previousSettings[inputName].provider) {
114
+ return;
115
+ }
116
+
117
+ // There was a previous file and an new file was uploaded
118
+ // Remove the previous file
119
+ strapi.plugin('upload').provider.delete(previousSettings[inputName]);
120
+ })
121
+ );
122
+ };
123
+
124
+ const updateProjectSettings = async newSettings => {
125
+ const store = strapi.store({ type: 'core', name: 'admin' });
126
+ const previousSettings = await store.get({ key: 'project-settings' });
127
+ const files = pick(newSettings, PROJECT_SETTINGS_FILE_INPUTS);
128
+
129
+ await uploadFiles(files);
130
+
131
+ PROJECT_SETTINGS_FILE_INPUTS.forEach(inputName => {
132
+ // If the user input exists but is not a formdata "file" remove it
133
+ if (newSettings[inputName] !== undefined && !(typeof newSettings[inputName] === 'object')) {
134
+ newSettings[inputName] = null;
135
+ return;
136
+ }
137
+
138
+ // If the user input is undefined reuse previous setting (do not update field)
139
+ if (!newSettings[inputName]) {
140
+ newSettings[inputName] = previousSettings[inputName];
141
+ return;
142
+ }
143
+
144
+ // Update the file
145
+ newSettings[inputName] = pick(newSettings[inputName], [
146
+ 'name',
147
+ 'hash',
148
+ 'url',
149
+ 'width',
150
+ 'height',
151
+ 'ext',
152
+ 'size',
153
+ 'provider',
154
+ ]);
155
+ });
156
+
157
+ // No await to proceed asynchronously
158
+ deleteOldFiles({ previousSettings, newSettings });
159
+
160
+ await store.set({
161
+ key: 'project-settings',
162
+ value: { ...previousSettings, ...newSettings },
163
+ });
164
+
165
+ return getProjectSettings();
166
+ };
167
+
168
+ module.exports = {
169
+ deleteOldFiles,
170
+ parseFilesData,
171
+ getProjectSettings,
172
+ updateProjectSettings,
173
+ };
@@ -6,6 +6,7 @@ import * as metrics from '../services/metrics';
6
6
  import * as token from '../services/token';
7
7
  import * as auth from '../services/auth';
8
8
  import * as apiToken from '../services/api-token';
9
+ import * as projectSettings from '../services/project-settings';
9
10
 
10
11
  type S = {
11
12
  role: typeof role;
@@ -16,6 +17,7 @@ type S = {
16
17
  auth: typeof auth;
17
18
  metrics: typeof metrics;
18
19
  'api-token': typeof apiToken;
20
+ 'project-settings': typeof projectSettings;
19
21
  };
20
22
 
21
23
  export function getService<T extends keyof S>(name: T): S[T];
@@ -0,0 +1,39 @@
1
+ 'use strict';
2
+
3
+ const { yup, validateYupSchemaSync } = require('@strapi/utils');
4
+
5
+ const MAX_IMAGE_WIDTH = 750;
6
+ const MAX_IMAGE_HEIGHT = MAX_IMAGE_WIDTH;
7
+ const MAX_IMAGE_FILE_SIZE = 1024 * 1024; // 1Mo
8
+ const ALLOWED_IMAGE_FILE_TYPES = ['image/jpeg', 'image/png', 'image/svg+xml'];
9
+
10
+ const updateProjectSettings = yup
11
+ .object({
12
+ menuLogo: yup.string(),
13
+ })
14
+ .noUnknown();
15
+
16
+ const updateProjectSettingsFiles = yup
17
+ .object({
18
+ menuLogo: yup.object({
19
+ name: yup.string(),
20
+ type: yup.string().oneOf(ALLOWED_IMAGE_FILE_TYPES),
21
+ size: yup.number().max(MAX_IMAGE_FILE_SIZE),
22
+ }),
23
+ })
24
+ .noUnknown();
25
+
26
+ const updateProjectSettingsImagesDimensions = yup.object({
27
+ menuLogo: yup.object({
28
+ width: yup.number().max(MAX_IMAGE_WIDTH),
29
+ height: yup.number().max(MAX_IMAGE_HEIGHT),
30
+ }),
31
+ });
32
+
33
+ module.exports = {
34
+ validateUpdateProjectSettings: validateYupSchemaSync(updateProjectSettings),
35
+ validateUpdateProjectSettingsFiles: validateYupSchemaSync(updateProjectSettingsFiles),
36
+ validateUpdateProjectSettingsImagesDimensions: validateYupSchemaSync(
37
+ updateProjectSettingsImagesDimensions
38
+ ),
39
+ };