Skip to main content

Shield Plugin for Roles and Permissions

Premium
5:22

Text Version of the Lesson

Until now, we checked if the user can do something by checking if users.is_admin or users.is_accountant is true.

This can be done better by using roles and permissions. And Filament has an excellent plugin for that called Shield which under the hood uses spatie/laravel-permission package.

This lesson will use the Shield package to add roles and permissions functionality.

roles


Composer Install Package

First, let's install the package via composer.

composer require bezhansalleh/filament-shield "^3.0@beta"

Then we need to add Spatie\Permission\Traits\HasRoles trait to the User Model.

app/Models/User.php:

use Spatie\Permission\Traits\HasRoles;
 
class User extends Authenticatable implements FilamentUser
{
use HasApiTokens, HasFactory, Notifiable;
use HasRoles;
 
// ...
}

Next, we need to register this package in the panel.

app/Providers/Filament/AdminPanelProvider.php:

class AdminPanelProvider extends PanelProvider
{
public function panel(Panel $panel): Panel
{
return $panel
// ...
->plugins([
\BezhanSalleh\FilamentShield\FilamentShieldPlugin::make()
]);
}
}

If you have multiple panels, you may choose to add it only for admin users. This way, only users with access to...

The Full Lesson is Only for Premium Members

Want to access all of our courses? (36 h 00 min)

You also get:

61 courses
Premium tutorials
Access to repositories
Private Discord
Get Premium for $129/year or $29/month

Already a member? Login here

Gareth avatar

thanks Povilas, great course and really shows the massive power filament contains.

👍 2
j.kludt avatar

Thx good work!

j.kludt avatar

What to do if I would like to clone the github project?
command: git clone https://github.com/LaravelDaily/Filament3-Course-Main.git
command: composer install
command: npm install
configure .env with database
command: php artisan migrate:fresh --seed
command: php artisan serve

Is this enough or is something missing?

Povilas Korop avatar

Should be enough, not sure about npm run dev or npm run build maybe.

Chris Peeters avatar

Great course on a fantastic package Povilas! Thank you!

Aditya Prasetyo avatar

How to if the scenario was one user one role ? can these package restricted default behaviour ?

Povilas Korop avatar

Not sure, could you ask this question to the package author on Github/Discord?

Diogo Pinto avatar

Why not remove the "multiple" from the select option? When editing or adding new users you only have the hability to select one role.

Ali Al Qahtani avatar

Good work👍

Emile Jobity avatar

Liked this tutorial. easy to follow. Easy to copy and paste the code than when watching video. Video is good too once there is a text format like this to support it in which to easily see code differences and copy the code. Honestly i was able to breeze through it quickly than video. Text is also easier to update than video and can support many more languages than video.

👍 1
boris021 avatar

Getting this error when i try to install filament shield:

composer require bezhansalleh/filament-shield "^3.0@beta" ./composer.json has been updated Running composer update bezhansalleh/filament-shield Loading composer repositories with package information Updating dependencies Your requirements could not be resolved to an installable set of packages.

Problem 1 - Root composer.json requires bezhansalleh/filament-shield 3.0@beta, found bezhansalleh/filament-shield[dev-main, dev-feature/performance-boost, v1.0.0, ..., 1.x-dev, v2.0.0, ..., 2.x-dev, 3.0.0-beta1, 3.0.0-beta2, 3.x-dev] but it does not match the constraint.

Installation failed, reverting ./composer.json and ./composer.lock to their original content.

Povilas Korop avatar

Not sure, maybe something is wrong with other versions of your other packages? Could you open the issue on shield's GitHub? For me it worked, at the time of writing this course.

Steve Popoola avatar

I noticed that even when you complete the Filament3 lessons, the progress bar never reaches 100%? Perhaps a bug thay needs to be fixed?

Povilas Korop avatar

The bar is changing only if you click on the button below the lesson to go to the next lesson.

Maybe in some cases you just clicked the lesson on the sidebar on the right, then the system doesn't really know if you "finished" the current lesson or not.

Steve Popoola avatar

Hi, I actually clicked from lesson to lesson from beginning to the end. I did actually think that what you mentioned was the case but I went back and clicked from lesson to lesson till the end and the percentage did not change. That was why I reported it.

Povilas Korop avatar

Weird, need to reproduce it (somehow) and debug it. Thanks for reporting! P.S. There's also "Mark as completed" button on top but I guess no one clicks it :)

Mihail Borovik avatar

95 %, then click "Mark as completed" and ... 100%

Ruslan Kovalchuk avatar

I use the button on top. Very comfortably.

delebash avatar

