Role   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 97
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 21
c 1
b 0
f 0
dl 0
loc 97
rs 10
wmc 6

6 Methods

Rating   Name   Duplication   Size   Complexity  
A grantPermission() 0 3 1
A regainPermission() 0 3 1
A permissions() 0 3 1
A parent() 0 3 1
A assigned() 0 5 1
A childen() 0 3 1
1
<?php
2
3
namespace Mrluke\Privileges\Models;
4
5
use Illuminate\Database\Eloquent\Model;
6
7
use Mrluke\Privileges\Contracts\Role as Contract;
8
use Mrluke\Privileges\Facades\Manager;
9
10
/**
11
 * Role model for package.
12
 *
13
 * @author    Łukasz Sitnicki (mr-luke)
14
 * @link      http://github.com/mr-luke/privileges
15
 *
16
 * @category  Laravel
17
 * @package   mr-luke/privileges
18
 * @license   MIT
19
 */
20
class Role extends Model implements Contract
21
{
22
    /**
23
     * The attributes that should be cast to native types.
24
     *
25
     * @var array
26
     */
27
    protected $casts = [
28
        'childs'       => 'array',
29
        'level'        => 'integer',
30
        'restrictions' => 'array'
31
    ];
32
33
    /**
34
     * The event map for the model.
35
     *
36
     * @var array
37
     */
38
    protected $dispatchesEvents = [
39
        'created'  => \Mrluke\Privileges\Events\Role\Created::class,
40
        'creating' => \Mrluke\Privileges\Events\Role\Creating::class,
41
        'deleted'  => \Mrluke\Privileges\Events\Role\Deleted::class,
42
        'deleting' => \Mrluke\Privileges\Events\Role\Deleting::class,
43
        'updated'  => \Mrluke\Privileges\Events\Role\Updated::class,
44
        'updating' => \Mrluke\Privileges\Events\Role\Updating::class,
45
    ];
46
47
    /**
48
     * The attributes that are mass assignable.
49
     *
50
     * @var array
51
     */
52
    protected $fillable = ['name', 'level', 'parent_id', 'childs', 'restrictions'];
53
54
    /**
55
     * The table associated with the model.
56
     *
57
     * @var string
58
     */
59
    protected $table = 'priv_roles';
60
61
    /**
62
     * Return assigned Authorizable models.
63
     *
64
     */
65
    public function assigned()
66
    {
67
        $model = Manager::getAuthorizableModel();
68
69
        return $this->belongsToMany($model, 'priv_auth_role', 'role_id', 'auth_id');
70
    }
71
72
    /**
73
     * Return related model.
74
     */
75
    public function parent()
76
    {
77
        return $this->belongsTo(Role::class, 'parent_id', 'id');
78
    }
79
80
    /**
81
     * Return related model.
82
     */
83
    public function childen()
84
    {
85
        return $this->hasMany(Role::class, 'parent_id', 'id');
86
    }
87
88
    /**
89
     * Return related model.
90
     */
91
    public function permissions()
92
    {
93
        return $this->morphMany(Permission::class, 'grantable');
94
    }
95
96
    /**
97
     * Grant or update premission for a Permitable.
98
     *
99
     * @param  string $scope
100
     * @param  int    $level
101
     * @return void
102
     */
103
    public function grantPermission(string $scope, int $level): void
104
    {
105
        Manager::grantPermission($this, $scope, $level);
106
    }
107
108
    /**
109
     * Regain permission for a Permitable.
110
     *
111
     * @param  string $scope
112
     * @return void
113
     */
114
    public function regainPermission(string $scope): void
115
    {
116
        Manager::regainPermission($this, $scope);
117
    }
118
}
119