User.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. namespace App\Models;
  3. use App\Enums\LanguageEnum;
  4. use App\Enums\UserTypeEnum;
  5. use Illuminate\Database\Eloquent\Factories\HasFactory;
  6. use Illuminate\Foundation\Auth\User as Authenticatable;
  7. use Illuminate\Notifications\Notifiable;
  8. use Laravel\Sanctum\HasApiTokens;
  9. use Illuminate\Database\Eloquent\Relations\BelongsToMany;
  10. use Carbon\Carbon;
  11. /**
  12. * @property int $id
  13. * @property string $name
  14. * @property string $email
  15. * @property \Illuminate\Support\Carbon|null $email_verified_at
  16. * @property string $password
  17. * @property UserTypeEnum $type
  18. * @property LanguageEnum $language
  19. * @property \Illuminate\Support\Carbon|null $created_at
  20. * @property \Illuminate\Support\Carbon|null $updated_at
  21. * @property-read \Illuminate\Notifications\DatabaseNotificationCollection<int, \Illuminate\Notifications\DatabaseNotification> $notifications
  22. * @property-read int|null $notifications_count
  23. * @property-read \Kalnoy\Nestedset\Collection<int, \App\Models\Permission> $permissions
  24. * @property-read int|null $permissions_count
  25. * @property-read \Illuminate\Database\Eloquent\Collection<int, \Laravel\Sanctum\PersonalAccessToken> $tokens
  26. * @property-read int|null $tokens_count
  27. * @method static \Database\Factories\UserFactory factory($count = null, $state = [])
  28. * @method static \Illuminate\Database\Eloquent\Builder<static>|User newModelQuery()
  29. * @method static \Illuminate\Database\Eloquent\Builder<static>|User newQuery()
  30. * @method static \Illuminate\Database\Eloquent\Builder<static>|User query()
  31. * @method static \Illuminate\Database\Eloquent\Builder<static>|User whereCreatedAt($value)
  32. * @method static \Illuminate\Database\Eloquent\Builder<static>|User whereEmail($value)
  33. * @method static \Illuminate\Database\Eloquent\Builder<static>|User whereEmailVerifiedAt($value)
  34. * @method static \Illuminate\Database\Eloquent\Builder<static>|User whereId($value)
  35. * @method static \Illuminate\Database\Eloquent\Builder<static>|User whereLanguage($value)
  36. * @method static \Illuminate\Database\Eloquent\Builder<static>|User whereName($value)
  37. * @method static \Illuminate\Database\Eloquent\Builder<static>|User wherePassword($value)
  38. * @method static \Illuminate\Database\Eloquent\Builder<static>|User whereType($value)
  39. * @method static \Illuminate\Database\Eloquent\Builder<static>|User whereUpdatedAt($value)
  40. * @mixin \Eloquent
  41. */
  42. class User extends Authenticatable
  43. {
  44. use HasFactory, Notifiable, HasApiTokens;
  45. protected $guarded = ["id"];
  46. /**
  47. * The attributes that should be hidden for serialization.
  48. *
  49. * @var array<int, string>
  50. */
  51. protected $hidden = ["password", "remember_token"];
  52. /**
  53. * Get the attributes that should be cast.
  54. *
  55. * @return array<string, string>
  56. */
  57. protected function casts(): array
  58. {
  59. return [
  60. "email_verified_at" => "datetime",
  61. "password" => "hashed",
  62. "type" => UserTypeEnum::class,
  63. "language" => LanguageEnum::class,
  64. ];
  65. }
  66. public function isAdmin(): bool
  67. {
  68. return $this->type === UserTypeEnum::ADMIN;
  69. }
  70. public function provider()
  71. {
  72. return $this->hasOne(Provider::class, "user_id");
  73. }
  74. public function client()
  75. {
  76. return $this->hasOne(Client::class, "user_id");
  77. }
  78. /**
  79. * Create a new access token for the user.
  80. */
  81. public function createAccessToken(string $deviceId): string
  82. {
  83. return $this->createToken(
  84. name: "access_token_{$deviceId}",
  85. abilities: ["access"],
  86. expiresAt: Carbon::now()->addMinutes(15),
  87. )->plainTextToken;
  88. }
  89. /**
  90. * Create a new refresh token for the user.
  91. */
  92. public function createRefreshToken(string $deviceId): string
  93. {
  94. return $this->createToken(
  95. name: "refresh_token_{$deviceId}",
  96. abilities: ["refresh"],
  97. expiresAt: Carbon::now()->addDays(30),
  98. )->plainTextToken;
  99. }
  100. /**
  101. * Delete all tokens (access and refresh) for a specific device.
  102. */
  103. public function deleteTokensByDevice(string $deviceId): void
  104. {
  105. $this->tokens()
  106. ->where("name", "like", "%_{$deviceId}")
  107. ->delete();
  108. }
  109. /**
  110. * @return BelongsToMany
  111. */
  112. public function permissions(): BelongsToMany
  113. {
  114. return $this->belongsToMany(
  115. Permission::class,
  116. "user_type_permissions",
  117. "user_type",
  118. "permission_id",
  119. );
  120. }
  121. /**
  122. * Create a new access token for the user in the app.
  123. */
  124. public function createAccessTokenApp(string $deviceId): string
  125. {
  126. return $this->createToken(
  127. name: "access_token_{$deviceId}",
  128. abilities: ["access"],
  129. expiresAt: Carbon::now()->addCentury(),
  130. )->plainTextToken;
  131. }
  132. /**
  133. * Create a new refresh token for the user in the app.
  134. */
  135. public function createRefreshTokenApp(string $deviceId): string
  136. {
  137. return $this->createToken(
  138. name: "refresh_token_{$deviceId}",
  139. abilities: ["refresh"],
  140. expiresAt: Carbon::now()->addCentury(),
  141. )->plainTextToken;
  142. }
  143. }