| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293 |
- <?php
- namespace App;
- use Illuminate\Notifications\Notifiable;
- use Illuminate\Contracts\Auth\MustVerifyEmail;
- use Illuminate\Foundation\Auth\User as Authenticatable;
- use Illuminate\Support\Collection;
- use Illuminate\Support\Facades\Auth;
- use Illuminate\Support\Facades\Gate;
- use Illuminate\Database\Eloquent\SoftDeletes;
- class User extends Authenticatable
- {
- use SoftDeletes;
- use Notifiable;
- /**
- * The attributes that are mass assignable.
- *
- * @var array
- */
- protected $fillable = [
- 'name', 'email', 'password', 'api_token'
- ];
- /**
- * The attributes that should be hidden for arrays.
- *
- * @var array
- */
- protected $hidden = [
- 'password', 'remember_token',
- ];
- /**
- * The attributes that should be cast to native types.
- *
- * @var array
- */
- protected $casts = [
- 'email_verified_at' => 'datetime',
- ];
- function hasRole($roles){
- return !!$roles->intersect($this->roles()->get())->count();
- }
- function isSuperAdmin(){
- $superAdmins=config("users.superAdmin");
- foreach ($superAdmins as $superAdmin){
- if($this['name']==$superAdmin){
- return true;
- }
- }
- return false;
- }
- function roles(){
- return $this->belongsToMany('App\Role','user_role','id_user','id_role');
- }
- function carriers(){
- return $this->belongsToMany('App\Carrier','carrier_user','user_id','carrier_id');
- }
- function authorities(){
- $authorities = new Collection([]);
- $this->roles()->each(function ($role)use(&$authorities){
- if($role->authorities()->get()->isNotEmpty()){
- if(!$authorities){
- $authorities=$role->authorities()->get();
- }else{
- $authorities=$authorities->merge($role->authorities()->get());
- }
- }
- });
- return $authorities;
- }
- function getPermittingOwnerIdsAttribute(){
- $ownerIds=[];
- if($this->isSuperAdmin()||Gate::allows('货主-可见全部')){
- $owners=Owner::all();
- $owners->each(function(Owner $owner)use(&$ownerIds){
- array_push($ownerIds,$owner['id']);
- });
- return $ownerIds;
- }
- $this->authorities()->each(function(Authority $authority)use(&$ownerIds){
- $ownerId=$authority->getOwnerIdAttribute();
- if($ownerId){array_push($ownerIds,$ownerId);}
- });
- return array_unique($ownerIds);
- }
- }
|