*/ protected $fillable = [ // phpcs:ignore 'name', 'password', 'email', 'api_key', 'last_login_at', ]; /** @var array The attributes that should be hidden for serialization */ protected $hidden = [ // phpcs:ignore 'api_key', 'password', ]; /** @var array The attributes that should be mutated to dates */ protected $dates = [ // phpcs:ignore 'last_login_at', ]; public function contact(): HasOne { return $this ->hasOne(Contact::class) ->withDefault(); } public function groups(): BelongsToMany { return $this->belongsToMany(Group::class, 'users_groups'); } public function isFreeloader(): bool { return $this->shiftEntries() ->where('freeloaded', true) ->count() >= config('max_freeloadable_shifts'); } public function license(): HasOne { return $this ->hasOne(License::class) ->withDefault(); } public function privileges(): Builder { /** @var Builder $builder */ $builder = Privilege::query() ->whereIn('id', function ($query): void { /** @var QueryBuilder $query */ $query->select('privilege_id') ->from('group_privileges') ->join('users_groups', 'users_groups.group_id', '=', 'group_privileges.group_id') ->where('users_groups.user_id', '=', $this->id) ->distinct(); }); return $builder; } public function getPrivilegesAttribute(): SupportCollection { return $this->privileges()->get(); } public function personalData(): HasOne { return $this ->hasOne(PersonalData::class) ->withDefault(); } public function settings(): HasOne { return $this ->hasOne(Settings::class) ->withDefault(); } public function state(): HasOne { return $this ->hasOne(State::class) ->withDefault(); } public function userAngelTypes(): BelongsToMany { return $this ->belongsToMany(AngelType::class, 'user_angel_type') ->using(UserAngelType::class) ->withPivot(UserAngelType::getPivotAttributes()); } public function isAngelTypeSupporter(AngelType $angelType): bool { return $this->userAngelTypes() ->wherePivot('angel_type_id', $angelType->id) ->wherePivot('supporter', true) ->exists(); } public function news(): HasMany { return $this->hasMany(News::class); } public function newsComments(): HasMany { return $this->hasMany(NewsComment::class); } public function oauth(): HasMany { return $this->hasMany(OAuth::class); } public function shiftEntries(): HasMany { return $this->hasMany(ShiftEntry::class); } public function worklogs(): HasMany { return $this->hasMany(Worklog::class); } public function worklogsCreated(): HasMany { return $this->hasMany(Worklog::class, 'creator_id'); } public function questionsAsked(): HasMany { return $this->hasMany(Question::class, 'user_id') ->where('user_id', $this->id); } public function questionsAnswered(): HasMany { return $this->hasMany(Question::class, 'answerer_id') ->where('answerer_id', $this->id); } public function messagesSent(): HasMany { return $this->hasMany(Message::class, 'user_id') ->orderBy('created_at', 'DESC') ->orderBy('id', 'DESC'); } public function messagesReceived(): HasMany { return $this->hasMany(Message::class, 'receiver_id') ->orderBy('read') ->orderBy('created_at', 'DESC') ->orderBy('id', 'DESC'); } /** * Returns a HasMany relation for all messages sent or received by the user. */ public function messages(): HasMany { return $this->messagesSent() ->union($this->messagesReceived()) ->orderBy('read') ->orderBy('id', 'DESC'); } public function shiftsCreated(): HasMany { return $this->hasMany(Shift::class, 'created_by'); } public function shiftsUpdated(): HasMany { return $this->hasMany(Shift::class, 'updated_by'); } public function getDisplayNameAttribute(): string { if ( config('display_full_name') && !empty(trim($this->personalData->first_name . $this->personalData->last_name)) ) { return trim( trim((string) $this->personalData->first_name) . ' ' . trim((string) $this->personalData->last_name) ); } return $this->name; } }