User.php 4.1 KB

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