Просмотр исходного кода

Code improvement

https://youtrack.weseek.co.jp/issue/GW-7759
- Rename setCustomizedBrandLogoSrc to setCustomizedLogoSrc
- Modify customizeLogoSrc value assignment in config.ts
- Add csrf back to customize-logo route
Mudana-Grune 3 лет назад
Родитель
Сommit
e3ee65b933

+ 5 - 5
packages/app/src/components/Admin/Customize/CustomizeLogoSetting.tsx

@@ -20,14 +20,14 @@ const CustomizeLogoSetting = (): JSX.Element => {
   const [isImageCropModalShow, setIsImageCropModalShow] = useState<boolean>(false);
   const [isDefaultLogo, setIsDefaultLogo] = useState<boolean>(true);
   const [retrieveError, setRetrieveError] = useState<string | null>(null);
-  const [customizedLogoSrc, setCustomizedBrandLogoSrc] = useState< string | null >(null);
+  const [customizedLogoSrc, setCustomizedLogoSrc] = useState< string | null >(null);
 
   const retrieveData = useCallback(async() => {
     try {
       const response = await apiv3Get('/customize-setting/customize-logo');
       const { isDefaultLogo, customizedLogoSrc } = response.data;
       setIsDefaultLogo(isDefaultLogo);
-      setCustomizedBrandLogoSrc(customizedLogoSrc);
+      setCustomizedLogoSrc(customizedLogoSrc);
     }
     catch (err) {
       setRetrieveError(err);
@@ -55,7 +55,7 @@ const CustomizeLogoSetting = (): JSX.Element => {
         customizedLogoSrc,
       });
       const { customizedParams } = response.data;
-      setCustomizedBrandLogoSrc(customizedParams.customizedLogoSrc);
+      setCustomizedLogoSrc(customizedParams.customizedLogoSrc);
       toastSuccess(t('toaster.update_successed', { target: t('admin:customize_setting.custom_logo') }));
     }
     catch (err) {
@@ -67,7 +67,7 @@ const CustomizeLogoSetting = (): JSX.Element => {
   const onClickDeleteBtn = useCallback(async() => {
     try {
       await apiv3Delete('/customize-setting/delete-brand-logo');
-      setCustomizedBrandLogoSrc(null);
+      setCustomizedLogoSrc(null);
       toastSuccess(t('toaster.update_successed', { target: t('admin:customize_setting.current_logo') }));
     }
     catch (err) {
@@ -82,7 +82,7 @@ const CustomizeLogoSetting = (): JSX.Element => {
       const formData = new FormData();
       formData.append('file', croppedImage);
       const { data } = await apiv3PostForm('/customize-setting/upload-brand-logo', formData);
-      setCustomizedBrandLogoSrc(data.attachment.filePathProxied);
+      setCustomizedLogoSrc(data.attachment.filePathProxied);
       toastSuccess(t('toaster.update_successed', { target: t('admin:customize_setting.current_logo') }));
     }
     catch (err) {

+ 4 - 2
packages/app/src/server/models/config.ts

@@ -190,7 +190,7 @@ export const defaultNotificationConfigs: { [key: string]: any } = {
 
 schema.statics.getLocalconfig = function(crowi) {
   const env = process.env;
-  const isDefaultLogo = crowi.configManager.getConfig('crowi', 'customize:isDefaultLogo');
+
   const localConfig = {
     crowi: {
       title: crowi.appService.getAppTitle(),
@@ -245,7 +245,9 @@ schema.statics.getLocalconfig = function(crowi) {
     globalLang: crowi.configManager.getConfig('crowi', 'app:globalLang'),
     pageLimitationL: crowi.configManager.getConfig('crowi', 'customize:showPageLimitationL'),
     pageLimitationXL: crowi.configManager.getConfig('crowi', 'customize:showPageLimitationXL'),
-    customizedLogoSrc: isDefaultLogo ? null : crowi.configManager.getConfig('crowi', 'customize:customizedLogoSrc'),
+    customizedLogoSrc: crowi.configManager.getConfig('crowi', 'customize:isDefaultLogo')
+      ? null
+      : crowi.configManager.getConfig('crowi', 'customize:customizedLogoSrc'),
     auditLogEnabled: crowi.configManager.getConfig('crowi', 'app:auditLogEnabled'),
     activityExpirationSeconds: crowi.configManager.getConfig('crowi', 'app:activityExpirationSeconds'),
     auditLogAvailableActions: crowi.activityService.getAvailableActions(false),

+ 25 - 25
packages/app/src/server/routes/apiv3/customize-setting.js

@@ -19,6 +19,7 @@ const multer = require('multer');
 
 const ErrorV3 = require('../../models/vo/error-apiv3');
 
+
 /**
  * @swagger
  *  tags:
@@ -687,7 +688,7 @@ module.exports = (crowi) => {
     return res.apiv3({ isDefaultLogo, customizedLogoSrc });
   });
 
-  router.put('/customize-logo', loginRequiredStrictly, adminRequired, validator.logo, apiV3FormValidator, async(req, res) => {
+  router.put('/customize-logo', csrf, loginRequiredStrictly, adminRequired, validator.logo, apiV3FormValidator, async(req, res) => {
 
     const {
       isDefaultLogo, customizedLogoSrc,
@@ -713,7 +714,7 @@ module.exports = (crowi) => {
   });
 
   router.post('/upload-brand-logo', uploads.single('file'), loginRequiredStrictly,
-    adminRequired, validator.logo, apiV3FormValidator, async(req, res) => {
+    adminRequired, csrf, validator.logo, apiV3FormValidator, async(req, res) => {
 
       if (req.file == null) {
         return res.apiv3Err(new ErrorV3('File error.', 'upload-brand-logo-failed'));
@@ -754,34 +755,33 @@ module.exports = (crowi) => {
       return res.apiv3({ attachment });
     });
 
-  router.delete('/delete-brand-logo', loginRequiredStrictly,
-    adminRequired, async(req, res) => {
+  router.delete('/delete-brand-logo', csrf, loginRequiredStrictly, adminRequired, async(req, res) => {
 
-      const attachments = await Attachment.find({ attachmentType: AttachmentType.BRAND_LOGO });
+    const attachments = await Attachment.find({ attachmentType: AttachmentType.BRAND_LOGO });
 
-      if (attachments == null) {
-        return res.apiv3Err(new ErrorV3('attachment not found', 'delete-brand-logo-failed'));
-      }
+    if (attachments == null) {
+      return res.apiv3Err(new ErrorV3('attachment not found', 'delete-brand-logo-failed'));
+    }
 
-      try {
-        await attachmentService.removeAllAttachments(attachments);
-        const isDefaultLogo = await crowi.configManager.getConfig('crowi', 'customize:isDefaultLogo');
-        // update attachmentId immediately
-        const attachmentConfigParams = {
-          'customize:customizedLogoSrc': null,
-        };
-        if (!isDefaultLogo) {
-          attachmentConfigParams['customize:customizedLogoSrc'] = null;
-        }
-        await crowi.configManager.updateConfigsInTheSameNamespace('crowi', attachmentConfigParams);
-      }
-      catch (err) {
-        logger.error(err);
-        return res.status(500).apiv3Err(new ErrorV3('Error while deleting logo', 'delete-brand-logo-failed'));
+    try {
+      await attachmentService.removeAllAttachments(attachments);
+      const isDefaultLogo = await crowi.configManager.getConfig('crowi', 'customize:isDefaultLogo');
+      // update attachmentId immediately
+      const attachmentConfigParams = {
+        'customize:customizedLogoSrc': null,
+      };
+      if (!isDefaultLogo) {
+        attachmentConfigParams['customize:customizedLogoSrc'] = null;
       }
+      await crowi.configManager.updateConfigsInTheSameNamespace('crowi', attachmentConfigParams);
+    }
+    catch (err) {
+      logger.error(err);
+      return res.status(500).apiv3Err(new ErrorV3('Error while deleting logo', 'delete-brand-logo-failed'));
+    }
 
-      return res.apiv3({ });
-    });
+    return res.apiv3({ });
+  });
 
   return router;
 };