9.8 KiB
9.8 KiB
Role-Based User Permission System Documentation
Overview
This car repairs shop application now includes a comprehensive role-based permission system that provides fine-grained access control across all modules. The system supports:
- Hierarchical Roles: Users can have multiple roles with different permissions
- Direct Permissions: Users can have permissions assigned directly, bypassing roles
- Branch-Based Access: Permissions can be scoped to specific branches
- Module-Based Organization: Permissions are organized by functional modules
Database Structure
Core Tables
- roles - Defines available roles in the system
- permissions - Defines granular permissions organized by modules
- role_permissions - Links roles to their permissions
- user_roles - Assigns roles to users (with optional branch scoping)
- user_permissions - Assigns direct permissions to users
Available Roles
| Role | Display Name | Description |
|---|---|---|
admin |
Administrator | Full system access across all branches |
manager |
Branch Manager | Full access within assigned branch |
service_supervisor |
Service Supervisor | Supervises service operations and technicians |
service_coordinator |
Service Coordinator | Coordinates service workflow and scheduling |
service_advisor |
Service Advisor | Interfaces with customers and manages service requests |
parts_manager |
Parts Manager | Manages inventory and parts ordering |
technician |
Technician | Performs vehicle service and repairs |
quality_inspector |
Quality Inspector | Performs quality inspections and audits |
customer_service |
Customer Service | Handles customer inquiries and support |
Permission Modules
Job Cards
job-cards.view- View job cards in own branchjob-cards.view-all- View job cards across all branchesjob-cards.view-own- View only assigned job cardsjob-cards.create- Create new job cardsjob-cards.update- Update job cards in own branchjob-cards.update-all- Update job cards across all branchesjob-cards.update-own- Update only assigned job cardsjob-cards.delete- Delete job cardsjob-cards.approve- Approve job cards for processingjob-cards.assign-technician- Assign technicians to job cards
Customers
customers.view- View customer informationcustomers.create- Create new customerscustomers.update- Update customer informationcustomers.delete- Delete customers
Vehicles
vehicles.view- View vehicle informationvehicles.create- Register new vehiclesvehicles.update- Update vehicle informationvehicles.delete- Delete vehicles
Inventory
inventory.view- View inventory itemsinventory.create- Add new inventory itemsinventory.update- Update inventory itemsinventory.delete- Delete inventory itemsinventory.stock-movements- Manage stock movementsinventory.purchase-orders- Manage purchase orders
Service Orders
service-orders.view- View service ordersservice-orders.create- Create new service ordersservice-orders.update- Update service ordersservice-orders.delete- Delete service ordersservice-orders.approve- Approve service orders
Appointments
appointments.view- View appointmentsappointments.create- Schedule new appointmentsappointments.update- Update appointmentsappointments.delete- Cancel appointments
Technicians
technicians.view- View technician informationtechnicians.create- Add new technicianstechnicians.update- Update technician informationtechnicians.delete- Remove technicianstechnicians.assign-work- Assign work to technicianstechnicians.view-performance- View technician performance reports
Reports
reports.view- View all reportsreports.financial- View financial and revenue reportsreports.operational- View operational and performance reportsreports.export- Export reports to various formats
User Management
users.view- View user accountsusers.create- Create new user accountsusers.update- Update user informationusers.delete- Delete user accountsusers.manage-roles- Assign and remove user roles
System Administration
system.settings- Configure system settingssystem.maintenance- Perform system maintenance tasks
Usage Examples
In Controllers/Livewire Components
// Check permission in component mount method
public function mount()
{
$this->authorize('create', JobCard::class);
}
// Check permission in methods
public function save()
{
if (!auth()->user()->hasPermission('job-cards.create')) {
abort(403, 'You do not have permission to create job cards.');
}
// ... rest of the method
}
// Filter data based on permissions
public function loadData()
{
$user = auth()->user();
if ($user->hasPermission('job-cards.view-all')) {
$this->jobCards = JobCard::all();
} elseif ($user->hasPermission('job-cards.view')) {
$this->jobCards = JobCard::where('branch_code', $user->branch_code)->get();
} else {
$this->jobCards = JobCard::where('service_advisor_id', $user->id)->get();
}
}
In Routes
// Protect routes with permission middleware
Route::get('/job-cards', JobCardIndex::class)
->middleware('permission:job-cards.view');
Route::get('/job-cards/create', JobCardCreate::class)
->middleware('permission:job-cards.create');
// Protect with role middleware
Route::prefix('admin')->middleware('role:admin,manager')->group(function () {
Route::get('/settings', AdminSettings::class);
});
In Blade Templates
{{-- Using Blade directives --}}
@hasPermission('job-cards.create')
<a href="{{ route('job-cards.create') }}" class="btn btn-primary">
Create Job Card
</a>
@endhasPermission
@hasRole('admin|manager')
<div class="admin-panel">
{{-- Admin content --}}
</div>
@endhasRole
@hasAnyPermission('reports.view|reports.financial')
<a href="{{ route('reports.index') }}">View Reports</a>
@endhasAnyPermission
{{-- Using permission component --}}
<x-permission-check permission="job-cards.update">
<button wire:click="updateJobCard">Update</button>
</x-permission-check>
<x-permission-check role="service_supervisor">
<div class="supervisor-tools">
{{-- Supervisor-only tools --}}
</div>
</x-permission-check>
In Policies
class JobCardPolicy
{
public function view(User $user, JobCard $jobCard): bool
{
// Admin can view all
if ($user->hasPermission('job-cards.view-all')) {
return true;
}
// Branch-level access
if ($user->hasPermission('job-cards.view') &&
$jobCard->branch_code === $user->branch_code) {
return true;
}
// Own records only
if ($user->hasPermission('job-cards.view-own') &&
$jobCard->service_advisor_id === $user->id) {
return true;
}
return false;
}
}
Artisan Commands
Assign Role to User
# Assign role without branch restriction
php artisan user:assign-role user@example.com admin
# Assign role with branch restriction
php artisan user:assign-role user@example.com service_advisor --branch=ACC
Seed Roles and Permissions
php artisan db:seed --class=RolesAndPermissionsSeeder
User Management Interface
The system includes a web interface for managing user roles and permissions:
- URL:
/user-management - Permission Required:
users.view - Features:
- Search and filter users
- Assign/remove roles
- Grant/revoke direct permissions
- Set branch-specific access
- Activate/deactivate users
Permission Hierarchy
- Super Admin:
adminrole bypasses all permission checks - Direct Permissions: Override role-based permissions
- Role Permissions: Standard role-based access
- Branch Scoping: Permissions can be limited to specific branches
Security Features
- Branch Isolation: Users can only access data within their assigned branch (unless granted cross-branch permissions)
- Temporal Permissions: Roles and permissions can have expiration dates
- Audit Trail: All role and permission changes are timestamped
- Middleware Protection: Routes are protected at the middleware level
- Policy-Based Authorization: Model operations use Laravel policies for fine-grained control
Best Practices
- Use Roles for Common Patterns: Assign permissions to roles rather than directly to users
- Branch Scoping: Always consider branch-level access when designing features
- Least Privilege: Grant only the minimum permissions required for a user's job function
- Regular Audits: Periodically review user permissions and remove unnecessary access
- Policy Classes: Use Laravel policies for complex authorization logic
- Middleware First: Apply middleware protection to routes before implementing view-level checks
Troubleshooting
Common Issues
- Permission Denied Errors: Check that the user has the required permission and that their role is active
- Branch Access Issues: Verify that the user's branch_code matches the resource's branch_code
- Middleware Conflicts: Ensure middleware is applied in the correct order
- Cache Issues: Clear application cache after changing permissions:
php artisan cache:clear
Debug Commands
# Check user's roles and permissions
php artisan tinker
>>> $user = User::find(1);
>>> $user->getAllPermissions();
>>> $user->roles;
This permission system provides a robust foundation for controlling access to all features in your car repairs shop application while maintaining flexibility for different organizational structures and workflows.