| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173 |
- <?php
- namespace App\Models;
- use Illuminate\Contracts\Auth\MustVerifyEmail;
- use Illuminate\Database\Eloquent\Factories\HasFactory;
- use Illuminate\Database\Eloquent\Relations\BelongsTo;
- use Illuminate\Database\Eloquent\Relations\BelongsToMany;
- use Illuminate\Database\Eloquent\Relations\HasMany;
- use Illuminate\Database\Eloquent\SoftDeletes;
- use Illuminate\Foundation\Auth\User as Authenticatable;
- use Illuminate\Notifications\Notifiable;
- use Illuminate\Support\Facades\DB;
- class User extends Authenticatable implements MustVerifyEmail
- {
- use HasFactory, Notifiable, SoftDeletes;
- const DEFAULT_SORT_BY = 'created_at';
- /**
- * The attributes that are mass assignable.
- *
- * @var list<string>
- */
- protected $fillable = [
- 'name',
- 'email',
- 'notification_email',
- 'phone',
- 'password',
- 'role',
- 'role_id',
- 'color',
- 'token_fcm',
- ];
- /**
- * The attributes that should be hidden for serialization.
- *
- * @var list<string>
- */
- protected $hidden = [
- 'password',
- 'remember_token',
- ];
- /**
- * Get the attributes that should be cast.
- *
- * @return array<string, string>
- */
- protected function casts(): array
- {
- return [
- 'email_verified_at' => 'datetime',
- 'password' => 'hashed',
- ];
- }
- /**
- * Route notifications for the FCM channel.
- *
- * @return string
- */
- public function routeNotificationForFcm(): string
- {
- return (string)$this->token_fcm;
- }
- public function getAppInstalledAttribute(): string
- {
- return $this->token_fcm ? 'Да' : 'Нет';
- }
- public function userNotifications(): HasMany
- {
- return $this->hasMany(UserNotification::class);
- }
- public function unreadUserNotifications(): HasMany
- {
- return $this->userNotifications()->whereNull('read_at');
- }
- public function roleModel(): BelongsTo
- {
- return $this->belongsTo(Role::class, 'role_id');
- }
- public function permissions(): BelongsToMany
- {
- return $this->belongsToMany(Permission::class, 'user_permissions')
- ->withPivot(['effect', 'reason', 'expires_at'])
- ->withTimestamps();
- }
- public function hasRole(string|array $roles): bool
- {
- $roles = is_array($roles) ? $roles : explode(',', $roles);
- $roles = array_map('trim', $roles);
- $role = $this->resolvedRoleSlug();
- if (!$role) {
- return false;
- }
- return count(array_intersect($roles, Role::effectiveRoles($role))) > 0;
- }
- public function hasPermission(string $permission): bool
- {
- return app(\App\Services\Access\AccessService::class)->can($this, $permission);
- }
- public function hasAnyPermission(array|string $permissions): bool
- {
- $permissions = is_array($permissions) ? $permissions : explode(',', $permissions);
- return app(\App\Services\Access\AccessService::class)->canAny($this, $permissions);
- }
- public function canViewField(string $module, string $field, ?string $entity = null): bool
- {
- return app(\App\Services\Access\AccessService::class)->canViewField($this, $module, $field, $entity);
- }
- public function canUpdateField(string $module, string $field, ?string $entity = null): bool
- {
- return app(\App\Services\Access\AccessService::class)->canUpdateField($this, $module, $field, $entity);
- }
- public function getEffectivePermissions(): \Illuminate\Support\Collection
- {
- return app(\App\Services\Access\AccessService::class)->getEffectivePermissions($this);
- }
- public function resolvedRoleSlug(): ?string
- {
- if ($this->getAttribute('role_id')) {
- $role = $this->relationLoaded('roleModel')
- ? $this->roleModel
- : $this->roleModel()->first();
- if ($role) {
- return $role->slug;
- }
- }
- return $this->role;
- }
- public static function assignUniqueFcmToken(int $userId, string $token): void
- {
- DB::transaction(function () use ($userId, $token) {
- self::query()
- ->where('id', '!=', $userId)
- ->where('token_fcm', $token)
- ->update(['token_fcm' => null]);
- self::query()
- ->where('id', $userId)
- ->update(['token_fcm' => $token]);
- });
- }
- public static function clearFcmToken(int $userId): void
- {
- self::query()
- ->where('id', $userId)
- ->update(['token_fcm' => null]);
- }
- }
|