User permissions on the portal.
This document explains the role-based access control system implemented for the Kifuliiru website's chat functionality.
The system controls access to the chat dashboard based on user roles stored in the profiles table. Only users with admin or super_admin roles can access the live chat dashboard.
profiles tablerole: 'viewer' in profiles tablerole: 'contributor' in profiles tablerole: 'admin' in profiles tablerole: 'super_admin' in profiles table/api/user/roleuserId (query parameter)The system uses the existing profiles table with a role field:
profiles {
id: string (primary key)
role: string ('viewer', 'contributor', 'admin', 'super_admin')
-- other profile fields
}
```text
### Frontend Integration
- **AuthContext**: Extended to include `roleInfo` and `checkUserRole()` function
- **Chat Component**: Conditionally shows dashboard button based on `canAccessChatDashboard(roleInfo)`
- **Utility Functions**: `roleUtils.ts` provides helper functions for role checking
## Usage Examples
### Checking User Access
```typescript
import { canAccessChatDashboard, getRoleDisplayName } from '@/lib/roleUtils';
// Check if user can access dashboard
if (canAccessChatDashboard(roleInfo)) {
// Show dashboard button
}
// Get user-friendly role name
const roleName = getRoleDisplayName(roleInfo.role);
```text
### Updating User Role
To grant admin access to a user, update their profile in the database:
```sql
UPDATE profiles
SET role = 'admin'
WHERE id = 'user-uuid-here';
```text
## Security Considerations
1. **API Protection**: The role checking API should be protected with proper authentication
2. **Database Security**: Ensure proper RLS (Row Level Security) policies are in place
3. **Role Validation**: Always validate roles on the server side, not just client side
4. **Audit Logging**: Consider logging role changes and dashboard access
## Testing
To test the role-based access control:
1. **New User**: Create a user account and verify dashboard is hidden
2. **Viewer Role**: Set user role to 'viewer' and verify dashboard is hidden
3. **Admin Role**: Set user role to 'admin' and verify dashboard is visible
4. **Super Admin Role**: Set user role to 'super_admin' and verify dashboard is visible
## Future Enhancements
- Add role management UI for administrators
- Implement role-based permissions for other features
- Add audit logging for role changes
- Create role hierarchy system with inheritance