Skip to content

Commit

Permalink
Merge pull request #9616 from weseek/fix/middlewares-abount-installation
Browse files Browse the repository at this point in the history
fix: Middlewares about installation
  • Loading branch information
mergify[bot] authored Feb 4, 2025
2 parents e7c79de + 84d34b3 commit b7b899f
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ describe('convertToLegacyFormat', () => {
});

test('should convert new format to legacy format', () => {
const installedAt = new Date();
const installedAtByOldestUser = new Date();

const growiInfo: IGrowiInfo<IGrowiAppAdditionalInfo> = {
version: '1.0.0',
appSiteUrl: 'https://example.com',
Expand All @@ -60,8 +63,8 @@ describe('convertToLegacyFormat', () => {
totalmem: 8589934592,
},
additionalInfo: {
installedAt: new Date(),
installedAtByOldestUser: new Date(),
installedAt,
installedAtByOldestUser,
currentUsersCount: 1,
currentActiveUsersCount: 1,
attachmentType: AttachmentMethodType.local,
Expand All @@ -88,8 +91,8 @@ describe('convertToLegacyFormat', () => {
},

// legacy properties
installedAt: new Date(),
installedAtByOldestUser: new Date(),
installedAt,
installedAtByOldestUser,
currentUsersCount: 1,
currentActiveUsersCount: 1,
attachmentType: AttachmentMethodType.local,
Expand Down
14 changes: 0 additions & 14 deletions apps/app/src/server/middlewares/application-not-installed.js

This file was deleted.

53 changes: 53 additions & 0 deletions apps/app/src/server/middlewares/application-not-installed.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import type { NextFunction, Request, Response } from 'express';
import createError, { isHttpError } from 'http-errors';

import type Crowi from '../crowi';

/**
* Middleware factory to check if the application is already installed
*/
export const generateCheckerMiddleware = (crowi: Crowi) => async(req: Request, res: Response, next: NextFunction): Promise<void> => {
const { appService } = crowi;

const isDBInitialized = await appService.isDBInitialized(true);

if (isDBInitialized) {
return next(createError(409, 'Application is already installed'));
}

return next();
};

/**
* Middleware to return HttpError 409 if the application is already installed
*/
export const allreadyInstalledMiddleware = async(req: Request, res: Response, next: NextFunction): Promise<void> => {
return next(createError(409, 'Application is already installed'));
};

/**
* Error handler to handle errors as API errors
*/
export const handleAsApiError = (error: Error, req: Request, res: Response, next: NextFunction): void => {
if (error == null) {
return next();
}

if (isHttpError(error)) {
const httpError = error as createError.HttpError;
res.status(httpError.status).json({ message: httpError.message });
return;
}

next();
};

/**
* Error handler to redirect to top page on error
*/
export const redirectToTopOnError = (error: Error, req: Request, res: Response, next: NextFunction): void => {
if (error != null) {
return res.redirect('/');
}
return next();
};
6 changes: 5 additions & 1 deletion apps/app/src/server/routes/apiv3/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import growiPlugin from '~/features/growi-plugin/server/routes/apiv3/admin';
import { factory as openaiRouteFactory } from '~/features/openai/server/routes';
import { allreadyInstalledMiddleware } from '~/server/middlewares/application-not-installed';
import loggerFactory from '~/utils/logger';

import { generateAddActivityMiddleware } from '../../middlewares/add-activity';
Expand Down Expand Up @@ -71,8 +72,11 @@ module.exports = (crowi, app) => {
userActivation.validateRegisterForm, userActivation.registerAction(crowi));

// installer
routerForAdmin.use('/installer', isInstalled
? allreadyInstalledMiddleware
: require('./installer')(crowi));

if (!isInstalled) {
routerForAdmin.use('/installer', require('./installer')(crowi));
return [router, routerForAdmin, routerForAuth];
}

Expand Down
13 changes: 8 additions & 5 deletions apps/app/src/server/routes/apiv3/installer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import loggerFactory from '~/utils/logger';

import type Crowi from '../../crowi';
import { generateAddActivityMiddleware } from '../../middlewares/add-activity';
import * as applicationNotInstalled from '../../middlewares/application-not-installed';
import { registerRules, registerValidation } from '../../middlewares/register-form-validator';
import { InstallerService, FailedToCreateAdminUserError } from '../../service/installer';

Expand All @@ -27,6 +28,12 @@ module.exports = (crowi: Crowi): Router => {

const router = express.Router();

// check application is not installed yet
router.use(
applicationNotInstalled.generateCheckerMiddleware(crowi),
applicationNotInstalled.handleAsApiError,
);

const minPasswordLength = configManager.getConfig('app:minPasswordLength');

/**
Expand Down Expand Up @@ -73,10 +80,6 @@ module.exports = (crowi: Crowi): Router => {
*/
// eslint-disable-next-line max-len
router.post('/', registerRules(minPasswordLength), registerValidation, addActivity, async(req: FormRequest, res: ApiV3Response) => {
const appService = crowi.appService;
if (appService == null) {
return res.apiv3Err(new ErrorV3('GROWI cannot be installed due to an internal error', 'app_service_not_setup'), 500);
}

if (!req.form.isValid) {
const errors = req.form.errors;
Expand Down Expand Up @@ -109,7 +112,7 @@ module.exports = (crowi: Crowi): Router => {
return res.apiv3Err(new ErrorV3(err, 'failed_to_install'));
}

await appService.setupAfterInstall();
await crowi.appService.setupAfterInstall();

const parameters = { action: SupportedAction.ACTION_USER_REGISTRATION_SUCCESS };
activityEvent.emit('update', res.locals.activity._id, parameters);
Expand Down
7 changes: 5 additions & 2 deletions apps/app/src/server/routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { middlewareFactory as rateLimiterFactory } from '~/features/rate-limiter
import { accessTokenParser } from '../middlewares/access-token-parser';
import { generateAddActivityMiddleware } from '../middlewares/add-activity';
import apiV1FormValidator from '../middlewares/apiv1-form-validator';
import * as applicationNotInstalled from '../middlewares/application-not-installed';
import { excludeReadOnlyUser, excludeReadOnlyUserIfCommentNotAllowed } from '../middlewares/exclude-read-only-user';
import injectResetOrderByTokenMiddleware from '../middlewares/inject-reset-order-by-token-middleware';
import injectUserRegistrationOrderByTokenMiddleware from '../middlewares/inject-user-registration-order-by-token-middleware';
Expand All @@ -30,7 +31,6 @@ autoReap.options.reapOnError = true; // continue reaping the file even if an err
/** @param {import('~/server/crowi').default} crowi Crowi instance */
module.exports = function(crowi, app) {
const autoReconnectToSearch = require('../middlewares/auto-reconnect-to-search')(crowi);
const applicationNotInstalled = require('../middlewares/application-not-installed')(crowi);
const applicationInstalled = require('../middlewares/application-installed')(crowi);
const loginRequiredStrictly = require('../middlewares/login-required')(crowi);
const loginRequired = require('../middlewares/login-required')(crowi, true);
Expand Down Expand Up @@ -83,7 +83,10 @@ module.exports = function(crowi, app) {
app.get('/admin' , applicationInstalled, loginRequiredStrictly , adminRequired , next.delegateToNext);

// installer
app.get('/installer' , applicationNotInstalled, next.delegateToNext);
app.get('/installer',
applicationNotInstalled.generateCheckerMiddleware(crowi),
next.delegateToNext,
applicationNotInstalled.redirectToTopOnError);

// OAuth
app.get('/passport/google' , loginPassport.loginWithGoogle, loginPassport.loginFailureForExternalAccount);
Expand Down

0 comments on commit b7b899f

Please sign in to comment.