173 lines
4.6 KiB
TypeScript
173 lines
4.6 KiB
TypeScript
import { useState, useEffect } from 'react';
|
|
import { Role, EntityType, PERMISSIONS, UserRole } from '@/types/roles';
|
|
import { useToast } from '@/hooks/use-toast';
|
|
|
|
const MOCK_ROLES: Role[] = [
|
|
{
|
|
id: 'admin-super',
|
|
name: 'Super Admin',
|
|
entityType: 'admin',
|
|
description: 'Full system access',
|
|
permissions: PERMISSIONS.admin.map(p => p.id),
|
|
userCount: 2,
|
|
isSystem: true,
|
|
},
|
|
{
|
|
id: 'admin-moderator',
|
|
name: 'Moderator',
|
|
entityType: 'admin',
|
|
description: 'Content and user management',
|
|
permissions: ['admin.users.read', 'admin.content.write'],
|
|
userCount: 5,
|
|
},
|
|
{
|
|
id: 'hotel-manager',
|
|
name: 'Hotel Manager',
|
|
entityType: 'hotel',
|
|
description: 'Full hotel management access',
|
|
permissions: PERMISSIONS.hotel.map(p => p.id),
|
|
userCount: 8,
|
|
isSystem: true,
|
|
},
|
|
{
|
|
id: 'hotel-receptionist',
|
|
name: 'Receptionist',
|
|
entityType: 'hotel',
|
|
description: 'Handle bookings and guests',
|
|
permissions: ['hotel.bookings.read', 'hotel.bookings.write', 'hotel.guests.read'],
|
|
userCount: 15,
|
|
},
|
|
{
|
|
id: 'restaurant-manager',
|
|
name: 'Restaurant Manager',
|
|
entityType: 'restaurant',
|
|
description: 'Full restaurant management',
|
|
permissions: PERMISSIONS.restaurant.map(p => p.id),
|
|
userCount: 6,
|
|
isSystem: true,
|
|
},
|
|
{
|
|
id: 'restaurant-waiter',
|
|
name: 'Waiter',
|
|
entityType: 'restaurant',
|
|
description: 'Take and manage orders',
|
|
permissions: ['restaurant.orders.read', 'restaurant.orders.write', 'restaurant.reservations.write'],
|
|
userCount: 20,
|
|
},
|
|
{
|
|
id: 'commerce-admin',
|
|
name: 'Commerce Admin',
|
|
entityType: 'commerce',
|
|
description: 'Full commerce access',
|
|
permissions: PERMISSIONS.commerce.map(p => p.id),
|
|
userCount: 4,
|
|
isSystem: true,
|
|
},
|
|
{
|
|
id: 'commerce-operator',
|
|
name: 'Store Operator',
|
|
entityType: 'commerce',
|
|
description: 'Manage products and orders',
|
|
permissions: ['commerce.products.read', 'commerce.orders.read', 'commerce.orders.write'],
|
|
userCount: 12,
|
|
},
|
|
];
|
|
|
|
export const useRolesPermissions = () => {
|
|
const [roles, setRoles] = useState<Role[]>(MOCK_ROLES);
|
|
const [userRoles, setUserRoles] = useState<UserRole[]>([]);
|
|
const [loading, setLoading] = useState(false);
|
|
const { toast } = useToast();
|
|
|
|
const getRolesByEntity = (entityType: EntityType): Role[] => {
|
|
return roles.filter(role => role.entityType === entityType);
|
|
};
|
|
|
|
const createRole = (role: Omit<Role, 'id' | 'userCount'>): void => {
|
|
const newRole: Role = {
|
|
...role,
|
|
id: `${role.entityType}-${Date.now()}`,
|
|
userCount: 0,
|
|
};
|
|
setRoles([...roles, newRole]);
|
|
toast({
|
|
title: 'Role Created',
|
|
description: `${role.name} has been created successfully.`,
|
|
});
|
|
};
|
|
|
|
const updateRole = (roleId: string, updates: Partial<Role>): void => {
|
|
setRoles(roles.map(role =>
|
|
role.id === roleId ? { ...role, ...updates } : role
|
|
));
|
|
toast({
|
|
title: 'Role Updated',
|
|
description: 'Role has been updated successfully.',
|
|
});
|
|
};
|
|
|
|
const deleteRole = (roleId: string): void => {
|
|
const role = roles.find(r => r.id === roleId);
|
|
if (role?.isSystem) {
|
|
toast({
|
|
title: 'Cannot Delete',
|
|
description: 'System roles cannot be deleted.',
|
|
variant: 'destructive',
|
|
});
|
|
return;
|
|
}
|
|
setRoles(roles.filter(role => role.id !== roleId));
|
|
toast({
|
|
title: 'Role Deleted',
|
|
description: 'Role has been deleted successfully.',
|
|
});
|
|
};
|
|
|
|
const assignUserRole = (userId: string, roleId: string, entityId?: string): void => {
|
|
const newUserRole: UserRole = {
|
|
userId,
|
|
roleId,
|
|
entityId,
|
|
assignedAt: new Date().toISOString(),
|
|
assignedBy: 'current-user',
|
|
};
|
|
setUserRoles([...userRoles, newUserRole]);
|
|
|
|
const role = roles.find(r => r.id === roleId);
|
|
if (role) {
|
|
updateRole(roleId, { userCount: role.userCount + 1 });
|
|
}
|
|
|
|
toast({
|
|
title: 'Role Assigned',
|
|
description: 'User role has been assigned successfully.',
|
|
});
|
|
};
|
|
|
|
const removeUserRole = (userId: string, roleId: string): void => {
|
|
setUserRoles(userRoles.filter(ur => !(ur.userId === userId && ur.roleId === roleId)));
|
|
|
|
const role = roles.find(r => r.id === roleId);
|
|
if (role && role.userCount > 0) {
|
|
updateRole(roleId, { userCount: role.userCount - 1 });
|
|
}
|
|
|
|
toast({
|
|
title: 'Role Removed',
|
|
description: 'User role has been removed successfully.',
|
|
});
|
|
};
|
|
|
|
return {
|
|
roles,
|
|
userRoles,
|
|
loading,
|
|
getRolesByEntity,
|
|
createRole,
|
|
updateRole,
|
|
deleteRole,
|
|
assignUserRole,
|
|
removeUserRole,
|
|
};
|
|
};
|