Completed
Push — master ( 826d66...63fb2c )
by Andrey
01:40
created

Administrable::setRolesAttribute()   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 1
1
<?php
2
namespace Itstructure\LaRbac\Models;
3
4
use Itstructure\LaRbac\Contracts\User as RbacUserContract;
5
6
/**
7
 * Class Administrable
8
 *
9
 * @package Itstructure\LaRbac\Models
10
 */
11
trait Administrable
12
{
13
    /**
14
     * New filled roles.
15
     *
16
     * @var array
17
     */
18
    private $_roles;
19
20
    /**
21
     * @var int|null
22
     */
23
    private $_countAdministrativeRoles;
24
25
    /**
26
     * User identifier.
27
     *
28
     * @return int
29
     */
30
    public function getIdAttribute(): int
31
    {
32
        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...
33
    }
34
35
    /**
36
     * User name.
37
     *
38
     * @return string
39
     */
40
    public function getNameAttribute(): string
41
    {
42
        return $this->attributes['name'];
43
    }
44
45
    /**
46
     * Set new filled roles.
47
     *
48
     * @param $value
49
     * @return void
50
     */
51
    public function setRolesAttribute($value): void
52
    {
53
        $this->_roles = $value;
54
    }
55
56
    /**
57
     * Get user roles by relation.
58
     *
59
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
60
     */
61
    public function roles()
62
    {
63
        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...
64
    }
65
66
    /**
67
     * Checks if User has access to $permissions.
68
     *
69
     * @param array $permissions
70
     * @return bool
71
     */
72
    public function hasAccess(array $permissions) : bool
73
    {
74
        // check if the permission is available in any role
75
        /* @var Role $role */
76
        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...
77
            if($role->hasAccess($permissions)) {
78
                return true;
79
            }
80
        }
81
        return false;
82
    }
83
84
    /**
85
     * Checks if the user belongs to role.
86
     *
87
     * @param string $roleSlug
88
     * @return bool
89
     */
90
    public function inRole(string $roleSlug): bool
91
    {
92
        return $this->roles()->where('slug', $roleSlug)->count() == 1;
93
    }
94
95
    /**
96
     * @param RbacUserContract $member
97
     * @param Role $role
98
     * @return bool
99
     */
100
    public function canAssignRole(RbacUserContract $member, Role $role): bool
101
    {
102
        if ($this->countAdministrativeRoles() == 0) {
103
            return false;
104
        }
105
        if ($this->getIdAttribute() != $member->getIdAttribute()) {
106
            return true;
107
        }
108
        if ($this->countAdministrativeRoles() > 1) {
109
            return true;
110
        }
111
        if (!$role->hasAccess([Permission::ADMIN_PERMISSION])) {
112
            return true;
113
        }
114
        if ($this->inRole($role->slug)) {
115
            return false;
116
        }
117
        return true;
118
    }
119
120
    /**
121
     * Synchronize user roles after save model.
122
     *
123
     * @param array $options
124
     * @return bool
125
     */
126
    public function save(array $options = [])
127
    {
128
        if (!parent::save($options)) {
129
            return false;
130
        }
131
132
        if (null !== $this->_roles) {
133
            $this->roles()->sync($this->_roles);
134
        }
135
136
        return true;
137
    }
138
139
    /**
140
     * @return int
141
     */
142
    private function countAdministrativeRoles(): int
143
    {
144
        if (null !== $this->_countAdministrativeRoles) {
145
            return $this->_countAdministrativeRoles;
146
        }
147
148
        $this->_countAdministrativeRoles = 0;
149
150
        /* @var Role $role */
151
        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...
152
            if($role->hasAccess([Permission::ADMIN_PERMISSION])) {
153
                $this->_countAdministrativeRoles += 1;
154
            }
155
        }
156
157
        return $this->_countAdministrativeRoles;
158
    }
159
}
160