User.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?php
  2. namespace App\Models;
  3. use App\Enums\LanguageEnum;
  4. use App\Enums\UserTypeEnum;
  5. use App\Traits\HasPermissions;
  6. use App\ValueObjects\Cpf;
  7. use Illuminate\Database\Eloquent\Factories\HasFactory;
  8. use Illuminate\Foundation\Auth\User as Authenticatable;
  9. use Illuminate\Notifications\Notifiable;
  10. use Laravel\Sanctum\HasApiTokens;
  11. use Illuminate\Database\Eloquent\Relations\BelongsToMany;
  12. use App\Models\Unit;
  13. use Carbon\Carbon;
  14. /**
  15. * @property int $id
  16. * @property string $name
  17. * @property string $email
  18. * @property string $password
  19. * @property string $status
  20. * @property string $user_type
  21. * @property \Illuminate\Support\Carbon|null $created_at
  22. * @property \Illuminate\Support\Carbon|null $updated_at
  23. * @property string|null $deleted_at
  24. * @property \Illuminate\Support\Carbon|null $last_login_at
  25. * @property Cpf|null $cpf
  26. * @property string|null $avatar_url
  27. * @property int|null $state_id
  28. * @property string|null $phone
  29. * @property string $access_scope
  30. * @property int|null $franchisor_user_type_id
  31. * @property LanguageEnum $language
  32. * @property-read \Illuminate\Notifications\DatabaseNotificationCollection<int, \Illuminate\Notifications\DatabaseNotification> $notifications
  33. * @property-read int|null $notifications_count
  34. * @property-read \Kalnoy\Nestedset\Collection<int, \App\Models\Permission> $permissions
  35. * @property-read int|null $permissions_count
  36. * @property-read \App\Models\State|null $state
  37. * @property-read \Illuminate\Database\Eloquent\Collection<int, \Laravel\Sanctum\PersonalAccessToken> $tokens
  38. * @property-read int|null $tokens_count
  39. * @property-read \Illuminate\Database\Eloquent\Collection<int, Unit> $units
  40. * @property-read int|null $units_count
  41. * @property-read \App\Models\UserType|null $userType
  42. * @method static \Database\Factories\UserFactory factory($count = null, $state = [])
  43. * @method static \Illuminate\Database\Eloquent\Builder<static>|User newModelQuery()
  44. * @method static \Illuminate\Database\Eloquent\Builder<static>|User newQuery()
  45. * @method static \Illuminate\Database\Eloquent\Builder<static>|User query()
  46. * @method static \Illuminate\Database\Eloquent\Builder<static>|User whereAccessScope($value)
  47. * @method static \Illuminate\Database\Eloquent\Builder<static>|User whereAvatarUrl($value)
  48. * @method static \Illuminate\Database\Eloquent\Builder<static>|User whereCpf($value)
  49. * @method static \Illuminate\Database\Eloquent\Builder<static>|User whereCreatedAt($value)
  50. * @method static \Illuminate\Database\Eloquent\Builder<static>|User whereDeletedAt($value)
  51. * @method static \Illuminate\Database\Eloquent\Builder<static>|User whereEmail($value)
  52. * @method static \Illuminate\Database\Eloquent\Builder<static>|User whereFranchisorUserTypeId($value)
  53. * @method static \Illuminate\Database\Eloquent\Builder<static>|User whereId($value)
  54. * @method static \Illuminate\Database\Eloquent\Builder<static>|User whereLastLoginAt($value)
  55. * @method static \Illuminate\Database\Eloquent\Builder<static>|User whereName($value)
  56. * @method static \Illuminate\Database\Eloquent\Builder<static>|User wherePassword($value)
  57. * @method static \Illuminate\Database\Eloquent\Builder<static>|User wherePhone($value)
  58. * @method static \Illuminate\Database\Eloquent\Builder<static>|User whereStateId($value)
  59. * @method static \Illuminate\Database\Eloquent\Builder<static>|User whereStatus($value)
  60. * @method static \Illuminate\Database\Eloquent\Builder<static>|User whereUpdatedAt($value)
  61. * @method static \Illuminate\Database\Eloquent\Builder<static>|User whereUserType($value)
  62. * @mixin \Eloquent
  63. */
  64. class User extends Authenticatable
  65. {
  66. use HasFactory, Notifiable, HasApiTokens, HasPermissions;
  67. protected $guarded = ["id"];
  68. /**
  69. * The attributes that should be hidden for serialization.
  70. *
  71. * @var array<int, string>
  72. */
  73. protected $hidden = ["password", "remember_token"];
  74. /**
  75. * Get the attributes that should be cast.
  76. *
  77. * @return array<string, string>
  78. */
  79. protected function casts(): array
  80. {
  81. return [
  82. "email_verified_at" => "datetime",
  83. "last_login_at" => "datetime",
  84. "password" => "hashed",
  85. "cpf" => Cpf::class,
  86. "language" => LanguageEnum::class,
  87. ];
  88. }
  89. public function isAdmin(): bool
  90. {
  91. return $this->activeUserType()?->system_key === 'ADMIN';
  92. }
  93. public function userType(): \Illuminate\Database\Eloquent\Relations\BelongsTo
  94. {
  95. return $this->belongsTo(\App\Models\UserType::class, 'franchisor_user_type_id');
  96. }
  97. public function activeUserType(): ?UserType
  98. {
  99. $unitId = request()->input('active_unit_id');
  100. if (!$unitId && $this->access_scope === 'unit') {
  101. $unitId = $this->units()->value('units.id');
  102. }
  103. if ($unitId) {
  104. return UserType::query()
  105. ->whereHas('unitUsers', fn ($query) => $query
  106. ->where('user_id', $this->id)
  107. ->where('unit_id', $unitId))
  108. ->first();
  109. }
  110. return $this->userType
  111. ?? UserType::where('slug', $this->user_type)->whereNull('unit_id')->first();
  112. }
  113. public function state(): \Illuminate\Database\Eloquent\Relations\BelongsTo
  114. {
  115. return $this->belongsTo(\App\Models\State::class, 'state_id');
  116. }
  117. public function units(): BelongsToMany
  118. {
  119. return $this->belongsToMany(Unit::class, 'unit_users', 'user_id', 'unit_id')
  120. ->withPivot('user_type_id')
  121. ->withTimestamps();
  122. }
  123. /**
  124. * Create a new access token for the user.
  125. */
  126. public function createAccessToken(string $deviceId): string
  127. {
  128. return $this->createToken(
  129. name: "access_token_{$deviceId}",
  130. abilities: ["access"],
  131. expiresAt: Carbon::now()->addMinutes(15),
  132. )->plainTextToken;
  133. }
  134. /**
  135. * Create a new refresh token for the user.
  136. */
  137. public function createRefreshToken(string $deviceId): string
  138. {
  139. return $this->createToken(
  140. name: "refresh_token_{$deviceId}",
  141. abilities: ["refresh"],
  142. expiresAt: Carbon::now()->addDays(30),
  143. )->plainTextToken;
  144. }
  145. /**
  146. * Delete all tokens (access and refresh) for a specific device.
  147. */
  148. public function deleteTokensByDevice(string $deviceId): void
  149. {
  150. $this->tokens()
  151. ->where("name", "like", "%_{$deviceId}")
  152. ->delete();
  153. }
  154. /**
  155. * @return BelongsToMany
  156. */
  157. public function permissions(): BelongsToMany
  158. {
  159. return $this->belongsToMany(
  160. Permission::class,
  161. "user_type_permissions",
  162. "user_type",
  163. "permission_id",
  164. );
  165. }
  166. }