Completed
Push — master ( 55ab89...0c5b4e )
by Song
06:39
created

Administrator::visible()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 7
nc 3
nop 1
dl 0
loc 14
rs 9.2
c 0
b 0
f 0
1
<?php
2
3
namespace Encore\Admin\Auth\Database;
4
5
use Illuminate\Auth\Authenticatable;
6
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
7
use Illuminate\Database\Eloquent\Model;
8
9
class Administrator extends Model implements AuthenticatableContract
10
{
11
    use Authenticatable;
12
13
    protected $fillable = ['username', 'password', 'name'];
14
15
    /**
16
     * Create a new Eloquent model instance.
17
     *
18
     * @param array $attributes
19
     */
20
    public function __construct(array $attributes = [])
21
    {
22
        $this->table = config('admin.database.users_table');
23
24
        parent::__construct($attributes);
25
    }
26
27
    /**
28
     * A User belongs to many roles.
29
     *
30
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
31
     */
32
    public function roles()
33
    {
34
        $pivotTable = config('admin.database.role_users_table');
35
36
        return $this->belongsToMany(Role::class, $pivotTable, 'user_id', 'role_id');
37
    }
38
39
    /**
40
     * Check if user has permission.
41
     *
42
     * @param $permission
43
     *
44
     * @return bool
45
     */
46
    public function can($permission)
47
    {
48
        foreach ($this->roles as $role) {
0 ignored issues
show
Documentation introduced by
The property roles does not exist on object<Encore\Admin\Auth\Database\Administrator>. 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...
49
            if ($role->can($permission)) {
50
                return true;
51
            }
52
        }
53
54
        return false;
55
    }
56
57
    /**
58
     * Check if user has no permission.
59
     *
60
     * @param $permission
61
     *
62
     * @return bool
63
     */
64
    public function cannot($permission)
65
    {
66
        return !$this->can($permission);
67
    }
68
69
    /**
70
     * Check if user is $roles.
71
     *
72
     * @param $roles
73
     *
74
     * @return mixed
75
     */
76
    public function isRole($roles)
77
    {
78
        if (is_string($roles)) {
79
            $roles = [$roles];
80
        }
81
82
        return $this->roles()->whereIn('slug', $roles)->exists();
83
    }
84
85
    /**
86
     * If visible for roles.
87
     *
88
     * @param $roles
89
     *
90
     * @return bool
91
     */
92
    public function visible($roles)
93
    {
94
        if (empty($roles)) {
95
            return true;
96
        }
97
98
        $roles = array_column($roles, 'slug');
99
100
        if ($this->isRole($roles) || $this->isRole('administrator')) {
101
            return true;
102
        }
103
104
        return false;
105
    }
106
}
107