'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; } public function token(){ $token=Cache::get('tokenUser_'.$this['id']); if($token){ Cache::put('tokenUser_'.$this['id'],$token,config('users.token_expire_seconds')); Cache::put('tokenStr_'.$token,$this['id'],config('users.token_expire_seconds')); return $token; } $token=md5(rand(1,intval(microtime(true)*10000)).'baoshi'); Cache::put('tokenUser_'.$this['id'],$token,config('users.token_expire_seconds')); Cache::put('tokenStr_'.$token,$this['id'],config('users.token_expire_seconds')); return $token; } public function touchToken(){ return $this->token(); } 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); } }