Taichi Masuyama 4 лет назад
Родитель
Сommit
99164c2fda

+ 0 - 25
packages/app/src/server/models/obsolete-page.js

@@ -565,31 +565,6 @@ export const getPageSchema = (crowi) => {
     return this.findOne({ path });
   };
 
-  /**
-   * @param {string} path Page path
-   * @param {User} user User instance
-   * @param {UserGroup[]} userGroups List of UserGroup instances
-   */
-  pageSchema.statics.findByPathAndViewer = async function(path, user, userGroups) {
-    if (path == null) {
-      throw new Error('path is required.');
-    }
-
-    const baseQuery = this.findOne({ path });
-
-    let relatedUserGroups = userGroups;
-    if (user != null && relatedUserGroups == null) {
-      validateCrowi();
-      const UserGroupRelation = crowi.model('UserGroupRelation');
-      relatedUserGroups = await UserGroupRelation.findAllUserGroupIdsRelatedToUser(user);
-    }
-
-    const queryBuilder = new PageQueryBuilder(baseQuery);
-    queryBuilder.addConditionToFilteringByViewer(user, relatedUserGroups, true);
-
-    return await queryBuilder.query.exec();
-  };
-
   /**
    * @param {string} path Page path
    * @param {User} user User instance

+ 3 - 2
packages/app/src/server/models/page.ts

@@ -173,12 +173,13 @@ const addViewerCondition = async(queryBuilder: PageQueryBuilder, user, userGroup
   queryBuilder.addConditionToFilteringByViewer(user, relatedUserGroups, true);
 };
 
-schema.statics.findByPathAndViewerV5 = async function(path: string | null, user, userGroups): Promise<IPage[]> {
+schema.statics.findByPathAndViewer = async function(path: string | null, user, userGroups, useFindOne = true): Promise<IPage[]> {
   if (path == null) {
     throw new Error('path is required.');
   }
 
-  const queryBuilder = new PageQueryBuilder(this.find({ path }));
+  const baseQuery = useFindOne ? this.findOne({ path }) : this.find({ path });
+  const queryBuilder = new PageQueryBuilder(baseQuery);
   await addViewerCondition(queryBuilder, user, userGroups);
 
   return queryBuilder.query.exec();

+ 9 - 4
packages/app/src/server/routes/page.js

@@ -563,17 +563,22 @@ module.exports = function(crowi, app) {
    * redirector
    */
   async function redirector(req, res, next, path) {
-    const pages = await Page.findByPathAndViewerV5(path, req.user);
+    const pages = await Page.findByPathAndViewer(path, req.user, null, false);
     const { redirectFrom } = req.query;
-    const query = redirectFrom == null ? '' : `?redirectFrom=${redirectFrom}`;
 
     if (pages.length >= 2) {
-      // WIP
+      // pass only redirectFrom since it is not sure whether the query params are related to the pages
       return res.render('layout-growi/select-go-to-page', { pages, redirectFrom });
     }
 
+    // pass all query params
+    let query = '';
+    Object.entries(req.query).forEach(([key, value], i) => {
+      query += i === 0 ? `?${key}=${value}` : `&${key}=${value}`;
+    });
+
     if (pages.length === 1) {
-      return res.redirect(`/${pages[0]._id}${query}`);
+      return res.safeRedirect(`/${pages[0]._id}${query}`);
     }
 
     return next(); // to page.notFound