User.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. namespace App;
  3. use Illuminate\Notifications\Notifiable;
  4. use Illuminate\Contracts\Auth\MustVerifyEmail;
  5. use Illuminate\Foundation\Auth\User as Authenticatable;
  6. use Illuminate\Support\Collection;
  7. use Illuminate\Support\Facades\Auth;
  8. use Illuminate\Support\Facades\Gate;
  9. use Illuminate\Database\Eloquent\SoftDeletes;
  10. class User extends Authenticatable
  11. {
  12. use SoftDeletes;
  13. use Notifiable;
  14. /**
  15. * The attributes that are mass assignable.
  16. *
  17. * @var array
  18. */
  19. protected $fillable = [
  20. 'name', 'email', 'password', 'api_token'
  21. ];
  22. /**
  23. * The attributes that should be hidden for arrays.
  24. *
  25. * @var array
  26. */
  27. protected $hidden = [
  28. 'password', 'remember_token',
  29. ];
  30. /**
  31. * The attributes that should be cast to native types.
  32. *
  33. * @var array
  34. */
  35. protected $casts = [
  36. 'email_verified_at' => 'datetime',
  37. ];
  38. function hasRole($roles){
  39. return !!$roles->intersect($this->roles()->get())->count();
  40. }
  41. function isSuperAdmin(){
  42. $superAdmins=config("users.superAdmin");
  43. foreach ($superAdmins as $superAdmin){
  44. if($this['name']==$superAdmin){
  45. return true;
  46. }
  47. }
  48. return false;
  49. }
  50. function roles(){
  51. return $this->belongsToMany('App\Role','user_role','id_user','id_role');
  52. }
  53. function carriers(){
  54. return $this->belongsToMany('App\Carrier','carrier_user','user_id','carrier_id');
  55. }
  56. function authorities(){
  57. $authorities = new Collection([]);
  58. $this->roles()->each(function ($role)use(&$authorities){
  59. if($role->authorities()->get()->isNotEmpty()){
  60. if(!$authorities){
  61. $authorities=$role->authorities()->get();
  62. }else{
  63. $authorities=$authorities->merge($role->authorities()->get());
  64. }
  65. }
  66. });
  67. return $authorities;
  68. }
  69. function getPermittingOwnerIdsAttribute(){
  70. $ownerIds=[];
  71. if($this->isSuperAdmin()||Gate::allows('货主-可见全部')){
  72. $owners=Owner::all();
  73. $owners->each(function(Owner $owner)use(&$ownerIds){
  74. array_push($ownerIds,$owner['id']);
  75. });
  76. return $ownerIds;
  77. }
  78. $this->authorities()->each(function(Authority $authority)use(&$ownerIds){
  79. $ownerId=$authority->getOwnerIdAttribute();
  80. if($ownerId){array_push($ownerIds,$ownerId);}
  81. });
  82. return array_unique($ownerIds);
  83. }
  84. }