A comprehensive Laravel application implementing Role-Based Access Control (RBAC) using the Spatie Laravel Permission package. This system features secure authentication, granular permission management, and a modern UI with server-side DataTables, Select2 dropdowns, and SweetAlert2 notifications.
- Prerequisites
- Installation
- Database Setup
- Running the Application
- Default Credentials
- Architecture Overview
- Features
- Testing
- Project Structure
Before you begin, ensure your development environment meets the following requirements:
- PHP:
>= 8.2 - Composer: Latest version
- Laravel Framework:
^12.0 - Database: MySQL
>= 8.0or PostgreSQL>= 13.0or SQLite>= 3.35 - Node.js:
>= 18.x - NPM:
>= 9.x(or Yarn>= 1.22) - Web Server: Apache or Nginx (or use
php artisan servefor local development)
Follow these steps to set up the project on your local machine:
git clone <repository-url>
cd rbaccomposer installnpm installCopy the example environment file and configure it:
cp .env.example .envEdit the .env file and configure your database connection:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=rbac
DB_USERNAME=root
DB_PASSWORD=php artisan key:generateExecute the migrations to create all necessary database tables:
php artisan migrateThis will create the following tables:
users- User accountsroles- System roles (Admin, Manager, User)permissions- Granular permissionsmodel_has_roles- User-role assignmentsmodel_has_permissions- User-permission assignmentsrole_has_permissions- Role-permission assignmentssections- Section entitiesclasses- Class entities (with section relationships)
Populate the database with default roles, permissions, and test users:
php artisan db:seedWhat gets seeded:
- Roles: Admin, Manager, User
- Permissions: Complete set of permissions for users, profiles, sections, and classes
- Users:
- Admin user:
admin@example.com/password - Manager user:
manager@example.com/password - Standard user:
user@example.com/password - 10 additional random users with randomly assigned roles
- Admin user:
To reset the database and seed it in one command:
php artisan migrate:fresh --seedphp artisan serveThe application will be available at: http://127.0.0.1:8000
In a separate terminal, run the Vite development server for hot module replacement:
npm run devThen access the application at: http://127.0.0.1:8000
To compile and minify assets for production:
npm run buildFor production environments:
- Set
APP_ENV=productionandAPP_DEBUG=falsein.env - Run
composer install --optimize-autoloader --no-dev - Run
npm run build - Run
php artisan config:cache - Run
php artisan route:cache - Run
php artisan view:cache - Configure your web server (Apache/Nginx) to point to the
publicdirectory
Use these credentials to log in and test different role permissions:
| Role | Password | Access Level | |
|---|---|---|---|
| Admin | admin@example.com |
password |
Full system access (all modules, all actions) |
| Manager | manager@example.com |
password |
User, Section, and Class management (no role assignment) |
| User | user@example.com |
password |
Profile view/edit only |
This application uses the Spatie Laravel Permission package to implement a robust, scalable RBAC system.
- Roles: Collections of permissions (e.g., Admin, Manager, User)
- Permissions: Granular actions (e.g.,
users.view,sections.create) - Policies: Laravel authorization gates for model-level checks
- Middleware: Route-level protection (
RoleMiddleware)
Permissions follow a resource.action naming convention:
users.view
users.create
users.update
users.delete
users.assignRoles
sections.view
sections.create
sections.update
sections.delete
classes.view
classes.create
classes.update
classes.delete
- Admin: All permissions (full system access)
- Manager: User, section, and class CRUD; cannot assign roles or manage permissions
- User: Profile view/update only
The RoleMiddleware enforces role-based route restrictions:
- Admin: Full access to all routes
- Manager: Access to user, section, and class management; blocked from role/permission routes
- User: Profile routes only
Authorization policies provide fine-grained control:
UserPolicy: Controls user management actionsProfilePolicy: Controls profile access (users can only edit their own)SectionPolicy: Controls section managementClassPolicy: Controls class management
- Admin: Full CRUD operations, role assignment
- Manager: View, create, update, delete users (no role assignment)
- Server-side DataTables with search, sort, pagination
- Role-based action button visibility
- SweetAlert2 delete confirmations
- Admin/Manager: Full CRUD operations
- Server-side DataTables integration
- Unique section names with validation
- Linked to Classes module
- Admin/Manager: Full CRUD operations
- Select2 AJAX dropdown for section selection
- Server-side search and pagination
- Dynamic section loading
- Server-side DataTables with section filtering
- Foreign key relationship to sections
- All authenticated users can view/edit their own profile
- Password update functionality
- Account deletion with confirmation
- DataTables: Server-side processing via Yajra package
- Select2: AJAX-powered dropdowns with search
- SweetAlert2: Beautiful confirmation dialogs and notifications
- Responsive Design: Tailwind CSS with mobile-first approach
- RBAC Blade Directives:
@can,@role,@cannotfor conditional rendering
Execute the test suite:
php artisan testOr with detailed output:
php artisan test --parallelThe application includes:
- Unit Tests: Role assignment, permission checks, service logic
- Feature Tests:
- RBAC middleware restrictions
- Policy authorization (e.g., Manager cannot assign roles)
- CRUD workflows (create/update/delete success and validation failures)
- Profile self-edit enforcement
Tests use an in-memory SQLite database by default. Configure phpunit.xml if you need a different setup.
rbac/
├── app/
│ ├── Http/
│ │ ├── Controllers/ # Resource controllers (User, Section, Class)
│ │ ├── Middleware/ # RoleMiddleware for route protection
│ │ └── Requests/ # Form request validation
│ ├── Models/ # Eloquent models (User, Section, ClassModel)
│ ├── Policies/ # Authorization policies
│ └── Services/ # Business logic layer
├── database/
│ ├── migrations/ # Database schema migrations
│ ├── seeders/ # Database seeders (roles, permissions, users)
│ └── factories/ # Model factories for testing
├── resources/
│ ├── js/
│ │ ├── users/ # User module JS (DataTable, Select2, alerts)
│ │ ├── sections/ # Section module JS
│ │ └── classes/ # Class module JS (with Select2 AJAX)
│ ├── views/
│ │ ├── users/ # User CRUD views + partials
│ │ ├── sections/ # Section CRUD views + partials
│ │ └── classes/ # Class CRUD views + partials
│ └── css/
│ └── app.css # Custom styles + DataTables CSS
├── routes/
│ └── web.php # Application routes
└── tests/
├── Feature/ # Feature tests (RBAC, CRUD)
└── Unit/ # Unit tests (services, policies)
- Backend: Laravel 12, PHP 8.2+
- RBAC: Spatie Laravel Permission
- Frontend: Tailwind CSS, Alpine.js
- DataTables: Yajra Laravel DataTables (server-side)
- Select2: AJAX-powered dropdowns
- Alerts: SweetAlert2
- Build Tool: Vite
- Testing: PHPUnit, Pest (optional)
php artisan optimize:clearcomposer require --dev barryvdh/laravel-ide-helper
php artisan ide-helper:generate
php artisan ide-helper:models --nowrite./vendor/bin/pintThis project is open-sourced software licensed under the MIT license.
For issues, questions, or contributions, please contact the development team or open an issue in the project repository.
Note: This is a technical assignment project demonstrating RBAC implementation best practices in Laravel. All passwords are set to password for demonstration purposes only.