2015-12-06 13:58:49 -05:00
|
|
|
<?php
|
2016-12-07 22:46:38 +00:00
|
|
|
|
2024-03-12 22:39:16 -04:00
|
|
|
namespace App\Models;
|
2015-12-06 13:58:49 -05:00
|
|
|
|
2024-03-18 21:23:13 -04:00
|
|
|
use App\Exceptions\DisplayException;
|
2024-03-12 22:39:16 -04:00
|
|
|
use App\Rules\Username;
|
|
|
|
|
use App\Facades\Activity;
|
2024-03-23 20:17:48 -04:00
|
|
|
use Filament\Models\Contracts\FilamentUser;
|
2024-04-06 10:11:10 -04:00
|
|
|
use Filament\Models\Contracts\HasAvatar;
|
|
|
|
|
use Filament\Models\Contracts\HasName;
|
2024-03-23 20:17:48 -04:00
|
|
|
use Filament\Panel;
|
2024-03-31 02:09:23 -04:00
|
|
|
use Illuminate\Support\Str;
|
2018-01-01 15:11:44 -06:00
|
|
|
use Illuminate\Validation\Rules\In;
|
2015-12-06 13:58:49 -05:00
|
|
|
use Illuminate\Auth\Authenticatable;
|
2016-12-07 22:46:38 +00:00
|
|
|
use Illuminate\Notifications\Notifiable;
|
2020-07-06 21:25:00 -07:00
|
|
|
use Illuminate\Database\Eloquent\Builder;
|
2024-03-12 22:39:16 -04:00
|
|
|
use App\Models\Traits\HasAccessTokens;
|
2015-12-06 13:58:49 -05:00
|
|
|
use Illuminate\Auth\Passwords\CanResetPassword;
|
2024-03-12 22:39:16 -04:00
|
|
|
use App\Traits\Helpers\AvailableLanguages;
|
2022-05-14 17:31:53 -04:00
|
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
2015-12-06 13:58:49 -05:00
|
|
|
use Illuminate\Foundation\Auth\Access\Authorizable;
|
2022-05-29 16:19:04 -04:00
|
|
|
use Illuminate\Database\Eloquent\Relations\MorphToMany;
|
2015-12-06 13:58:49 -05:00
|
|
|
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
|
|
|
|
|
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
|
|
|
|
|
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
|
2024-03-12 22:39:16 -04:00
|
|
|
use App\Notifications\SendPasswordReset as ResetPasswordNotification;
|
2015-12-06 13:58:49 -05:00
|
|
|
|
2019-09-05 21:53:33 -07:00
|
|
|
/**
|
2024-03-12 22:39:16 -04:00
|
|
|
* App\Models\User.
|
2022-05-14 17:31:53 -04:00
|
|
|
*
|
2021-01-27 20:52:11 -08:00
|
|
|
* @property int $id
|
|
|
|
|
* @property string|null $external_id
|
|
|
|
|
* @property string $uuid
|
|
|
|
|
* @property string $username
|
|
|
|
|
* @property string $email
|
|
|
|
|
* @property string|null $name_first
|
|
|
|
|
* @property string|null $name_last
|
|
|
|
|
* @property string $password
|
2022-05-22 14:57:06 -04:00
|
|
|
* @property string|null $remember_token
|
2021-01-27 20:52:11 -08:00
|
|
|
* @property string $language
|
|
|
|
|
* @property bool $root_admin
|
|
|
|
|
* @property bool $use_totp
|
|
|
|
|
* @property string|null $totp_secret
|
2022-05-22 14:57:06 -04:00
|
|
|
* @property \Illuminate\Support\Carbon|null $totp_authenticated_at
|
2021-01-27 20:52:11 -08:00
|
|
|
* @property bool $gravatar
|
2022-05-22 14:57:06 -04:00
|
|
|
* @property \Illuminate\Support\Carbon|null $created_at
|
|
|
|
|
* @property \Illuminate\Support\Carbon|null $updated_at
|
2024-03-12 22:39:16 -04:00
|
|
|
* @property \Illuminate\Database\Eloquent\Collection|\App\Models\ApiKey[] $apiKeys
|
2022-05-14 17:31:53 -04:00
|
|
|
* @property int|null $api_keys_count
|
2022-05-22 14:57:06 -04:00
|
|
|
* @property string $name
|
2022-05-14 17:31:53 -04:00
|
|
|
* @property \Illuminate\Notifications\DatabaseNotificationCollection|\Illuminate\Notifications\DatabaseNotification[] $notifications
|
|
|
|
|
* @property int|null $notifications_count
|
2024-03-12 22:39:16 -04:00
|
|
|
* @property \Illuminate\Database\Eloquent\Collection|\App\Models\RecoveryToken[] $recoveryTokens
|
2022-05-14 17:31:53 -04:00
|
|
|
* @property int|null $recovery_tokens_count
|
2024-03-12 22:39:16 -04:00
|
|
|
* @property \Illuminate\Database\Eloquent\Collection|\App\Models\Server[] $servers
|
2022-05-14 17:31:53 -04:00
|
|
|
* @property int|null $servers_count
|
2024-03-12 22:39:16 -04:00
|
|
|
* @property \Illuminate\Database\Eloquent\Collection|\App\Models\UserSSHKey[] $sshKeys
|
2022-05-14 17:31:53 -04:00
|
|
|
* @property int|null $ssh_keys_count
|
2024-03-12 22:39:16 -04:00
|
|
|
* @property \Illuminate\Database\Eloquent\Collection|\App\Models\ApiKey[] $tokens
|
2022-05-22 14:57:06 -04:00
|
|
|
* @property int|null $tokens_count
|
2022-05-14 17:31:53 -04:00
|
|
|
*
|
|
|
|
|
* @method static \Database\Factories\UserFactory factory(...$parameters)
|
|
|
|
|
* @method static Builder|User newModelQuery()
|
|
|
|
|
* @method static Builder|User newQuery()
|
|
|
|
|
* @method static Builder|User query()
|
|
|
|
|
* @method static Builder|User whereCreatedAt($value)
|
|
|
|
|
* @method static Builder|User whereEmail($value)
|
|
|
|
|
* @method static Builder|User whereExternalId($value)
|
|
|
|
|
* @method static Builder|User whereGravatar($value)
|
|
|
|
|
* @method static Builder|User whereId($value)
|
|
|
|
|
* @method static Builder|User whereLanguage($value)
|
|
|
|
|
* @method static Builder|User whereNameFirst($value)
|
|
|
|
|
* @method static Builder|User whereNameLast($value)
|
|
|
|
|
* @method static Builder|User wherePassword($value)
|
|
|
|
|
* @method static Builder|User whereRememberToken($value)
|
|
|
|
|
* @method static Builder|User whereRootAdmin($value)
|
|
|
|
|
* @method static Builder|User whereTotpAuthenticatedAt($value)
|
|
|
|
|
* @method static Builder|User whereTotpSecret($value)
|
|
|
|
|
* @method static Builder|User whereUpdatedAt($value)
|
|
|
|
|
* @method static Builder|User whereUseTotp($value)
|
|
|
|
|
* @method static Builder|User whereUsername($value)
|
|
|
|
|
* @method static Builder|User whereUuid($value)
|
2022-10-14 10:59:20 -06:00
|
|
|
*
|
2022-05-14 17:31:53 -04:00
|
|
|
* @mixin \Eloquent
|
2019-09-05 21:53:33 -07:00
|
|
|
*/
|
2024-04-13 16:30:20 +03:00
|
|
|
class User extends Model implements AuthenticatableContract, AuthorizableContract, CanResetPasswordContract, FilamentUser, HasAvatar, HasName
|
2015-12-06 13:58:49 -05:00
|
|
|
{
|
2020-04-03 23:22:35 -07:00
|
|
|
use Authenticatable;
|
2024-03-17 00:40:18 -04:00
|
|
|
use Authorizable {can as protected canned; }
|
2020-04-03 23:22:35 -07:00
|
|
|
use AvailableLanguages;
|
|
|
|
|
use CanResetPassword;
|
2022-05-22 19:03:51 -04:00
|
|
|
use HasAccessTokens;
|
2020-04-03 23:22:35 -07:00
|
|
|
use Notifiable;
|
2015-12-06 13:58:49 -05:00
|
|
|
|
2021-01-23 12:33:34 -08:00
|
|
|
public const USER_LEVEL_USER = 0;
|
|
|
|
|
public const USER_LEVEL_ADMIN = 1;
|
2017-10-26 23:49:54 -05:00
|
|
|
|
2018-01-25 21:26:06 -06:00
|
|
|
/**
|
|
|
|
|
* The resource name for this model when it is transformed into an
|
|
|
|
|
* API representation using fractal.
|
|
|
|
|
*/
|
2021-01-23 12:33:34 -08:00
|
|
|
public const RESOURCE_NAME = 'user';
|
2018-01-25 21:26:06 -06:00
|
|
|
|
2017-05-01 15:28:43 -04:00
|
|
|
/**
|
|
|
|
|
* Level of servers to display when using access() on a user.
|
|
|
|
|
*/
|
2022-10-14 10:59:20 -06:00
|
|
|
protected string $accessLevel = 'all';
|
2017-05-01 15:28:43 -04:00
|
|
|
|
2015-12-06 13:58:49 -05:00
|
|
|
/**
|
|
|
|
|
* The table associated with the model.
|
|
|
|
|
*/
|
|
|
|
|
protected $table = 'users';
|
|
|
|
|
|
|
|
|
|
/**
|
2017-01-12 15:40:24 -05:00
|
|
|
* A list of mass-assignable variables.
|
2015-12-06 13:58:49 -05:00
|
|
|
*/
|
2017-09-24 21:12:30 -05:00
|
|
|
protected $fillable = [
|
2018-02-24 14:47:53 -06:00
|
|
|
'external_id',
|
2017-09-24 21:12:30 -05:00
|
|
|
'username',
|
|
|
|
|
'email',
|
|
|
|
|
'name_first',
|
|
|
|
|
'name_last',
|
|
|
|
|
'password',
|
|
|
|
|
'language',
|
|
|
|
|
'use_totp',
|
|
|
|
|
'totp_secret',
|
2017-11-18 13:35:33 -05:00
|
|
|
'totp_authenticated_at',
|
2017-09-24 21:12:30 -05:00
|
|
|
'gravatar',
|
|
|
|
|
'root_admin',
|
|
|
|
|
];
|
2015-12-06 13:58:49 -05:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The attributes excluded from the model's JSON form.
|
|
|
|
|
*/
|
2017-11-19 16:30:00 -06:00
|
|
|
protected $hidden = ['password', 'remember_token', 'totp_secret', 'totp_authenticated_at'];
|
2015-12-06 13:58:49 -05:00
|
|
|
|
2017-06-24 19:49:09 -05:00
|
|
|
/**
|
|
|
|
|
* Default values for specific fields in the database.
|
|
|
|
|
*/
|
|
|
|
|
protected $attributes = [
|
2018-02-25 16:08:01 -06:00
|
|
|
'external_id' => null,
|
2017-06-24 19:49:09 -05:00
|
|
|
'root_admin' => false,
|
|
|
|
|
'language' => 'en',
|
|
|
|
|
'use_totp' => false,
|
|
|
|
|
'totp_secret' => null,
|
2024-03-31 02:09:23 -04:00
|
|
|
'name_first' => '',
|
|
|
|
|
'name_last' => '',
|
2017-06-24 19:49:09 -05:00
|
|
|
];
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Rules verifying that the data being stored matches the expectations of the database.
|
|
|
|
|
*/
|
2022-10-14 10:59:20 -06:00
|
|
|
public static array $validationRules = [
|
2024-03-31 02:09:23 -04:00
|
|
|
'uuid' => 'nullable|string|size:36|unique:users,uuid',
|
2020-09-26 16:29:26 -07:00
|
|
|
'email' => 'required|email|between:1,191|unique:users,email',
|
|
|
|
|
'external_id' => 'sometimes|nullable|string|max:191|unique:users,external_id',
|
|
|
|
|
'username' => 'required|between:1,191|unique:users,username',
|
2024-03-31 02:09:23 -04:00
|
|
|
'name_first' => 'nullable|string|between:0,191',
|
|
|
|
|
'name_last' => 'nullable|string|between:0,191',
|
2019-12-08 11:02:59 -08:00
|
|
|
'password' => 'sometimes|nullable|string',
|
2017-06-24 19:49:09 -05:00
|
|
|
'root_admin' => 'boolean',
|
2020-06-25 21:42:21 -07:00
|
|
|
'language' => 'string',
|
2017-06-24 19:49:09 -05:00
|
|
|
'use_totp' => 'boolean',
|
|
|
|
|
'totp_secret' => 'nullable|string',
|
|
|
|
|
];
|
2017-04-01 13:14:49 -04:00
|
|
|
|
2024-03-19 21:08:49 -04:00
|
|
|
protected function casts(): array
|
|
|
|
|
{
|
|
|
|
|
return [
|
|
|
|
|
'root_admin' => 'boolean',
|
|
|
|
|
'use_totp' => 'boolean',
|
|
|
|
|
'gravatar' => 'boolean',
|
|
|
|
|
'totp_authenticated_at' => 'datetime',
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-18 21:23:13 -04:00
|
|
|
protected static function booted(): void
|
|
|
|
|
{
|
2024-03-31 02:09:23 -04:00
|
|
|
static::creating(function (self $user) {
|
2024-04-18 23:58:55 +02:00
|
|
|
$user->uuid = Str::uuid()->toString();
|
2024-03-31 02:09:23 -04:00
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
|
2024-03-18 21:23:13 -04:00
|
|
|
static::deleting(function (self $user) {
|
|
|
|
|
throw_if($user->servers()->count() > 0, new DisplayException(__('admin/user.exceptions.user_has_servers')));
|
|
|
|
|
|
|
|
|
|
throw_if(request()->user()?->id === $user->id, new DisplayException(__('admin/user.exceptions.user_is_self')));
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-24 14:42:45 -04:00
|
|
|
public function getRouteKeyName(): string
|
|
|
|
|
{
|
|
|
|
|
return 'id';
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-01 15:11:44 -06:00
|
|
|
/**
|
|
|
|
|
* Implement language verification by overriding Eloquence's gather
|
|
|
|
|
* rules function.
|
|
|
|
|
*/
|
2022-10-14 10:59:20 -06:00
|
|
|
public static function getRules(): array
|
2018-01-01 15:11:44 -06:00
|
|
|
{
|
2019-12-08 10:44:58 -08:00
|
|
|
$rules = parent::getRules();
|
2019-09-04 22:26:28 -07:00
|
|
|
|
2021-01-23 12:33:34 -08:00
|
|
|
$rules['language'][] = new In(array_keys((new self())->getAvailableLanguages()));
|
|
|
|
|
$rules['username'][] = new Username();
|
2018-01-01 15:11:44 -06:00
|
|
|
|
|
|
|
|
return $rules;
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-04 21:45:21 -04:00
|
|
|
/**
|
|
|
|
|
* Return the user model in a format that can be passed over to Vue templates.
|
|
|
|
|
*/
|
|
|
|
|
public function toVueObject(): array
|
|
|
|
|
{
|
2024-04-04 21:45:57 -04:00
|
|
|
return collect($this->toArray())->except(['id', 'external_id'])->toArray();
|
2024-04-04 21:45:21 -04:00
|
|
|
}
|
|
|
|
|
|
2016-09-03 17:09:00 -04:00
|
|
|
/**
|
|
|
|
|
* Send the password reset notification.
|
|
|
|
|
*
|
2017-08-21 22:10:48 -05:00
|
|
|
* @param string $token
|
2016-09-03 17:09:00 -04:00
|
|
|
*/
|
|
|
|
|
public function sendPasswordResetNotification($token)
|
|
|
|
|
{
|
2022-05-29 18:48:35 -04:00
|
|
|
Activity::event('auth:reset-password')
|
2022-05-28 17:03:58 -04:00
|
|
|
->withRequestMetadata()
|
|
|
|
|
->subject($this)
|
|
|
|
|
->log('sending password reset email');
|
|
|
|
|
|
2016-09-03 17:09:00 -04:00
|
|
|
$this->notify(new ResetPasswordNotification($token));
|
|
|
|
|
}
|
2017-01-18 21:13:05 +01:00
|
|
|
|
2017-06-10 22:28:44 -05:00
|
|
|
/**
|
2018-05-13 10:50:56 -04:00
|
|
|
* Store the username as a lowercase string.
|
2017-06-10 22:28:44 -05:00
|
|
|
*/
|
2018-02-11 16:39:50 -06:00
|
|
|
public function setUsernameAttribute(string $value)
|
2017-06-10 22:28:44 -05:00
|
|
|
{
|
2018-02-11 16:39:50 -06:00
|
|
|
$this->attributes['username'] = mb_strtolower($value);
|
2017-06-10 22:28:44 -05:00
|
|
|
}
|
|
|
|
|
|
2017-09-15 22:13:33 -05:00
|
|
|
/**
|
2018-05-13 10:50:56 -04:00
|
|
|
* Return a concatenated result for the accounts full name.
|
2017-09-15 22:13:33 -05:00
|
|
|
*/
|
2022-10-14 10:59:20 -06:00
|
|
|
public function getNameAttribute(): string
|
2017-09-15 22:13:33 -05:00
|
|
|
{
|
2019-11-03 12:20:11 -08:00
|
|
|
return trim($this->name_first . ' ' . $this->name_last);
|
2017-09-15 22:13:33 -05:00
|
|
|
}
|
|
|
|
|
|
2017-02-09 17:43:54 -05:00
|
|
|
/**
|
|
|
|
|
* Returns all servers that a user owns.
|
|
|
|
|
*/
|
2022-10-14 10:59:20 -06:00
|
|
|
public function servers(): HasMany
|
2017-02-09 17:43:54 -05:00
|
|
|
{
|
|
|
|
|
return $this->hasMany(Server::class, 'owner_id');
|
|
|
|
|
}
|
2017-04-13 23:49:47 -04:00
|
|
|
|
2022-10-14 10:59:20 -06:00
|
|
|
public function apiKeys(): HasMany
|
2020-03-22 18:15:38 -07:00
|
|
|
{
|
|
|
|
|
return $this->hasMany(ApiKey::class)
|
|
|
|
|
->where('key_type', ApiKey::TYPE_ACCOUNT);
|
|
|
|
|
}
|
2020-07-02 21:55:25 -07:00
|
|
|
|
2022-10-14 10:59:20 -06:00
|
|
|
public function recoveryTokens(): HasMany
|
2020-07-02 21:55:25 -07:00
|
|
|
{
|
|
|
|
|
return $this->hasMany(RecoveryToken::class);
|
|
|
|
|
}
|
2020-07-06 21:25:00 -07:00
|
|
|
|
2022-05-14 17:31:53 -04:00
|
|
|
public function sshKeys(): HasMany
|
|
|
|
|
{
|
|
|
|
|
return $this->hasMany(UserSSHKey::class);
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-29 16:19:04 -04:00
|
|
|
/**
|
2022-10-14 10:59:20 -06:00
|
|
|
* Returns all the activity logs where this user is the subject — not to
|
2022-05-29 16:19:04 -04:00
|
|
|
* be confused by activity logs where this user is the _actor_.
|
|
|
|
|
*/
|
|
|
|
|
public function activity(): MorphToMany
|
|
|
|
|
{
|
|
|
|
|
return $this->morphToMany(ActivityLog::class, 'subject', 'activity_log_subjects');
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-06 21:25:00 -07:00
|
|
|
/**
|
2022-10-14 10:59:20 -06:00
|
|
|
* Returns all the servers that a user can access by way of being the owner of the
|
2020-07-06 21:25:00 -07:00
|
|
|
* server, or because they are assigned as a subuser for that server.
|
|
|
|
|
*/
|
2022-10-14 10:59:20 -06:00
|
|
|
public function accessibleServers(): Builder
|
2020-07-06 21:25:00 -07:00
|
|
|
{
|
2020-07-09 21:32:31 -07:00
|
|
|
return Server::query()
|
2020-07-06 21:25:00 -07:00
|
|
|
->select('servers.*')
|
|
|
|
|
->leftJoin('subusers', 'subusers.server_id', '=', 'servers.id')
|
|
|
|
|
->where(function (Builder $builder) {
|
|
|
|
|
$builder->where('servers.owner_id', $this->id)->orWhere('subusers.user_id', $this->id);
|
|
|
|
|
})
|
|
|
|
|
->groupBy('servers.id');
|
|
|
|
|
}
|
2024-03-16 18:53:53 -04:00
|
|
|
|
2024-03-24 14:42:45 -04:00
|
|
|
public function subusers(): HasMany
|
|
|
|
|
{
|
|
|
|
|
return $this->hasMany(Subuser::class);
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-16 18:53:53 -04:00
|
|
|
protected function checkPermission(Server $server, string $permission = ''): bool
|
|
|
|
|
{
|
|
|
|
|
if ($this->root_admin || $server->owner_id === $this->id) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$subuser = $server->subusers->where('user_id', $this->id)->first();
|
|
|
|
|
if (!$subuser || empty($permission)) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$check = in_array($permission, $subuser->permissions);
|
|
|
|
|
|
|
|
|
|
return $check;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Laravel's policies strictly check for the existence of a real method,
|
|
|
|
|
* this checks if the ability is one of our permissions and then checks if the user can do it or not
|
|
|
|
|
* Otherwise it calls the Authorizable trait's parent method
|
|
|
|
|
*/
|
|
|
|
|
public function can($abilities, mixed $arguments = []): bool
|
|
|
|
|
{
|
|
|
|
|
if (is_string($abilities) && str_contains($abilities, '.')) {
|
|
|
|
|
[$permission, $key] = str($abilities)->explode('.', 2);
|
|
|
|
|
|
|
|
|
|
if (isset(Permission::permissions()[$permission]['keys'][$key])) {
|
|
|
|
|
if ($arguments instanceof Server) {
|
|
|
|
|
return $this->checkPermission($arguments, $abilities);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $this->canned($abilities, $arguments);
|
|
|
|
|
}
|
2024-03-23 20:17:48 -04:00
|
|
|
|
2024-04-06 23:58:10 -04:00
|
|
|
public function isLastRootAdmin(): bool
|
|
|
|
|
{
|
|
|
|
|
$rootAdmins = User::query()->where('root_admin', true)->limit(2)->get();
|
|
|
|
|
|
2024-04-13 16:30:20 +03:00
|
|
|
return once(fn () => $rootAdmins->count() === 1 && $rootAdmins->first()->is($this));
|
2024-04-06 23:58:10 -04:00
|
|
|
}
|
|
|
|
|
|
2024-03-23 20:17:48 -04:00
|
|
|
public function canAccessPanel(Panel $panel): bool
|
|
|
|
|
{
|
|
|
|
|
return $this->root_admin;
|
|
|
|
|
}
|
2024-04-06 10:11:10 -04:00
|
|
|
|
|
|
|
|
public function getFilamentName(): string
|
|
|
|
|
{
|
|
|
|
|
return $this->name_first ?: $this->username;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getFilamentAvatarUrl(): ?string
|
|
|
|
|
{
|
|
|
|
|
return 'https://gravatar.com/avatar/' . md5(strtolower($this->email));
|
|
|
|
|
}
|
2015-12-06 13:58:49 -05:00
|
|
|
}
|