PLATFORM
  • Tails

    Create websites with TailwindCSS

  • Blocks

    Design blocks for your website

  • Wave

    Start building the next great SAAS

  • Pines

    Alpine & Tailwind UI Library

  • Auth

    Plug'n Play Authentication for Laravel

  • Designer comingsoon

    Create website designs with AI

  • DevBlog comingsoon

    Blog platform for developers

  • Static

    Build a simple static website

  • SaaS Adventure

    21-day program to build a SAAS

Question By
Solved

Role and Permissions

Solved
cyberekklesia

Oct 21st, 2022 08:40 PM

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.

bobbyiliev

Oct 22nd, 2022 09:00 AM

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!

cyberekklesia

Oct 22nd, 2022 07:17 PM

Thank you Bobby for your response.

  1. I successfully configured the BREAD as you explained. Capture.JPG

Each of them was configured with the corresponding Module from TeamTeaTime: Capture.JPG

Models from the picture above:

  • TeamTeaTime\Forum\Models\Category
  • TeamTeaTime\Forum\Models\Thread
  • TeamTeaTime\Forum\Models\Post
  1. Then I added a Policy to extend ForumPolicy with the following code:

https://codeshare.io/YLK3vm

  1. 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...

cyberekklesia

Oct 23rd, 2022 01:21 PM

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

bobbyiliev

Oct 24th, 2022 12:54 AM

Hey there,

Let me test that out and I'll update you soon!

bobbyiliev

Oct 24th, 2022 08:29 AM

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!

cyberekklesia

Oct 24th, 2022 11:09 AM

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

bobbyiliev

Oct 25th, 2022 05:33 AM

Best Answer

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!

Report
1
cyberekklesia

Oct 25th, 2022 07:44 AM

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?

Report
1
bobbyiliev

Oct 25th, 2022 08:16 AM

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