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

Merge branch 'fix-api-to-use-granted-user-groups' into granted-groups-tests

Futa Arai 2 лет назад
Родитель
Сommit
b55fa47a03

+ 1 - 1
apps/app/src/client/services/side-effects/drawio-modal-launcher-for-view.ts

@@ -44,7 +44,7 @@ export const useDrawioModalLauncherForView = (opts?: {
     const currentMarkdown = currentPage.revision.body;
     const newMarkdown = mdu.replaceDrawioInMarkdown(drawioMxFile, currentMarkdown, bol, eol);
 
-    const grantUserGroupIds = currentPage.grantedGroups?.map((g) => {
+    const grantUserGroupIds = currentPage.grantedGroups.map((g) => {
       return {
         type: g.type,
         item: g.item._id,

+ 1 - 1
apps/app/src/client/services/side-effects/handsontable-modal-launcher-for-view.ts

@@ -43,7 +43,7 @@ export const useHandsontableModalLauncherForView = (opts?: {
     const currentMarkdown = currentPage.revision.body;
     const newMarkdown = mtu.replaceMarkdownTableInMarkdown(table, currentMarkdown, bol, eol);
 
-    const grantUserGroupIds = currentPage.grantedGroups?.map((g) => {
+    const grantUserGroupIds = currentPage.grantedGroups.map((g) => {
       return {
         type: g.type,
         item: g.item._id,

+ 2 - 2
apps/app/src/components/PageAlert/FixPageGrantAlert.tsx

@@ -1,6 +1,6 @@
 import React, { useEffect, useState, useCallback } from 'react';
 
-import { PageGrant } from '@growi/core';
+import { GroupType, PageGrant } from '@growi/core';
 import { useTranslation } from 'react-i18next';
 import {
   Modal, ModalHeader, ModalBody, ModalFooter,
@@ -58,7 +58,7 @@ const FixPageGrantModal = (props: ModalProps): JSX.Element => {
     try {
       await apiv3Put(`/page/${pageId}/grant`, {
         grant: selectedGrant,
-        grantedGroup: selectedGroup?._id,
+        grantedGroups: selectedGroup?._id != null ? [{ item: selectedGroup?._id, type: GroupType.userGroup }] : null,
       });
 
       toastSuccess(t('Successfully updated'));

+ 2 - 1
apps/app/src/components/PageAlert/PageGrantAlert.tsx

@@ -1,5 +1,6 @@
 import React from 'react';
 
+import { isPopulated } from '@growi/core';
 import { useTranslation } from 'react-i18next';
 
 import { useSWRxCurrentPage } from '~/stores/page';
@@ -33,7 +34,7 @@ export const PageGrantAlert = (): JSX.Element => {
         return (
           <>
             <i className="icon-fw icon-organization"></i><strong>{
-              typeof pageData.grantedGroups[0].item !== 'string' && pageData.grantedGroups[0].item.name
+              isPopulated(pageData.grantedGroups[0].item) && pageData.grantedGroups[0].item.name
             }</strong>
           </>
         );

+ 2 - 2
apps/app/src/components/PageEditor.tsx

@@ -5,7 +5,7 @@ import React, {
 import EventEmitter from 'events';
 import nodePath from 'path';
 
-import type { IPageHasId, GroupType } from '@growi/core';
+import type { IPageHasId } from '@growi/core';
 import { pathUtils } from '@growi/core/dist/utils';
 import detectIndent from 'detect-indent';
 import { useTranslation } from 'next-i18next';
@@ -180,7 +180,7 @@ const PageEditor = React.memo((): JSX.Element => {
       return;
     }
     const grantedGroups = grantData.grantedGroups?.map((group) => {
-      return { item: group.id, type: group.type as GroupType };
+      return { item: group.id, type: group.type };
     });
     const optionsToSave = {
       isSlackEnabled: isSlackEnabled ?? false,

+ 1 - 2
apps/app/src/components/PageEditorByHackmd.tsx

@@ -4,7 +4,6 @@ import React, {
 
 import EventEmitter from 'events';
 
-import { GroupType } from '@growi/core';
 import { pathUtils } from '@growi/core/dist/utils';
 import Link from 'next/link';
 import { useRouter } from 'next/router';
@@ -100,7 +99,7 @@ export const PageEditorByHackmd = (): JSX.Element => {
       return;
     }
     const grantedGroups = grantData.grantedGroups?.map((group) => {
-      return { item: group.id, type: group.type as GroupType };
+      return { item: group.id, type: group.type };
     });
     const optionsToSave = {
       isSlackEnabled: isSlackEnabled ?? false,

+ 2 - 2
apps/app/src/components/SavePageControls/GrantSelector.tsx

@@ -1,7 +1,7 @@
 import React, { useCallback, useState } from 'react';
 
 import { isPopulated } from '@growi/core';
-import type { IUserGroupHasId } from '@growi/core';
+import type { GroupType, IUserGroupHasId } from '@growi/core';
 import { useTranslation } from 'next-i18next';
 import {
   UncontrolledDropdown,
@@ -38,7 +38,7 @@ type Props = {
   grantedGroups?: {
     id: string,
     name: string,
-    type: string,
+    type: GroupType,
   }[]
 
   onUpdateGrant?: (grantData: IPageGrantData) => void,

+ 2 - 2
apps/app/src/interfaces/page.ts

@@ -1,4 +1,4 @@
-import type { IPageHasId, Nullable } from '@growi/core';
+import type { GroupType, IPageHasId, Nullable } from '@growi/core';
 
 import type { IPageOperationProcessData } from './page-operation';
 
@@ -13,7 +13,7 @@ export type IPageGrantData = {
   grantedGroups?: {
     id: string,
     name: string,
-    type: string,
+    type: GroupType,
   }[]
 }
 

+ 3 - 3
apps/app/src/pages/[[...path]].page.tsx

@@ -417,15 +417,15 @@ class MultiplePagesHitsError extends ExtensibleCustomError {
 
 // apply parent page grant fot creating page
 async function applyGrantToPage(props: Props, ancestor: any) {
-  await ancestor.populate('grantedGroups');
+  await ancestor.populate('grantedGroups.item');
   const grant = {
     grant: ancestor.grant,
   };
   const grantedGroups = ancestor.grantedGroups ? {
     grantedGroups: ancestor.grantedGroups.map((group) => {
       return {
-        id: group.id,
-        name: group.name,
+        id: group.item._id,
+        name: group.item.name,
         type: group.type,
       };
     }),

+ 1 - 0
apps/app/src/server/models/page.ts

@@ -121,6 +121,7 @@ const schema = new Schema<PageDocument, PageModel>({
       const uniqueItemValues = new Set(arr.map(e => e.item));
       return arr.length === uniqueItemValues.size;
     }, 'grantedGroups contains non unique item'],
+    default: [],
   },
   creator: { type: ObjectId, ref: 'User', index: true },
   lastUpdateUser: { type: ObjectId, ref: 'User' },

+ 3 - 1
apps/app/src/server/routes/apiv3/page.js

@@ -198,6 +198,8 @@ module.exports = (crowi) => {
       param('pageId').isMongoId().withMessage('pageId is required'),
       body('grant').isInt().withMessage('grant is required'),
       body('grantedGroups').optional().isArray().withMessage('grantedGroups must be an array'),
+      body('grantedGroups.*.type').isString().withMessage('grantedGroups type is required'),
+      body('grantedGroups.*.item').isMongoId().withMessage('grantedGroups item is required'),
     ],
     export: [
       query('format').isString().isIn(['md', 'pdf']),
@@ -571,7 +573,7 @@ module.exports = (crowi) => {
     try {
       const shouldUseV4Process = false;
       const grantData = { grant, grantedGroups };
-      data = await this.crowi.pageService.updateGrant(page, req.user, grantData, shouldUseV4Process);
+      data = await crowi.pageService.updateGrant(page, req.user, grantData, shouldUseV4Process);
     }
     catch (err) {
       logger.error('Error occurred while processing calcApplicableGrantData.', err);