Role::permissions()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Itstructure\LaRbac\Models;
4
5
use Illuminate\Support\Str;
6
use Illuminate\Database\Eloquent\Model;
7
8
/**
9
 * Class Role
10
 *
11
 * @package Itstructure\LaRbac\Models
12
 *
13
 * @author Andrey Girnik <[email protected]>
14
 */
15
class Role extends Model
16
{
17
    const ADMIN_ROLE = 'admin';
18
    const MANAGER_ROLE = 'manager';
19
    const EDITOR_ROLE = 'editor';
20
    const USER_ROLE = 'user';
21
22
    /**
23
     * @var array
24
     */
25
    protected $fillable = [
26
        'name', 'slug', 'description', 'permissions'
27
    ];
28
29
    /**
30
     * @var string
31
     */
32
    private $_userModelClass;
33
34
    /**
35
     * @var array
36
     */
37
    private $_permissions;
38
39
    /**
40
     * Role constructor.
41
     * @param array $attributes
42
     */
43
    public function __construct(array $attributes = [])
44
    {
45
        $this->_userModelClass = config('rbac.userModelClass');
46
47
        parent::__construct($attributes);
48
    }
49
50
    /**
51
     * Synchronize role permissions after save model.
52
     * @param array $options
53
     * @return bool
54
     */
55
    public function save(array $options = [])
56
    {
57
        if (!parent::save($options)) {
58
            return false;
59
        }
60
61
        if (null !== $this->_permissions) {
62
            $this->permissions()->sync($this->_permissions);
63
        }
64
65
        return true;
66
    }
67
68
    /**
69
     * Get users by relation.
70
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
71
     */
72
    public function users()
73
    {
74
        return $this->belongsToMany($this->_userModelClass, 'user_role', 'role_id', 'user_id')->withTimestamps();
75
    }
76
77
    /**
78
     * Set name.
79
     * Set slug by name.
80
     * @param $value
81
     * @return void
82
     */
83
    public function setNameAttribute($value)
84
    {
85
        $this->attributes['name'] = $value;
86
        $this->attributes['slug'] = Str::slug($value, '-');
87
    }
88
89
    /**
90
     * Set permissions.
91
     * @param $value
92
     * @return void
93
     */
94
    public function setPermissionsAttribute($value)
95
    {
96
        $this->_permissions = $value;
97
    }
98
99
    /**
100
     * Get permissions by relation.
101
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
102
     */
103
    public function permissions()
104
    {
105
        return $this->belongsToMany(Permission::class, 'role_permission', 'role_id', 'permission_id')->withTimestamps();
106
    }
107
108
    /**
109
     * Check if role has permissions, transferred to the function.
110
     * @param array $permissions
111
     * @return bool
112
     */
113
    public function hasAccess(array $permissions) : bool
114
    {
115
        foreach ($permissions as $permission) {
116
117
            if ($this->hasPermission($permission)) {
118
                return true;
119
            }
120
        }
121
122
        return false;
123
    }
124
125
    /**
126
     * Check if role has single permission, transferred to the function.
127
     * @param string $permission
128
     * @return bool
129
     */
130
    private function hasPermission(string $permission) : bool
131
    {
132
        return in_array($permission, $this->permissions()->pluck('slug')->toArray());
133
    }
134
}
135