Role and Permissions
Hello Bobby
Going further with laravel-forum, I'm extending forum policies following this example: https://github.com/Team-Tea-Time/laravel-forum/issues/323
At this point, I need to know how I can get the user permission. Here is an example:
public function createCategories($user): bool
{
//return $user->can('add','categories');
//return $user->can('add_categories','categories');
return $user->can('add_categories');
}
All of them return false.
How do I know the user can add categories based on Admin Role/Permission configuration?
P.S: The following reading hasn't help much or at least I don't understand - https://voyager-docs.devdojo.com/core-concepts/roles-and-permissions
Thank you in advance.
Hi there,
As the Forum package uses a separate category model (TeamTeaTime\Forum\Models\Category
), you need to do the following:
- Create a new BREAD for the forum_categories table:
- Make sure to set the model to:
TeamTeaTime\Forum\Models\Category
- Then if you go to your Roles you will be able to define the permissions for that Model:
Then you should be able to pass the forum category to that can()
helper function.
Let me know how it goes!
Thank you Bobby for your response.
- I successfully configured the BREAD as you explained.
Each of them was configured with the corresponding Module from TeamTeaTime:
Models from the picture above:
- TeamTeaTime\Forum\Models\Category
- TeamTeaTime\Forum\Models\Thread
- TeamTeaTime\Forum\Models\Post
- Then I added a Policy to extend ForumPolicy with the following code:
https://codeshare.io/YLK3vm
- Result: I still can't get it to work. Note, if I write return true or return false, the blade works as expected.
I'm guessing that I'm not reaching the configured role and permission which in fact I just confirmed that was properly recorded.
Can you help me with the code? I believe that is related to return $user->can('add'); and that I'm missing a use. Well, I'm just guessing...
Following Riari example, the extended class gives me an error saying "Voyager not found". https://flareapp.io/share/yPaONXp5#F70
So, as mentioned before, when I use true or false the blade works as expected. Somehow, I'm still missing something to be able to get the user role/permission from Voyager.
Any suggestions? Thank you
Hey there,
Let me test that out and I'll update you soon!
Hey,
I've not fully tested this yet, but looking into the error that you've shared, I think that you should only add the following:
use TCG\Voyager\Facades\Voyager;
Add it somewhere in the beginning of the file after the opening php tag.
Let me know how it goes!
Thank you Bobby for your response.
Well, I have tried that too... Here is the code: https://flareapp.io/share/RmrOdMOP#F70
Note: Without (new Voyager) it gives me an error saying Voyager not found.
Anyways, if in anything helps, what would be the right way (or your recommended way), if I just wanted to create a new class to access roles and permissions? I think that is my challenge right now (lack of knowledge).
Anyways, some sample code would be great.
Thank you
Hey there,
Sorry for the delay, I had to set that up and test it to see what might be going on.
I ended up being able to get it working with the following:
return $user->hasPermission('add_forum_categories');
So rather than using the Voyager facade or the can()
helper function, you can just do that with the $user->hasPermission
method!
Let me know if you have any questions!
















That worked! As always happens, it was such a simple solution... Thank you Bobby!
This might be a good post that I could write: Wave with laravel-forum. What do you think?
















That sounds great! I'll be looking forward to reading it!