From 4f1c78f8e1bcbcf1592e800755c0a8aa7867c54a Mon Sep 17 00:00:00 2001 From: Tola Leng Date: Mon, 26 May 2025 17:50:53 +0800 Subject: [PATCH] Refactor: Implement permission notice for admin role --- .../settings/general/GeneralSettingsPanel.tsx | 37 ++++++-- .../settings/general/SystemSettingsTab.tsx | 2 +- .../user-management/UserManagement.tsx | 91 +++++++------------ application/src/hooks/useSystemSettings.tsx | 10 +- 4 files changed, 70 insertions(+), 70 deletions(-) diff --git a/application/src/components/settings/general/GeneralSettingsPanel.tsx b/application/src/components/settings/general/GeneralSettingsPanel.tsx index 95e6a8a..7480d0c 100644 --- a/application/src/components/settings/general/GeneralSettingsPanel.tsx +++ b/application/src/components/settings/general/GeneralSettingsPanel.tsx @@ -1,14 +1,15 @@ - import React, { useState, useEffect } from 'react'; import { Button } from "@/components/ui/button"; import { Card, CardContent, CardDescription, CardFooter, CardHeader, CardTitle } from "@/components/ui/card"; import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs"; -import { Settings, Mail } from "lucide-react"; +import { Settings, Mail, ShieldAlert } from "lucide-react"; import { Form } from "@/components/ui/form"; import { useForm } from "react-hook-form"; import { useLanguage } from "@/contexts/LanguageContext"; import { useSystemSettings } from "@/hooks/useSystemSettings"; import { GeneralSettings } from "@/services/settingsService"; +import { Alert, AlertDescription } from "@/components/ui/alert"; +import { authService } from "@/services/authService"; import SystemSettingsTab from './SystemSettingsTab'; import MailSettingsTab from './MailSettingsTab'; import { GeneralSettingsPanelProps } from './types'; @@ -18,6 +19,10 @@ const GeneralSettingsPanel: React.FC = () => { const [isEditing, setIsEditing] = useState(false); const [activeTab, setActiveTab] = useState("system"); + // Get current user to check permissions + const currentUser = authService.getCurrentUser(); + const isSuperAdmin = currentUser?.role === "superadmin"; + const { settings, isLoading, @@ -50,7 +55,7 @@ const GeneralSettingsPanel: React.FC = () => { }); useEffect(() => { - if (settings) { + if (settings && isSuperAdmin) { // Initialize form with existing settings, using system_name for appName if meta.appName is not set const appName = settings.meta?.appName || settings.system_name || ''; @@ -74,7 +79,7 @@ const GeneralSettingsPanel: React.FC = () => { } }); } - }, [settings, form]); + }, [settings, form, isSuperAdmin]); const handleSave = async (formData: GeneralSettings) => { try { @@ -134,6 +139,27 @@ const GeneralSettingsPanel: React.FC = () => { } }; + // Show permission notice for admin users + if (!isSuperAdmin) { + return ( +
+ + + {t("generalSettings", "menu")} + + + + + + Permission Notice: As an admin user, you do not have access to view or modify system and mail settings. These settings can only be accessed and modified by Super Admins. Contact your Super Admin if you need to make changes to system configuration or mail settings. + + + + +
+ ); + } + if (isLoading) { return
Loading settings...
; } @@ -147,9 +173,6 @@ const GeneralSettingsPanel: React.FC = () => { {t("generalSettings", "menu")} - - {t("monitorSSLCertificates", "ssl")} -
diff --git a/application/src/components/settings/general/SystemSettingsTab.tsx b/application/src/components/settings/general/SystemSettingsTab.tsx index 377d11e..1f16f23 100644 --- a/application/src/components/settings/general/SystemSettingsTab.tsx +++ b/application/src/components/settings/general/SystemSettingsTab.tsx @@ -48,7 +48,7 @@ const SystemSettingsTab: React.FC = ({ form, isEditing, settin diff --git a/application/src/components/settings/user-management/UserManagement.tsx b/application/src/components/settings/user-management/UserManagement.tsx index 4d7248e..5865377 100644 --- a/application/src/components/settings/user-management/UserManagement.tsx +++ b/application/src/components/settings/user-management/UserManagement.tsx @@ -5,22 +5,18 @@ import AddUserDialog from "./AddUserDialog"; import EditUserDialog from "./EditUserDialog"; import DeleteUserDialog from "./DeleteUserDialog"; import { useUserManagement } from "./hooks"; -import { - Accordion, - AccordionContent, - AccordionItem, - AccordionTrigger -} from "@/components/ui/accordion"; -import { UserCog, Loader2, AlertCircle } from "lucide-react"; +import { Accordion, AccordionContent, AccordionItem, AccordionTrigger } from "@/components/ui/accordion"; +import { UserCog, Loader2, AlertCircle, Info, Users, ShieldAlert } from "lucide-react"; import { Button } from "@/components/ui/button"; - +import { Alert, AlertDescription } from "@/components/ui/alert"; +import { authService } from "@/services/authService"; const UserManagement = () => { - const { - users, - loading, + const { + users, + loading, error, newUserForm, - isAddUserDialogOpen, + isAddUserDialogOpen, setIsAddUserDialogOpen, isDialogOpen, setIsDialogOpen, @@ -38,9 +34,11 @@ const UserManagement = () => { onAddUser, fetchUsers } = useUserManagement(); - - return ( -
+ + // Get the current logged in user to check their role + const loggedInUser = authService.getCurrentUser(); + const isSuperAdmin = loggedInUser?.role === "superadmin"; + return
@@ -52,66 +50,39 @@ const UserManagement = () => {

User Management

- + {isSuperAdmin && }
- {loading ? ( -
+ {!isSuperAdmin && + + + Permission Notice: As an admin user, you have access to view and modify existing user details. However, only Super Admins have permission to create new user accounts. Contact your Super Admin if you need to add a new user. + + } + + {loading ?

Loading users...

-
- ) : error ? ( -
+
: error ?

Failed to load users

{error}

-
- ) : ( - - )} +
: } - + - + - +
-
- ); +
; }; - export default UserManagement; + diff --git a/application/src/hooks/useSystemSettings.tsx b/application/src/hooks/useSystemSettings.tsx index 8a08a10..f7f37f4 100644 --- a/application/src/hooks/useSystemSettings.tsx +++ b/application/src/hooks/useSystemSettings.tsx @@ -2,6 +2,7 @@ import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query'; import { toast } from "@/components/ui/use-toast"; import { useLanguage } from "@/contexts/LanguageContext"; +import { authService } from "@/services/authService"; import { GeneralSettings } from "@/services/settingsService"; interface ApiResponse { @@ -14,7 +15,11 @@ export function useSystemSettings() { const queryClient = useQueryClient(); const { t } = useLanguage(); - // Fetch settings from API + // Check if user is super admin + const currentUser = authService.getCurrentUser(); + const isSuperAdmin = currentUser?.role === "superadmin"; + + // Fetch settings from API - only if user is super admin const { data: settings, isLoading, @@ -56,7 +61,8 @@ export function useSystemSettings() { }); return null; } - } + }, + enabled: isSuperAdmin, // Only run query if user is super admin }); // Update settings mutation