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

[8.x] Guard against invalid guard in make:policy #34792

Merged
merged 4 commits into from
Oct 12, 2020
Merged

[8.x] Guard against invalid guard in make:policy #34792

merged 4 commits into from
Oct 12, 2020

Conversation

t73biz
Copy link
Contributor

@t73biz t73biz commented Oct 11, 2020

Problem:

When using artisan to make a policy, and an invalid guard is provided to the command, as is the case in this example,
php artisan make:policy --model=Project --guard=foo ProjectPolicy the generated code for the policy is invalid.

The $dummyUser used in the replaceModel method is set to null, which then fails to generate correctly.

Sample code generated with bad guard.

<?php

namespace App\Policies;

use App\Models\Project;
use App\Models\User;
use Illuminate\Auth\Access\HandlesAuthorization;

/**
 * Class ProjectPolicy
 * @package App\Policies
 */
class ProjectPolicy
{
    use HandlesAuthorization;

    /**
     * Determine whether the user can view any Projects.
     *
     * @param  User  $
     * @return mixed
     */
    public function viewAny(User $)
    {
        //
    }

    /**
     * Determine whether the user can view the Project.
     *
     * @param  User  $
     * @param  Project  $project
     * @return mixed
     */
    public function view(User $, Project $project)
    {
        //
    }

    /**
     * Determine whether the user can create Projects.
     *
     * @param  User  $
     * @return mixed
     */
    public function create(User $)
    {
        //
    }

    /**
     * Determine whether the user can update the Project.
     *
     * @param  User  $
     * @param  Project  $project
     * @return mixed
     */
    public function update(User $, Project $project)
    {
        //
    }

    /**
     * Determine whether the user can delete the Project.
     *
     * @param  User  $
     * @param  Project  $project
     * @return mixed
     */
    public function delete(User $, Project $project)
    {
        //
    }

    /**
     * Determine whether the user can restore the Project.
     *
     * @param  User  $
     * @param  Project  $project
     * @return mixed
     */
    public function restore(User $, Project $project)
    {
        //
    }

    /**
     * Determine whether the user can permanently delete the Project.
     *
     * @param  User  $
     * @param  Project  $project
     * @return mixed
     */
    public function forceDelete(User $, Project $project)
    {
        //
    }
}

Solution:

The solution proposed here is to check for the existence of the guard, and throw an exception if it is null.

@GrahamCampbell GrahamCampbell changed the title Policy make command generates bad code with invalid guard provided. [8.x] Guard against invalid guard in make:policy Oct 12, 2020
@taylorotwell taylorotwell merged commit 8854db1 into laravel:8.x Oct 12, 2020
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.

2 participants