Skip to content

[13.x] Use unique IDs on client model #1757

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

Merged
merged 3 commits into from
Jun 27, 2024
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
4 changes: 1 addition & 3 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,10 @@ jobs:
fail-fast: true
matrix:
php: [8.1, 8.2, 8.3]
laravel: [9, 10, 11]
laravel: [10, 11]
exclude:
- php: 8.1
laravel: 11
- php: 8.3
laravel: 9

name: PHP ${{ matrix.php }} - Laravel ${{ matrix.laravel }}

Expand Down
6 changes: 6 additions & 0 deletions UPGRADE.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ PR: https://github.com/laravel/passport/pull/1734

PHP 8.1 is now the minimum required version.

### Minimum Laravel Version

PR: https://github.com/laravel/passport/pull/1757

Laravel 10.0 is now the minimum required version.

### OAuth2 Server

PR: https://github.com/laravel/passport/pull/1734
Expand Down
18 changes: 9 additions & 9 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@
"ext-json": "*",
"ext-openssl": "*",
"firebase/php-jwt": "^6.4",
"illuminate/auth": "^9.21|^10.0|^11.0",
"illuminate/console": "^9.21|^10.0|^11.0",
"illuminate/container": "^9.21|^10.0|^11.0",
"illuminate/contracts": "^9.21|^10.0|^11.0",
"illuminate/cookie": "^9.21|^10.0|^11.0",
"illuminate/database": "^9.21|^10.0|^11.0",
"illuminate/encryption": "^9.21|^10.0|^11.0",
"illuminate/http": "^9.21|^10.0|^11.0",
"illuminate/support": "^9.21|^10.0|^11.0",
"illuminate/auth": "^10.0|^11.0",
"illuminate/console": "^10.0|^11.0",
"illuminate/container": "^10.0|^11.0",
"illuminate/contracts": "^10.0|^11.0",
"illuminate/cookie": "^10.0|^11.0",
"illuminate/database": "^10.0|^11.0",
"illuminate/encryption": "^10.0|^11.0",
"illuminate/http": "^10.0|^11.0",
"illuminate/support": "^10.0|^11.0",
"lcobucci/jwt": "^5.0",
"league/oauth2-server": "^9.0",
"nyholm/psr7": "^1.5",
Expand Down
21 changes: 2 additions & 19 deletions database/factories/ClientFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,32 +28,15 @@ public function modelName()
*/
public function definition()
{
return $this->ensurePrimaryKeyIsSet([
return [
'user_id' => null,
'name' => $this->faker->company(),
'secret' => Str::random(40),
'redirect' => $this->faker->url(),
'personal_access_client' => false,
'password_client' => false,
'revoked' => false,
]);
}

/**
* Ensure the primary key is set on the model when using UUIDs.
*
* @param array $data
* @return array
*/
protected function ensurePrimaryKeyIsSet(array $data)
{
if (Passport::clientUuids()) {
$keyName = (new ($this->modelName()))->getKeyName();

$data[$keyName] = (string) Str::orderedUuid();
}

return $data;
];
}

/**
Expand Down
33 changes: 25 additions & 8 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,19 +59,16 @@ class Client extends Model
public $plainSecret;

/**
* Bootstrap the model and its traits.
* Create a new Eloquent model instance.
*
* @param array $attributes
* @return void
*/
public static function boot()
public function __construct(array $attributes = [])
{
parent::boot();
parent::__construct($attributes);

static::creating(function ($model) {
if (Passport::clientUuids()) {
$model->{$model->getKeyName()} = $model->{$model->getKeyName()} ?: (string) Str::orderedUuid();
}
});
$this->usesUniqueIds = Passport::clientUuids();
}

/**
Expand Down Expand Up @@ -203,6 +200,26 @@ public function confidential()
return ! empty($this->secret);
}

/**
* Get the columns that should receive a unique identifier.
*
* @return array
*/
public function uniqueIds()
{
return Passport::clientUuids() ? [$this->getKeyName()] : [];
}

/**
* Generate a new key for the model.
*
* @return string
*/
public function newUniqueId()
{
return Passport::clientUuids() ? (string) Str::orderedUuid() : null;
}

/**
* Get the auto-incrementing key type.
*
Expand Down