Completed
Push — master ( 63fb2c...2853ec )
by Andrey
01:28
created

Administrable::getNameAttribute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Itstructure\LaRbac\Models;
4
5
use Itstructure\LaRbac\Contracts\User as RbacUserContract;
6
7
/**
8
 * Class Administrable
9
 *
10
 * @package Itstructure\LaRbac\Models
11
 */
12
trait Administrable
13
{
14
    /**
15
     * New filled roles.
16
     *
17
     * @var array
18
     */
19
    private $_roles;
20
21
    /**
22
     * @var int|null
23
     */
24
    private $_countAdministrativeRoles;
25
26
    /**
27
     * User identifier.
28
     *
29
     * @return int
30
     */
31
    public function getIdAttribute(): int
32
    {
33
        return $this->attributes['id'];
0 ignored issues
show
Bug introduced by
The property attributes does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
34
    }
35
36
    /**
37
     * User name.
38
     *
39
     * @return string
40
     */
41
    public function getNameAttribute(): string
42
    {
43
        return $this->attributes['name'];
44
    }
45
46
    /**
47
     * Set new filled roles.
48
     *
49
     * @param $value
50
     *
51
     * @return void
52
     */
53
    public function setRolesAttribute($value): void
54
    {
55
        $this->_roles = $value;
56
    }
57
58
    /**
59
     * Get user roles by relation.
60
     *
61
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
62
     */
63
    public function roles()
64
    {
65
        return $this->belongsToMany(Role::class, 'user_role', 'user_id', 'role_id')->withTimestamps();
0 ignored issues
show
Bug introduced by
It seems like belongsToMany() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
66
    }
67
68
    /**
69
     * Checks if User has access to $permissions.
70
     *
71
     * @param array $permissions
72
     *
73
     * @return bool
74
     */
75
    public function hasAccess(array $permissions) : bool
76
    {
77
        // check if the permission is available in any role
78
        /* @var Role $role */
79
        foreach ($this->roles as $role) {
0 ignored issues
show
Bug introduced by
The property roles does not seem to exist. Did you mean _roles?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
80
81
            if($role->hasAccess($permissions)) {
82
                return true;
83
            }
84
        }
85
        return false;
86
    }
87
88
    /**
89
     * Checks if the user belongs to role.
90
     *
91
     * @param string $roleSlug
92
     *
93
     * @return bool
94
     */
95
    public function inRole(string $roleSlug): bool
96
    {
97
        return $this->roles()->where('slug', $roleSlug)->count() == 1;
98
    }
99
100
    /**
101
     * Can assign role checking.
102
     *
103
     * @param RbacUserContract $member
104
     * @param Role $role
105
     *
106
     * @return bool
107
     */
108
    public function canAssignRole(RbacUserContract $member, Role $role): bool
109
    {
110
        if ($this->countAdministrativeRoles() == 0) {
111
            return false;
112
        }
113
114
        if ($this->getIdAttribute() != $member->getIdAttribute()) {
115
            return true;
116
        }
117
118
        if ($this->countAdministrativeRoles() > 1) {
119
            return true;
120
        }
121
122
        if (!$role->hasAccess([Permission::ADMIN_PERMISSION])) {
123
            return true;
124
        }
125
126
        if ($this->inRole($role->slug)) {
127
            return false;
128
        }
129
130
        return true;
131
    }
132
133
    /**
134
     * Synchronize user roles after save model.
135
     *
136
     * @param array $options
137
     *
138
     * @return bool
139
     */
140
    public function save(array $options = [])
141
    {
142
        if (!parent::save($options)) {
143
            return false;
144
        }
145
146
        if (null !== $this->_roles) {
147
            $this->roles()->sync($this->_roles);
148
        }
149
150
        return true;
151
    }
152
153
    /**
154
     * @return int
155
     */
156
    private function countAdministrativeRoles(): int
157
    {
158
        if (null !== $this->_countAdministrativeRoles) {
159
            return $this->_countAdministrativeRoles;
160
        }
161
162
        $this->_countAdministrativeRoles = 0;
163
164
        /* @var Role $role */
165
        foreach ($this->roles as $role) {
0 ignored issues
show
Bug introduced by
The property roles does not seem to exist. Did you mean _roles?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
166
167
            if($role->hasAccess([Permission::ADMIN_PERMISSION])) {
168
                $this->_countAdministrativeRoles += 1;
169
            }
170
        }
171
172
        return $this->_countAdministrativeRoles;
173
    }
174
}
175