AdminRole   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 119
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 29
dl 0
loc 119
rs 10
c 0
b 0
f 0
wmc 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A onDeleting() 0 5 1
A permissions() 0 7 1
A __construct() 0 5 1
A can() 0 3 1
A onUpdated() 0 6 2
A onCreated() 0 6 2
A cannot() 0 3 1
A administrators() 0 7 1
1
<?php
2
3
namespace Yeelight\Models;
4
5
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
6
use Prettus\Repository\Contracts\Transformable;
7
use Prettus\Repository\Traits\TransformableTrait;
8
9
/**
10
 * Class AdminRole
11
 *
12
 * @category Yeelight
13
 *
14
 * @package Yeelight\Models
15
 *
16
 * @author Sheldon Lee <[email protected]>
17
 *
18
 * @license https://opensource.org/licenses/MIT MIT
19
 *
20
 * @link https://www.yeelight.com
21
 */
22
class AdminRole extends BaseModel implements Transformable
23
{
24
    use TransformableTrait;
25
26
    /**
27
     * Indicates if the model should be auto set user_id.
28
     *
29
     * @var bool
30
     */
31
    protected $autoUserId = false;
32
33
    /**
34
     * Indicates if the model should be recorded ips.
35
     *
36
     * @var bool
37
     */
38
    protected $ips = false;
39
40
    /**
41
     * Indicates if the model should be recorded users.
42
     *
43
     * @var bool
44
     */
45
    protected $update_users = false;
46
47
    protected $primaryKey = 'id';
48
49
    // Fields to be converted to Carbon object automatically
50
    protected $dates = [];
51
52
    protected $fillable = ['name', 'slug'];
53
54
    /**
55
     * Create a new Eloquent model instance.
56
     *
57
     * @param array $attributes
58
     */
59
    public function __construct(array $attributes = [])
60
    {
61
        $this->setTable(config('yeelight.backend.database.admin_roles_table'));
62
63
        parent::__construct($attributes);
64
    }
65
66
    /**
67
     * A role belongs to many users.
68
     *
69
     * @return BelongsToMany
70
     */
71
    public function administrators() : BelongsToMany
72
    {
73
        $pivotTable = config('yeelight.backend.database.admin_role_users_table');
74
75
        $relatedModel = config('yeelight.backend.database.admin_users_model');
76
77
        return $this->belongsToMany($relatedModel, $pivotTable, 'role_id', 'user_id');
78
    }
79
80
    /**
81
     * A role belongs to many permissions.
82
     *
83
     * @return BelongsToMany
84
     */
85
    public function permissions() : BelongsToMany
86
    {
87
        $pivotTable = config('yeelight.backend.database.admin_role_permissions_table');
88
89
        $relatedModel = config('yeelight.backend.database.admin_permissions_model');
90
91
        return $this->belongsToMany($relatedModel, $pivotTable, 'role_id', 'permission_id')->withTimestamps();
92
    }
93
94
    /**
95
     * Check user has permission.
96
     *
97
     * @param $permission
98
     *
99
     * @return bool
100
     */
101
    public function can(string $permission) : bool
102
    {
103
        return $this->permissions()->where('slug', $permission)->exists();
104
    }
105
106
    /**
107
     * Check user has no permission.
108
     *
109
     * @param $permission
110
     *
111
     * @return bool
112
     */
113
    public function cannot(string $permission) : bool
114
    {
115
        return !$this->can($permission);
116
    }
117
118
    public function onCreated()
119
    {
120
        parent::onCreated();
121
        $permissions = array_filter(request()->get('permissions', []));
122
        if (!empty($permissions)) {
123
            $this->permissions()->sync($permissions);
124
        }
125
    }
126
127
    public function onUpdated()
128
    {
129
        parent::onUpdated();
130
        $permissions = array_filter(request()->get('permissions', []));
131
        if (!empty($permissions)) {
132
            $this->permissions()->sync($permissions);
133
        }
134
    }
135
136
    public function onDeleting()
137
    {
138
        parent::onDeleting();
139
        $this->administrators()->detach();
140
        $this->permissions()->sync([]);
141
    }
142
}
143