User.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. namespace Modules\User\Models;
  3. use Catch\Base\CatchModel as Model;
  4. use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
  5. use Illuminate\Database\Eloquent\Casts\Attribute;
  6. use Laravel\Sanctum\HasApiTokens;
  7. use Modules\Channel\Exceptions\ChannelBusinessException;
  8. use Modules\Common\Errors\Errors;
  9. use Modules\Common\Repository\Options\Modules;
  10. use Modules\User\Models\Traits\UserRelations;
  11. use Illuminate\Auth\Authenticatable;
  12. /**
  13. * @property int $id
  14. * @property string $username
  15. * @property string $email
  16. * @property string $avatar
  17. * @property string $password
  18. * @property int $creator_id
  19. * @property int $status
  20. * @property string $login_ip
  21. * @property int $login_at
  22. * @property int $created_at
  23. * @property int $updated_at
  24. * @property string $remember_token
  25. */
  26. class User extends Model implements AuthenticatableContract
  27. {
  28. use Authenticatable, UserRelations, HasApiTokens;
  29. protected $fillable = [
  30. 'id', 'username', 'email', 'avatar', 'password', 'remember_token', 'creator_id',
  31. 'status', 'department_id', 'login_ip', 'login_at', 'created_at', 'updated_at', 'deleted_at',
  32. 'remark'
  33. ];
  34. /**
  35. * @var array|string[]
  36. */
  37. public array $searchable = [
  38. 'username' => 'like',
  39. 'email' => 'like',
  40. 'status' => '='
  41. ];
  42. /**
  43. * @var string
  44. */
  45. protected $table = 'users';
  46. protected array $fields = ['id', 'username', 'email', 'avatar', 'creator_id', 'status', 'department_id', 'created_at'];
  47. /**
  48. * @var array|string[]
  49. */
  50. protected array $form = ['username', 'email', 'password', 'department_id'];
  51. /**
  52. * @var array|string[]
  53. */
  54. protected array $formRelations = ['roles', 'jobs'];
  55. /**
  56. * password
  57. *
  58. * @return Attribute
  59. */
  60. protected function password(): Attribute
  61. {
  62. return new Attribute(
  63. // get: fn($value) => '',
  64. set: fn ($value) => bcrypt($value),
  65. );
  66. }
  67. /**
  68. * is super admin
  69. *
  70. * @return bool
  71. */
  72. public function isSuperAdmin(): bool
  73. {
  74. return $this->{$this->primaryKey} == config('catch.super_admin');
  75. }
  76. /**
  77. * update
  78. * @param $id
  79. * @param array $data
  80. * @return mixed
  81. */
  82. public function updateBy($id, array $data): mixed
  83. {
  84. if (isset($data['password'])) {
  85. if (empty($data['password'])) {
  86. if (isset($data['password'])) {
  87. unset($data['password']);
  88. }
  89. } else {
  90. $data['password'] = bcrypt($data['password']);
  91. }
  92. } else{
  93. if (isset($data['password'])){
  94. unset($data['password']);
  95. }
  96. }
  97. return parent::updateBy($id, $data);
  98. }
  99. /**
  100. * 如果$showApp为空那么就不展示所有isApp为true的模块
  101. * 否则展示所有isApp为false以及showApp对应的name的模块
  102. * @param string $showApp
  103. */
  104. public function showPermissions($showApp = '') {
  105. $hiddenAppModuleNames = Modules::getAppModules()->reject(function ($module) use ($showApp) {
  106. return $showApp == $module['name'];
  107. })->pluck('name');
  108. $enablePermissions = $this->getAttribute('permissions')->reject(function ($permission) use ($hiddenAppModuleNames){
  109. return $hiddenAppModuleNames->contains($permission['module']);
  110. })->values()->all();
  111. $this->setAttribute('permissions', $enablePermissions);
  112. }
  113. /**
  114. * 保证邮箱没有被使用
  115. * @param $email
  116. */
  117. public function emailUnique($email) {
  118. $user = $this->where([
  119. 'email' => $email,
  120. 'deleted_at' => 0,
  121. ])->first();
  122. if($user) {
  123. ChannelBusinessException::throwError(Errors::EMAIL_EXISTS);
  124. }
  125. }
  126. }