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

refactor: simplify user page access checks and remove redundant conditions

Shun Miyazawa 2 месяцев назад
Родитель
Сommit
9eed67be0d
2 измененных файлов с 19 добавлено и 34 удалено
  1. 15 22
      apps/app/src/pages/[[...path]]/page-data-props.ts
  2. 4 12
      apps/app/src/server/routes/apiv3/page/index.ts

+ 15 - 22
apps/app/src/pages/[[...path]]/page-data-props.ts

@@ -173,28 +173,21 @@ export async function getPageDataForInitial(
     const isTargetUserPage = isUserPage(pagePath) || isUsersTopPage(pagePath);
     const isTargetUserPage = isUserPage(pagePath) || isUsersTopPage(pagePath);
 
 
     if (isTargetUserPage) {
     if (isTargetUserPage) {
-      const isOwnPage =
-        user != null &&
-        (pagePath === `/user/${user.username}` ||
-          pagePath.startsWith(`/user/${user.username}/`));
-
-      if (!isOwnPage) {
-        return {
-          props: {
-            currentPathname: resolvedPagePath,
-            isIdenticalPathPage: false,
-            pageWithMeta: {
-              data: null,
-              meta: {
-                isNotFound: true,
-                isForbidden: true,
-              },
-            } satisfies IDataWithRequiredMeta<null, IPageNotFoundInfo>,
-            skipSSR: false,
-            redirectFrom,
-          },
-        };
-      }
+      return {
+        props: {
+          currentPathname: resolvedPagePath,
+          isIdenticalPathPage: false,
+          pageWithMeta: {
+            data: null,
+            meta: {
+              isNotFound: true,
+              isForbidden: true,
+            },
+          } satisfies IDataWithRequiredMeta<null, IPageNotFoundInfo>,
+          skipSSR: false,
+          redirectFrom,
+        },
+      };
     }
     }
   }
   }
 
 

+ 4 - 12
apps/app/src/server/routes/apiv3/page/index.ts

@@ -228,22 +228,14 @@ module.exports = (crowi: Crowi) => {
         }
         }
 
 
         if (isHidingUserPages && page != null) {
         if (isHidingUserPages && page != null) {
-          const pagePath = page.path;
           const isTargetUserPage =
           const isTargetUserPage =
             isUserPage(page.path) || isUsersTopPage(page.path);
             isUserPage(page.path) || isUsersTopPage(page.path);
 
 
           if (isTargetUserPage) {
           if (isTargetUserPage) {
-            const isOwnPage =
-              user != null &&
-              (pagePath === `/user/${user.username}` ||
-                pagePath.startsWith(`/user/${user.username}/`));
-
-            if (!isOwnPage) {
-              return res.apiv3Err(
-                new ErrorV3('Page is forbidden', 'page-is-forbidden'),
-                403,
-              );
-            }
+            return res.apiv3Err(
+              new ErrorV3('Page is forbidden', 'page-is-forbidden'),
+              403,
+            );
           }
           }
         }
         }