User.php 3.4 KB

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