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

[5.3] Guess policy method from class name #16807

Merged
merged 1 commit into from
Dec 15, 2016

Conversation

alepeino
Copy link
Contributor

Very minor improvement for handling the case of Actions That Don't Require Models so that the policy and the ability to authorize can be guessed from a class name.

Example:

public function create()
{
    $this->authorize(Post::class);

    // ...
}

@@ -48,7 +48,7 @@ public function authorizeForUser($user, $ability, $arguments = [])
*/
protected function parseAbilityAndArguments($ability, $arguments)
{
if (is_string($ability)) {
if (is_string($ability) && (! class_exists($ability))) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you remove the additional parenthesis?

@JosephSilber
Copy link
Member

JosephSilber commented Dec 15, 2016

This originally wasn't added because it's a little brittle. People use all sorts of crazy ability names, and it may overlap with an existing class name.

Maybe we indeed shouldn't care, and just cater to the 99%. However, we stick to that rule when the 1% has a (less elegant) way to achieve their desired result. In this case however, if you have an ability whose name matches an existing class, there'd be no recourse.

Maybe instead of checking for class_exists we should check if it looks like a class name? Since a method on the ability cannot have a slash in it, there's no way this would break for anyone.

@taylorotwell taylorotwell merged commit e0e3220 into laravel:5.3 Dec 15, 2016
@alepeino alepeino deleted the policy-method-guess branch December 15, 2016 16:18
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

4 participants