Completed
Pull Request — master (#163)
by
unknown
08:39
created

Administrator::isAdministrator()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
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
/**
10
 * Class Administrator.
11
 *
12
 * @property Role[] $roles
13
 */
14
class Administrator extends Model implements AuthenticatableContract
15
{
16
    use Authenticatable;
17
18
    protected $fillable = ['username', 'password', 'name'];
19
20
    /**
21
     * Create a new Eloquent model instance.
22
     *
23
     * @param array $attributes
24
     */
25
    public function __construct(array $attributes = [])
26
    {
27
        $this->table = config('admin.database.users_table');
28
29
        parent::__construct($attributes);
30
    }
31
32
    /**
33
     * A user has and belongs to many roles.
34
     *
35
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
36
     */
37
    public function roles()
38
    {
39
        $pivotTable = config('admin.database.role_users_table');
40
41
        return $this->belongsToMany(Role::class, $pivotTable, 'user_id', 'role_id');
42
    }
43
44
    /**
45
     * A User has and belongs to many permissions.
46
     *
47
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
48
     */
49
    public function permissions()
50
    {
51
        $pivotTable = config('admin.database.user_permissions_table');
52
53
        return $this->belongsToMany(Permission::class, $pivotTable, 'user_id', 'permission_id');
54
    }
55
56
    /**
57
     * Check if user has permission.
58
     *
59
     * @param $permission
60
     *
61
     * @return bool
62
     */
63
    public function can($permission)
64
    {
65
        if ($this->isAdministrator()) {
66
            return true;
67
        }
68
69
        if (method_exists($this, 'permissions')) {
70
            if ($this->permissions()->where('slug', $permission)->exists()) {
71
                return true;
72
            }
73
        }
74
75
        foreach ($this->roles as $role) {
76
            if ($role->can($permission)) {
77
                return true;
78
            }
79
        }
80
81
        return false;
82
    }
83
84
    /**
85
     * Check if user has no permission.
86
     *
87
     * @param $permission
88
     *
89
     * @return bool
90
     */
91
    public function cannot($permission)
92
    {
93
        return !$this->can($permission);
94
    }
95
96
    /**
97
     * Check if user is administrator.
98
     *
99
     * @return mixed
100
     */
101
    public function isAdministrator()
102
    {
103
        return $this->isRole('administrator');
104
    }
105
106
    /**
107
     * Check if user is $role.
108
     *
109
     * @param string $role
110
     *
111
     * @return mixed
112
     */
113
    public function isRole($role)
114
    {
115
        return $this->roles()->where('slug', $role)->exists();
116
    }
117
118
    /**
119
     * Check if user in $roles.
120
     *
121
     * @param array $roles
122
     *
123
     * @return mixed
124
     */
125
    public function inRoles($roles = [])
126
    {
127
        return $this->roles()->whereIn('slug', (array) $roles)->exists();
128
    }
129
130
    /**
131
     * If visible for roles.
132
     *
133
     * @param $roles
134
     *
135
     * @return bool
136
     */
137
    public function visible($roles)
138
    {
139
        if (empty($roles)) {
140
            return true;
141
        }
142
143
        $roles = array_column($roles, 'slug');
144
145
        if ($this->inRoles($roles) || $this->isAdministrator()) {
146
            return true;
147
        }
148
149
        return false;
150
    }
151
}
152