*/ use HasAccessTokens; /** @use HasFactory */ use HasFactory; use HasRealtimeIdentifier; use HasVersion4Uuids; use Notifiable; public const int USER_LEVEL_USER = 0; public const int USER_LEVEL_ADMIN = 1; public const string RESOURCE_NAME = 'user'; protected $attributes = [ 'external_id' => null, 'root_admin' => false, 'language' => 'en', 'use_totp' => false, 'totp_secret' => null, ]; /** * @return array */ protected function casts(): array { return [ 'root_admin' => 'boolean', 'use_totp' => 'boolean', 'gravatar' => 'boolean', 'password' => 'hashed', 'totp_authenticated_at' => 'datetime', ]; } /** * The column holding the automatically generated v4 UUID for the user. * * @return list */ public function uniqueIds(): array { return ['uuid']; } /** * @return array */ public function toVueObject(): array { $attributes = $this->toArray(); unset($attributes['id'], $attributes['external_id']); return [...$attributes, 'identifier' => $this->identifier]; } /** * @param string $token */ public function sendPasswordResetNotification($token): void { Activity::event('auth:reset-password') ->withRequestMetadata() ->subject($this) ->log('sending password reset email'); $this->notify(new ResetPasswordNotification($token)); } /** * Returns all servers that a user owns. * * @return HasMany */ public function servers(): HasMany { return $this->hasMany(Server::class, 'owner_id'); } /** * @return HasMany */ public function apiKeys(): HasMany { return $this->hasMany(ApiKey::class) ->where('key_type', ApiKey::TYPE_ACCOUNT); } /** * @return HasMany */ public function recoveryTokens(): HasMany { return $this->hasMany(RecoveryToken::class); } /** * @return HasMany */ 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_. * * @return MorphToMany */ public function activity(): MorphToMany { return $this->morphToMany(ActivityLog::class, 'subject', 'activity_log_subjects'); } /** * 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. * * @return Builder */ public function accessibleServers(): Builder { return Server::query() ->select('servers.*') ->leftJoin('subusers', 'subusers.server_id', '=', 'servers.id') ->where(function (Builder $builder): void { $builder->where('servers.owner_id', $this->id)->orWhere('subusers.user_id', $this->id); }) ->groupBy('servers.id'); } /** * The owner and root admins hold every permission, and root admins also receive the * admin websocket events. Anyone else holds what their subuser entry grants, if any. * * @return array */ public function permissionsOn(Server $server): array { if ($this->root_admin || $this->id === $server->owner_id) { $permissions = ['*']; if ($this->root_admin) { $permissions[] = 'admin.websocket.errors'; $permissions[] = 'admin.websocket.install'; $permissions[] = 'admin.websocket.transfer'; } return $permissions; } return $server->subusers()->where('user_id', $this->id)->first()->permissions ?? []; } /** * Store the username as a lowercase string. Reads fall through to the stored * column, so the get side is the string it was written as. * * @return Attribute */ protected function username(): Attribute { return Attribute::make( set: fn (string $value): string => mb_strtolower($value), ); } /** * @return Attribute */ protected function name(): Attribute { return Attribute::make( get: fn (): string => mb_trim($this->name_first.' '.$this->name_last), ); } }