User.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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\Common\Repository\Options\Modules;
  8. use Modules\User\Models\Traits\UserRelations;
  9. use Illuminate\Auth\Authenticatable;
  10. /**
  11. * @property int $id
  12. * @property string $username
  13. * @property string $email
  14. * @property string $avatar
  15. * @property string $password
  16. * @property int $creator_id
  17. * @property int $status
  18. * @property string $login_ip
  19. * @property int $login_at
  20. * @property int $created_at
  21. * @property int $updated_at
  22. * @property string $remember_token
  23. */
  24. class User extends Model implements AuthenticatableContract
  25. {
  26. use Authenticatable, UserRelations, HasApiTokens;
  27. protected $fillable = [
  28. 'id', 'username', 'email', 'avatar', 'password', 'remember_token', 'creator_id', 'status', 'department_id', 'login_ip', 'login_at', 'created_at', 'updated_at', 'deleted_at'
  29. ];
  30. /**
  31. * @var array|string[]
  32. */
  33. public array $searchable = [
  34. 'username' => 'like',
  35. 'email' => 'like',
  36. 'status' => '='
  37. ];
  38. /**
  39. * @var string
  40. */
  41. protected $table = 'users';
  42. protected array $fields = ['id', 'username', 'email', 'avatar', 'creator_id', 'status', 'department_id', 'created_at'];
  43. /**
  44. * @var array|string[]
  45. */
  46. protected array $form = ['username', 'email', 'password', 'department_id'];
  47. /**
  48. * @var array|string[]
  49. */
  50. protected array $formRelations = ['roles', 'jobs'];
  51. /**
  52. * password
  53. *
  54. * @return Attribute
  55. */
  56. protected function password(): Attribute
  57. {
  58. return new Attribute(
  59. // get: fn($value) => '',
  60. set: fn ($value) => bcrypt($value),
  61. );
  62. }
  63. /**
  64. * is super admin
  65. *
  66. * @return bool
  67. */
  68. public function isSuperAdmin(): bool
  69. {
  70. return $this->{$this->primaryKey} == config('catch.super_admin');
  71. }
  72. /**
  73. * update
  74. * @param $id
  75. * @param array $data
  76. * @return mixed
  77. */
  78. public function updateBy($id, array $data): mixed
  79. {
  80. if (isset($data['password'])) {
  81. if (empty($data['password'])) {
  82. if (isset($data['password'])) {
  83. unset($data['password']);
  84. }
  85. } else {
  86. $data['password'] = bcrypt($data['password']);
  87. }
  88. } else{
  89. if (isset($data['password'])){
  90. unset($data['password']);
  91. }
  92. }
  93. return parent::updateBy($id, $data);
  94. }
  95. /**
  96. * 如果$showApp为空那么就不展示所有isApp为true的模块
  97. * 否则展示所有isApp为false以及showApp对应的name的模块
  98. * @param string $showApp
  99. */
  100. public function showPermissions($showApp = '') {
  101. $hiddenAppModuleNames = Modules::getAppModules()->reject(function ($module) use ($showApp) {
  102. return $showApp == $module['name'];
  103. })->pluck('name');
  104. $enablePermissions = $this->getAttribute('permissions')->reject(function ($permission) use ($hiddenAppModuleNames){
  105. return $hiddenAppModuleNames->contains($permission['module']);
  106. });
  107. $this->setAttribute('permissions', $enablePermissions);
  108. }
  109. }