Role Management

What is Role Management/Permission?
With the help of tables, we will understand the basic concept of role and permission. What they are actually:
Role Table:

S/N

Role

Permission

1

Admin

Create,
Edit, Delete, Users

2

Stakeholder

Create,
Edit

Permission Table:

S/N

Permission

Description

1

Create

Grants
authorization to create any type of record except users

2

Edit

Grants
authorization to edit any record except users.

3

Delete

Grants
authorization to delete any record except users.

4

Users

Grants
permission to display, create, edit, assign user roles, and delete users.

Role: Basically role represents a group of tasks that a user that is assigned the role is allowed to perform. For example: a user role that allows user to edit their own listing page. And the system admin can be regarded as owner of the system and as such, is permitted to perform all the tasks in the system. Admin has power to create users, delete, assigning role to user and edit products etc…
Permission: The ability to do actions on your site like viewing, create, delete and edit etc is governed by permissions. Permission grants authorization to a role to perform a specific task. A user must be granted a permission in order to do the corresponding action on the site.
In this article, with simple demo of role based, we have implemented a role based authentication in Laravel along with its native authentication system. So, just follow the steps and see how the role permission works in Laravel.
Note: Source code for this is available on GitHub: https://github.com/digitalcrm/Role-Management

1. Install Laravel Project:

composer create-project –prefer-dist laravel/laravel RoleManagement “5.8.*”

2. Database Configuration:

2.1. Create database: Open phpMyAdmin and create database, like this:

2.2. Configure with Laravel: After database has been created, next open .env file and add database credentials, like this:

3. Create Models and Migrations

3.1. Create a Role model and migration to running this command: php artisan make:model Role –m

3.2. Create a role_user migration:
php artisan make:migration create_role_user_table

3.3. Modify Role migration table: Open database/migrations/create_roles_table.php

3.4. Modify create_role_user_table migration table:
Open database/migrations/create_role_user_table.php.

3.5. Running the migration: After the migration table has been setup you can create the roles and role_user tables by running the migrations: php artisan migrate.

will create the following tables in your database.

4. Make Authentication Scaffolding

Run the artisan command: php artisan make:auth

5. Database Entries:

5.1. After running the migration command, now we need to fill the data manually for testing the role permission, So, fill the data in roles and role_users table:
5.1.1. Open and fill the data manually in roles table:

5.1.2. Open and fill the data manually in role_user table:

5.1.3. Also register user in users table using authentication, go to /register url and register the users:

6. Model Setup

6.1. Open app/Role.php model and define Many to Many relationship, like this:

6.2. Open default created app/User.php model file and define Many to Many relationship, like this:

6.3. Also, open User.php and include these methods which will be used to check if user has a particular role or roles:

7. Create Controller:

Now we need two controllers for admin and superadmin, so let’s create:
7.1. Run the artisan command: php artisan make:controller AdminController

Open AdminController and add code like this:

7.2. Run the artisan command: php artisan make:controller SuperAdminController

Open SuperAdminController and add code like this:

8. Create View:

Next, we have to create views files for admin and super admin.
8.1. Create view for admin: resource/views/admin/home.blade.php

8.2. Create view for superadmin: resource/views/superadmin/home.blade.php

9. Define Routes:

Open routes/web.php and create routes for admin and superadmin:

10. Create Middleware:

Next, we need a middleware for our role. So, run the artisan command for creating middleware:
php artisan make:middleware CheckRole

Open app/Http/CheckRole.php file and add code:

11. Register The Middleware:

Open app/Http/kernel.php file and include role middleware under $routeMiddleware array, like this:

12. Results:

Only authorized users can have privileged to access the certain part of your applications:
Admin Login:

After admin login if super admin wants access:

If admin access this:


Note: For the source code, you can visit the GitHub directory: https://github.com/digitalcrm/Role-Management
GitHub link:
Laravel-Media-Library: https://github.com/digitalcrm/LaravelMedia
Role Management: https://github.com/digitalcrm/Role-Management
Newsletter Package: https://github.com/digitalcrm/pkg-newsletter
Form-Package: https://github.com/digitalcrm/Form-Contact-Package

Leave a comment

Your email address will not be published. Required fields are marked *