Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[1.x] Make team show use policies #379

Merged

Conversation

richardbishopme
Copy link
Contributor

@richardbishopme richardbishopme commented Oct 22, 2020

Problem

I have a need to customise whether a team should be shown. At the moment, the TeamController show method checks if the user belongsToTeam and aborts if not:

Http\Inertia\TeamController
Http\LIvewire\TeamController

public function show(Request $request, $teamId)
{
        $team = Jetstream::newTeamModel()->findOrFail($teamId);

        if (! $request->user()->belongsToTeam($team)) {
            abort(403);
        }

        return view('teams.show', [
            'user' => $request->user(),
            'team' => $team,
        ]);
}

If I want to change this, I would need to override the TeamController show method rather than use the original.

Solution

I inspected the code and noted that the stub and test fixture for the TeamPolicy already checks for belongsToTeam:

public function view(User $user, Team $team)
{
        return $user->belongsToTeam($team);
}

With that in mind, I changed the Livewire and Inertia controllers to the following code so that it uses the policy, thus allowing the developer to customise the access policy. The tests still pass.

From

if (! $request->user()->belongsToTeam($team)) {
      abort(403);
}

To

if(Gate::denies('view', $team )) {
       abort(403);
}

@taylorotwell taylorotwell merged commit eb35206 into laravel:1.x Oct 22, 2020
@richardbishopme richardbishopme deleted the feature/Make-Team-Show-Use-Policies branch October 22, 2020 12:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants