From 926c8563d06386031e26a6be81f3b4d963f59e4a Mon Sep 17 00:00:00 2001 From: Matthew Penner Date: Thu, 15 Dec 2022 12:26:34 -0700 Subject: [PATCH] user: cleanup --- app/Models/User.php | 77 ++++++++----------- .../Api/Application/UserTransformer.php | 4 +- .../Api/Client/UserTransformer.php | 2 +- .../ApplicationApiIntegrationTestCase.php | 2 +- .../Application/Users/UserControllerTest.php | 12 +-- 5 files changed, 44 insertions(+), 53 deletions(-) diff --git a/app/Models/User.php b/app/Models/User.php index afdbf6566..6ffe07091 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -41,6 +41,10 @@ use Pterodactyl\Notifications\SendPasswordReset as ResetPasswordNotification; * @property bool $gravatar * @property \Illuminate\Support\Carbon|null $created_at * @property \Illuminate\Support\Carbon|null $updated_at + * @property string $avatar_url + * @property string|null $admin_role_name + * @property string $md5 + * @property \Pterodactyl\Models\AdminRole|null $adminRole * @property \Illuminate\Database\Eloquent\Collection|\Pterodactyl\Models\ApiKey[] $apiKeys * @property int|null $api_keys_count * @property \Illuminate\Notifications\DatabaseNotificationCollection|\Illuminate\Notifications\DatabaseNotification[] $notifications @@ -127,10 +131,6 @@ class User extends Model implements 'root_admin', ]; - protected $appends = [ - 'md5', - ]; - /** * Cast values to correct type. */ @@ -192,11 +192,9 @@ class User extends Model implements */ public function toReactObject(): array { - $object = Collection::make($this->toArray())->except(['id', 'external_id'])->toArray(); - $object['avatar_url'] = $this->avatarURL(); - $object['role_name'] = $this->adminRoleName(); - - return $object; + return Collection::make($this->append(['avatar_url', 'admin_role_name'])->toArray()) + ->except(['id', 'external_id', 'admin_role', 'admin_role_id']) + ->toArray(); } /** @@ -222,22 +220,34 @@ class User extends Model implements $this->attributes['username'] = mb_strtolower($value); } - public function avatarURL(): string + public function avatarUrl(): Attribute { - return 'https://www.gravatar.com/avatar/' . $this->md5 . '.jpg'; + return Attribute::make( + get: fn () => 'https://www.gravatar.com/avatar/' . $this->md5 . '.jpg', + ); + } + + public function adminRoleName(): Attribute + { + return Attribute::make( + get: fn () => is_null($this->adminRole) ? ($this->root_admin ? 'None' : null) : $this->adminRole->name, + ); + } + + public function md5(): Attribute + { + return Attribute::make( + get: fn () => md5(strtolower($this->email)), + ); } /** - * Gets the name of the role assigned to a user. + * Returns all the activity logs where this user is the subject — not to + * be confused by activity logs where this user is the _actor_. */ - public function adminRoleName(): ?string + public function activity(): MorphToMany { - $role = $this->adminRole; - if (is_null($role)) { - return $this->root_admin ? 'None' : null; - } - - return $role->name; + return $this->morphToMany(ActivityLog::class, 'subject', 'activity_log_subjects'); } public function adminRole(): HasOne @@ -245,14 +255,6 @@ class User extends Model implements return $this->hasOne(AdminRole::class, 'id', 'admin_role_id'); } - /** - * Returns all servers that a user owns. - */ - public function servers(): HasMany - { - return $this->hasMany(Server::class, 'owner_id'); - } - public function apiKeys(): HasMany { return $this->hasMany(ApiKey::class) @@ -264,27 +266,16 @@ class User extends Model implements return $this->hasMany(RecoveryToken::class); } + public function servers(): HasMany + { + return $this->hasMany(Server::class, 'owner_id'); + } + public function sshKeys(): HasMany { return $this->hasMany(UserSSHKey::class); } - /** - * Returns all the activity logs where this user is the subject — not to - * be confused by activity logs where this user is the _actor_. - */ - public function activity(): MorphToMany - { - return $this->morphToMany(ActivityLog::class, 'subject', 'activity_log_subjects'); - } - - public function md5(): Attribute - { - return Attribute::make( - get: fn () => md5(strtolower($this->email)), - ); - } - /** * Returns all the servers that a user can access by way of being the owner of the * server, or because they are assigned as a subuser for that server. diff --git a/app/Transformers/Api/Application/UserTransformer.php b/app/Transformers/Api/Application/UserTransformer.php index c50a9d8c6..2a3065654 100644 --- a/app/Transformers/Api/Application/UserTransformer.php +++ b/app/Transformers/Api/Application/UserTransformer.php @@ -37,9 +37,9 @@ class UserTransformer extends Transformer 'language' => $model->language, 'root_admin' => (bool) $model->root_admin, '2fa' => (bool) $model->use_totp, - 'avatar_url' => $model->avatarURL(), + 'avatar_url' => $model->avatar_url, 'admin_role_id' => $model->admin_role_id, - 'role_name' => $model->adminRoleName(), + 'role_name' => $model->admin_role_name, 'created_at' => self::formatTimestamp($model->created_at), 'updated_at' => self::formatTimestamp($model->updated_at), ]; diff --git a/app/Transformers/Api/Client/UserTransformer.php b/app/Transformers/Api/Client/UserTransformer.php index 7a2f07754..c076e6b99 100644 --- a/app/Transformers/Api/Client/UserTransformer.php +++ b/app/Transformers/Api/Client/UserTransformer.php @@ -25,7 +25,7 @@ class UserTransformer extends Transformer 'uuid' => $model->uuid, 'username' => $model->username, 'email' => $model->email, - 'image' => $model->avatarURL(), + 'image' => $model->avatar_url, '2fa_enabled' => $model->use_totp, 'created_at' => self::formatTimestamp($model->created_at), ]; diff --git a/tests/Integration/Api/Application/ApplicationApiIntegrationTestCase.php b/tests/Integration/Api/Application/ApplicationApiIntegrationTestCase.php index 1c7e7e001..a137a324d 100644 --- a/tests/Integration/Api/Application/ApplicationApiIntegrationTestCase.php +++ b/tests/Integration/Api/Application/ApplicationApiIntegrationTestCase.php @@ -92,7 +92,7 @@ abstract class ApplicationApiIntegrationTestCase extends IntegrationTestCase /** * Return a transformer that can be used for testing purposes. * - * @deprecated Instantiate the transformer directly. + * @deprecated instantiate the transformer directly */ protected function getTransformer(string $abstract): Transformer { diff --git a/tests/Integration/Api/Application/Users/UserControllerTest.php b/tests/Integration/Api/Application/Users/UserControllerTest.php index 43cc83f5a..738f5843f 100644 --- a/tests/Integration/Api/Application/Users/UserControllerTest.php +++ b/tests/Integration/Api/Application/Users/UserControllerTest.php @@ -55,8 +55,8 @@ class UserControllerTest extends ApplicationApiIntegrationTestCase 'admin_role_id' => $this->getApiUser()->admin_role_id, 'root_admin' => $this->getApiUser()->root_admin, '2fa' => $this->getApiUser()->use_totp, - 'avatar_url' => $this->getApiUser()->avatarURL(), - 'role_name' => $this->getApiUser()->adminRoleName(), + 'avatar_url' => $this->getApiUser()->avatar_url, + 'role_name' => $this->getApiUser()->admin_role_name, 'created_at' => $this->formatTimestamp($this->getApiUser()->created_at), 'updated_at' => $this->formatTimestamp($this->getApiUser()->updated_at), ], @@ -73,8 +73,8 @@ class UserControllerTest extends ApplicationApiIntegrationTestCase 'admin_role_id' => $user->admin_role_id, 'root_admin' => (bool) $user->root_admin, '2fa' => (bool) $user->use_totp, - 'avatar_url' => $user->avatarURL(), - 'role_name' => $user->adminRoleName(), + 'avatar_url' => $user->avatar_url, + 'role_name' => $user->admin_role_name, 'created_at' => $this->formatTimestamp($user->created_at), 'updated_at' => $this->formatTimestamp($user->updated_at), ], @@ -108,8 +108,8 @@ class UserControllerTest extends ApplicationApiIntegrationTestCase 'admin_role_id' => $user->admin_role_id, 'root_admin' => (bool) $user->root_admin, '2fa' => (bool) $user->use_totp, - 'avatar_url' => $user->avatarURL(), - 'role_name' => $user->adminRoleName(), + 'avatar_url' => $user->avatar_url, + 'role_name' => $user->admin_role_name, 'created_at' => $this->formatTimestamp($user->created_at), 'updated_at' => $this->formatTimestamp($user->updated_at), ],