Procházet zdrojové kódy

Pass all query params & use safeRedirect

Taichi Masuyama před 4 roky
rodič
revize
4cbf21c3fa

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

@@ -556,31 +556,6 @@ export const getPageSchema = (crowi) => {
     return this.findOne({ path });
     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 {string} path Page path
    * @param {User} user User instance
    * @param {User} user User instance

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

@@ -157,18 +157,20 @@ schema.statics.getParentIdAndFillAncestors = async function(path: string): Promi
   return parentId;
   return parentId;
 };
 };
 
 
-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) {
   if (path == null) {
     throw new Error('path is required.');
     throw new Error('path is required.');
   }
   }
 
 
+  const baseQuery = useFindOne ? this.findOne({ path }) : this.find({ path });
+
   let relatedUserGroups = userGroups;
   let relatedUserGroups = userGroups;
   if (user != null && relatedUserGroups == null) {
   if (user != null && relatedUserGroups == null) {
     const UserGroupRelation: any = mongoose.model('UserGroupRelation');
     const UserGroupRelation: any = mongoose.model('UserGroupRelation');
     relatedUserGroups = await UserGroupRelation.findAllUserGroupIdsRelatedToUser(user);
     relatedUserGroups = await UserGroupRelation.findAllUserGroupIdsRelatedToUser(user);
   }
   }
 
 
-  const queryBuilder = new PageQueryBuilder(this.find({ path }));
+  const queryBuilder = new PageQueryBuilder(baseQuery);
   queryBuilder.addConditionToFilteringByViewer(user, relatedUserGroups, true);
   queryBuilder.addConditionToFilteringByViewer(user, relatedUserGroups, true);
 
 
   return queryBuilder.query.exec();
   return queryBuilder.query.exec();

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

@@ -563,9 +563,11 @@ module.exports = function(crowi, app) {
    * redirector
    * redirector
    */
    */
   async function redirector(req, res, next, path) {
   async function redirector(req, res, next, path) {
-    const pages = await Page.findByPathAndViewerV5(path, req.user);
-    const { redirectFrom } = req.query;
-    const query = redirectFrom == null ? '' : `?redirectFrom=${redirectFrom}`;
+    const pages = await Page.findByPathAndViewer(path, req.user, null, false);
+    let query = '';
+    Object.entries(req.query).forEach(([key, value], i) => {
+      query += i === 0 ? `?${key}=${value}` : `&${key}=${value}`;
+    });
 
 
     if (pages.length >= 2) {
     if (pages.length >= 2) {
       // TODO: return res.render('layout-growi/select_same_path_page', renderVars);
       // TODO: return res.render('layout-growi/select_same_path_page', renderVars);
@@ -574,7 +576,7 @@ module.exports = function(crowi, app) {
     }
     }
 
 
     if (pages.length === 1) {
     if (pages.length === 1) {
-      return res.redirect(`/${pages[0]._id}${query}`);
+      return res.safeRedirect(`/${pages[0]._id}${query}`);
     }
     }
 
 
     return next(); // to page.notFound
     return next(); // to page.notFound