user: cleanup

This commit is contained in:
Matthew Penner 2022-12-15 12:26:34 -07:00
parent 7f669828c6
commit 926c8563d0
No known key found for this signature in database
5 changed files with 44 additions and 53 deletions

View file

@ -41,6 +41,10 @@ use Pterodactyl\Notifications\SendPasswordReset as ResetPasswordNotification;
* @property bool $gravatar * @property bool $gravatar
* @property \Illuminate\Support\Carbon|null $created_at * @property \Illuminate\Support\Carbon|null $created_at
* @property \Illuminate\Support\Carbon|null $updated_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 \Illuminate\Database\Eloquent\Collection|\Pterodactyl\Models\ApiKey[] $apiKeys
* @property int|null $api_keys_count * @property int|null $api_keys_count
* @property \Illuminate\Notifications\DatabaseNotificationCollection|\Illuminate\Notifications\DatabaseNotification[] $notifications * @property \Illuminate\Notifications\DatabaseNotificationCollection|\Illuminate\Notifications\DatabaseNotification[] $notifications
@ -127,10 +131,6 @@ class User extends Model implements
'root_admin', 'root_admin',
]; ];
protected $appends = [
'md5',
];
/** /**
* Cast values to correct type. * Cast values to correct type.
*/ */
@ -192,11 +192,9 @@ class User extends Model implements
*/ */
public function toReactObject(): array public function toReactObject(): array
{ {
$object = Collection::make($this->toArray())->except(['id', 'external_id'])->toArray(); return Collection::make($this->append(['avatar_url', 'admin_role_name'])->toArray())
$object['avatar_url'] = $this->avatarURL(); ->except(['id', 'external_id', 'admin_role', 'admin_role_id'])
$object['role_name'] = $this->adminRoleName(); ->toArray();
return $object;
} }
/** /**
@ -222,22 +220,34 @@ class User extends Model implements
$this->attributes['username'] = mb_strtolower($value); $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; return $this->morphToMany(ActivityLog::class, 'subject', 'activity_log_subjects');
if (is_null($role)) {
return $this->root_admin ? 'None' : null;
}
return $role->name;
} }
public function adminRole(): HasOne public function adminRole(): HasOne
@ -245,14 +255,6 @@ class User extends Model implements
return $this->hasOne(AdminRole::class, 'id', 'admin_role_id'); 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 public function apiKeys(): HasMany
{ {
return $this->hasMany(ApiKey::class) return $this->hasMany(ApiKey::class)
@ -264,27 +266,16 @@ class User extends Model implements
return $this->hasMany(RecoveryToken::class); return $this->hasMany(RecoveryToken::class);
} }
public function servers(): HasMany
{
return $this->hasMany(Server::class, 'owner_id');
}
public function sshKeys(): HasMany public function sshKeys(): HasMany
{ {
return $this->hasMany(UserSSHKey::class); 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 * 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. * server, or because they are assigned as a subuser for that server.

View file

@ -37,9 +37,9 @@ class UserTransformer extends Transformer
'language' => $model->language, 'language' => $model->language,
'root_admin' => (bool) $model->root_admin, 'root_admin' => (bool) $model->root_admin,
'2fa' => (bool) $model->use_totp, '2fa' => (bool) $model->use_totp,
'avatar_url' => $model->avatarURL(), 'avatar_url' => $model->avatar_url,
'admin_role_id' => $model->admin_role_id, 'admin_role_id' => $model->admin_role_id,
'role_name' => $model->adminRoleName(), 'role_name' => $model->admin_role_name,
'created_at' => self::formatTimestamp($model->created_at), 'created_at' => self::formatTimestamp($model->created_at),
'updated_at' => self::formatTimestamp($model->updated_at), 'updated_at' => self::formatTimestamp($model->updated_at),
]; ];

View file

@ -25,7 +25,7 @@ class UserTransformer extends Transformer
'uuid' => $model->uuid, 'uuid' => $model->uuid,
'username' => $model->username, 'username' => $model->username,
'email' => $model->email, 'email' => $model->email,
'image' => $model->avatarURL(), 'image' => $model->avatar_url,
'2fa_enabled' => $model->use_totp, '2fa_enabled' => $model->use_totp,
'created_at' => self::formatTimestamp($model->created_at), 'created_at' => self::formatTimestamp($model->created_at),
]; ];

View file

@ -92,7 +92,7 @@ abstract class ApplicationApiIntegrationTestCase extends IntegrationTestCase
/** /**
* Return a transformer that can be used for testing purposes. * 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 protected function getTransformer(string $abstract): Transformer
{ {

View file

@ -55,8 +55,8 @@ class UserControllerTest extends ApplicationApiIntegrationTestCase
'admin_role_id' => $this->getApiUser()->admin_role_id, 'admin_role_id' => $this->getApiUser()->admin_role_id,
'root_admin' => $this->getApiUser()->root_admin, 'root_admin' => $this->getApiUser()->root_admin,
'2fa' => $this->getApiUser()->use_totp, '2fa' => $this->getApiUser()->use_totp,
'avatar_url' => $this->getApiUser()->avatarURL(), 'avatar_url' => $this->getApiUser()->avatar_url,
'role_name' => $this->getApiUser()->adminRoleName(), 'role_name' => $this->getApiUser()->admin_role_name,
'created_at' => $this->formatTimestamp($this->getApiUser()->created_at), 'created_at' => $this->formatTimestamp($this->getApiUser()->created_at),
'updated_at' => $this->formatTimestamp($this->getApiUser()->updated_at), 'updated_at' => $this->formatTimestamp($this->getApiUser()->updated_at),
], ],
@ -73,8 +73,8 @@ class UserControllerTest extends ApplicationApiIntegrationTestCase
'admin_role_id' => $user->admin_role_id, 'admin_role_id' => $user->admin_role_id,
'root_admin' => (bool) $user->root_admin, 'root_admin' => (bool) $user->root_admin,
'2fa' => (bool) $user->use_totp, '2fa' => (bool) $user->use_totp,
'avatar_url' => $user->avatarURL(), 'avatar_url' => $user->avatar_url,
'role_name' => $user->adminRoleName(), 'role_name' => $user->admin_role_name,
'created_at' => $this->formatTimestamp($user->created_at), 'created_at' => $this->formatTimestamp($user->created_at),
'updated_at' => $this->formatTimestamp($user->updated_at), 'updated_at' => $this->formatTimestamp($user->updated_at),
], ],
@ -108,8 +108,8 @@ class UserControllerTest extends ApplicationApiIntegrationTestCase
'admin_role_id' => $user->admin_role_id, 'admin_role_id' => $user->admin_role_id,
'root_admin' => (bool) $user->root_admin, 'root_admin' => (bool) $user->root_admin,
'2fa' => (bool) $user->use_totp, '2fa' => (bool) $user->use_totp,
'avatar_url' => $user->avatarURL(), 'avatar_url' => $user->avatar_url,
'role_name' => $user->adminRoleName(), 'role_name' => $user->admin_role_name,
'created_at' => $this->formatTimestamp($user->created_at), 'created_at' => $this->formatTimestamp($user->created_at),
'updated_at' => $this->formatTimestamp($user->updated_at), 'updated_at' => $this->formatTimestamp($user->updated_at),
], ],