feat: simplify permission checks by removing redundant role conditions in useCan hook
This commit is contained in:
parent
953a395f24
commit
85ba5631b3
@ -104,7 +104,7 @@ const hrItems: NavMenuItem[] = [
|
|||||||
|
|
||||||
const sistemItems: NavMenuItem[] = [
|
const sistemItems: NavMenuItem[] = [
|
||||||
{ title: 'Pengaturan', href: '/admin/settings', icon: Settings, permission: ['settings.view_system', 'settings.view_homepage', 'settings.view_social_media', 'settings.view_hr'] },
|
{ title: 'Pengaturan', href: '/admin/settings', icon: Settings, permission: ['settings.view_system', 'settings.view_homepage', 'settings.view_social_media', 'settings.view_hr'] },
|
||||||
// { title: 'Role & Permission', href: rolesIndex.url(), icon: Shield, permission: 'roles.view' },
|
{ title: 'Role & Permission', href: rolesIndex.url(), icon: Shield, permission: 'roles.view' },
|
||||||
// { title: 'Log Aktivitas', href: '#', icon: Activity, permission: 'activity_logs.view' },
|
// { title: 'Log Aktivitas', href: '#', icon: Activity, permission: 'activity_logs.view' },
|
||||||
];
|
];
|
||||||
|
|
||||||
|
|||||||
@ -18,8 +18,8 @@ type PageProps = {
|
|||||||
|
|
||||||
function extractNames(items?: RoleOrPermission[]): string[] {
|
function extractNames(items?: RoleOrPermission[]): string[] {
|
||||||
if (!items) {
|
if (!items) {
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
|
|
||||||
return items.map((item) => (typeof item === 'string' ? item : item.name));
|
return items.map((item) => (typeof item === 'string' ? item : item.name));
|
||||||
}
|
}
|
||||||
@ -33,40 +33,32 @@ export function useCan() {
|
|||||||
|
|
||||||
function can(permission: string): boolean {
|
function can(permission: string): boolean {
|
||||||
if (!user) {
|
if (!user) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (roleNames.includes('developer') || roleNames.includes('owner')) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
return permissionNames.includes(permission);
|
return permissionNames.includes(permission);
|
||||||
}
|
}
|
||||||
|
|
||||||
function canAny(...permissions: string[]): boolean {
|
function canAny(...permissions: string[]): boolean {
|
||||||
if (!user) {
|
if (!user) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (roleNames.includes('developer') || roleNames.includes('owner')) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
return permissions.some((p) => permissionNames.includes(p));
|
return permissions.some((p) => permissionNames.includes(p));
|
||||||
}
|
}
|
||||||
|
|
||||||
function hasRole(role: string): boolean {
|
function hasRole(role: string): boolean {
|
||||||
if (!user) {
|
if (!user) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
return roleNames.includes(role);
|
return roleNames.includes(role);
|
||||||
}
|
}
|
||||||
|
|
||||||
function hasAnyRole(roles: string[]): boolean {
|
function hasAnyRole(roles: string[]): boolean {
|
||||||
if (!user) {
|
if (!user) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
return roles.some((role) => roleNames.includes(role));
|
return roles.some((role) => roleNames.includes(role));
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user