Admin.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. /*
  3. * This file is part of the Jiannei/lumen-api-starter.
  4. *
  5. * (c) Jiannei <longjian.huang@foxmail.com>
  6. *
  7. * This source file is subject to the MIT license that is bundled
  8. * with this source code in the file LICENSE.
  9. */
  10. namespace App\Repositories\Models\Base;
  11. use App\Repositories\Models\Model;
  12. use Database\Factories\UserFactory;
  13. use Illuminate\Auth\Authenticatable;
  14. use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
  15. use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
  16. use Illuminate\Database\Eloquent\Factories\HasFactory;
  17. use Illuminate\Support\Facades\DB;
  18. use Laravel\Lumen\Auth\Authorizable;
  19. use Spatie\Permission\Traits\HasRoles;
  20. use Tymon\JWTAuth\Contracts\JWTSubject;
  21. class Admin extends Model implements AuthenticatableContract, AuthorizableContract, JWTSubject
  22. {
  23. protected $table = 'base_admins';
  24. use Authenticatable, Authorizable, HasFactory, HasRoles;
  25. /**
  26. * The attributes that are mass assignable.
  27. *
  28. * @var array
  29. */
  30. protected $guarded = [];
  31. /**
  32. * The attributes excluded from the model's JSON form.
  33. *
  34. * @var array
  35. */
  36. protected $hidden = [
  37. 'password',
  38. ];
  39. protected $casts = [
  40. 'extra_fields' => 'json'
  41. ];
  42. /**
  43. * 兼容 Laravel 8 的 Factory.
  44. *
  45. * @return UserFactory
  46. */
  47. protected static function newFactory()
  48. {
  49. return UserFactory::new();
  50. }
  51. /**
  52. * Get the identifier that will be stored in the subject claim of the JWT.
  53. *
  54. * @return mixed
  55. */
  56. public function getJWTIdentifier()
  57. {
  58. return $this->getKey();
  59. }
  60. /**
  61. * Return a key value array, containing any custom claims to be added to the JWT.
  62. *
  63. * @return array
  64. */
  65. public function getJWTCustomClaims()
  66. {
  67. return ['role' => 'admin'];
  68. }
  69. public function getRolesNameAttribute()
  70. {
  71. return DB::table(config('permission.table_names.roles'))->select(['id', 'name', 'nickname'])->whereIn('id',
  72. DB::table(config('permission.table_names.model_has_roles'))->where('model_id', $this->attributes['id'])->pluck('role_id')
  73. )->get();
  74. }
  75. public function department()
  76. {
  77. return $this->belongsTo(Department::class)->select(['id', 'name'])->withDefault(['id' => 0, 'name' => '--']);
  78. }
  79. public function getHeadimgAttribute($val)
  80. {
  81. if (empty($val)) {
  82. return path_to_url('default/headImg.jpeg');
  83. }
  84. return path_to_url($val);
  85. }
  86. /**
  87. * 是否为超级管理员
  88. * @return bool
  89. */
  90. public function isSuperAdmin()
  91. {
  92. return in_array($this->attributes['id'], config('site.superAdmin_ids', []));
  93. }
  94. /**
  95. * 检查用户名是否存在
  96. * @param $username
  97. * @param $ignore_id
  98. * @return bool
  99. */
  100. public static function checkUsernameIsUnique($username, $ignore_id = 0)
  101. {
  102. return self::query()->where('username', $username)->where('id', '<>', $ignore_id)->exists();
  103. }
  104. }