Skip to content

Commit

Permalink
Rename the authorized method
Browse files Browse the repository at this point in the history
  • Loading branch information
rubenvanassche committed Dec 16, 2021
1 parent b128615 commit af0d92d
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 6 deletions.
8 changes: 7 additions & 1 deletion src/Resolvers/DataFromSomethingResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,13 @@ private function ensureRequestIsValid(string $class, Request $value): void
private function ensureRequestIsAuthorized(string $class): void
{
/** @psalm-suppress UndefinedMethod */
if ($class::authorized() === false) {
// TODO: remove this with the next major release
if (method_exists($class, 'authorized') && $class::authorized() === false) {
throw new AuthorizationException();
}

/** @psalm-suppress UndefinedMethod */
if (method_exists($class, 'authorize') && $class::authorize() === false) {
throw new AuthorizationException();
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Support/DataClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ private function resolveMagicalMethods()
$methods = collect($this->class->getMethods(ReflectionMethod::IS_STATIC));

$this->hasAuthorizationMethod = $methods->contains(
fn (ReflectionMethod $method) => $method->getName() === 'authorized' && $method->isPublic()
fn (ReflectionMethod $method) => in_array($method->getName(), ['authorize', 'authorized']) && $method->isPublic()
);

$this->creationMethods = $methods
Expand Down
11 changes: 10 additions & 1 deletion tests/Fakes/RequestData.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class RequestData extends Data
public static array $rules = [];
public static ?Closure $validatorClosure = null;
public static bool $enableAuthorizeFailure = false;
public static bool $enableAuthorizedFailure = false;

public function __construct(
#[Max(10)]
Expand Down Expand Up @@ -43,19 +44,27 @@ public static function withValidator(Validator $validator): void
}
}

public static function authorized()
public static function authorize()
{
if (self::$enableAuthorizeFailure) {
return false;
}
}

public static function authorized()
{
if (self::$enableAuthorizedFailure) {
return false;
}
}

public static function clear()
{
self::$attributes = [];
self::$rules = [];
self::$messages = [];
self::$validatorClosure = null;
self::$enableAuthorizeFailure = false;
self::$enableAuthorizedFailure = false;
}
}
8 changes: 8 additions & 0 deletions tests/RequestDataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,14 @@ public function it_can_check_for_authorisation()
$this->validRequest()->assertStatus(403);
}

/** @test */
public function it_can_check_for_authorisation_with_wrong_method_name()
{
RequestData::$enableAuthorizedFailure = true;

$this->validRequest()->assertStatus(403);
}

/** @test */
public function it_can_manually_override_how_the_data_object_will_be_constructed()
{
Expand Down
6 changes: 3 additions & 3 deletions tests/Support/DataClassTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,19 +67,19 @@ public static function fromDoNotIncludeE(string $other, string $extra): static
public function it_can_check_if_a_data_class_has_an_authorisation_method()
{
$withMethod = new class (null) extends Data {
public static function authorized(): bool
public static function authorize(): bool
{
}
};

$withNonStaticMethod = new class (null) extends Data {
public function authorized(): bool
public function authorize(): bool
{
}
};

$withNonPublicMethod = new class (null) extends Data {
protected static function authorized(): bool
protected static function authorize(): bool
{
}
};
Expand Down

0 comments on commit af0d92d

Please sign in to comment.