Completed
Push — master ( f4bdd5...957dc6 )
by ARCANEDEV
04:22
created

PermissionsGroup::permissions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 4
rs 10
c 1
b 0
f 0
ccs 2
cts 2
cp 1
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php namespace Arcanedev\LaravelAuth\Models;
2
3
use Arcanedev\LaravelAuth\Bases\Model;
4
use Arcanedev\LaravelAuth\Traits\Slugable;
5
6
/**
7
 * Class     PermissionsGroup
8
 *
9
 * @package  Arcanedev\LaravelAuth\Models
10
 * @author   ARCANEDEV <[email protected]>
11
 *
12
 * @property  int                                       id
13
 * @property  string                                    name
14
 * @property  string                                    slug
15
 * @property  string                                    description
16
 * @property  \Carbon\Carbon                            created_at
17
 * @property  \Carbon\Carbon                            updated_at
18
 * @property  \Illuminate\Database\Eloquent\Collection  permissions
19
 */
20
class PermissionsGroup extends Model
21
{
22
    /* ------------------------------------------------------------------------------------------------
23
     |  Traits
24
     | ------------------------------------------------------------------------------------------------
25
     */
26
    use Slugable;
27
28
    /* ------------------------------------------------------------------------------------------------
29
     |  Properties
30
     | ------------------------------------------------------------------------------------------------
31
     */
32
    /**
33
     * The attributes that are mass assignable.
34
     *
35
     * @var array
36
     */
37
    protected $fillable = ['name', 'slug', 'description'];
38
39
    /* ------------------------------------------------------------------------------------------------
40
     |  Constructor
41
     | ------------------------------------------------------------------------------------------------
42
     */
43
    /**
44
     * Create a new Eloquent model instance.
45
     *
46
     * @param  array  $attributes
47
     */
48 260
    public function __construct(array $attributes = [])
49
    {
50 260
        $this->setTable(config('laravel-auth.permissions-group.table', 'permissions_group'));
51
52 260
        parent::__construct($attributes);
53 260
    }
54
55
    /* ------------------------------------------------------------------------------------------------
56
     |  Relationships
57
     | ------------------------------------------------------------------------------------------------
58
     */
59
    /**
60
     * Permissions Groups has many permissions.
61
     *
62
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
63
     */
64 52
    public function permissions()
65
    {
66 52
        return $this->hasMany(Permission::class, 'group_id');
67
    }
68
69
    /* ------------------------------------------------------------------------------------------------
70
     |  Getters & Setters
71
     | ------------------------------------------------------------------------------------------------
72
     */
73
    /**
74
     * Set the name attribute.
75
     *
76
     * @param  string  $name
77
     */
78 52
    public function setNameAttribute($name)
79
    {
80 52
        $this->attributes['name'] = $name;
81 52
        $this->setSlugAttribute($name);
82 52
    }
83
84
    /**
85
     * Set the slug attribute.
86
     *
87
     * @param  string  $slug
88
     */
89 52
    public function setSlugAttribute($slug)
90
    {
91 52
        $this->attributes['slug'] = $this->slugify($slug);
92 52
    }
93
94
    /* ------------------------------------------------------------------------------------------------
95
     |  CRUD Functions
96
     | ------------------------------------------------------------------------------------------------
97
     */
98
    /**
99
     * Create and attach a permission.
100
     *
101
     * @param  array  $attributes
102
     * @param  bool   $reload
103
     */
104 8
    public function createPermission(array $attributes, $reload = true)
105
    {
106 8
        $this->permissions()->create($attributes);
107
108 8
        if ($reload) {
109 8
            $this->load('permissions');
110 6
        }
111 8
    }
112
113
    /**
114
     * Attach the permission to a group.
115
     *
116
     * @param  \Arcanedev\LaravelAuth\Models\Permission  $permission
117
     * @param  bool                                      $reload
118
     */
119 24
    public function attachPermission(&$permission, $reload = true)
120
    {
121 24
        if ($this->hasPermission($permission)) {
122 4
            return;
123
        }
124
125 24
        $permission = $this->permissions()->save($permission);
126
127 24
        if ($reload) {
128 24
            $this->load('permissions');
129 18
        }
130 24
    }
131
132
    /**
133
     * Attach the permission by id to a group.
134
     *
135
     * @param  int   $id
136
     * @param  bool  $reload
137
     *
138
     * @return \Arcanedev\LaravelAuth\Models\Permission
139
     */
140 4
    public function attachPermissionById($id, $reload = true)
141
    {
142 4
        $permission = $this->getPermissionById($id);
143
144 4
        if ( ! is_null($permission)) {
145 4
            $this->attachPermission($permission, $reload);
146 3
        }
147
148 4
        return $permission;
149
    }
150
151
    /**
152
     * Attach a collection of permissions to the group.
153
     *
154
     * @param  \Illuminate\Database\Eloquent\Collection|array  $permissions
155
     * @param  bool                                            $reload
156
     *
157
     * @return \Illuminate\Database\Eloquent\Collection|array
158
     */
159 12
    public function attachPermissions($permissions, $reload = true)
160
    {
161 12
        $permissions = $this->permissions()->saveMany($permissions);
162
163 12
        if ($reload) {
164 12
            $this->load('permissions');
165 9
        }
166
167 12
        return $permissions;
168
    }
169
170
    /**
171
     * Attach the permission from a group.
172
     *
173
     * @param  \Arcanedev\LaravelAuth\Models\Permission  $permission
174
     * @param  bool                                      $reload
175
     */
176 12
    public function detachPermission(&$permission, $reload = true)
177
    {
178 12
        if ( ! $this->hasPermission($permission)) {
179 8
            return;
180
        }
181
182 12
        $permission = $this->getPermissionFromGroup($permission);
183
184 12
        $permission->update([
185 12
            'group_id' => 0,
186 9
        ]);
187
188 12
        if ($reload) {
189 12
            $this->load('permissions');
190 9
        }
191 12
    }
192
193
    /**
194
     * Attach the permission by id to a group.
195
     *
196
     * @param  int   $id
197
     * @param  bool  $reload
198
     *
199
     * @return \Arcanedev\LaravelAuth\Models\Permission
200
     */
201 4
    public function detachPermissionById($id, $reload = true)
202
    {
203 4
        $permission = $this->getPermissionById($id);
204
205 4
        if ( ! is_null($permission)) {
206 4
            $this->detachPermission($permission, $reload);
207 3
        }
208
209 4
        return $permission;
210
    }
211
212
    /**
213
     * Detach multiple permissions by ids.
214
     *
215
     * @param  array  $ids
216
     * @param  bool   $reload
217
     *
218
     * @return int
219
     */
220 4
    public function detachPermissions(array $ids, $reload = true)
221
    {
222 4
        $detached = $this->permissions()->whereIn('id', $ids)->update([
223
            'group_id' => 0
224 4
        ]);
225
226 4
        if ($reload) {
227 4
            $this->load('permissions');
228 3
        }
229
230 4
        return $detached;
231
    }
232
233
    /**
234
     * Detach all permissions from the group.
235
     *
236
     * @param  bool  $reload
237
     *
238
     * @return int
239
     */
240 8
    public function detachAllPermissions($reload = true)
241
    {
242 8
        $detached = $this->permissions()->update([
243
            'group_id' => 0
244 8
        ]);
245
246 8
        if ($reload) {
247 4
            $this->load('permissions');
248 3
        }
249
250 8
        return $detached;
251
    }
252
253
    /* ------------------------------------------------------------------------------------------------
254
     |  Check Functions
255
     | ------------------------------------------------------------------------------------------------
256
     */
257
    /**
258
     * Check if role has the given permission (Permission Model or Id).
259
     *
260
     * @param  \Arcanedev\LaravelAuth\Models\Permission|int  $id
261
     *
262
     * @return bool
263
     */
264 32
    public function hasPermission($id)
265
    {
266 32
        if ($id instanceof Permission) {
267 32
            $id = $id->getKey();
268 24
        }
269
270 32
        return ! is_null($this->getPermissionFromGroup($id));
271
    }
272
273
    /**
274
     * Get a permission from the group.
275
     *
276
     * @param  \Arcanedev\LaravelAuth\Models\Permission|int  $id
277
     *
278
     * @return \Arcanedev\LaravelAuth\Models\Permission|null
279
     */
280 32
    private function getPermissionFromGroup($id)
281
    {
282 32
        if ($id instanceof Permission) {
283 12
            $id = $id->getKey();
284 9
        }
285
286 32
        $this->load('permissions');
287
288 32
        return $this->permissions->filter(function (Permission $permission) use ($id) {
289 32
            return $permission->id == $id;
290 32
        })->first();
291
    }
292
293
    /**
294
     * Get a permission by id.
295
     *
296
     * @param  int  $id
297
     *
298
     * @return \Arcanedev\LaravelAuth\Models\Permission|null
299
     */
300 8
    private function getPermissionById($id)
301
    {
302 8
        return $this->permissions()
303 8
            ->getRelated()
304 8
            ->where('id', $id)
305 8
            ->first();
306
    }
307
}
308