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

refactor devide applicationNotInstalled and applicationInstalled

Yuki Takei 5 лет назад
Родитель
Сommit
7896c915a8

+ 13 - 0
src/server/middlewares/application-installed.js

@@ -0,0 +1,13 @@
+module.exports = (crowi) => {
+  const { appService } = crowi;
+
+  return async(req, res, next) => {
+    const isInstalled = await appService.isDBInitialized();
+
+    if (!isInstalled) {
+      return res.redirect('/installer');
+    }
+
+    return next();
+  };
+};

+ 14 - 0
src/server/middlewares/application-not-installed.js

@@ -0,0 +1,14 @@
+module.exports = (crowi) => {
+  const { appService } = crowi;
+
+  return async(req, res, next) => {
+    const isInstalled = await appService.isDBInitialized();
+
+    if (isInstalled) {
+      req.flash('errorMessage', req.t('message.application_already_installed'));
+      return res.redirect('admin');
+    }
+
+    return next();
+  };
+};

+ 7 - 6
src/server/routes/index.js

@@ -4,7 +4,8 @@ const autoReap = require('multer-autoreap');
 autoReap.options.reapOnError = true; // continue reaping the file even if an error occurs
 
 module.exports = function(crowi, app) {
-  const middlewares = require('../util/middlewares')(crowi, app);
+  const applicationNotInstalled = require('../middlewares/application-not-installed')(crowi);
+  const applicationInstalled = require('../middlewares/application-installed')(crowi);
   const accessTokenParser = require('../middlewares/access-token-parser')(crowi);
   const loginRequiredStrictly = require('../middlewares/login-required')(crowi);
   const loginRequired = require('../middlewares/login-required')(crowi, true);
@@ -32,7 +33,7 @@ module.exports = function(crowi, app) {
 
   /* eslint-disable max-len, comma-spacing, no-multi-spaces */
 
-  app.get('/'                        , middlewares.applicationInstalled, loginRequired , page.showTopPage);
+  app.get('/'                        , applicationInstalled, loginRequired , page.showTopPage);
 
   // API v3
   app.use('/api-docs', require('./apiv3/docs')(crowi));
@@ -40,20 +41,20 @@ module.exports = function(crowi, app) {
 
   // installer
   if (!isInstalled) {
-    app.get('/installer'               , middlewares.applicationNotInstalled , installer.index);
-    app.post('/installer'              , middlewares.applicationNotInstalled , form.register , csrf, installer.install);
+    app.get('/installer'               , applicationNotInstalled , installer.index);
+    app.post('/installer'              , applicationNotInstalled , form.register , csrf, installer.install);
     return;
   }
 
   app.get('/login/error/:reason'     , login.error);
-  app.get('/login'                   , middlewares.applicationInstalled     , login.preLogin, login.login);
+  app.get('/login'                   , applicationInstalled     , login.preLogin, login.login);
   app.get('/login/invited'           , login.invited);
   app.post('/login/activateInvited'  , form.invited                         , csrf, login.invited);
   app.post('/login'                  , form.login                           , csrf, loginPassport.loginWithLocal, loginPassport.loginWithLdap, loginPassport.loginFailure);
   app.post('/_api/login/testLdap'    , loginRequiredStrictly , form.login , loginPassport.testLdapCredentials);
 
   app.post('/register'               , form.register                        , csrf, login.register);
-  app.get('/register'                , middlewares.applicationInstalled     , login.preLogin, login.register);
+  app.get('/register'                , applicationInstalled     , login.preLogin, login.register);
   app.get('/logout'                  , logout.logout);
 
   app.get('/admin'                          , loginRequiredStrictly , adminRequired , admin.index);

+ 1 - 23
src/server/util/middlewares.js

@@ -9,7 +9,7 @@ const md5 = require('md5');
 const entities = require('entities');
 
 module.exports = (crowi) => {
-  const { configManager, appService } = crowi;
+  const { configManager } = crowi;
 
   const middlewares = {};
 
@@ -158,28 +158,6 @@ module.exports = (crowi) => {
     };
   };
 
-  // this is for Installer
-  middlewares.applicationNotInstalled = async function(req, res, next) {
-    const isInstalled = await appService.isDBInitialized();
-
-    if (isInstalled) {
-      req.flash('errorMessage', req.t('message.application_already_installed'));
-      return res.redirect('admin'); // admin以外はadminRequiredで'/'にリダイレクトされる
-    }
-
-    return next();
-  };
-
-  middlewares.applicationInstalled = async function(req, res, next) {
-    const isInstalled = await appService.isDBInitialized();
-
-    if (!isInstalled) {
-      return res.redirect('/installer');
-    }
-
-    return next();
-  };
-
   middlewares.awsEnabled = function() {
     return function(req, res, next) {
       if ((configManager.getConfig('crowi', 'aws:region') !== '' || this.configManager.getConfig('crowi', 'aws:customEndpoint') !== '')