User.php 4.4 KB

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