User.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. namespace App;
  3. use App\Traits\ModelLogChanging;
  4. use Illuminate\Database\Eloquent\SoftDeletes;
  5. use Illuminate\Notifications\Notifiable;
  6. use Illuminate\Foundation\Auth\User as Authenticatable;
  7. use Illuminate\Support\Collection;
  8. use Illuminate\Support\Facades\Cache;
  9. use Illuminate\Support\Facades\Gate;
  10. use App\Traits\ModelTimeFormat;
  11. class User extends Authenticatable
  12. {
  13. use ModelLogChanging;
  14. use ModelTimeFormat;
  15. use Notifiable;
  16. use SoftDeletes;
  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 userDetail(){
  79. return $this->hasOne('App\UserDetail','user_id','id');
  80. }
  81. function logistics(){
  82. return $this->belongsToMany('App\Logistic','logistic_user','user_id','logistic_id');
  83. }
  84. function userWorkgroups(){
  85. return $this->belongsToMany('App\UserWorkgroup','user_workgroup_user','user_id','user_workgroup_id');
  86. }
  87. function authorities(){
  88. $authorities = new Collection([]);
  89. $user=User::query()->where('id',$this['id'])->with('roles.authorities')->first();
  90. $user->roles->each(function ($role)use(&$authorities){
  91. if($role->authorities){
  92. if(!$authorities){
  93. $authorities=$role->authorities;
  94. }else{
  95. $authorities=$authorities->merge($role->authorities);
  96. }
  97. }
  98. });
  99. return $authorities;
  100. }
  101. function getPermittingOwnerIdsAttribute(){
  102. $ownerIds=[];
  103. if($this->isSuperAdmin()||Gate::allows('货主-可见全部')){
  104. $owners=Owner::query()->whereNull('deleted_at')->get();
  105. $owners->each(function(Owner $owner)use(&$ownerIds){
  106. array_push($ownerIds,$owner['id']);
  107. });
  108. return $ownerIds;
  109. }
  110. $this->authorities()->each(function(Authority $authority)use(&$ownerIds){
  111. $ownerId=$authority->getOwnerIdAttribute();
  112. if($ownerId){array_push($ownerIds,$ownerId);}
  113. });
  114. return array_unique($ownerIds);
  115. }
  116. function getPermittingWorkgroupIds($allowAll=false){
  117. $workgroupIds=[];
  118. if ($this->isSuperAdmin()||$allowAll){
  119. $workgroups=UserWorkgroup::all();
  120. $workgroups->each(function (UserWorkgroup $workgroup)use(&$workgroupIds){
  121. array_push($workgroupIds,$workgroup['id']);
  122. });
  123. }else{
  124. $this->authorities()->each(function(Authority $authority)use(&$workgroupIds){
  125. if($authority->type=="工作组"){array_push($workgroupIds,$authority->relevance);}
  126. });
  127. }
  128. return $workgroupIds;
  129. }
  130. }