Completed
Push — master ( 04c3aa...51bf30 )
by ARCANEDEV
03:46
created

Role::setSlugAttribute()   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 1
crap 1
1
<?php namespace Arcanedev\LaravelAuth\Models;
2
3
use Arcanedev\LaravelAuth\Bases\Model;
4
use Arcanedev\LaravelAuth\Traits\Activatable;
5
use Arcanedev\LaravelAuth\Traits\AuthRoleRelationships;
6
use Arcanedev\LaravelAuth\Traits\Slugable;
7
use Arcanesoft\Contracts\Auth\Models\Role as RoleContract;
8
use Illuminate\Database\Eloquent\Model as Eloquent;
9
10
/**
11
 * Class     Role
12
 *
13
 * @package  Arcanedev\LaravelAuth\Models
14
 * @author   ARCANEDEV <[email protected]>
15
 *
16
 * @property  int                                       id
17
 * @property  string                                    name
18
 * @property  string                                    slug
19
 * @property  string                                    description
20
 * @property  bool                                      is_active
21
 * @property  bool                                      is_locked
22
 * @property  \Carbon\Carbon                            created_at
23
 * @property  \Carbon\Carbon                            updated_at
24
 * @property  \Illuminate\Database\Eloquent\Collection  users
25
 * @property  \Illuminate\Database\Eloquent\Collection  permissions
26
 */
