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

authorizeResource does not work for models with compound names #18432

Closed
jpuck opened this issue Mar 20, 2017 · 3 comments
Closed

authorizeResource does not work for models with compound names #18432

jpuck opened this issue Mar 20, 2017 · 3 comments

Comments

@jpuck
Copy link
Contributor

jpuck commented Mar 20, 2017

  • Laravel Version: 5.4.15
  • PHP Version: PHP 7.0.15-0ubuntu0.16.04.4
  • Database Driver & Version: mysql Ver 14.14 Distrib 5.7.17, for Linux (x86_64) using EditLine wrapper

Description:

authorizeResource does not seem to work for models with more than 1 part to its name.

Steps To Reproduce:

here's a quick demo app in which you can see authorizeResource works fine for the Post model, but not for the CompoundName model.

This show method will always return unauthorized:

class CompoundNameController extends Controller
{
    public function __construct()
    {
        $this->authorizeResource(CompoundName::class);
    }

    // ...

    public function show(CompoundName $compoundName)
    {
        return $compoundName;
    }
}

However, explicit authorization works fine as expected:

class CompoundNameController extends Controller
{
    public function __construct()
    {
        //
    }

    // ...

    public function show(CompoundName $compoundName)
    {
        $this->authorize('view', $compoundName);
        return $compoundName;
    }
}
@devcircus
Copy link
Contributor

devcircus commented Mar 20, 2017

Just submitted a PR that should fix the issue. Looks like the "authorizesResource" method was calling "strtolower" on the base_class of the model. Calling "lcfirst" should fix it.
Also, as the second parameter, you could pass the camelcased string and it would work without the pull request. eg.

    public function __construct()
    {
        $this->authorizeResource(CompoundName::class, 'compoundName');
    }

@jpuck jpuck closed this as completed Mar 20, 2017
@dschreij
Copy link

Your tip of passing the variable's compound name as the second argument saved the day!

@pedrofurtado
Copy link

#16184

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

No branches or pull requests

4 participants