Is there a way to have a guest landing page of my products page. This would be where un-authenticated users can view the products list/detail but not edit or add anything? Also including all of the sort and search features.

Povilas Korop avatar

You can use Filament Tables on a public Livewire page, yes. I'm adding to the list to write a tutorial about it, meanwhile here's an example how we used Filament FORMS on a Public page.

👍 1
Michael avatar

Hi Povilas, how to attach default role on user sign up?

Povilas Korop avatar

We've just published a tutorial about it, just replace the 'role' string field with whatever logic you want.

Elberson Games avatar

Thank you very much for the course Povilas, I learned a lot directly from Brazil

👍 3
Joe avatar

This type of text-based tutorial is definitely better than video in my opinion. Not only is it easier to follow through at my own pace (and skip over things I know) but it will also be much easier to refer back to specifc parts of the tutorial in the coming days/weeks.

Yimy David avatar

In case I already use the php artisan shield:install and then add new modules, what command should I use to regenerate the permissions and roles

Ruslan Kovalchuk avatar

Thank you

kaleemullah avatar

Absolutely , No regret to purchase membership for livetime. very helpfull course.

chethiya avatar

filement shield Unknown column 'roles' in 'field list' error came when try add role from user adding page. Any soutions

Modestas avatar

You most likely missed ->relationship('roles') in your code. Hard to say without the code that you have

steff700 avatar

Great course. How do i handle user-specific data records. I have data records of different users in one table and of course i only want to allow access to the user's own rows. Do i save the user_id in the data records and filter accordingly? Do you have a tip or tutorial for that case?

Modestas avatar

Yes, this is so called "multi-tenancy" or a scoped query. You can take a look at our tutorial (keep in mind, it is a bit outdated!) to get an idea:

https://laraveldaily.com/post/simple-laravel-multi-tenancy-with-single-trait

steff700 avatar

Hi, i am trying to install filament breezy, https://laraveldaily.com/post/laravel-filament-12-best-plugins-with-demos I know, this tut is for filament v2. In v3 is no config/filament.php etc. I cant transfer it myself. Also the original docu of filament-breezy, i find hard for beginners. My question; "Using this package is very easy." also with v3? Now i am on step, Update the auth guard (Laravel 11, Filament 3.2.92)

Modestas avatar

Don't always follow the tutorial to the letter! In some cases, it's worth more to look into the package readme itself as it will have updated instructions. In this case, https://github.com/jeffgreco13/filament-breezy - has the required updated instructions for a v3 of Filament :)

steff700 avatar

I have done that and I will do it again. It doesn't seem to be as "very easy" with version 3 as described here with version 2 with 7 lines of code: https://laraveldaily.com/post/laravel-filament-12-best-plugins-with-demos :)

Modestas avatar

Things change :) But to be honest with you, a lot of stuff became more complicated (but also more powerful) with v3 ;)

Loganathan Natarajan avatar
Loganathan Natarajan

All in all, thanks for your detailed explanation

David O'Connell avatar

should the canAccessPanel method in the User class return false after the if statements?

Modestas avatar

You are right, we missed it! Updated :)

chris tophe avatar

Hi, I'm implementing Laravel Shield with multiple authentication panels, each using different guards and user models. I took this course primarily for the last chapter covering Shield installation with multiple user types, but the implementation isn't working as expected since all users seem to use the same guard. Current Setup I have two separate panels:

Admin Panel

Panel name: 'admin' Uses "web" guard Users are in Admin model/table

Animator Panel

Panel name: 'anim' Uses "anim" guard Users are in Anim model/table

Issue Description The problem occurs right at the installation phase. After running php artisan shield:install, regardless of which panel I select, the generated "RolePolicy" file always uses the default User model: phpCopypublic function viewAny(User $user): bool { return $user->can('view_any_role'); } However, I don't have a "User" model in my application. When I proceed with php artisan shield:generate --panel, I get an error stating that the User model doesn't exist. Current Workaround I've found a temporary workaround by:

Manually publishing the config file Replacing 'fqcn' => 'App\Models\User' with 'fqcn' => 'App\Models\MYUSERMODEL'

However, this solution is incomplete because during policy generation, I can only have policies using either the "admin" model/guard OR the "anim" guard, but not both simultaneously. Question I feel I'm missing something fundamental in the setup process. Could someone explain the proper way to implement Laravel Shield with multiple panels using different guards and user models? Any guidance on the correct approach would be greatly appreciated.

🥳 1
Mark Henry avatar

You make it easy to understand. Thank you.

We'd Love Your Feedback

Tell us what you like or what we can improve

Feel free to share anything you like or dislike about this page or the platform in general.