27
class Role extends Model implements RoleContract
28
{
29
    /* ------------------------------------------------------------------------------------------------
30
     |  Traits
31
     | ------------------------------------------------------------------------------------------------
32
     */
33
    use Activatable, AuthRoleRelationships, Slugable;
34
35
    /* ------------------------------------------------------------------------------------------------
36
     |  Properties
37
     | ------------------------------------------------------------------------------------------------
38
     */
39
    /**
40
     * The attributes that are mass assignable.
41
     *
42
     * @var array
43
     */
44
    protected $fillable = ['name', 'slug', 'description'];
45
46
    /**
47
     * The attributes that should be casted to native types.
48
     *
49
     * @var array
50
     */
51
    protected $casts = [
52
        'is_active' => 'boolean',
53
        'is_locked' => 'boolean',
54
    ];
55
56
    /* ------------------------------------------------------------------------------------------------
57
     |  Constructor
58
     | ------------------------------------------------------------------------------------------------
59
     */
60
    /**
61
     * Create a new Eloquent model instance.
62
     *
63
     * @param  array  $attributes
64 260
     */
65
    public function __construct(array $attributes = [])
66 260
    {
67
        $this->setTable(config('laravel-auth.roles.table', 'roles'));
68 260
69 260
        parent::__construct($attributes);
70
    }
71
72
    /* ------------------------------------------------------------------------------------------------
73
     |  Getters & Setters
74
     | ------------------------------------------------------------------------------------------------
75
     */
76
    /**
77
     * Set the name attribute.
78
     *
79
     * @param  string  $name
80 108
     */
81
    public function setNameAttribute($name)
82 108
    {
83 108
        $this->attributes['name'] = $name;
84 108
        $this->setSlugAttribute($name);
85
    }
86
87
    /**
88
     * Set the slug attribute.
89
     *
90
     * @param  string  $slug
91 108
     */
92
    public function setSlugAttribute($slug)
93 108
    {
94 108
        $this->attributes['slug'] = $this->slugify($slug);
95
    }
96
97
    /* ------------------------------------------------------------------------------------------------
98
     |  CRUD Functions
99
     | ------------------------------------------------------------------------------------------------
100
     */
101
    /**
102
     * Attach a permission to a role.
103
     *
104
     * @param  \Arcanesoft\Contracts\Auth\Models\User|int  $user
105
     * @param  bool                                        $reload
106 12
     */
107
    public function attachUser($user, $reload = true)
108 12
    {
109 4
        if ($this->hasUser($user)) {
110
            return;
111
        }
112 12
113
        $this->users()->attach($user);
114 12
115 12
        if ($reload) {
116 9
            $this->load('users');
117 12
        }
118
    }
119
120
    /**
121
     * Detach a user from a role.
122
     *
123
     * @param  \Arcanesoft\Contracts\Auth\Models\User|int  $user
124
     * @param  bool                                    $reload
125
     *
126
     * @return int
127 4
     */
128
    public function detachUser($user, $reload = true)
129 4
    {
130 4
        if ($user instanceof Eloquent) {
0 ignored issues
show
Bug introduced by
The class Illuminate\Database\Eloquent\Model does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
131 3
            $user = (array) $user->getKey();
132
        }
133 4
134
        $result = $this->users()->detach($user);
135 4
136 4
        if ($reload) {
137 3
            $this->load('users');
138
        }
139 4
140
        return $result;
141
    }
142
143
    /**
144
     * Detach all users from a role.
145
     *
146
     * @param  bool  $reload
147
     *
148
     * @return int
149 4
     */
150
    public function detachAllUsers($reload = true)
151 4
    {
152
        $result = $this->users()->detach();
153 4
154 4
        if ($reload) {
155 3
            $this->load('users');
156
        }
157 4
158
        return $result;
159
    }
160
161
    /**
162
     * Check if role has the given user (User Model or Id).
163
     *
164
     * @param  \Arcanesoft\Contracts\Auth\Models\User|int  $id
165
     *
166
     * @return bool
167 12
     */
168
    public function hasUser($id)
169 12
    {
170 12
        if ($id instanceof Eloquent) {
0 ignored issues
show
Bug introduced by
The class Illuminate\Database\Eloquent\Model does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
171 9
            $id = $id->getKey();
172
        }
173 12
174
        return $this->users->contains($id);
175
    }
176
177
    /**
178
     * Attach a permission to a role.
179
     *
180
     * @param  \Arcanesoft\Contracts\Auth\Models\Permission|int  $permission
181
     * @param  bool                                              $reload
182 36
     */
183
    public function attachPermission($permission, $reload = true)
184 36
    {
185 4
        if ($this->hasPermission($permission)) {
186
            return;
187
        }
188 36
189
        $this->permissions()->attach($permission);
190 36
191 36
        if ($reload) {
192 27
            $this->load('permissions');
193 36
        }
194
    }
195
196
    /**
197
     * Detach a permission from a role.
198
     *
199
     * @param  \Arcanesoft\Contracts\Auth\Models\Permission|int  $permission
200
     * @param  bool                                              $reload
201
     *
202
     * @return int
203 4
     */
204
    public function detachPermission($permission, $reload = true)
205 4
    {
206 4
        if ($permission instanceof Eloquent) {
0 ignored issues
show
Bug introduced by
The class Illuminate\Database\Eloquent\Model does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
207 3
            $permission = (array) $permission->getKey();
208
        }
209 4
210
        $result = $this->permissions()->detach($permission);
211 4
212 4
        if ($reload) {
213 3
            $this->load('permissions');
214
        }
215 4
216
        return $result;
217
    }
218
219
    /**
220
     * Detach all permissions from a role.
221
     *
222
     * @param  bool  $reload
223
     *
224
     * @return int
225 4
     */
226
    public function detachAllPermissions($reload = true)
227 4
    {
228
        $result = $this->permissions()->detach();
229 4
230 4
        if ($reload) {
231 3
            $this->load('permissions');
232
        }
233 4
234
        return $result;
235
    }
236
237
    /**
238
     * Check if role has the given permission (Permission Model or Id).
239
     *
240
     * @param  mixed  $id
241
     *
242
     * @return bool
243 36
     */
244
    public function hasPermission($id)
245 36
    {
246 36
        if ($id instanceof Eloquent) {
0 ignored issues
show
Bug introduced by
The class Illuminate\Database\Eloquent\Model does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
247 27
            $id = $id->getKey();
248
        }
249 36
250
        return $this->permissions->contains($id);
251
    }
252
253
    /* ------------------------------------------------------------------------------------------------
254
     |  Check Functions
255
     | ------------------------------------------------------------------------------------------------
256
     */
257
    /**
258
     * Check if role is associated with a permission by slug.
259
     *
260
     * @param  string  $slug
261
     *
262
     * @return bool
263
     */
264
    public function can($slug)
265 12
    {
266 12
        $permissions = $this->permissions->filter(function($permission) use ($slug) {
267 12
            return $permission->checkSlug($slug);
268
        });
269 12
270
        return $permissions->count() === 1;
271
    }
272
273
    /**
274
     * Check if a role is associated with any of given permissions.
275
     *
276
     * @param  array  $permissions
277
     * @param  array  &$failedPermissions
278
     *
279
     * @return bool
280 8
     */
281
    public function canAny(array $permissions, array &$failedPermissions = [])
282 8
    {
283 8
        foreach ($permissions as $permission) {
284 8
            if ( ! $this->can($permission)) {
285 6
                $failedPermissions[] = $permission;
286 6
            }
287
        }
288 8
289
        return count($permissions) !== count($failedPermissions);
290
    }
291
292
    /**
293
     * Check if role is associated with all given permissions.
294
     *
295
     * @param  array  $permissions
296
     * @param  array  &$failedPermissions
297
     *
298
     * @return bool
299 4
     */
300
    public function canAll(array $permissions, array &$failedPermissions = [])
301 4
    {
302
        $this->canAny($permissions, $failedPermissions);
303 4
304
        return count($failedPermissions) === 0;
305
    }
306
307
    /**
308
     * Check if the role is locked.
309
     *
310
     * @return bool
311 8
     */
312
    public function isLocked()
313 8
    {
314
        return $this->is_locked;
315
    }
316
}
317