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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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?

return [$ability, $arguments];
}

Expand Down
35 changes: 28 additions & 7 deletions tests/Foundation/FoundationAuthorizesRequestsTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ public function test_basic_gate_check()

$gate = $this->getBasicGate();

$gate->define('foo', function () {
$gate->define('baz', function () {
$_SERVER['_test.authorizes.trait'] = true;

return true;
});

$response = (new FoundationTestAuthorizeTraitClass)->authorize('foo');
$response = (new FoundationTestAuthorizeTraitClass)->authorize('baz');

$this->assertInstanceOf(Response::class, $response);
$this->assertTrue($_SERVER['_test.authorizes.trait']);
Expand All @@ -33,11 +33,11 @@ public function test_exception_is_thrown_if_gate_check_fails()
{
$gate = $this->getBasicGate();

$gate->define('foo', function () {
$gate->define('baz', function () {
return false;
});

(new FoundationTestAuthorizeTraitClass)->authorize('foo');
(new FoundationTestAuthorizeTraitClass)->authorize('baz');
}

public function test_policies_may_be_called()
Expand All @@ -54,15 +54,29 @@ public function test_policies_may_be_called()
$this->assertTrue($_SERVER['_test.authorizes.trait.policy']);
}

public function test_policy_method_may_be_guessed()
public function test_policy_method_may_be_guessed_passing_model_instance()
{
unset($_SERVER['_test.authorizes.trait.policy']);

$gate = $this->getBasicGate();

$gate->policy(FoundationAuthorizesRequestTestClass::class, FoundationAuthorizesRequestTestPolicy::class);

$response = (new FoundationTestAuthorizeTraitClass)->authorize([new FoundationAuthorizesRequestTestClass]);
$response = (new FoundationTestAuthorizeTraitClass)->authorize(new FoundationAuthorizesRequestTestClass);

$this->assertInstanceOf(Response::class, $response);
$this->assertTrue($_SERVER['_test.authorizes.trait.policy']);
}

public function test_policy_method_may_be_guessed_passing_class_name()
{
unset($_SERVER['_test.authorizes.trait.policy']);

$gate = $this->getBasicGate();

$gate->policy(FoundationAuthorizesRequestTestClass::class, FoundationAuthorizesRequestTestPolicy::class);

$response = (new FoundationTestAuthorizeTraitClass)->authorize(FoundationAuthorizesRequestTestClass::class);

$this->assertInstanceOf(Response::class, $response);
$this->assertTrue($_SERVER['_test.authorizes.trait.policy']);
Expand Down Expand Up @@ -115,7 +129,14 @@ public function update()
return true;
}

public function test_policy_method_may_be_guessed()
public function test_policy_method_may_be_guessed_passing_model_instance()
{
$_SERVER['_test.authorizes.trait.policy'] = true;

return true;
}

public function test_policy_method_may_be_guessed_passing_class_name()
{
$_SERVER['_test.authorizes.trait.policy'] = true;

Expand Down