User.php 7.1 KB

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