MosesEsan / mesan-laravel-entrust-user-roles-permission

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.

Home Page:https://medium.com/@mosesesan/tutorial-6-how-to-build-a-laravel-5-4-4ba44f26b853

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Laravel 5.4 Administration Module with Role-based permissions using Entrust package

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.

Testing

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.

Roles Management

Click the “New Role” button to add a new role and assign the relevant permissions.

New Role

After adding the new role, click the “Show” button to view the role information

Role Info

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.

New User Users Management

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

Logged In



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

Permission Error

Tutorial

Step 1: Create new project and install Entrust

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 

Step 2: Add Entrust Provider and Facades

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.

Step 3: Set Up Database

For this project we will be creating the following tables:

  1. roles — stores role records
  2. permissions — stores permission records
  3. role_user — stores many-to-many relations between roles and users
  4. permission_role — stores many-to-many relations between roles and permissions
  5. users
  6. clients
  7. jobs
  8. 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.

About

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.

https://medium.com/@mosesesan/tutorial-6-how-to-build-a-laravel-5-4-4ba44f26b853


Languages

Language:PHP 64.9%Language:HTML 33.9%Language:JavaScript 0.8%Language:Vue 0.4%