User.php 4.3 KB

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