Replies: 1 comment
-
So creating an AdminModel turned out to be more complex than I thought since you have to make quite a bit of changes to how laravel handles the guards. Not sure if this is due to me using Jetstream or something else. I'll investigate a bit more later when I have time. For now I found a simpler solution. Simply give the user model a guard column.
Unfortunately this will make it harder to allow the same users to view admin panel and the rest of the app protected by the web guard. I suppose we can add some logic to the guardName() method but I can see that getting quite messy. Will need to think about that a bit more. Being able to pass the guard to the setRole() method would make this all much simpler IMO. |
Beta Was this translation helpful? Give feedback.
-
Right now if you have multiple guards sharing the same model you can't pass a guard parameter to the
assignRole()
method. This is odd since all the look up methods and create methods for roles accept a guard name as a parameter. You just can't seem to specify it when assigning a role. Is there a reason why not? This has been discussed a few times before. Here is a issue from 2017 where someone did a PR to add the ability to add a guard name as a parameter toassignRole()
but it looks like it was rejected: #565 .Why would I want this?
In my case I have an app where there is an admin panel only certain people should have access to. So we setup a guard called admin for that. The rest of the app is regular users with the default web guard for that. We want users that can access the admin guard to have access to other parts of app guarded with the web guard. So that's why we use same provider. We also want to be able to have different permissions/roles for the admin vs web guard.
I realize that the linked issue has some suggestions for how to deal with this. In my case I'll probably just extend my user model with something like AdminUser and just query that when in the admin panel. The AdminUser will have one method on it:
Haven't tried it but I'm guessing it will work.
But it would be nice if we didn't have to do that and I'm struggling to understand why a guard parameter couldn't be added to assignRole(). Seems like it would make things much easier for situations like this. And doesn't seem too tricky to do at first glance.
Is there a good reason for not having this parameter that I'm not understanding?
Beta Was this translation helpful? Give feedback.
All reactions