User.php 6.4 KB

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