Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
0.00% covered (danger)
0.00%
0 / 1
40.00% covered (danger)
40.00%
2 / 5
CRAP
25.00% covered (danger)
25.00%
6 / 24
User
0.00% covered (danger)
0.00%
0 / 1
40.00% covered (danger)
40.00%
2 / 5
72.75
25.00% covered (danger)
25.00%
6 / 24
 hasRole
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
1 / 1
 isSuperAdmin
0.00% covered (danger)
0.00%
0 / 1
3.07
80.00% covered (warning)
80.00%
4 / 5
 roles
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
1 / 1
 authorities
0.00% covered (danger)
0.00%
0 / 1
12.00
0.00% covered (danger)
0.00%
0 / 7
 getPermittingOwnerIdsAttribute
0.00% covered (danger)
0.00%
0 / 1
20.00
0.00% covered (danger)
0.00%
0 / 10
<?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;
class User extends Authenticatable
{
    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 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);
    }
}