DEV Community

Cover image for Generate automatic permission names from routes with spatie Laravel-permissions
Muhammad Halim Dirgantara
Muhammad Halim Dirgantara

Posted on

Generate automatic permission names from routes with spatie Laravel-permissions

disclaimer: This article is taken from my article which has been published in the medium without being modified

In Laravel, Laravel-Spatie is a package that is widely used as a tool for managing users, roles and permissions in the database.

Spatie Laravel-Permission Introiduction

We just need to create roles and permissions to synchronize with the user, so we can set what roles can perform a function or what permissions are allowed to perform a function.

In this article we will not discuss how to install laravel or how to install the laravel-spatie package which you can directly check on the respective documentation pages, but we will directly try to create a function in the seeder that is able to create permissions automatically without having to create them one by one manually.

Before we use laravel-spatie please follow the prerequisites in laravel-spatie for important considerations regarding your User models!

Spatie Laravel-Permission Prerequisites

You can follow the installation documentation from this link

Spatie Laravel-Permission Installation

After we install laravel-spatie then we can directly create a function to generate permissions from the existing route in our application.

Create a seeder using the name PermissionSeeder by using this command:

php artisan make:seeder PermissionSeeder
Enter fullscreen mode Exit fullscreen mode

Previously if we were going to create a seeder for the permission name like this:

//user permission View | Create | Edit | Delete => USER
Permission::create([‘name’ => ‘view user’]);
Permission::create([‘name’ => ‘create user’]);
Permission::create([‘name’ => ‘edit user’]);
Permission::create([‘name’ => ‘update user’]);
Permission::create([‘name’ => ‘delete user’]);
//user permisson View | Create | Edit | Delete => ROLE
Permission::create([‘name’ => ‘view role’]);
Permission::create([‘name’ => ‘create role’]);
Permission::create([‘name’ => ‘edit role’]);
Permission::create([‘name’ => ‘update role’]);
Permission::create([‘name’ => ‘delete role’]);
//user permisson View | Create | Edit | Delete => PERMISSION
Permission::create([‘name’ => ‘view permission’]);
Permission::create([‘name’ => ‘create permission’]);
Permission::create([‘name’ => ‘edit permission’]);
Permission::create([‘name’ => ‘update permission’]);
Permission::create([‘name’ => ‘delete permission’]);
Enter fullscreen mode Exit fullscreen mode

Then we will replace it with a function like this,
First, we retrieve data from the route in our application with this command :

$routeCollection = Route::getRoutes()->get();
Enter fullscreen mode Exit fullscreen mode

data retrieved from route

We save the array that we got from that array into routeCollection variable, and then we will iterate over each data in the array for us to process with the command

foreach ($routeCollection as $item) {
   //.... processing collection item
}
Enter fullscreen mode Exit fullscreen mode

every item has this data

we will retrieve the value of the array action of the item using the command:

foreach ($routeCollection as $item) {
$name = $item->action;
}
Enter fullscreen mode Exit fullscreen mode

array of action in every item

Next we will check every action array that we get whether it has an “as” variable, because not every action array has it.

if(!empty($name[‘as’])) {
.....//processing item
}
Enter fullscreen mode Exit fullscreen mode

if the action array has a value of “as” then we will process it using the following string function, but first we save the value of the variable name with the value “as” into the variable permission

$permission = $name[‘as’];
Enter fullscreen mode Exit fullscreen mode

we change the case of the permission variable value to lowercase

$permission = trim(strtolower($permission));
Enter fullscreen mode Exit fullscreen mode

then we use the preg_replace function to convert each period, dash into a space.

$permission = preg_replace(‘/[\s.,-]+/’, ‘ ‘, $permission);
Enter fullscreen mode Exit fullscreen mode

then the value obtained is a word with lowercase letters and separated by spaces.
We will save the value of the variable into the permissions table in the database using the create . function

Permission::create([
    ‘name’ => $permission
]);
Enter fullscreen mode Exit fullscreen mode

Here is the name of the permission that we get by using the steps above

Image description

The overall code is as follows :

$routeCollection = Route::getRoutes()->get();
foreach ($routeCollection as $item) {
$name = $item->action;
  if(!empty($name[‘as’])) {
    $permission = $name[‘as’];
    $permission = trim(strtolower($permission));
    $permission = preg_replace(‘/[\s.,-]+/’, ‘ ‘, $permission);
    Permission::create([
        ‘name’ => $permission
     ]);
  }
}
Enter fullscreen mode Exit fullscreen mode

Once we have the permission names we generated from the route, we can assign each permission to the role we have.

In the next article, we will discuss how to do so that on each existing route we can check permissions based on the route on each function in the controller.

You can follow the development of the functions in this article via this github link

Github Laravel 9 Fortify Stisla using Spatie Laravel-Permissiona

Top comments (0)