user: cleanup
This commit is contained in:
parent
7f669828c6
commit
926c8563d0
5 changed files with 44 additions and 53 deletions
|
@ -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.
|
||||
|
|
|
@ -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),
|
||||
];
|
||||
|
|
|
@ -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),
|
||||
];
|
||||
|
|
|
@ -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
|
||||
{
|
||||
|
|
|
@ -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),
|
||||
],
|
||||
|
|
Loading…
Reference in a new issue