| 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

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