A PHP Administration Module with an ACL(Access Control List) based on Roles and Permissions, developed using the Laravel 5.4 framework and Entrust package.
In your browser, navigate to
http://mosesesan.com/demos/entrust-admin-module/
You should be presented with Laravel welcome page, click login at the top right then login in using the admin credentials below.
email: adminuser@test.com
password: adminpwd
After logging in, you will be redirected to the home page, at the top right, click the roles link to go to the roles page.
Click the “New Role” button to add a new role and assign the relevant permissions.
After adding the new role, click the “Show” button to view the role information
Create a new role and test the edit and delete operations.
Now, lets add a new user and assign a role to them. Click the users link to go to the users page.
Click the “New User” button to add a new user and assign them the Senior Consultant role. Make sure you remember the password you set for the new user.
After successfully creating the new user, test the “Show” and “Edit” operations.
Create a new user and test delete operation.
For the final test, log out and log back in using the credentials of our newly created user.
Once you log in, you should notice that the Roles and Users link is missing in the navigation bar on the top right of the page, this is because the user is not an Admin user
If you attempt to access to the roles or users page using the url:
http://mosesesan.com/demos/entrust-admin-module/admin/roles
http://mosesesan.com/demos/entrust-admin-module/admin/users
You should be presented with our 403 page
- Step 1: Create new project and install Entrust
- Step 2: Add Entrust Provider and Facades
- Step 3: Set Up Database
- Step 4: Create Models
- Step 5: Create Our Database Seeder
- Step 6: Create Our Authentication
- Step 7: Create Our Controllers and Routes
- Step 8: Create Our Views
- Step 9: Update Our Navigation Menu and Add Error Page
- Step 10: Testing
Create Laravel project
laravel new administration-module
Open composer.json and update the require object to include entrust
"require": {
"php": ">=5.6.4",
"laravel/framework": "5.4.*",
"laravel/tinker": "~1.0",
"zizaco/entrust": "5.2.x-dev"
}
Then, run
composer update
Open up config/app.php, find the providers array and add the entrust provider:
Zizaco\Entrust\EntrustServiceProvider::class,
Find the aliases array and add the entrust facades:
'Entrust' => Zizaco\Entrust\EntrustFacade::class,
Run the command below from the command line to publish the package config file.
php artisan vendor:publish
After you run this command you will see a new file in the config directory called entrust.php.
Entrust package provides it’s in-built middleware that way we can use it, open app/Http/Kernel.php and add the middleware.
protected $routeMiddleware = [
...
'role' => \Zizaco\Entrust\Middleware\EntrustRole::class,
'permission' => \Zizaco\Entrust\Middleware\EntrustPermission::class,
'ability' => \Zizaco\Entrust\Middleware\EntrustAbility::class,
];
Now that the installation is done, lets move on to creating our database tables.
For this project we will be creating the following tables:
- roles — stores role records
- permissions — stores permission records
- role_user — stores many-to-many relations between roles and users
- permission_role — stores many-to-many relations between roles and permissions
- users
- clients
- jobs
- candidates
This first 4 tables are part of the Entrust package and are created with the entrust migration file which can be generated by running the command below.
php artisan entrust:migration
This command generates the entrust migration file (_entrust_setup_tables.php) in the database/migrations directory.
The full tutorial is available on my blog.