Skip to content
On this page

Role Permission

Dashcode is provided with role-permission support with Spatie's Laravel-permission package. Here we will describe how can you use role and permission to support your new features or modules.

TIP

You can find Laravel-permission official documentation here Laravel-permission.

Step-1: Creating a permission

First, log in as super admin and go to the permission page and create permission from the permission page. In order to create permission you need to provide the "Module Name" and the "Permission Name". For example, you are adding a post module so the Module Name will be post, and the Permission Name can be index or create or edit.

Step-2: Creating a role

Once you have created all the permissions you can create a role now. Go to the roles page and create a role with all necessary permissions.

Step-3: Assigning role to user

After creating a role you can assign the role to the user. Create a new user and select the role that you assign to this user.

Step-4: Access control via Controller

You need to check permission for each route on the associated controller.

For resource controller you can use the following code snippet.

php
/**
 * Handle permission of this resource controller.
 */
public function __construct()
{
    $this->authorizeResource(User::class, 'user');
}

For single route and method you can use the following code snippet.

php
/**
 * Handle permission of single method.
 */
if (auth()->user()->cannot('database_backup viewAny')) {
    abort(403);
}

Step-5: Filtering blade view

If you want to display or hide anything on the blade template depending on the permission then you can use the following code snippet.

blade
@can('menu role_permission_roles')
    <x-active-link :active-route="'roles.*'" :to="'roles.index'" :text="__('Roles')"/>
@endcan

COPYRIGHT © 2022 Codeshaper, All rights reserved.