mirror of
https://github.com/ichsankurnia/hats-app.git
synced 2026-12-06 22:26:27 +00:00
Improve RBAC management and permissions UI
Unify interactive and read-only role permission rows, refine role and menu forms and labels, and correct the access-permissions sidebar route. Update role payloads and admin-menu handling.
This commit is contained in:
@@ -75,11 +75,12 @@ export const RoleForm: FC<Props> = ({ isUpdate, roleId, initialData }) => {
|
||||
}
|
||||
});
|
||||
} else if (Array.isArray(initialData.privilege) && initialData.privilege.length > 0) {
|
||||
// Support legacy privilege format
|
||||
initialData.privilege.forEach((item: any) => {
|
||||
const mId = item.menu_id || item.id;
|
||||
if (mId && Array.isArray(item.action)) {
|
||||
initialSelection[mId] = item.action.map((a: any) => (typeof a === 'string' ? a : a.name || a.code || String(a)));
|
||||
initialSelection[mId] = item.action.map((a: any) =>
|
||||
typeof a === 'string' ? a : a.name || a.code || String(a)
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -105,7 +106,8 @@ export const RoleForm: FC<Props> = ({ isUpdate, roleId, initialData }) => {
|
||||
setSelectedPermissions((prev) => {
|
||||
const next = { ...prev };
|
||||
menus.forEach((m) => {
|
||||
if (m.is_admin_menu && m.id !== undefined && next[m.id]) {
|
||||
const isAdmin = Boolean(m.is_admin_menu);
|
||||
if (isAdmin && m.id !== undefined && next[m.id]) {
|
||||
delete next[m.id];
|
||||
}
|
||||
});
|
||||
@@ -182,7 +184,8 @@ export const RoleForm: FC<Props> = ({ isUpdate, roleId, initialData }) => {
|
||||
if (!menu || !actions || actions.length === 0) return;
|
||||
|
||||
// Rule: If allow_admin_panel is false, exclude Admin menus
|
||||
if (menu.is_admin_menu && !allowAdminPanel) {
|
||||
const isAdmin = Boolean(menu.is_admin_menu);
|
||||
if (isAdmin && !allowAdminPanel) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -197,19 +200,18 @@ export const RoleForm: FC<Props> = ({ isUpdate, roleId, initialData }) => {
|
||||
menu_id: menu.id!,
|
||||
menu_name: menu.name,
|
||||
menu_path: menu.path_url || '#',
|
||||
is_admin_menu: Boolean(menu.is_admin_menu),
|
||||
is_admin_menu: isAdmin,
|
||||
action: safeActions,
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
const payload: Record<string, any> = {
|
||||
code: values.name?.trim().replace(/\s/g, '_').toUpperCase(),
|
||||
name: values.name?.trim(),
|
||||
description: values.description?.trim() || '',
|
||||
allow_admin_panel: Boolean(allowAdminPanel),
|
||||
allow_admin: Boolean(allowAdminPanel), // For backward compatibility with legacy endpoints
|
||||
role_detail: roleDetail,
|
||||
privilege: roleDetail, // For backward compatibility with legacy endpoints
|
||||
};
|
||||
|
||||
if (!isUpdate) {
|
||||
@@ -223,7 +225,7 @@ export const RoleForm: FC<Props> = ({ isUpdate, roleId, initialData }) => {
|
||||
return;
|
||||
}
|
||||
|
||||
toast.success('Role successfully created.');
|
||||
toast.success('Role created successfully.');
|
||||
router.push('/admin/rbac/user-role');
|
||||
router.refresh();
|
||||
} else {
|
||||
@@ -237,7 +239,7 @@ export const RoleForm: FC<Props> = ({ isUpdate, roleId, initialData }) => {
|
||||
return;
|
||||
}
|
||||
|
||||
toast.success('Role successfully updated.');
|
||||
toast.success('Role updated successfully.');
|
||||
router.push('/admin/rbac/user-role');
|
||||
router.refresh();
|
||||
}
|
||||
@@ -262,11 +264,11 @@ export const RoleForm: FC<Props> = ({ isUpdate, roleId, initialData }) => {
|
||||
</Link>
|
||||
<div>
|
||||
<CardTitle className="text-xl">
|
||||
{isUpdate ? 'Edit User Role' : 'Create New User Role'}
|
||||
{isUpdate ? 'Edit Role' : 'Create Role'}
|
||||
</CardTitle>
|
||||
<CardDescription className="mt-1">
|
||||
{isUpdate
|
||||
? `Configure role permissions for role ID ${roleId}`
|
||||
? `Update role permissions and configuration for role ID ${roleId}`
|
||||
: 'Define role details and assign hierarchical menu access'}
|
||||
</CardDescription>
|
||||
</div>
|
||||
@@ -275,7 +277,7 @@ export const RoleForm: FC<Props> = ({ isUpdate, roleId, initialData }) => {
|
||||
<div className="flex items-center gap-2 self-end sm:self-auto">
|
||||
<Link href="/admin/rbac/user-role">
|
||||
<Button variant="outline" size="sm" disabled={isLoading}>
|
||||
Batal
|
||||
Cancel
|
||||
</Button>
|
||||
</Link>
|
||||
<Button
|
||||
@@ -285,57 +287,62 @@ export const RoleForm: FC<Props> = ({ isUpdate, roleId, initialData }) => {
|
||||
disabled={isLoading || menuLoading}
|
||||
>
|
||||
{isLoading && <Icons.spinner className="mr-1.5 h-4 w-4 animate-spin" />}
|
||||
Simpan Perubahan
|
||||
Save Changes
|
||||
</Button>
|
||||
</div>
|
||||
</CardHeader>
|
||||
</Card>
|
||||
|
||||
{/* Form Body */}
|
||||
<Form form={form} layout="vertical" scrollToFirstError>
|
||||
<Form form={form} layout="vertical" scrollToFirstError size='small'>
|
||||
{/* Section A: General Information */}
|
||||
<Card className="mb-6">
|
||||
<CardHeader className="pb-4">
|
||||
<CardTitle className="text-base font-semibold">
|
||||
Informasi Umum (General Information)
|
||||
General Information
|
||||
</CardTitle>
|
||||
<CardDescription>
|
||||
Masukkan nama peran, deskripsi, dan hak akses admin panel
|
||||
Enter role name, description, and admin panel privileges
|
||||
</CardDescription>
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
<div className="grid grid-cols-1 md:grid-cols-2 gap-4">
|
||||
<div className="grid grid-cols-1 md:grid-cols-2 gap-4 gap-y-0">
|
||||
<Form.Item
|
||||
name="name"
|
||||
label="Role Name"
|
||||
rules={[{ required: true, message: 'Role name is required!' }]}
|
||||
>
|
||||
<Input className="input font-medium" placeholder="e.g. Team Leader or SYSADMIN" />
|
||||
<Input className="input font-medium" placeholder="e.g. Team Leader or Administrator" />
|
||||
</Form.Item>
|
||||
|
||||
<div className="p-3.5 rounded-lg border border-zinc-200 dark:border-zinc-800 bg-zinc-50/60 dark:bg-zinc-900/40 flex items-center justify-between">
|
||||
<div>
|
||||
<div className="p-3.5 rounded-lg -mt-5!">
|
||||
<div className='mb-2'>
|
||||
<span className="text-sm font-semibold text-zinc-900 dark:text-zinc-100 block">
|
||||
Allow Admin Panel
|
||||
</span>
|
||||
<span className="text-xs text-muted-foreground block mt-0.5">
|
||||
Mengizinkan role ini mengakses menu administrasi dan admin panel
|
||||
Grants this role access to administration and admin panel menus
|
||||
</span>
|
||||
</div>
|
||||
<Form.Item name="allow_admin_panel" valuePropName="checked" className="mb-0">
|
||||
<Form.Item name="allow_admin_panel" valuePropName="checked">
|
||||
<Switch
|
||||
size='medium'
|
||||
checked={allowAdminPanel}
|
||||
onChange={handleAdminToggle}
|
||||
style={{
|
||||
backgroundColor: allowAdminPanel ? '#EA512C' : undefined,
|
||||
}}
|
||||
className="[&.ant-switch-checked]:bg-his! [&.ant-switch-checked:hover]:bg-his/90!"
|
||||
/>
|
||||
</Form.Item>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<Form.Item name="description" label="Description" className="mt-2 mb-0">
|
||||
<Input.TextArea
|
||||
<Input.TextArea size='large'
|
||||
className="input"
|
||||
rows={2}
|
||||
placeholder="Deskripsi tugas dan tanggung jawab role ini..."
|
||||
placeholder="Describe role responsibilities and permission scope..."
|
||||
/>
|
||||
</Form.Item>
|
||||
</CardContent>
|
||||
@@ -347,14 +354,14 @@ export const RoleForm: FC<Props> = ({ isUpdate, roleId, initialData }) => {
|
||||
<div className="flex flex-col sm:flex-row sm:items-center justify-between gap-2">
|
||||
<div>
|
||||
<CardTitle className="text-base font-semibold">
|
||||
Detil Hak Akses & Access Permissions
|
||||
Menu Permissions & Access Rights
|
||||
</CardTitle>
|
||||
<CardDescription className="mt-1">
|
||||
Atur izin akses untuk setiap menu yang tersedia dalam aplikasi
|
||||
Configure access permissions for each menu available in the application
|
||||
</CardDescription>
|
||||
</div>
|
||||
<div className="text-xs text-muted-foreground bg-zinc-100 dark:bg-zinc-800 px-2.5 py-1 rounded self-start sm:self-auto font-mono">
|
||||
{Object.keys(selectedPermissions).length} menu dikonfigurasi
|
||||
{Object.keys(selectedPermissions).length} menus configured
|
||||
</div>
|
||||
</div>
|
||||
</CardHeader>
|
||||
@@ -362,7 +369,7 @@ export const RoleForm: FC<Props> = ({ isUpdate, roleId, initialData }) => {
|
||||
{menuLoading ? (
|
||||
<div className="py-12 flex flex-col items-center justify-center text-muted-foreground">
|
||||
<Icons.spinner className="h-6 w-6 animate-spin mb-3 text-his" />
|
||||
<span className="text-sm">Memuat struktur menu dari server...</span>
|
||||
<span className="text-sm">Loading menu structure from server...</span>
|
||||
</div>
|
||||
) : (
|
||||
<MenuPermissionTree
|
||||
@@ -380,8 +387,8 @@ export const RoleForm: FC<Props> = ({ isUpdate, roleId, initialData }) => {
|
||||
{/* Bottom Action Bar */}
|
||||
<div className="mt-6 flex justify-end items-center gap-3">
|
||||
<Link href="/admin/rbac/user-role">
|
||||
<Button variant="outline" type="button" disabled={isLoading}>
|
||||
Batal
|
||||
<Button variant="outline" type="button" disabled={isLoading} className='text-zinc-800! dark:text-white'>
|
||||
Cancel
|
||||
</Button>
|
||||
</Link>
|
||||
<Button
|
||||
@@ -391,7 +398,7 @@ export const RoleForm: FC<Props> = ({ isUpdate, roleId, initialData }) => {
|
||||
disabled={isLoading || menuLoading}
|
||||
>
|
||||
{isLoading && <Icons.spinner className="mr-1.5 h-4 w-4 animate-spin" />}
|
||||
Simpan Perubahan
|
||||
Save Changes
|
||||
</Button>
|
||||
</div>
|
||||
</Form>
|
||||
@@ -402,14 +409,14 @@ export const RoleForm: FC<Props> = ({ isUpdate, roleId, initialData }) => {
|
||||
<AlertDialog open={true}>
|
||||
<AlertDialogContent>
|
||||
<AlertDialogHeader>
|
||||
<AlertDialogTitle>Konfirmasi Simpan Role?</AlertDialogTitle>
|
||||
<AlertDialogTitle>Save Role Configuration?</AlertDialogTitle>
|
||||
<AlertDialogDescription>
|
||||
Konfigurasi hak akses role ini akan disimpan dan langsung berlaku bagi pengguna yang memiliki role ini.
|
||||
These permission settings will be saved and immediately applied to users assigned to this role.
|
||||
</AlertDialogDescription>
|
||||
</AlertDialogHeader>
|
||||
<AlertDialogFooter>
|
||||
<AlertDialogCancel disabled={isLoading} onClick={() => setShowConfirm(false)}>
|
||||
Batal
|
||||
Cancel
|
||||
</AlertDialogCancel>
|
||||
<AlertDialogAction
|
||||
disabled={isLoading}
|
||||
@@ -420,7 +427,7 @@ export const RoleForm: FC<Props> = ({ isUpdate, roleId, initialData }) => {
|
||||
}}
|
||||
>
|
||||
{isLoading && <Icons.spinner className="mr-1.5 h-4 w-4 animate-spin" />}
|
||||
Lanjutkan
|
||||
Save Changes
|
||||
</AlertDialogAction>
|
||||
</AlertDialogFooter>
|
||||
</AlertDialogContent>
|
||||
@@ -431,4 +438,3 @@ export const RoleForm: FC<Props> = ({ isUpdate, roleId, initialData }) => {
|
||||
};
|
||||
|
||||
export default RoleForm;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user