User.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. namespace App;
  3. use App\Traits\ModelLogChanging;
  4. use Illuminate\Notifications\Notifiable;
  5. use Illuminate\Foundation\Auth\User as Authenticatable;
  6. use Illuminate\Support\Collection;
  7. use Illuminate\Support\Facades\Cache;
  8. use Illuminate\Support\Facades\Gate;
  9. use App\Traits\ModelTimeFormat;
  10. use Tymon\JWTAuth\Contracts\JWTSubject;
  11. //class User extends Authenticatable implements JWTSubject
  12. class User extends Authenticatable
  13. {
  14. use ModelLogChanging;
  15. use ModelTimeFormat;
  16. use Notifiable;
  17. /**
  18. * The attributes that are mass assignable.
  19. *
  20. * @var array
  21. */
  22. protected $fillable = [
  23. 'name', 'email', 'password'
  24. ];
  25. /**
  26. * The attributes that should be hidden for arrays.
  27. *
  28. * @var array
  29. */
  30. protected $hidden = [
  31. 'password', 'remember_token',
  32. ];
  33. /**
  34. * The attributes that should be cast to native types.
  35. *
  36. * @var array
  37. */
  38. protected $casts = [
  39. 'email_verified_at' => 'datetime',
  40. ];
  41. // function hasRole($roles){
  42. // return !!$roles->intersect($this->roles()->get())->count();
  43. // }
  44. function isSuperAdmin(){
  45. $superAdmins=config("users.superAdmin");
  46. foreach ($superAdmins as $superAdmin){
  47. if($this['name']==$superAdmin){
  48. return true;
  49. }
  50. }
  51. return false;
  52. }
  53. /**
  54. * @param null $expireTime 手动设置过期时间则覆盖其中默认时间,分钟为单位
  55. * @return mixed|string
  56. */
  57. public function token($expireTime=null){
  58. if(!$expireTime){
  59. $expireTime=config('users.token_expire_minutes');
  60. }
  61. $token=Cache::get('tokenUser_'.$this['id']);
  62. if($token){
  63. Cache::put('tokenUser_'.$this['id'],$token,$expireTime);
  64. Cache::put('tokenStr_'.$token,$this['id'],$expireTime);
  65. return $token;
  66. }
  67. $token=md5(rand(1,intval(microtime(true)*10000)).'baoshi');
  68. Cache::put('tokenUser_'.$this['id'],$token,$expireTime);
  69. Cache::put('tokenStr_'.$token,$this['id'],$expireTime);
  70. return $token;
  71. }
  72. public function touchToken(){
  73. return $this->token();
  74. }
  75. function roles(){
  76. return $this->belongsToMany('App\Role','user_role','id_user','id_role');
  77. }
  78. function logistics(){
  79. return $this->belongsToMany('App\Logistic','logistic_user','user_id','logistic_id');
  80. }
  81. function userWorkgroups(){
  82. return $this->belongsToMany('App\UserWorkgroup','user_workgroup_user','user_id','user_workgroup_id');
  83. }
  84. function authorities(){
  85. $authorities = new Collection([]);
  86. $user=User::query()->where('id',$this['id'])->with('roles.authorities')->first();
  87. $user->roles->each(function ($role)use(&$authorities){
  88. if($role->authorities){
  89. if(!$authorities){
  90. $authorities=$role->authorities;
  91. }else{
  92. $authorities=$authorities->merge($role->authorities);
  93. }
  94. }
  95. });
  96. return $authorities;
  97. }
  98. function getPermittingOwnerIdsAttribute(){
  99. $ownerIds=[];
  100. if($this->isSuperAdmin()||Gate::allows('货主-可见全部')){
  101. $owners=Owner::query()->whereNull('deleted_at')->get();
  102. $owners->each(function(Owner $owner)use(&$ownerIds){
  103. array_push($ownerIds,$owner['id']);
  104. });
  105. return $ownerIds;
  106. }
  107. $this->authorities()->each(function(Authority $authority)use(&$ownerIds){
  108. $ownerId=$authority->getOwnerIdAttribute();
  109. if($ownerId){array_push($ownerIds,$ownerId);}
  110. });
  111. return array_unique($ownerIds);
  112. }
  113. function getPermittingWorkgroupIds($allowAll=false){
  114. $workgroupIds=[];
  115. if ($this->isSuperAdmin()||$allowAll){
  116. $workgroups=UserWorkgroup::all();
  117. $workgroups->each(function (UserWorkgroup $workgroup)use(&$workgroupIds){
  118. array_push($workgroupIds,$workgroup['id']);
  119. });
  120. }else{
  121. $this->authorities()->each(function(Authority $authority)use(&$workgroupIds){
  122. if($authority->type=="工作组"){array_push($workgroupIds,$authority->relevance);}
  123. });
  124. }
  125. return $workgroupIds;
  126. }
  127. // //jwt
  128. // public function getJWTIdentifier()
  129. // {
  130. // return $this->getKey();
  131. // }
  132. //
  133. // public function getJWTCustomClaims()
  134. // {
  135. // return [];
  136. // }
  137. }