Completed
Push — master ( d42b43...5ac77a )
by ARCANEDEV
8s
created

src/Models/Role.php (1 issue)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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\Permission as PermissionContract;
8
use Arcanesoft\Contracts\Auth\Models\Role as RoleContract;
9
use Illuminate\Database\Eloquent\Model as Eloquent;
10
11
/**
12
 * Class     Role
13
 *
14
 * @package  Arcanedev\LaravelAuth\Models
15
 * @author   ARCANEDEV <[email protected]>
16
 *
17
 * @property  int                                       id
18
 * @property  string                                    name
19
 * @property  string                                    slug
20
 * @property  string                                    description
21
 * @property  bool                                      is_active
22
 * @property  bool                                      is_locked
23
 * @property  \Carbon\Carbon                            created_at
24
 * @property  \Carbon\Carbon                            updated_at
25
 * @property  \Illuminate\Database\Eloquent\Collection  users
26
 * @property  \Illuminate\Database\Eloquent\Collection  permissions
27
 */
28
class Role extends Model implements RoleContract
29
{
30
    /* ------------------------------------------------------------------------------------------------
31
     |  Traits
32
     | ------------------------------------------------------------------------------------------------
33
     */
34
    use Activatable, AuthRoleRelationships, Slugable;
35
36
    /* ------------------------------------------------------------------------------------------------
37
     |  Properties
38
     | ------------------------------------------------------------------------------------------------
39
     */
40
    /**
41
     * The attributes that are mass assignable.
42
     *
43
     * @var array
44
     */
45
    protected $fillable = ['name', 'slug', 'description'];
46
47
    /**
48
     * The attributes that should be casted to native types.
49
     *
50
     * @var array
51
     */
52
    protected $casts = [
53
        'is_active' => 'boolean',
54
        'is_locked' => 'boolean',
55
    ];
56
57
    /* ------------------------------------------------------------------------------------------------
58
     |  Constructor
59
     | ------------------------------------------------------------------------------------------------
60
     */
61
    /**
62
     * Create a new Eloquent model instance.
63
     *
64
     * @param  array  $attributes
65
     */
66 528
    public function __construct(array $attributes = [])
67
    {
68 528
        $this->setTable(config('laravel-auth.roles.table', 'roles'));
69
70 528
        parent::__construct($attributes);
71 528
    }
72
73
    /* ------------------------------------------------------------------------------------------------
74
     |  Getters & Setters
75
     | ------------------------------------------------------------------------------------------------
76
     */
77
    /**
78
     * Set the name attribute.
79
     *
80
     * @param  string  $name
81
     */
82 224
    public function setNameAttribute($name)
83
    {
84 224
        $this->attributes['name'] = $name;
85 224
        $this->setSlugAttribute($name);
86 224
    }
87
88
    /**
89
     * Set the slug attribute.
90
     *
91
     * @param  string  $slug
92
     */
93 224
    public function setSlugAttribute($slug)
94
    {
95 224
        $this->attributes['slug'] = $this->slugify($slug);
96 224
    }
97
98
    /* ------------------------------------------------------------------------------------------------
99
     |  CRUD Functions
100
     | ------------------------------------------------------------------------------------------------
101
     */
102
    /**
103
     * Attach a permission to a role.
104
     *
105
     * @param  \Arcanesoft\Contracts\Auth\Models\User|int  $user
106
     * @param  bool                                        $reload
107
     */
108 24
    public function attachUser($user, $reload = true)
109
    {
110 24
        if ( ! $this->hasUser($user)) {
111 24
            $this->users()->attach($user);
112 24
            $this->loadUsers($reload);
113 18
        }
114 24
    }
115
116
    /**
117
     * Detach a user from a role.
118
     *
119
     * @param  \Arcanesoft\Contracts\Auth\Models\User|int  $user
120
     * @param  bool                                        $reload
121
     *
122
     * @return int
123
     */
124 8
    public function detachUser($user, $reload = true)
125
    {
126 8
        if ($user instanceof Eloquent) {
127 8
            $user = (array) $user->getKey();
128 6
        }
129
130 8
        $result = $this->users()->detach($user);
131 8
        $this->loadUsers($reload);
132
133 8
        return $result;
134
    }
135
136
    /**
137
     * Detach all users from a role.
138
     *
139
     * @param  bool  $reload
140
     *
141
     * @return int
142
     */
143 8
    public function detachAllUsers($reload = true)
144
    {
145 8
        $result = $this->users()->detach();
146 8
        $this->loadUsers($reload);
147
148 8
        return $result;
149
    }
150
151
    /**
152
     * Check if role has the given user (User Model or Id).
153
     *
154
     * @param  \Arcanesoft\Contracts\Auth\Models\User|int  $id
155
     *
156
     * @return bool
157
     */
158 24
    public function hasUser($id)
159
    {
160 24
        if ($id instanceof Eloquent) {
161 24
            $id = $id->getKey();
162 18
        }
163
164 24
        return $this->users->contains($id);
165
    }
166
167
    /**
168
     * Attach a permission to a role.
169
     *
170
     * @param  \Arcanesoft\Contracts\Auth\Models\Permission|int  $permission
171
     * @param  bool                                              $reload
172
     */
173 72
    public function attachPermission($permission, $reload = true)
174
    {
175 72
        if ( ! $this->hasPermission($permission)) {
176 72
            $this->permissions()->attach($permission);
177 72
            $this->loadPermissions($reload);
178 54
        }
179 72
    }
180
181
    /**
182
     * Detach a permission from a role.
183
     *
184
     * @param  \Arcanesoft\Contracts\Auth\Models\Permission|int  $permission
185
     * @param  bool                                              $reload
186
     *
187
     * @return int
188
     */
189 8
    public function detachPermission($permission, $reload = true)
190
    {
191 8
        if ($permission instanceof Eloquent) {
192 8
            $permission = (array) $permission->getKey();
193 6
        }
194
195 8
        $result = $this->permissions()->detach($permission);
196 8
        $this->loadPermissions($reload);
197
198 8
        return $result;
199
    }
200
201
    /**
202
     * Detach all permissions from a role.
203
     *
204
     * @param  bool  $reload
205
     *
206
     * @return int
207
     */
208 8
    public function detachAllPermissions($reload = true)
209
    {
210 8
        $result = $this->permissions()->detach();
211 8
        $this->loadPermissions($reload);
212
213 8
        return $result;
214
    }
215
216
    /**
217
     * Check if role has the given permission (Permission Model or Id).
218
     *
219
     * @param  mixed  $id
220
     *
221
     * @return bool
222
     */
223 72
    public function hasPermission($id)
224
    {
225 72
        if ($id instanceof Eloquent) {
0 ignored issues
show
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...
226 72
            $id = $id->getKey();
227 54
        }
228
229 72
        return $this->permissions->contains($id);
230
    }
231
232
    /* ------------------------------------------------------------------------------------------------
233
     |  Check Functions
234
     | ------------------------------------------------------------------------------------------------
235
     */
236
    /**
237
     * Check if role is associated with a permission by slug.
238
     *
239
     * @param  string  $slug
240
     *
241
     * @return bool
242
     */
243
    public function can($slug)
244
    {
245 24
        $permissions = $this->permissions->filter(function(PermissionContract $permission) use ($slug) {
246 24
            return $permission->checkSlug($slug);
247 24
        });
248
249 24
        return $permissions->count() === 1;
250
    }
251
252
    /**
253
     * Check if a role is associated with any of given permissions.
254
     *
255
     * @param  array  $permissions
256
     * @param  array  &$failedPermissions
257
     *
258
     * @return bool
259
     */
260 16
    public function canAny(array $permissions, array &$failedPermissions = [])
261
    {
262 16
        foreach ($permissions as $permission) {
263 16
            if ( ! $this->can($permission)) {
264 16
                $failedPermissions[] = $permission;
265 12
            }
266 12
        }
267
268 16
        return count($permissions) !== count($failedPermissions);
269
    }
270
271
    /**
272
     * Check if role is associated with all given permissions.
273
     *
274
     * @param  array  $permissions
275
     * @param  array  &$failedPermissions
276
     *
277
     * @return bool
278
     */
279 8
    public function canAll(array $permissions, array &$failedPermissions = [])
280
    {
281 8
        $this->canAny($permissions, $failedPermissions);
282
283 8
        return count($failedPermissions) === 0;
284
    }
285
286
    /**
287
     * Check if the role is locked.
288
     *
289
     * @return bool
290
     */
291 16
    public function isLocked()
292
    {
293 16
        return $this->is_locked;
294
    }
295
296
    /* ------------------------------------------------------------------------------------------------
297
     |  Other Functions
298
     | ------------------------------------------------------------------------------------------------
299
     */
300
    /**
301
     * Load the users.
302
     *
303
     * @param  bool  $load
304
     *
305
     * @return self
306
     */
307 24
    protected function loadUsers($load = true)
308
    {
309 24
        return $load ? $this->load('users') : $this;
310
    }
311
312
    /**
313
     * Load the permissions.
314
     *
315
     * @param  bool  $load
316
     *
317
     * @return self
318
     */
319 72
    protected function loadPermissions($load = true)
320
    {
321 72
        return $load ? $this->load('permissions') : $this;
322
    }
323
}
324