-
I'm struggling with getting this to work as expected when using teams. First up is the creation of custom middleware as detailed here https://spatie.be/docs/laravel-permission/v5/basic-usage/teams-permissions#working-with-teams-permissions I have created the middleware but am not sure my implementation in protected $middlewarePriority = [
\Illuminate\Cookie\Middleware\EncryptCookies::class,
StartSession::class,
ShareErrorsFromSession::class,
AuthenticatesRequests::class,
ThrottleRequests::class,
ThrottleRequestsWithRedis::class,
AuthenticateSession::class,
SubstituteBindings::class,
TeamsPermission::class, // my custom middleware
Authorize::class,
]; It only seems to work however if I also register my middleware in the protected $middlewareGroups = [
'web' => [
EncryptCookies::class,
AddQueuedCookiesToResponse::class,
StartSession::class,
// \Illuminate\Session\Middleware\AuthenticateSession::class,
ShareErrorsFromSession::class,
VerifyCsrfToken::class,
SubstituteBindings::class,
TeamsPermission::class, // my custom middleware
CheckIsSuspended::class
], Any additional guidance would be appreciated. Secondly testing..... I am running my seeds during I am assuming that perhaps my second point is related to my first and our middleware isn't correct? Paging @erikn69 as he seems to be the legend when it comes to team permissions 😉 |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 18 replies
-
Yes, it must be on
Maybe you aren't using tests correctly, but i can't know that UPDATE: laravel-permission/tests/TeamHasRolesTest.php Lines 25 to 27 in 08cc9bd |
Beta Was this translation helpful? Give feedback.
-
Perhaps I'm being dumb, which is entirely possible. But getPermissionsTeamId doesn't appear to be working for me as I would expect? I created a simple test to just test roles and permissions were working as I expected without the rest of the app following the example you provided, when I add a test to check the value of getPermissionsTeamId it comes back as null? public function test_that_the_team_id_is_set_correctly_via_the_middleware()
{
setPermissionsTeamId($this->team->id);
$this->user2->load('roles');
$this->user->assignRole('admin', 'manager');
$this->actingAs($this->user)
->withSession(['team_id' => $this->team->id])
->get(route('dashboard'));
$this->assertEquals($this->team->id, getPermissionsTeamId());
} |
Beta Was this translation helpful? Give feedback.
-
I think my main issue is how I've implemented a method to return the users roles for a given team (ie what roles have been assigned to the user for the given team). Is there a cleaner way to do this? /**
* Returns the Roles associated with the current user for the given team
*
* @param User $user
* @param int $teamId
* @return Collection|mixed|Role[]
*/
public function getTeamRoles(self $user, int $teamId)
{
$originalTeam = app(PermissionRegistrar::class)->getPermissionsTeamId();
app(PermissionRegistrar::class)->setPermissionsTeamId($teamId);
$user->load('roles');
setPermissionsTeamId($originalTeam);
return $user->roles;
} |
Beta Was this translation helpful? Give feedback.
I think my main issue is how I've implemented a method to return the users roles for a given team (ie what roles have been assigned to the user for the given team).
Is there a cleaner way to do this?