User.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. public function token(){
  52. $token=Cache::get('tokenUser_'.$this['id']);
  53. if($token){
  54. Cache::put('tokenUser_'.$this['id'],$token,config('users.token_expire_seconds'));
  55. Cache::put('tokenStr_'.$token,$this['id'],config('users.token_expire_seconds'));
  56. return $token;
  57. }
  58. $token=md5(rand(1,intval(microtime(true)*10000)).'baoshi');
  59. Cache::put('tokenUser_'.$this['id'],$token,config('users.token_expire_seconds'));
  60. Cache::put('tokenStr_'.$token,$this['id'],config('users.token_expire_seconds'));
  61. return $token;
  62. }
  63. public function touchToken(){
  64. return $this->token();
  65. }
  66. function roles(){
  67. return $this->belongsToMany('App\Role','user_role','id_user','id_role');
  68. }
  69. function carriers(){
  70. return $this->belongsToMany('App\Carrier','carrier_user','user_id','carrier_id');
  71. }
  72. function authorities(){
  73. $authorities = new Collection([]);
  74. $this->roles()->each(function ($role)use(&$authorities){
  75. if($role->authorities()->get()->isNotEmpty()){
  76. if(!$authorities){
  77. $authorities=$role->authorities()->get();
  78. }else{
  79. $authorities=$authorities->merge($role->authorities()->get());
  80. }
  81. }
  82. });
  83. return $authorities;
  84. }
  85. function getPermittingOwnerIdsAttribute(){
  86. $ownerIds=[];
  87. if($this->isSuperAdmin()||Gate::allows('货主-可见全部')){
  88. $owners=Owner::all();
  89. $owners->each(function(Owner $owner)use(&$ownerIds){
  90. array_push($ownerIds,$owner['id']);
  91. });
  92. return $ownerIds;
  93. }
  94. $this->authorities()->each(function(Authority $authority)use(&$ownerIds){
  95. $ownerId=$authority->getOwnerIdAttribute();
  96. if($ownerId){array_push($ownerIds,$ownerId);}
  97. });
  98. return array_unique($ownerIds);
  99. }
  100. }