Skip to content

Commit

Permalink
feat(frontend): Add checking permissions for all routes in AdminCP
Browse files Browse the repository at this point in the history
  • Loading branch information
aXenDeveloper committed Oct 19, 2024
1 parent 85455af commit 7e55312
Show file tree
Hide file tree
Showing 18 changed files with 168 additions and 7 deletions.
3 changes: 2 additions & 1 deletion packages/backend-ai-google/.swcrc
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@
},
"module": {
"type": "commonjs"
}
},
"minify": true
}
3 changes: 2 additions & 1 deletion packages/backend-ai-open-ai/.swcrc
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@
},
"module": {
"type": "commonjs"
}
},
"minify": true
}
3 changes: 2 additions & 1 deletion packages/backend-email-resend/.swcrc
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@
},
"module": {
"type": "commonjs"
}
},
"minify": true
}
3 changes: 2 additions & 1 deletion packages/backend-email-smtp/.swcrc
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@
},
"module": {
"type": "commonjs"
}
},
"minify": true
}
3 changes: 2 additions & 1 deletion packages/backend/.swcrc
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@
},
"module": {
"type": "commonjs"
}
},
"minify": true
}
3 changes: 2 additions & 1 deletion packages/frontend/.swcrc
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,6 @@
"module": {
"type": "nodenext",
"strict": true
}
},
"minify": true
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ import {
getPaginationTool,
SearchParamsPagination,
} from '@/graphql/get-pagination-tool';
import {
checkAdminPermissionPage,
checkAdminPermissionPageMetadata,
} from '@/graphql/get-session-admin-data';
import {
Admin__Core_Files__Show,
Admin__Core_Files__ShowQuery,
Expand Down Expand Up @@ -32,8 +36,16 @@ export interface FilesAdvancedCoreAdminViewProps {
searchParams: Promise<SearchParamsPagination>;
}

const permission = {
plugin_code: 'core',
group: 'advanced',
permission: 'can_manage_advanced_files',
};

export const generateMetadataFilesAdvancedCoreAdmin =
async (): Promise<Metadata> => {
const perm = await checkAdminPermissionPageMetadata(permission);
if (perm) return perm;
const t = await getTranslations('admin.core.advanced.files');

return {
Expand All @@ -44,6 +56,8 @@ export const generateMetadataFilesAdvancedCoreAdmin =
export const FilesAdvancedCoreAdminView = async ({
searchParams,
}: FilesAdvancedCoreAdminViewProps) => {
const perm = await checkAdminPermissionPage(permission);
if (perm) return perm;
const variables = await getPaginationTool({
searchParams,
defaultPageSize: 10,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ import {
getPaginationTool,
SearchParamsPagination,
} from '@/graphql/get-pagination-tool';
import {
checkAdminPermissionPage,
checkAdminPermissionPageMetadata,
} from '@/graphql/get-session-admin-data';
import {
Admin__Core_Languages__Show,
Admin__Core_Languages__ShowQuery,
Expand Down Expand Up @@ -36,7 +40,15 @@ interface Props {
searchParams: Promise<SearchParamsPagination>;
}

const permission = {
plugin_code: 'core',
group: 'can_manage_langs',
permission: '',
};

export const generateMetadataLangsCoreAdmin = async (): Promise<Metadata> => {
const perm = await checkAdminPermissionPageMetadata(permission);
if (perm) return perm;
const t = await getTranslations('admin.core.langs');

return {
Expand All @@ -45,6 +57,8 @@ export const generateMetadataLangsCoreAdmin = async (): Promise<Metadata> => {
};

export const LangsCoreAdminView = async ({ searchParams }: Props) => {
const perm = await checkAdminPermissionPage(permission);
if (perm) return perm;
const variables = await getPaginationTool({
searchParams,
defaultPageSize: 10,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ import { DateFormat } from '@/components/date-format';
import { Badge } from '@/components/ui/badge';
import { Card } from '@/components/ui/card';
import { HeaderContent } from '@/components/ui/header-content';
import {
checkAdminPermissionPage,
checkAdminPermissionPageMetadata,
} from '@/graphql/get-session-admin-data';
import { CONFIG } from '@/helpers/config-with-env';
import { redirect } from '@/navigation';
import { ExternalLink } from 'lucide-react';
Expand All @@ -21,9 +25,17 @@ interface Props {
}>;
}

const permission = {
plugin_code: 'core',
group: 'can_manage_plugins',
permission: '',
};

export async function generateMetadataDevPluginAdminLayout({
params,
}: Props): Promise<Metadata> {
const perm = await checkAdminPermissionPageMetadata(permission);
if (perm) return perm;
const { code } = await params;
const data = await getPluginDataAdmin({ code });
if (data.admin__core_plugins__show.edges.length === 0) return {};
Expand All @@ -39,6 +51,8 @@ export async function generateMetadataDevPluginAdminLayout({
}

export const DevPluginAdminLayout = async ({ params, children }: Props) => {
const perm = await checkAdminPermissionPage(permission);
if (perm) return perm;
const { code, locale } = await params;
if (!CONFIG.node_development) redirect({ href: '/admin', locale });
const [data, t] = await Promise.all([
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,24 @@ 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 {
checkAdminPermissionPage,
checkAdminPermissionPageMetadata,
} from '@/graphql/get-session-admin-data';
import { Metadata } from 'next';
import { getTranslations } from 'next-intl/server';

import { ContentEditorAdmin } from './content';

const permission = {
plugin_code: 'core',
group: 'styles',
permission: 'can_manage_styles_editor',
};

export const generateMetadataEditorAdmin = async (): Promise<Metadata> => {
const perm = await checkAdminPermissionPageMetadata(permission);
if (perm) return perm;
const t = await getTranslations('admin.core.styles.editor');

return {
Expand All @@ -16,6 +28,8 @@ export const generateMetadataEditorAdmin = async (): Promise<Metadata> => {
};

export const EditorAdminView = async () => {
const perm = await checkAdminPermissionPage(permission);
if (perm) return perm;
const [t, data] = await Promise.all([
getTranslations('admin.core.styles.editor'),
getGlobalData(),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { TranslationsProvider } from '@/components/translations-provider';
import { HeaderContent } from '@/components/ui/header-content';
import { fetcher } from '@/graphql/fetcher';
import {
checkAdminPermissionPage,
checkAdminPermissionPageMetadata,
} from '@/graphql/get-session-admin-data';
import {
Admin__Core_Nav__Show,
Admin__Core_Nav__ShowQuery,
Expand All @@ -25,7 +29,15 @@ const getData = async () => {
return data;
};

const permission = {
plugin_code: 'core',
group: 'styles',
permission: 'can_manage_styles_nav',
};

export const generateMetadataNavAdmin = async (): Promise<Metadata> => {
const perm = await checkAdminPermissionPageMetadata(permission);
if (perm) return perm;
const t = await getTranslations('admin.core.styles.nav');

return {
Expand All @@ -34,6 +46,8 @@ export const generateMetadataNavAdmin = async (): Promise<Metadata> => {
};

export const NavAdminView = async () => {
const perm = await checkAdminPermissionPage(permission);
if (perm) return perm;
const [data, t] = await Promise.all([
getData(),
getTranslations('admin.core.styles.nav'),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ import {
getPaginationTool,
SearchParamsPagination,
} from '@/graphql/get-pagination-tool';
import {
checkAdminPermissionPage,
checkAdminPermissionPageMetadata,
} from '@/graphql/get-session-admin-data';
import {
Admin__Core_Groups__Show,
Admin__Core_Groups__ShowQuery,
Expand Down Expand Up @@ -34,8 +38,16 @@ export interface GroupsMembersAdminViewProps {
searchParams: Promise<SearchParamsPagination>;
}

const permission = {
plugin_code: 'members',
group: 'can_manage_groups',
permission: '',
};

export const generateMetadataGroupsMembersAdmin =
async (): Promise<Metadata> => {
const perm = await checkAdminPermissionPageMetadata(permission);
if (perm) return perm;
const t = await getTranslations('admin.members.groups');

return {
Expand All @@ -46,6 +58,8 @@ export const generateMetadataGroupsMembersAdmin =
export const GroupsMembersAdminView = async ({
searchParams,
}: GroupsMembersAdminViewProps) => {
const perm = await checkAdminPermissionPage(permission);
if (perm) return perm;
const variables = await getPaginationTool({
searchParams,
sortByEnum: ShowAdminGroupsSortingColumnEnum,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ import {
getPaginationTool,
SearchParamsPagination,
} from '@/graphql/get-pagination-tool';
import {
checkAdminPermissionPage,
checkAdminPermissionPageMetadata,
} from '@/graphql/get-session-admin-data';
import {
Admin__Core_Staff_Administrators__Show,
Admin__Core_Staff_Administrators__ShowQuery,
Expand All @@ -32,8 +36,16 @@ const getData = async (
return data;
};

const permission = {
plugin_code: 'members',
group: 'staff',
permission: 'can_manage_staff_administrators',
};

export const generateMetadataAdministratorsStaffAdmin =
async (): Promise<Metadata> => {
const perm = await checkAdminPermissionPageMetadata(permission);
if (perm) return perm;
const t = await getTranslations('admin.members.staff.administrators');

return {
Expand All @@ -46,6 +58,8 @@ export const AdministratorsStaffAdminView = async ({
}: {
searchParams: Promise<SearchParamsPagination>;
}) => {
const perm = await checkAdminPermissionPage(permission);
if (perm) return perm;
const variables = await getPaginationTool({
searchParams,
sortByEnum: ShowAdminStaffAdministratorsSortingColumnEnum,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ import {
getPaginationTool,
SearchParamsPagination,
} from '@/graphql/get-pagination-tool';
import {
checkAdminPermissionPage,
checkAdminPermissionPageMetadata,
} from '@/graphql/get-session-admin-data';
import {
Admin__Core_Staff_Moderators__Show,
Admin__Core_Staff_Moderators__ShowQuery,
Expand All @@ -31,8 +35,16 @@ const getData = async (
return data;
};

const permission = {
plugin_code: 'members',
group: 'staff',
permission: 'can_manage_staff_moderators',
};

export const generateMetadataModeratorsStaffAdmin =
async (): Promise<Metadata> => {
const perm = await checkAdminPermissionPageMetadata(permission);
if (perm) return perm;
const t = await getTranslations('admin.members.staff.moderators');

return {
Expand All @@ -45,6 +57,8 @@ export const ModeratorsStaffAdminView = async ({
}: {
searchParams: Promise<SearchParamsPagination>;
}) => {
const perm = await checkAdminPermissionPage(permission);
if (perm) return perm;
const variables = await getPaginationTool({
searchParams,
sortByEnum: ShowAdminStaffModeratorsSortingColumnEnum,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ import { Card } from '@/components/ui/card';
import { AvatarUser } from '@/components/ui/user/avatar';
import { GroupFormat } from '@/components/ui/user/group-format';
import { fetcher } from '@/graphql/fetcher';
import {
checkAdminPermissionPage,
checkAdminPermissionPageMetadata,
} from '@/graphql/get-session-admin-data';
import {
Admin__Core_Members__Show__Item,
Admin__Core_Members__Show__ItemQuery,
Expand Down Expand Up @@ -34,7 +38,15 @@ interface Props {
params: Promise<{ id: string }>;
}

const permission = {
plugin_code: 'members',
group: 'users',
permission: 'can_manage_users',
};

export const generateMetadataUserMembersAdmin = async ({ params }: Props) => {
const perm = await checkAdminPermissionPageMetadata(permission);
if (perm) return perm;
const { id } = await params;
const data = await getData({ id: +id });

Expand All @@ -50,6 +62,8 @@ export const generateMetadataUserMembersAdmin = async ({ params }: Props) => {
};

export const UserMembersAdminView = async ({ params }: Props) => {
const perm = await checkAdminPermissionPage(permission);
if (perm) return perm;
const { id } = await params;
const [
t,
Expand Down
Loading

0 comments on commit 7e55312

Please sign in to comment.