102 lines
2.9 KiB
PHP
102 lines
2.9 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
// use Illuminate\Contracts\Auth\MustVerifyEmail;
|
|
use App\Actions\Permissions\PermissionService;
|
|
use App\Enums\Permission;
|
|
use Database\Factories\UserFactory;
|
|
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
|
use Illuminate\Database\Eloquent\Attributes\Hidden;
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
use Illuminate\Database\Eloquent\Relations\MorphMany;
|
|
use Illuminate\Foundation\Auth\User as Authenticatable;
|
|
use Illuminate\Notifications\Notifiable;
|
|
use Laravel\Fortify\TwoFactorAuthenticatable;
|
|
|
|
#[Fillable(['name', 'email', 'password', 'must_change_password_on_next_login'])]
|
|
#[Hidden(['password', 'two_factor_secret', 'two_factor_recovery_codes', 'remember_token'])]
|
|
class User extends Authenticatable
|
|
{
|
|
/** @use HasFactory<UserFactory> */
|
|
use HasFactory, Notifiable, TwoFactorAuthenticatable;
|
|
|
|
protected static function booted(): void
|
|
{
|
|
static::deleting(function (self $user): void {
|
|
$user->permissionAssignments()->delete();
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Get the attributes that should be cast.
|
|
*
|
|
* @return array<string, string>
|
|
*/
|
|
protected function casts(): array
|
|
{
|
|
return [
|
|
'email_verified_at' => 'datetime',
|
|
'must_change_password_on_next_login' => 'bool',
|
|
'password' => 'hashed',
|
|
'two_factor_confirmed_at' => 'datetime',
|
|
];
|
|
}
|
|
|
|
public function permissionAssignments(): MorphMany
|
|
{
|
|
return $this->morphMany(PermissionAssignment::class, 'assignee');
|
|
}
|
|
|
|
public function teams(): BelongsToMany
|
|
{
|
|
return $this->belongsToMany(Team::class)
|
|
->withTimestamps();
|
|
}
|
|
|
|
public function gitConnections(): HasMany
|
|
{
|
|
return $this->hasMany(UserGitConnection::class)
|
|
->orderBy('provider')
|
|
->orderBy('base_url');
|
|
}
|
|
|
|
public function userApplications(): HasMany
|
|
{
|
|
return $this->hasMany(UserApplication::class)
|
|
->orderBy('name')
|
|
->orderBy('id');
|
|
}
|
|
|
|
public function assignedTickets(): HasMany
|
|
{
|
|
return $this->hasMany(Ticket::class, 'assignee_user_id');
|
|
}
|
|
|
|
public function authoredTickets(): HasMany
|
|
{
|
|
return $this->hasMany(Ticket::class, 'author_user_id');
|
|
}
|
|
|
|
public function scopeVisibleToProject(Builder $query, Project $project): Builder
|
|
{
|
|
$permissionService = app(PermissionService::class);
|
|
|
|
$allowedUserIds = User::query()
|
|
->select('id')
|
|
->cursor()
|
|
->filter(fn (User $candidate) => $permissionService->allows(
|
|
$candidate,
|
|
Permission::ProjectRead,
|
|
$project
|
|
))
|
|
->pluck('id')
|
|
->all();
|
|
|
|
return $query->whereKey($allowedUserIds);
|
|
}
|
|
}
|