User.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. /** @var UserService $userService */
  44. $userService = app('UserService');
  45. return !!$roles->intersect($userService->getRoles())->count();
  46. }
  47. function isSuperAdmin(){
  48. $superAdmins=config("users.superAdmin");
  49. foreach ($superAdmins as $superAdmin){
  50. if($this['name']==$superAdmin){
  51. return true;
  52. }
  53. }
  54. return false;
  55. }
  56. /**
  57. * @param null $expireTime 手动设置过期时间则覆盖其中默认时间,分钟为单位
  58. * @return mixed|string
  59. */
  60. public function token($expireTime=null){
  61. if(!$expireTime){
  62. $expireTime=config('users.token_expire_minutes');
  63. }
  64. $token=Cache::get('tokenUser_'.$this['id']);
  65. if($token){
  66. Cache::put('tokenUser_'.$this['id'],$token,$expireTime);
  67. Cache::put('tokenStr_'.$token,$this['id'],$expireTime);
  68. return $token;
  69. }
  70. $token=md5(rand(1,intval(microtime(true)*10000)).'baoshi');
  71. Cache::put('tokenUser_'.$this['id'],$token,$expireTime);
  72. Cache::put('tokenStr_'.$token,$this['id'],$expireTime);
  73. return $token;
  74. }
  75. public function touchToken(){
  76. return $this->token();
  77. }
  78. function roles(){
  79. return $this->belongsToMany('App\Role','user_role','id_user','id_role');
  80. }
  81. function carriers(){
  82. return $this->belongsToMany('App\Carrier','carrier_user','user_id','carrier_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. $this->roles()->each(function ($role)use(&$authorities){
  90. if($role->authorities()->get()->isNotEmpty()){
  91. if(!$authorities){
  92. $authorities=$role->authorities()->get();
  93. }else{
  94. $authorities=$authorities->merge($role->authorities()->get());
  95. }
  96. }
  97. });
  98. return $authorities;
  99. }
  100. function getPermittingOwnerIdsAttribute(){
  101. $ownerIds=[];
  102. if($this->isSuperAdmin()||Gate::allows('货主-可见全部')){
  103. $owners=Owner::query()->whereNull('deleted_at')->get();
  104. $owners->each(function(Owner $owner)use(&$ownerIds){
  105. array_push($ownerIds,$owner['id']);
  106. });
  107. return $ownerIds;
  108. }
  109. $this->authorities()->each(function(Authority $authority)use(&$ownerIds){
  110. $ownerId=$authority->getOwnerIdAttribute();
  111. if($ownerId){array_push($ownerIds,$ownerId);}
  112. });
  113. return array_unique($ownerIds);
  114. }
  115. function getPermittingWorkgroupIds($allowAll=false){
  116. $workgroupIds=[];
  117. if ($this->isSuperAdmin()||$allowAll){
  118. $workgroups=UserWorkgroup::all();
  119. $workgroups->each(function (UserWorkgroup $workgroup)use(&$workgroupIds){
  120. array_push($workgroupIds,$workgroup['id']);
  121. });
  122. }else{
  123. $this->authorities()->each(function(Authority $authority)use(&$workgroupIds){
  124. if($authority->type=="工作组"){array_push($workgroupIds,$authority->relevance);}
  125. });
  126. }
  127. return $workgroupIds;
  128. }
  129. }