Skip to content

Commit

Permalink
Rename user method to tokenable
Browse files Browse the repository at this point in the history
  • Loading branch information
amaelftah committed Jan 20, 2020
1 parent fcff677 commit 95f3761
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 17 deletions.
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ Within your web application's UI, you may wish to list each of the user's tokens

## Customization

You may customize the "user" model and the personal access token model used by Airlock via the `useUserModel` and `usePersonalAccessTokenModel` methods. Typically, you should call these methods from the `boot` method of your `AppServiceProvider`:
You may customize the the personal access token model used by Airlock via the `usePersonalAccessTokenModel` methods. Typically, you should call this method from the `boot` method of your `AppServiceProvider`:

```php
use App\Airlock\CustomPersonalAccessToken;
Expand All @@ -184,8 +184,6 @@ use Laravel\Airlock\Airlock;

public function boot()
{
Airlock::useUserModel(CustomUser::class);

Airlock::usePersonalAccessTokenModel(
CustomPersonalAccessToken::class
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public function up()
{
Schema::create('personal_access_tokens', function (Blueprint $table) {
$table->bigIncrements('id');
$table->morphs('model');
$table->morphs('tokenable');
$table->string('name');
$table->string('token', 64)->unique();
$table->text('abilities')->nullable();
Expand Down
8 changes: 4 additions & 4 deletions src/Guard.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,22 +60,22 @@ public function __invoke(Request $request)
return;
}

return $this->supportsTokens($accessToken->user) ? $accessToken->user->withAccessToken(
return $this->supportsTokens($accessToken->tokenable) ? $accessToken->tokenable->withAccessToken(
tap($accessToken->forceFill(['last_used_at' => now()]))->save()
) : null;
}
}

/**
* Determine if the user model supports API tokens.
* Determine if the tokenable model supports API tokens.
*
* @param \Illuminate\Contracts\Auth\Authenticatable|null $user
* @return bool
*/
protected function supportsTokens($user = null)
protected function supportsTokens($tokenable = null)
{
return in_array(HasApiTokens::class, class_uses_recursive(
$user ? get_class($user) : null
$tokenable ? get_class($tokenable) : null
));
}
}
4 changes: 2 additions & 2 deletions src/HasApiTokens.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ trait HasApiTokens
protected $accessToken;

/**
* Get the access tokens that belong to the user.
* Get the access tokens that belong to model.
*/
public function tokens()
{
return $this->morphMany(Airlock::$personalAccessTokenModel, 'model');
return $this->morphMany(Airlock::$personalAccessTokenModel, 'tokenable');
}

/**
Expand Down
6 changes: 3 additions & 3 deletions src/PersonalAccessToken.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ class PersonalAccessToken extends Model implements HasAbilities
];

/**
* Get the user that the access token belongs to.
* Get the tokenable model that the access token belongs to.
*/
public function user()
public function tokenable()
{
return $this->morphTo('model');
return $this->morphTo('tokenable');
}

/**
Expand Down
8 changes: 4 additions & 4 deletions tests/GuardTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,8 @@ public function test_authentication_with_token_fails_if_expired()
]);

$token = PersonalAccessToken::forceCreate([
'model_id' => $user->id,
'model_type' => get_class($user),
'tokenable_id' => $user->id,
'tokenable_type' => get_class($user),
'name' => 'Test',
'token' => hash('sha256', 'test'),
'created_at' => now()->subMinutes(60),
Expand Down Expand Up @@ -148,8 +148,8 @@ public function test_authentication_is_successful_with_token_if_no_session_prese
]);

$token = PersonalAccessToken::forceCreate([
'model_id' => $user->id,
'model_type' => get_class($user),
'tokenable_id' => $user->id,
'tokenable_type' => get_class($user),
'name' => 'Test',
'token' => hash('sha256', 'test'),
]);
Expand Down

0 comments on commit 95f3761

Please sign in to comment.