User.php 6.5 KB

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