Skip to content

Commit

Permalink
feat(frontend): Add checkPermissionSessionAdmin to checking permissio…
Browse files Browse the repository at this point in the history
…ns for admin
  • Loading branch information
aXenDeveloper committed Oct 17, 2024
1 parent bbd76fe commit 5579987
Show file tree
Hide file tree
Showing 27 changed files with 186 additions and 17 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ pnpm create vitnode-app@latest

| 🛠️ Software | Minimum | Recommended |
| :---------- | :------ | :---------- |
| Node.js | 18.17 | 20 |
| Node.js | 20 | 20 |
| PostgreSQL | 14 | 16 |

| 🖥️ Hardware | Minimum | Development |
Expand Down
8 changes: 8 additions & 0 deletions packages/backend/src/core/admin/ai/test/test.resolver.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { AdminAuthGuards, AdminPermission } from '@/utils';
import { UseGuards } from '@nestjs/common';
import { Args, Mutation, Resolver } from '@nestjs/graphql';

import { TestAdminCoreAiService } from './test.service';
Expand All @@ -7,6 +9,12 @@ export class TestAdminCoreAiResolver {
constructor(private readonly service: TestAdminCoreAiService) {}

@Mutation(() => String)
@UseGuards(AdminAuthGuards)
@AdminPermission({
plugin_code: 'core',
group: 'settings',
permission: 'can_manage_settings_ai',
})
async admin__core_ai__test(
@Args('prompt', { type: () => String }) prompt: string,
): Promise<string> {
Expand Down
7 changes: 6 additions & 1 deletion packages/backend/src/core/admin/email/logs/logs.resolver.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AdminAuthGuards } from '@/utils';
import { AdminAuthGuards, AdminPermission } from '@/utils';
import { UseGuards } from '@nestjs/common';
import { Args, Query, Resolver } from '@nestjs/graphql';

Expand All @@ -11,6 +11,11 @@ export class LogsAdminEmailResolver {

@Query(() => LogsAdminEmailObj)
@UseGuards(AdminAuthGuards)
@AdminPermission({
plugin_code: 'core',
group: 'settings',
permission: 'can_manage_settings_email',
})
async admin__core_email__logs(
@Args() args: LogsAdminEmailArgs,
): Promise<LogsAdminEmailObj> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AdminAuthGuards } from '@/utils';
import { AdminAuthGuards, AdminPermission } from '@/utils';
import { UseGuards } from '@nestjs/common';
import { Args, Mutation, Resolver } from '@nestjs/graphql';

Expand All @@ -12,6 +12,11 @@ export class EditAdminEmailSettingsResolver {

@Mutation(() => ShowAdminEmailSettingsServiceObj)
@UseGuards(AdminAuthGuards)
@AdminPermission({
plugin_code: 'core',
group: 'settings',
permission: 'can_manage_settings_email',
})
async admin__core_email_settings__edit(
@Args() args: EditAdminEmailSettingsServiceArgs,
): Promise<ShowAdminEmailSettingsServiceObj> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AdminAuthGuards } from '@/utils';
import { AdminAuthGuards, AdminPermission } from '@/utils';
import { UseGuards } from '@nestjs/common';
import { Query, Resolver } from '@nestjs/graphql';

Expand All @@ -11,6 +11,11 @@ export class ShowAdminEmailSettingsResolver {

@Query(() => ShowAdminEmailSettingsServiceObj)
@UseGuards(AdminAuthGuards)
@AdminPermission({
plugin_code: 'core',
group: 'settings',
permission: 'can_manage_settings_email',
})
admin__core_email_settings__show(): ShowAdminEmailSettingsServiceObj {
return this.service.show();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { CurrentUser, User } from '@/decorators';
import { AdminAuthGuards } from '@/utils';
import { AdminAuthGuards, AdminPermission } from '@/utils';
import { UseGuards } from '@nestjs/common';
import { Args, Mutation, Resolver } from '@nestjs/graphql';

Expand All @@ -12,6 +12,11 @@ export class TestAdminEmailSettingsResolver {

@Mutation(() => String)
@UseGuards(AdminAuthGuards)
@AdminPermission({
plugin_code: 'core',
group: 'settings',
permission: 'can_manage_settings_email',
})
async admin__core_email_settings__test(
@Args() args: TestAdminEmailSettingsServiceArgs,
@CurrentUser() user: User,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AdminAuthGuards } from '@/utils';
import { AdminAuthGuards, AdminPermission } from '@/utils';
import { UseGuards } from '@nestjs/common';
import { Args, Mutation, Resolver } from '@nestjs/graphql';

Expand All @@ -12,6 +12,11 @@ export class EditAdminManifestMetadataResolver {

@Mutation(() => ShowAdminManifestMetadataObj)
@UseGuards(AdminAuthGuards)
@AdminPermission({
plugin_code: 'core',
group: 'settings',
permission: 'can_manage_settings_metadata',
})
async admin__core_manifest_metadata__edit(
@Args() args: EditAdminManifestMetadataObj,
): Promise<ShowAdminManifestMetadataObj> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AdminAuthGuards } from '@/utils';
import { AdminAuthGuards, AdminPermission } from '@/utils';
import { UseGuards } from '@nestjs/common';
import { Query, Resolver } from '@nestjs/graphql';

Expand All @@ -11,6 +11,11 @@ export class ShowAdminManifestMetadataResolver {

@Query(() => ShowAdminManifestMetadataObj)
@UseGuards(AdminAuthGuards)
@AdminPermission({
plugin_code: 'core',
group: 'settings',
permission: 'can_manage_settings_metadata',
})
admin__core_manifest_metadata__show(): ShowAdminManifestMetadataObj {
return this.service.show();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AdminAuthGuards } from '@/utils';
import { AdminAuthGuards, AdminPermission } from '@/utils';
import { UseGuards } from '@nestjs/common';
import { Args, Mutation, Resolver } from '@nestjs/graphql';

Expand All @@ -12,6 +12,11 @@ export class EditAdminCaptchaSecurityResolver {

@Mutation(() => ShowAdminCaptchaSecurityObj)
@UseGuards(AdminAuthGuards)
@AdminPermission({
plugin_code: 'core',
group: 'settings',
permission: 'can_manage_settings_security',
})
admin__core_security__captcha__edit(
@Args() args: EditAdminCaptchaSecurityArgs,
): ShowAdminCaptchaSecurityObj {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { AdminAuthGuards } from '@/utils/guards/admin-auth.guard';
import {
AdminAuthGuards,
AdminPermission,
} from '@/utils/guards/admin-auth.guard';
import { UseGuards } from '@nestjs/common';
import { Query, Resolver } from '@nestjs/graphql';

Expand All @@ -11,6 +14,11 @@ export class ShowAdminCaptchaSecurityResolver {

@Query(() => ShowAdminCaptchaSecurityObj)
@UseGuards(AdminAuthGuards)
@AdminPermission({
plugin_code: 'core',
group: 'settings',
permission: 'can_manage_settings_security',
})
admin__core_security__captcha__show(): ShowAdminCaptchaSecurityObj {
return this.service.show();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AdminAuthGuards } from '@/utils';
import { AdminAuthGuards, AdminPermission } from '@/utils';
import { UseGuards } from '@nestjs/common';
import { Args, Mutation, Resolver } from '@nestjs/graphql';

Expand All @@ -14,6 +14,11 @@ export class EditAdminAuthorizationSettingsResolver {

@Mutation(() => ShowAdminAuthorizationSettingsObj)
@UseGuards(AdminAuthGuards)
@AdminPermission({
plugin_code: 'core',
group: 'settings',
permission: 'can_manage_settings_authorization',
})
admin__core_authorization_settings__edit(
@Args() args: EditAdminAuthorizationSettingsArgs,
): ShowAdminAuthorizationSettingsObj {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AdminAuthGuards } from '@/utils';
import { AdminAuthGuards, AdminPermission } from '@/utils';
import { UseGuards } from '@nestjs/common';
import { Args, Mutation, Resolver } from '@nestjs/graphql';

Expand All @@ -11,6 +11,11 @@ export class EditAdminMainSettingsResolver {

@Mutation(() => EditAdminSettingsObj)
@UseGuards(AdminAuthGuards)
@AdminPermission({
plugin_code: 'core',
group: 'settings',
permission: 'can_manage_settings_main',
})
async admin__core_main_settings__edit(
@Args() args: EditAdminMainSettingsArgs,
): Promise<EditAdminSettingsObj> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ShowCoreTerms } from '@/core/terms/show/show.dto';
import { AdminAuthGuards } from '@/utils';
import { AdminAuthGuards, AdminPermission } from '@/utils';
import { UseGuards } from '@nestjs/common';
import { Args, Mutation, Resolver } from '@nestjs/graphql';

Expand All @@ -12,6 +12,11 @@ export class CreateAdminTermsSettingsResolver {

@Mutation(() => ShowCoreTerms)
@UseGuards(AdminAuthGuards)
@AdminPermission({
plugin_code: 'core',
group: 'settings',
permission: 'can_manage_settings_terms',
})
async admin__core_terms_settings__create(
@Args() args: CreateAdminTermsSettingsArgs,
): Promise<ShowCoreTerms> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AdminAuthGuards } from '@/utils';
import { AdminAuthGuards, AdminPermission } from '@/utils';
import { UseGuards } from '@nestjs/common';
import { Args, Mutation, Resolver } from '@nestjs/graphql';

Expand All @@ -10,6 +10,11 @@ export class DeleteAdminTermsSettingsResolver {

@Mutation(() => String)
@UseGuards(AdminAuthGuards)
@AdminPermission({
plugin_code: 'core',
group: 'settings',
permission: 'can_manage_settings_terms',
})
async admin__core_terms_settings__delete(
@Args({ name: 'code', type: () => String }) code: string,
): Promise<string> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ShowCoreTerms } from '@/core/terms/show/show.dto';
import { AdminAuthGuards } from '@/utils';
import { AdminAuthGuards, AdminPermission } from '@/utils';
import { UseGuards } from '@nestjs/common';
import { Args, Mutation, Resolver } from '@nestjs/graphql';

Expand All @@ -12,6 +12,11 @@ export class EditAdminTermsSettingsResolver {

@Mutation(() => ShowCoreTerms)
@UseGuards(AdminAuthGuards)
@AdminPermission({
plugin_code: 'core',
group: 'settings',
permission: 'can_manage_settings_terms',
})
async admin__core_terms_settings__edit(
@Args() args: EditAdminTermsSettingsArgs,
): Promise<ShowCoreTerms> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ErrorView } from '@/views/theme/views/error/error-view';
import { fetcher } from './fetcher';
import { getAdminIdCookie, getUserIdCookie } from './get-user-id-cookie';
import { getAdminIdCookie } from './get-user-id-cookie';
import {
Admin__Sessions__Authorization,
Admin__Sessions__AuthorizationQuery,
Expand Down Expand Up @@ -27,3 +28,26 @@ export const getSessionAdminData = async () => {

return data;
};

export const checkPermissionSessionAdmin = async ({
plugin_code,
group,
permission,
}: {
plugin_code: string;
group: string;
permission: string;
}) => {
const {
admin__sessions__authorization: { permissions },
} = await getSessionAdminData();
const findPlugin = permissions.find(item => item.plugin_code === plugin_code);
const findGroup = findPlugin?.groups.find(item => item.id === group);
if (findGroup?.permissions.length === 0) return;
const findPermission = findGroup?.permissions.find(
item => item === permission,
);
if (!findPermission) return <ErrorView code="403" />;

return;
};
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import gql from 'graphql-tag';
export type Admin__Sessions__AuthorizationQueryVariables = Types.Exact<{ [key: string]: never; }>;


export type Admin__Sessions__AuthorizationQuery = { __typename?: 'Query', admin__sessions__authorization: { __typename?: 'AuthorizationAdminSessionsObj', version: string, restart_server: boolean, user: { __typename?: 'UserWithDangerousInfo', email: string, id: number, name_seo: string, name: string, avatar_color: string, language: string, avatar?: { __typename?: 'AvatarUser', id: number, dir_folder: string, file_name: string }, group: { __typename?: 'GroupUser', color?: string, id: number, name: Array<{ __typename?: 'StringLanguage', language_code: string, value: string }> }, files_permissions: { __typename?: 'FilesPermissionsCoreSessions', allow_upload: boolean, max_storage_for_submit: number, space_used: number, total_max_storage: number } } }, admin__nav__show: Array<{ __typename?: 'ShowAdminNavObj', code: string, nav: Array<{ __typename?: 'ShowAdminNavPluginsObj', code: string, icon?: string, children?: Array<{ __typename?: 'ShowAdminNavPlugins', icon?: string, code: string }> }> }> };
export type Admin__Sessions__AuthorizationQuery = { __typename?: 'Query', admin__sessions__authorization: { __typename?: 'AuthorizationAdminSessionsObj', version: string, restart_server: boolean, user: { __typename?: 'UserWithDangerousInfo', email: string, id: number, name_seo: string, name: string, avatar_color: string, language: string, avatar?: { __typename?: 'AvatarUser', id: number, dir_folder: string, file_name: string }, group: { __typename?: 'GroupUser', color?: string, id: number, name: Array<{ __typename?: 'StringLanguage', language_code: string, value: string }> }, files_permissions: { __typename?: 'FilesPermissionsCoreSessions', allow_upload: boolean, max_storage_for_submit: number, space_used: number, total_max_storage: number } }, permissions: Array<{ __typename?: 'PermissionsStaffObjWithoutPluginName', plugin_code: string, groups: Array<{ __typename?: 'PermissionsStaff', id: string, permissions: Array<string> }> }> }, admin__nav__show: Array<{ __typename?: 'ShowAdminNavObj', code: string, nav: Array<{ __typename?: 'ShowAdminNavPluginsObj', code: string, icon?: string, children?: Array<{ __typename?: 'ShowAdminNavPlugins', icon?: string, code: string }> }> }> };


export const Admin__Sessions__Authorization = gql`
Expand Down Expand Up @@ -39,6 +39,13 @@ export const Admin__Sessions__Authorization = gql`
}
version
restart_server
permissions {
groups {
id
permissions
}
plugin_code
}
}
admin__nav__show {
code
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ query Admin__sessions__authorization {
}
version
restart_server
permissions {
groups {
id
permissions
}
plugin_code
}
}
admin__nav__show {
code
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { TranslationsProvider } from '@/components/translations-provider';
import { Card } from '@/components/ui/card';
import { HeaderContent } from '@/components/ui/header-content';
import { getGlobalData } from '@/graphql/get-global-data';
import { checkPermissionSessionAdmin } from '@/graphql/get-session-admin-data';
import { Metadata } from 'next';
import { getTranslations } from 'next-intl/server';

Expand All @@ -16,6 +17,12 @@ export const generateMetadataAiSettingsAdmin = async (): Promise<Metadata> => {
};

export const AiSettingsCoreAdminView = async () => {
const perm = await checkPermissionSessionAdmin({
plugin_code: 'core',
group: 'settings',
permission: 'can_manage_settings_ai',
});
if (perm) return perm;
const [
t,
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Card } from '@/components/ui/card';
import { HeaderContent } from '@/components/ui/header-content';
import { fetcher } from '@/graphql/fetcher';
import { getGlobalData } from '@/graphql/get-global-data';
import { checkPermissionSessionAdmin } from '@/graphql/get-session-admin-data';
import {
Admin__Core_Authorization_Settings__Show,
Admin__Core_Authorization_Settings__ShowQuery,
Expand Down Expand Up @@ -31,6 +32,12 @@ export const generateMetadataAuthorizationSettingsAdmin =
};

export const AuthorizationSettingsCoreAdminView = async () => {
const perm = await checkPermissionSessionAdmin({
plugin_code: 'core',
group: 'settings',
permission: 'can_manage_settings_authorization',
});
if (perm) return perm;
const [
t,
data,
Expand Down
Loading

0 comments on commit 5579987

Please sign in to comment.