Role-Based Access Control
security Role-Based Access Control (RBAC)
MediDash implements a comprehensive role-based access control (RBAC) system to ensure that users can only access the features and data that are appropriate for their role. The system is built on four core components that work together to provide secure and scalable authentication and authorization.
src/app/core
directory, organized into logical subdirectories for easy maintenance.
label 1. Role Definition (role.ts)
The Role enum is the foundation of the
RBAC system. It defines all available user roles in the application, with
each
role assigned a unique string identifier.
File Location
Key Benefits
- Centralized role management for consistency.
- Type-safe role references to prevent errors.
- Easy to extend with new roles as your application grows.
- Consistent naming conventions for all roles.
Implementation
export enum Role {
Admin = 'ADMIN',
Doctor = 'DOCTOR',
Patient = 'PATIENT',
Nurse = 'NURSE' // Example of an additional role
}
person_outline 2. User Model (user.ts)
The User class defines the data
structure for user information throughout the application, including
authentication details and role assignments.
File Location
Key Properties
id- A unique identifier for each user.username- The user's login credential.role- The user's assigned role, based on theRoleenum.token- The JWT authentication token for the user.
Implementation
import { Role } from './role';
export class User {
id!: number;
username!: string;
password!: string;
firstName!: string;
lastName!: string;
token!: string;
role!: Role;
img!: string;
}
login 3. Authentication Service (auth.service.ts)
The AuthService is the heart of the
authentication system. It handles user login, session management, and
role-based permissions using the NGX Permissions library.
File Location
Core Responsibilities
- Authenticates users and loads their permissions.
- Clears user sessions and permissions on logout.
- Provides access to the current user's data.
- Persists user sessions to local storage.
Key Features
- JWT token management for secure authentication.
- Dynamic role-based permission loading.
- Reactive user state management with RxJS.
- Built-in error handling and validation.
shield 4. Authentication Guard (auth.guard.ts)
The AuthGuard protects routes based on user authentication status and role permissions, ensuring secure navigation.
File Location
Protection Mechanisms
- Route access validation
- Role-based restrictions
- Unauthorized access prevention
- Automatic redirection
- Session state checking
Implementation Example
In your route configuration, protect routes like this:
{
path: 'admin',
canActivate: [AuthGuard],
data: {
role: Role.Admin,
},
loadChildren: () =>
import('./admin/admin.routes').then((m) => m.ADMIN_ROUTE),
},
Multiple roles example:
data: { role: [Role.Admin, Role.Doctor] }
extension Extending Role Functionality
To add new roles or modify existing role behavior, follow these best practices:
Adding New Roles
- Add the new role to the
Roleenum inrole.ts - Update route protections as needed
- Create corresponding UI components and navigation
- Configure permissions in NGX Permissions if required
- Update user data to include the new role
Best Practices
- Use descriptive role names
- Maintain role hierarchy consistency
- Document role permissions clearly
- Test role access thoroughly
- Implement role-based UI elements