Home > Blockchain >  How To Create Permission and Assign Middleware To Route Dynamically Using Spatie Permission Package
How To Create Permission and Assign Middleware To Route Dynamically Using Spatie Permission Package

Time:08-30

I have some Voucher Types:

  • Payment
  • Receipt
  • Contra -Journal
  • Etc.

User can Create multiple Voucher under Voucher Type.

Payment

  • Payment 1
  • Payment 2
  • Payment 3
  • Etc.

Receipt

  • Receipt 1
  • Receipt 2
  • Receipt 3
  • Etc.

Contra

  • Contra 1
  • Contra 2
  • Contra 3
  • Etc.

Journal

  • Journal 1
  • Journal 2
  • Journal 3
  • Etc.

In Laravel, I am using Spatie Permission package, Using this package i can create multiple permission dynamically, and also assign them to role but how can I validate every voucher permission to route using middleware?

CodePudding user response:

Route::middleware([ 'auth' ])->group(function () {
    Route::get('/', function () {
        if (Auth::check()) {
            if (Auth::user()->hasRole('SuperAdmin')) {
                return redirect()->route('superadmin.dashboard');
            } elseif (Auth::user()->hasRole('Admin')) {
                return redirect()->route('admin.dashboard');
            } elseif (Auth::user()->hasRole('Vendor')) {
                return redirect()->route('vendor.dashboard');
            }
        } else {
            return redirect('/login');
        }
    });
});

CodePudding user response:

A role can be assigned to any user: $user->assignRole('writer');
A role can be removed from a user: $user->removeRole('writer');

A permission can be given to a role: $role->givePermissionTo('edit articles');
You can determine if a role has a certain permission: $role->hasPermissionTo('edit articles');
A permission can be revoked from a role: $role->revokePermissionTo('edit articles');

Assigning Direct Permissions To A User: $user->assignRole('writer');
$user->givePermissionTo('delete articles');

  • Related