User.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. //
  83. public function isAdmin(): bool
  84. {
  85. return $this->type === UserTypeEnum::ADMIN;
  86. }
  87. public function canModerateProviders(): bool
  88. {
  89. return $this->isAdmin();
  90. }
  91. public function canModerateClients(): bool
  92. {
  93. return $this->isAdmin();
  94. }
  95. //
  96. public function client()
  97. {
  98. return $this->hasOne(Client::class, 'user_id');
  99. }
  100. public function provider()
  101. {
  102. return $this->hasOne(Provider::class, 'user_id');
  103. }
  104. //
  105. public function deviceTokens()
  106. {
  107. return $this->hasMany(DeviceToken::class);
  108. }
  109. public function pushNotificationLogs()
  110. {
  111. return $this->hasMany(PushNotificationLog::class);
  112. }
  113. //
  114. // Create a new access token for the user.
  115. public function createAccessToken(string $deviceId): string
  116. {
  117. return $this->createToken(
  118. name: "access_token_{$deviceId}",
  119. abilities: ['access'],
  120. expiresAt: Carbon::now()->addMinutes(15),
  121. )->plainTextToken;
  122. }
  123. // Create a new refresh token for the user.
  124. public function createRefreshToken(string $deviceId): string
  125. {
  126. return $this->createToken(
  127. name: "refresh_token_{$deviceId}",
  128. abilities: ['refresh'],
  129. expiresAt: Carbon::now()->addDays(30),
  130. )->plainTextToken;
  131. }
  132. // Delete all tokens (access and refresh) for a specific device.
  133. public function deleteTokensByDevice(string $deviceId): void
  134. {
  135. $this->tokens()
  136. ->where('name', 'like', "%_{$deviceId}")
  137. ->delete();
  138. }
  139. public function permissions(): BelongsToMany
  140. {
  141. return $this->belongsToMany(
  142. Permission::class,
  143. 'user_type_permissions',
  144. 'user_type',
  145. 'permission_id',
  146. );
  147. }
  148. // Create a new access token for the user in the app.
  149. public function createAccessTokenApp(string $deviceId): string
  150. {
  151. return $this->createToken(
  152. name: "access_token_{$deviceId}",
  153. abilities: ['access'],
  154. expiresAt: Carbon::now()->addCentury(),
  155. )->plainTextToken;
  156. }
  157. // Create a new refresh token for the user in the app.
  158. public function createRefreshTokenApp(string $deviceId): string
  159. {
  160. return $this->createToken(
  161. name: "refresh_token_{$deviceId}",
  162. abilities: ['refresh'],
  163. expiresAt: Carbon::now()->addCentury(),
  164. )->plainTextToken;
  165. }
  166. }