asifmushtaq38's avatar

How to dynamically handle role permissions

I have watched this video. https://laracasts.com/series/whats-new-in-laravel-5-1/episodes/16 (disappointed that he didn't added the route level permissions using middleware) I understand all the aspect, but I'm confused that how I can handle the permission dynamically? Let suppose, I have created the permission 'edit-manager' so this permission act as editing the manager. So how I can handle it dynamically? Should I edit the source code again and add this permission check as $user->can('edit-manager')? or how I can handle it? I have to edit the code each time when I create the new permission?

0 likes
11 replies
Robstar's avatar

I use https://github.com/spatie/laravel-permission in all projects.

I have routes mapped to all basic route permissions, so for a basic entity page I'd have the following:

  • users.index
  • users.destroy
  • users.update (covers edit and update routes)
  • users.create (covers create and store routes)
  • // any other ADHOC permissions

I have a frontend UI for users to create roles and assign permissions to each role so the system have fully granular permissions.

All permissions are stored in a config file that is refreshed during deployment, that ultimately creates permissions in my database if needed.

1 like
asifmushtaq38's avatar

@ROBSTAR - Hi can you please explain this part?

All permissions are stored in a config file that is refreshed during deployment, that ultimately creates permissions in my database if needed.

How it could be dynamically? Do you mean permissions will be prepared before deployment?

Robstar's avatar

I wrote a tiny artisan command to refresh the permissions from my config file. This ran during deployment.

1 like
asifmushtaq38's avatar

@ROBSTAR - I'm sorry for that, but I'm not understanding you, can you please explain? where first you wrote the permissions? direct in database? then fetch permissions and save in the config file? or reverse process?

jekinney's avatar

Similar to what @robstar stated. If you really need dynamic you have to figure out your workflow (assuming that is your question too) meaning you need to tie into some events and listen to update things as needed.

Example: You have a basic cms where you can create pages and menus. When you create a page and menu link you also trigger a permission(s) to be created and assigned to admin user or role at least for maybe editing that page. And assign roles or users or even guest (anyone) permissions to view. So you are not hard coding the permission persay but on request seeing if a relationship exists to allow access.

polymorphic relationships help a lot in your permission table along with a type 'create', 'edit', 'read', 'delete' etc.

1 like
Robstar's avatar

@ASIFMUSHTAQ38 - Personally, I store all permissions in a config file, I use this as my single source of truth to populate the database.

I've seen people create separate crud screens to create permissions. I've never seen the need for that.

If you use https://github.com/spatie/laravel-permission all the work is done for you. IN your application you need to use a few helper functions and middleware that has been created for you.

asifmushtaq38's avatar

@ROBSTAR - Can you share your "permissions in config file" demo project? I know it is frustrating you, but I'm new to laravel so that's why I want a working demo to understanding it.

Robstar's avatar

Sure, this is my config/permissions.php file.

<?php

/**
 * A grouped array of all relevant user permissions within then system
 * For update check against .edit and .update routes
 * For create check against .create and .store routes
 */

return [

    'Nesting Analysis' => [
        'route' => 'nesting',
        'description' => 'Access and manage nesting analysis functions',
        'icon' => 'cogs',
        'rules' => [
            'Create an analysis request' => 'nesting.create',
            // additional rules ...
        ],
    ],

    // additional entities ...
];

The actual permissions are contained within the rules key. The other keys are used to populate a Vue component I have on my roles screen that allows administrators to assign individual permissions to roles. THis allows permissions to be fully granular.

1 like
Robstar's avatar

@ASIFMUSHTAQ38 - There's no online tutorial I know of, I wrote it a while ago myself when I discovered the laravel-permissions package.

  • I have a single file (above) containing all my permissions (the route, icon and description keys are for my UI to manage permissions, with I built in Vue)
  • When I add / remove permissions I run a Laravel artisna command (system:sync-permissions)
  • This command loops through my array of permissions. If the permission does not exist in the database it creates it
  • During my deployment process, the console command is ran to sync. permissions from the config with my live database

Please or to participate in this conversation.