Role::users()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Thinkstudeo\Rakshak;
4
5
use Illuminate\Database\Eloquent\Model;
6
use Illuminate\Support\Str;
7
use Thinkstudeo\Rakshak\Traits\UuidAsPrimaryKey;
8
9
class Role extends Model
10
{
11
    use UuidAsPrimaryKey;
12
13
    // protected $table = 'roles';
14
15
    // protected $primaryKey = 'id';
16
17
    public $incrementing = false;
18
19
    protected $keyType = 'string';
20
21
    /**
22
     * The fields which are guarded against mass assigment.
23
     *
24
     * @var array
25
     */
26
    protected $guarded = ['id'];
27
28
    /**
29
     * Set the slug attribute from the name attribute value.
30
     *
31
     * @param string $value
32
     * @return void
33
     */
34
    public function setNameAttribute($value)
35
    {
36
        $this->slug = Str::slug($value, '-');
37
38
        $this->attributes['name'] = $value;
39
    }
40
41
    /**
42
     * Path to the Role.
43
     *
44
     * @return string
45
     */
46
    public function path()
47
    {
48
        return '/'.config('rakshak.route_prefix')."/roles/{$this->{$this->getRouteKeyName()}}";
49
    }
50
51
    /**
52
     * Get all the Abilities associatex with the Role.
53
     *
54
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
55
     */
56
    public function abilities()
57
    {
58
        return $this->belongsToMany(Ability::class);
59
    }
60
61
    /**
62
     * Get all the Users associated with the Role.
63
     *
64
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
65
     */
66
    public function users()
67
    {
68
        return $this->belongsToMany(config('auth.providers.users.model'));
69
    }
70
71
    /**
72
     * Add the Ability provided as parameter to the given Role.
73
     *
74
     * @param \Thinkstudeo\Rakshak\Ability|string $ability
75
     * @return \Thinkstudeo\Rakshak\Role
76
     */
77
    public function addAbility($ability)
78
    {
79
        $model = $ability instanceof Ability ? $ability : Ability::whereName($ability)->firstOrFail();
80
81
        $this->abilities()->save($model);
82
83
        return $this;
84
    }
85
86
    /**
87
     * Remove the Ability provided as parameter from the given Role.
88
     *
89
     * @param string|\Thinkstudeo\Rakshak\Ability $ability
90
     * @return \Thinkstudeo\Rakshak\Role
91
     */
92
    public function retractAbility($ability)
93
    {
94
        $model = $ability instanceof Ability ? $ability : Ability::whereName($ability)->firstOrFail();
95
96
        $this->abilities()->detach($model->id);
97
98
        return $this;
99
    }
100
101
    /**
102
     * Determine whether the Ability is associated with the given Role.
103
     *
104
     * @param \Thinkstudeo\Rakshak\Ability|string $ability
105
     * @return bool
106
     */
107
    public function hasAbility($ability)
108
    {
109
        $model = $ability instanceof Ability ? $ability : Ability::whereName($ability)->firstOrFail();
110
111
        return (bool) $this->abilities()->whereName($model->name)->count();
112
    }
113
}
114