From 59d90a26ebc7a491e00b7bdb5a2d365b73cc20d8 Mon Sep 17 00:00:00 2001 From: aXenDeveloper Date: Mon, 26 Feb 2024 15:36:23 +0100 Subject: [PATCH] feat(forum): Add permissions to query for show forums in AdminCP --- .../forum/forums/create/create.service.ts | 5 +-- .../forum/forums/create/dto/create.args.ts | 8 ++-- .../admin/forum/forums/show/dto/show.obj.ts | 45 +++++++++++++++++-- .../admin/forum/forums/show/show.service.ts | 21 ++++++++- backend/src/schema.gql | 36 ++++++++------- .../content/permissions/permissions.tsx | 8 ++-- .../hooks/use-create-edit-form-forum-admin.ts | 8 ++-- .../table/hooks/use-forum-forums-admin-api.ts | 4 +- frontend/graphql/hooks.ts | 41 ++++++++++------- 9 files changed, 121 insertions(+), 55 deletions(-) diff --git a/backend/src/admin/forum/forums/create/create.service.ts b/backend/src/admin/forum/forums/create/create.service.ts index b90640b4e..644b9192d 100644 --- a/backend/src/admin/forum/forums/create/create.service.ts +++ b/backend/src/admin/forum/forums/create/create.service.ts @@ -84,10 +84,7 @@ export class CreateForumForumsService { permissions.groups.map(item => ({ forum_id: data[0].id, group_id: item.id, - can_create: item.create, - can_read: item.read, - can_reply: item.reply, - can_view: item.view + ...item })) ); } diff --git a/backend/src/admin/forum/forums/create/dto/create.args.ts b/backend/src/admin/forum/forums/create/dto/create.args.ts index 5412ddc7e..482ccfd3b 100644 --- a/backend/src/admin/forum/forums/create/dto/create.args.ts +++ b/backend/src/admin/forum/forums/create/dto/create.args.ts @@ -14,16 +14,16 @@ class GroupsPermissionsCreateForumForums { id: number; @Field(() => Boolean) - view: boolean; + can_view: boolean; @Field(() => Boolean) - read: boolean; + can_read: boolean; @Field(() => Boolean) - create: boolean; + can_create: boolean; @Field(() => Boolean) - reply: boolean; + can_reply: boolean; } @InputType() diff --git a/backend/src/admin/forum/forums/show/dto/show.obj.ts b/backend/src/admin/forum/forums/show/dto/show.obj.ts index 32819f327..88e842e51 100644 --- a/backend/src/admin/forum/forums/show/dto/show.obj.ts +++ b/backend/src/admin/forum/forums/show/dto/show.obj.ts @@ -1,16 +1,55 @@ -import { Field, ObjectType, OmitType } from "@nestjs/graphql"; +import { Field, Int, ObjectType, OmitType } from "@nestjs/graphql"; import { ShowForumForumsWithParent } from "../../../../../forum/forums/show/dto/show.obj"; import { PageInfo } from "@/types/database/pagination.type"; +@ObjectType() +class GroupsPermissionsForumForums { + @Field(() => Int) + id: number; + + @Field(() => Boolean) + can_view: boolean; + + @Field(() => Boolean) + can_read: boolean; + + @Field(() => Boolean) + can_create: boolean; + + @Field(() => Boolean) + can_reply: boolean; +} + +@ObjectType() +export class PermissionsForumForums { + @Field(() => Boolean) + can_all_view: boolean; + + @Field(() => Boolean) + can_all_read: boolean; + + @Field(() => Boolean) + can_all_create: boolean; + + @Field(() => Boolean) + can_all_reply: boolean; + + @Field(() => [GroupsPermissionsForumForums]) + groups: GroupsPermissionsForumForums[]; +} + @ObjectType() export class ShowForumForumsAdmin extends OmitType(ShowForumForumsWithParent, [ "permissions" -] as const) {} +] as const) { + @Field(() => PermissionsForumForums) + permissions: PermissionsForumForums; +} @ObjectType() export class ShowForumForumsAdminObj { - @Field(() => [ShowForumForumsAdmin]) + @Field(() => [ShowForumForumsWithParent]) edges: ShowForumForumsAdmin[]; @Field(() => PageInfo) diff --git a/backend/src/admin/forum/forums/show/show.service.ts b/backend/src/admin/forum/forums/show/show.service.ts index 8f490f0de..e24dbee87 100644 --- a/backend/src/admin/forum/forums/show/show.service.ts +++ b/backend/src/admin/forum/forums/show/show.service.ts @@ -50,7 +50,8 @@ export class ShowForumForumsAdminService { name: true, description: true } - } + }, + permissions: true } }); @@ -77,6 +78,13 @@ export class ShowForumForumsAdminService { parent: forum.parent_id ? { ...forum.parent, _count: { children: 0 } } : null, + permissions: { + can_all_view: forum.can_all_view, + can_all_read: forum.can_all_read, + can_all_create: forum.can_all_create, + can_all_reply: forum.can_all_reply, + groups: forum.permissions + }, _count: { children: children.length }, children: await Promise.all( children.map(async child => { @@ -93,7 +101,16 @@ export class ShowForumForumsAdminService { return { ...child, - children, + children: children.map(child => ({ + ...child, + permissions: { + can_all_view: child.can_all_view, + can_all_read: child.can_all_read, + can_all_create: child.can_all_create, + can_all_reply: child.can_all_reply, + groups: child.permissions + } + })), _count: { children: children.length } }; }) diff --git a/backend/src/schema.gql b/backend/src/schema.gql index 2880e14df..e03c877f9 100644 --- a/backend/src/schema.gql +++ b/backend/src/schema.gql @@ -66,11 +66,19 @@ type GroupUser { } input GroupsPermissionsCreateForumForums { - create: Boolean! + can_create: Boolean! + can_read: Boolean! + can_reply: Boolean! + can_view: Boolean! + id: Int! +} + +type GroupsPermissionsForumForums { + can_create: Boolean! + can_read: Boolean! + can_reply: Boolean! + can_view: Boolean! id: Int! - read: Boolean! - reply: Boolean! - view: Boolean! } enum LayoutAdminInstallEnum { @@ -140,6 +148,14 @@ input PermissionsCreateForumForums { groups: [GroupsPermissionsCreateForumForums!]! } +type PermissionsForumForums { + can_all_create: Boolean! + can_all_read: Boolean! + can_all_reply: Boolean! + can_all_view: Boolean! + groups: [GroupsPermissionsForumForums!]! +} + type PermissionsForumForumsCount { can_create: Boolean! can_read: Boolean! @@ -425,18 +441,8 @@ type ShowForumForums { position: Int! } -type ShowForumForumsAdmin { - children: [ChildrenShowForumForums!]! - created: Int! - description: [TextLanguage!]! - id: Int! - name: [TextLanguage!]! - parent: ShowForumForums - position: Int! -} - type ShowForumForumsAdminObj { - edges: [ShowForumForumsAdmin!]! + edges: [ShowForumForumsWithParent!]! pageInfo: PageInfo! } diff --git a/frontend/admin/forum/views/forums/create-edit/content/permissions/permissions.tsx b/frontend/admin/forum/views/forums/create-edit/content/permissions/permissions.tsx index 98fe13812..4fb94f4d0 100644 --- a/frontend/admin/forum/views/forums/create-edit/content/permissions/permissions.tsx +++ b/frontend/admin/forum/views/forums/create-edit/content/permissions/permissions.tsx @@ -15,20 +15,20 @@ export const PermissionsContentCreateEditFormForumAdmin = () => { { groups: z.array( z.object({ id: z.number(), - view: z.boolean(), - read: z.boolean(), - create: z.boolean(), - reply: z.boolean() + can_view: z.boolean(), + can_read: z.boolean(), + can_create: z.boolean(), + can_reply: z.boolean() }) ) }) diff --git a/frontend/admin/forum/views/forums/table/hooks/use-forum-forums-admin-api.ts b/frontend/admin/forum/views/forums/table/hooks/use-forum-forums-admin-api.ts index 8375c56e9..8c767b9a8 100644 --- a/frontend/admin/forum/views/forums/table/hooks/use-forum-forums-admin-api.ts +++ b/frontend/admin/forum/views/forums/table/hooks/use-forum-forums-admin-api.ts @@ -2,12 +2,12 @@ import { useState } from "react"; import { toast } from "sonner"; import { useTranslations } from "next-intl"; -import { type ShowForumForumsAdmin } from "@/graphql/hooks"; import { mutationUpdateDataApi } from "./mutation-update-data-api"; import { buildTree, flattenTree, type FlatTree } from "../use-functions"; +import type { ChildrenShowForumForums } from "@/graphql/hooks"; export interface ShowForumForumsAdminWithChildren - extends Omit { + extends Omit { children: ShowForumForumsAdminWithChildren[]; } diff --git a/frontend/graphql/hooks.ts b/frontend/graphql/hooks.ts index de3004562..031dce173 100644 --- a/frontend/graphql/hooks.ts +++ b/frontend/graphql/hooks.ts @@ -89,11 +89,20 @@ export type GroupUser = { }; export type GroupsPermissionsCreateForumForums = { - create: Scalars['Boolean']['input']; + can_create: Scalars['Boolean']['input']; + can_read: Scalars['Boolean']['input']; + can_reply: Scalars['Boolean']['input']; + can_view: Scalars['Boolean']['input']; id: Scalars['Int']['input']; - read: Scalars['Boolean']['input']; - reply: Scalars['Boolean']['input']; - view: Scalars['Boolean']['input']; +}; + +export type GroupsPermissionsForumForums = { + __typename?: 'GroupsPermissionsForumForums'; + can_create: Scalars['Boolean']['output']; + can_read: Scalars['Boolean']['output']; + can_reply: Scalars['Boolean']['output']; + can_view: Scalars['Boolean']['output']; + id: Scalars['Int']['output']; }; export const LayoutAdminInstallEnum = { @@ -383,6 +392,15 @@ export type PermissionsCreateForumForums = { groups: Array; }; +export type PermissionsForumForums = { + __typename?: 'PermissionsForumForums'; + can_all_create: Scalars['Boolean']['output']; + can_all_read: Scalars['Boolean']['output']; + can_all_reply: Scalars['Boolean']['output']; + can_all_view: Scalars['Boolean']['output']; + groups: Array; +}; + export type PermissionsForumForumsCount = { __typename?: 'PermissionsForumForumsCount'; can_create: Scalars['Boolean']['output']; @@ -819,20 +837,9 @@ export type ShowForumForums = { position: Scalars['Int']['output']; }; -export type ShowForumForumsAdmin = { - __typename?: 'ShowForumForumsAdmin'; - children: Array; - created: Scalars['Int']['output']; - description: Array; - id: Scalars['Int']['output']; - name: Array; - parent?: Maybe; - position: Scalars['Int']['output']; -}; - export type ShowForumForumsAdminObj = { __typename?: 'ShowForumForumsAdminObj'; - edges: Array; + edges: Array; pageInfo: PageInfo; }; @@ -1283,7 +1290,7 @@ export type Admin__Forum_Forums__ShowQueryVariables = Exact<{ }>; -export type Admin__Forum_Forums__ShowQuery = { __typename?: 'Query', admin__forum_forums__show: { __typename?: 'ShowForumForumsAdminObj', edges: Array<{ __typename?: 'ShowForumForumsAdmin', id: number, position: number, created: number, description: Array<{ __typename?: 'TextLanguage', language_code: string, value: string }>, name: Array<{ __typename?: 'TextLanguage', language_code: string, value: string }>, children: Array<{ __typename?: 'ChildrenShowForumForums', created: number, id: number, position: number, description: Array<{ __typename?: 'TextLanguage', language_code: string, value: string }>, name: Array<{ __typename?: 'TextLanguage', language_code: string, value: string }> }> }>, pageInfo: { __typename?: 'PageInfo', count: number, endCursor?: number | null, hasNextPage: boolean, hasPreviousPage: boolean, startCursor?: number | null, totalCount: number } } }; +export type Admin__Forum_Forums__ShowQuery = { __typename?: 'Query', admin__forum_forums__show: { __typename?: 'ShowForumForumsAdminObj', edges: Array<{ __typename?: 'ShowForumForumsWithParent', id: number, position: number, created: number, description: Array<{ __typename?: 'TextLanguage', language_code: string, value: string }>, name: Array<{ __typename?: 'TextLanguage', language_code: string, value: string }>, children: Array<{ __typename?: 'ChildrenShowForumForums', created: number, id: number, position: number, description: Array<{ __typename?: 'TextLanguage', language_code: string, value: string }>, name: Array<{ __typename?: 'TextLanguage', language_code: string, value: string }> }> }>, pageInfo: { __typename?: 'PageInfo', count: number, endCursor?: number | null, hasNextPage: boolean, hasPreviousPage: boolean, startCursor?: number | null, totalCount: number } } }; export type Admin__Core_Groups__ShowQueryVariables = Exact<{ first?: InputMaybe;