# 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 1. **roles** - Defines available roles in the system 2. **permissions** - Defines granular permissions organized by modules 3. **role_permissions** - Links roles to their permissions 4. **user_roles** - Assigns roles to users (with optional branch scoping) 5. **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 branch - `job-cards.view-all` - View job cards across all branches - `job-cards.view-own` - View only assigned job cards - `job-cards.create` - Create new job cards - `job-cards.update` - Update job cards in own branch - `job-cards.update-all` - Update job cards across all branches - `job-cards.update-own` - Update only assigned job cards - `job-cards.delete` - Delete job cards - `job-cards.approve` - Approve job cards for processing - `job-cards.assign-technician` - Assign technicians to job cards ### Customers - `customers.view` - View customer information - `customers.create` - Create new customers - `customers.update` - Update customer information - `customers.delete` - Delete customers ### Vehicles - `vehicles.view` - View vehicle information - `vehicles.create` - Register new vehicles - `vehicles.update` - Update vehicle information - `vehicles.delete` - Delete vehicles ### Inventory - `inventory.view` - View inventory items - `inventory.create` - Add new inventory items - `inventory.update` - Update inventory items - `inventory.delete` - Delete inventory items - `inventory.stock-movements` - Manage stock movements - `inventory.purchase-orders` - Manage purchase orders ### Service Orders - `service-orders.view` - View service orders - `service-orders.create` - Create new service orders - `service-orders.update` - Update service orders - `service-orders.delete` - Delete service orders - `service-orders.approve` - Approve service orders ### Appointments - `appointments.view` - View appointments - `appointments.create` - Schedule new appointments - `appointments.update` - Update appointments - `appointments.delete` - Cancel appointments ### Technicians - `technicians.view` - View technician information - `technicians.create` - Add new technicians - `technicians.update` - Update technician information - `technicians.delete` - Remove technicians - `technicians.assign-work` - Assign work to technicians - `technicians.view-performance` - View technician performance reports ### Reports - `reports.view` - View all reports - `reports.financial` - View financial and revenue reports - `reports.operational` - View operational and performance reports - `reports.export` - Export reports to various formats ### User Management - `users.view` - View user accounts - `users.create` - Create new user accounts - `users.update` - Update user information - `users.delete` - Delete user accounts - `users.manage-roles` - Assign and remove user roles ### System Administration - `system.settings` - Configure system settings - `system.maintenance` - Perform system maintenance tasks ## Usage Examples ### In Controllers/Livewire Components ```php // 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 ```php // 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 ```blade {{-- Using Blade directives --}} @hasPermission('job-cards.create') Create Job Card @endhasPermission @hasRole('admin|manager')