User::permissions()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 16
ccs 0
cts 4
cp 0
crap 2
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/*
3
 * This file is part of the Laravel Platfourm package.
4
 *
5
 * (c) Avtandil Kikabidze aka LONGMAN <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Longman\Platfourm\User\Models\Eloquent;
12
13
use Illuminate\Auth\Authenticatable;
14
use Illuminate\Auth\Passwords\CanResetPassword;
15
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
16
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
17
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
18
use Longman\Platfourm\Auth\Traits\LoginAsPossibility;
19
use Longman\Platfourm\Database\Eloquent\ActionLog\ActionLogTrait;
20
use Longman\Platfourm\Database\Eloquent\EntityLock\EntityLockTrait;
21
use Longman\Platfourm\Database\Eloquent\Model;
22
use Longman\Platfourm\Database\Eloquent\Traits\SoftDeletes;
23
use Longman\Platfourm\Database\Eloquent\Traits\UuidForPrimary;
24
use Longman\Platfourm\User\Models\Eloquent\EntrustUserTrait;
25
26
class User extends Model implements AuthenticatableContract, AuthorizableContract, CanResetPasswordContract
27
{
28
    use Authenticatable, CanResetPassword, SoftDeletes, UuidForPrimary,
29
        ActionLogTrait, EntityLockTrait, LoginAsPossibility;
30
31
    /**
32
     * The database table used by the model.
33
     *
34
     * @var string
35
     */
36
    protected $table = 'users';
37
38
    /**
39
     * The attributes that are mass assignable.
40
     *
41
     * @var array
42
     */
43
    protected $fillable = [
44
        'email',
45
        'password',
46
        'firstname',
47
        'avatar',
48
        'lastname',
49
        'role_id',
50
        'is_developer',
51
        'country_id',
52
        'mobile_number',
53
        'gender',
54
        'birth_date',
55
        'address',
56
        'activate_token',
57
        'status',
58
    ];
59
60
    protected $dates = ['deleted_at', 'birth_date'];
61
62
    /**
63
     * The attributes excluded from the model's JSON form.
64
     *
65
     * @var array
66
     */
67
    protected $hidden = ['password', 'remember_token'];
68
69
    public $incrementing = false;
70
71
    protected $keyType = 'string';
72
73
    protected $searchableFields = ['email', 'firstname', 'lastname'];
74
75
    protected $filterableFields = ['status' => '='];
76
77
    protected $sortableFields = ['email', 'firstname', 'lastname', 'status', 'created_at', 'updated_at'];
78
79
    public function isDeveloper()
80
    {
81
        return (bool)$this->getAttribute('is_developer');
82
    }
83
84
    public function getAvatar()
85
    {
86
        $avatar = $this->getAttribute('avatar');
87
        if (!empty($avatar)) {
88
            $path = config('cms.user.avatar.path', 'cache/avatar');
89
            return '/' . $path . '/' . $avatar;
90
        }
91
92
        $default_path = config('cms.user.avatar.default');
93
        if (empty($default_path)) {
94
            return null;
95
        }
96
97
        return '/' . $default_path;
98
    }
99
100
    public function getFullname()
101
    {
102
        return $this->getAttribute('firstname') . ' ' . $this->getAttribute('lastname');
103
    }
104
105
    public function canLogin()
106
    {
107
        return $this->getAttribute('status') == 1;
108
    }
109
110
    public function role()
111
    {
112
        $model = Role::class;
113
        if (class_exists(\App\Models\Role::class)) {
114
            $model = \App\Models\Role::class;
115
        }
116
        return $this->belongsTo($model);
117
    }
118
119
    public function permissions()
120
    {
121
        return $this->role->permissions;
0 ignored issues
show
Documentation introduced by
The property role does not exist on object<Longman\Platfourm...r\Models\Eloquent\User>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
122
123
        /*$roleModel = Role::class;
0 ignored issues
show
Unused Code Comprehensibility introduced by
47% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
124
        if (class_exists(\App\Models\Role::class)) {
125
            $roleModel = \App\Models\Role::class;
126
        }
127
128
        $permissionModel = Permission::class;
129
        if (class_exists(\App\Models\Permission::class)) {
130
            $permissionModel = \App\Models\Permission::class;
131
        }
132
133
        return $this->hasManyThrough($roleModel, $permissionModel);*/
134
    }
135
136 View Code Duplication
    public function hasRole($name, $requireAll = false)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
137
    {
138
        if (is_array($name)) {
139
            foreach ($name as $roleName) {
140
                $hasRole = $this->hasRole($roleName);
141
142
                if ($hasRole && !$requireAll) {
143
                    return true;
144
                } elseif (!$hasRole && $requireAll) {
145
                    return false;
146
                }
147
            }
148
            return $requireAll;
149
        } else {
150
            return $this->role->name == $name;
0 ignored issues
show
Documentation introduced by
The property role does not exist on object<Longman\Platfourm...r\Models\Eloquent\User>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
151
        }
152
153
        return false;
0 ignored issues
show
Unused Code introduced by
return false; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
154
    }
155
156 View Code Duplication
    public function can($permission, $requireAll = false)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
157
    {
158
        if (is_array($permission)) {
159
            foreach ($permission as $permName) {
160
                $hasPerm = $this->can($permName);
161
162
                if ($hasPerm && !$requireAll) {
163
                    return true;
164
                } elseif (!$hasPerm && $requireAll) {
165
                    return false;
166
                }
167
            }
168
169
            // If we've made it this far and $requireAll is FALSE, then NONE of the perms were found
170
            // If we've made it this far and $requireAll is TRUE, then ALL of the perms were found.
171
            // Return the value of $requireAll;
172
            return $requireAll;
173
        } else {
174
            // Validate against the Permission table
175
            foreach ($this->role->cachedPermissions() as $perm) {
0 ignored issues
show
Documentation introduced by
The property role does not exist on object<Longman\Platfourm...r\Models\Eloquent\User>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
176
                if (str_is($permission, $perm->name)) {
177
                    return true;
178
                }
179
            }
180
        }
181
182
        return false;
183
    }
184
185
    public function toArray()
186
    {
187
        $this->permissions = $this->role->permissions()->get(['id', 'name'])->toArray();
0 ignored issues
show
Documentation introduced by
The property permissions does not exist on object<Longman\Platfourm...r\Models\Eloquent\User>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
Documentation introduced by
The property role does not exist on object<Longman\Platfourm...r\Models\Eloquent\User>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
188
189
        $array = parent::toArray();
190
191
        if (property_exists($this, 'loginasData')) {
192
            $array['loginasData'] = $this->getLoginAsData();
193
        }
194
195
        $array['avatar'] = $this->getAvatar();
196
197
        return $array;
198
    }
199
200
    public function country()
201
    {
202
        return $this->hasOne('App\Models\Country');
203
    }
204
205
    public function accessLog()
206
    {
207
        return $this->hasMany('App\Models\AccessLog');
208
    }
209
210
}
211