User.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <?php
  2. namespace App\Models;
  3. use Illuminate\Contracts\Auth\MustVerifyEmail;
  4. use Illuminate\Database\Eloquent\Factories\HasFactory;
  5. use Illuminate\Database\Eloquent\Relations\BelongsTo;
  6. use Illuminate\Database\Eloquent\Relations\BelongsToMany;
  7. use Illuminate\Database\Eloquent\Relations\HasMany;
  8. use Illuminate\Database\Eloquent\SoftDeletes;
  9. use Illuminate\Foundation\Auth\User as Authenticatable;
  10. use Illuminate\Notifications\Notifiable;
  11. use Illuminate\Support\Facades\DB;
  12. class User extends Authenticatable implements MustVerifyEmail
  13. {
  14. use HasFactory, Notifiable, SoftDeletes;
  15. const DEFAULT_SORT_BY = 'created_at';
  16. /**
  17. * The attributes that are mass assignable.
  18. *
  19. * @var list<string>
  20. */
  21. protected $fillable = [
  22. 'name',
  23. 'email',
  24. 'notification_email',
  25. 'phone',
  26. 'password',
  27. 'role',
  28. 'role_id',
  29. 'color',
  30. 'token_fcm',
  31. ];
  32. /**
  33. * The attributes that should be hidden for serialization.
  34. *
  35. * @var list<string>
  36. */
  37. protected $hidden = [
  38. 'password',
  39. 'remember_token',
  40. ];
  41. /**
  42. * Get the attributes that should be cast.
  43. *
  44. * @return array<string, string>
  45. */
  46. protected function casts(): array
  47. {
  48. return [
  49. 'email_verified_at' => 'datetime',
  50. 'password' => 'hashed',
  51. ];
  52. }
  53. /**
  54. * Route notifications for the FCM channel.
  55. *
  56. * @return string
  57. */
  58. public function routeNotificationForFcm(): string
  59. {
  60. return (string)$this->token_fcm;
  61. }
  62. public function getAppInstalledAttribute(): string
  63. {
  64. return $this->token_fcm ? 'Да' : 'Нет';
  65. }
  66. public function userNotifications(): HasMany
  67. {
  68. return $this->hasMany(UserNotification::class);
  69. }
  70. public function unreadUserNotifications(): HasMany
  71. {
  72. return $this->userNotifications()->whereNull('read_at');
  73. }
  74. public function roleModel(): BelongsTo
  75. {
  76. return $this->belongsTo(Role::class, 'role_id');
  77. }
  78. public function permissions(): BelongsToMany
  79. {
  80. return $this->belongsToMany(Permission::class, 'user_permissions')
  81. ->withPivot(['effect', 'reason', 'expires_at'])
  82. ->withTimestamps();
  83. }
  84. public function hasRole(string|array $roles): bool
  85. {
  86. $roles = is_array($roles) ? $roles : explode(',', $roles);
  87. $roles = array_map('trim', $roles);
  88. $role = $this->resolvedRoleSlug();
  89. if (!$role) {
  90. return false;
  91. }
  92. return count(array_intersect($roles, Role::effectiveRoles($role))) > 0;
  93. }
  94. public function hasPermission(string $permission): bool
  95. {
  96. return app(\App\Services\Access\AccessService::class)->can($this, $permission);
  97. }
  98. public function hasAnyPermission(array|string $permissions): bool
  99. {
  100. $permissions = is_array($permissions) ? $permissions : explode(',', $permissions);
  101. return app(\App\Services\Access\AccessService::class)->canAny($this, $permissions);
  102. }
  103. public function canViewField(string $module, string $field, ?string $entity = null): bool
  104. {
  105. return app(\App\Services\Access\AccessService::class)->canViewField($this, $module, $field, $entity);
  106. }
  107. public function canUpdateField(string $module, string $field, ?string $entity = null): bool
  108. {
  109. return app(\App\Services\Access\AccessService::class)->canUpdateField($this, $module, $field, $entity);
  110. }
  111. public function getEffectivePermissions(): \Illuminate\Support\Collection
  112. {
  113. return app(\App\Services\Access\AccessService::class)->getEffectivePermissions($this);
  114. }
  115. public function resolvedRoleSlug(): ?string
  116. {
  117. if ($this->getAttribute('role_id')) {
  118. $role = $this->relationLoaded('roleModel')
  119. ? $this->roleModel
  120. : $this->roleModel()->first();
  121. if ($role) {
  122. return $role->slug;
  123. }
  124. }
  125. return $this->role;
  126. }
  127. public static function assignUniqueFcmToken(int $userId, string $token): void
  128. {
  129. DB::transaction(function () use ($userId, $token) {
  130. self::query()
  131. ->where('id', '!=', $userId)
  132. ->where('token_fcm', $token)
  133. ->update(['token_fcm' => null]);
  134. self::query()
  135. ->where('id', $userId)
  136. ->update(['token_fcm' => $token]);
  137. });
  138. }
  139. public static function clearFcmToken(int $userId): void
  140. {
  141. self::query()
  142. ->where('id', $userId)
  143. ->update(['token_fcm' => null]);
  144. }
  145. }