Skip to content

Commit

Permalink
[4.x] Add tokenCant() helper function to HasApiTokens (#552)
Browse files Browse the repository at this point in the history
* [4.x] Add tokenCant helper function to `HasApiTokens`

* Simplified to just use the reverse of the tokenCan()

* fixed style issue
  • Loading branch information
chester-sykes authored Dec 11, 2024
1 parent 8bdfb87 commit 6980642
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/HasApiTokens.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,17 @@ public function tokenCan(string $ability)
return $this->accessToken && $this->accessToken->can($ability);
}

/**
* Determine if the current API token does not have a given scope.
*
* @param string $ability
* @return bool
*/
public function tokenCant(string $ability)
{
return ! $this->tokenCan($ability);
}

/**
* Create a new personal access token for the user.
*
Expand Down
13 changes: 13 additions & 0 deletions tests/Feature/HasApiTokensTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,19 @@ public function test_can_check_token_abilities()
$this->assertTrue($class->tokenCan('foo'));
}

public function test_check_token_cant_ability()
{
$class = new ClassThatHasApiTokens;

$newToken = $class->createToken('test', ['foo']);

$class->withAccessToken($newToken->accessToken);

$this->assertTrue($class->tokenCant('bar'));

$this->assertFalse($class->tokenCant('foo'));
}

public function test_token_checksum_is_valid()
{
$config = require __DIR__.'/../../config/sanctum.php';
Expand Down

0 comments on commit 6980642

Please sign in to comment.