Issues (210)

src/Traits/HasRole.php (1 issue)

Labels
Severity
1
<?php
2
3
namespace Pratiksh\Adminetic\Traits;
4
5
use Pratiksh\Adminetic\Models\Admin\Role;
6
7
trait HasRole
8
{
9
    // User Belongs To Many Roles
10
    public function roles()
11
    {
12
        return $this->belongsToMany(Role::class)->withTimeStamps();
0 ignored issues
show
It seems like belongsToMany() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

12
        return $this->/** @scrutinizer ignore-call */ belongsToMany(Role::class)->withTimeStamps();
Loading history...
13
    }
14
15
    // Check is user has given role
16
    public function hasRole($role)
17
    {
18
        return $this->roles->where('name', trim($role))->count() == 1 || $this->roles->where('name', 'superuser')->count() == 1 || $this->roles->where('name', 'superadmin')->count() == 1;
19
    }
20
21
    // Check if user is superadmin
22
    public function isSuperAdmin()
23
    {
24
        $roles = $this->roles->pluck('name')->toArray();
25
26
        return in_array('superadmin', $roles);
27
    }
28
29
    // Check BREAD Access
30
    public function userCanDo($model, $bread)
31
    {
32
        /*   $permissions = $this->permissions->whereIn('role_id', $this->roles->pluck('id')->toArray())->where('model', trim($model))->get([$bread]); */
33
        $can = [];
34
        foreach ($this->roles as $role) {
35
            $permissions = $role->permissions->whereIn('role_id', $role->id)->whereIn('model', trim($model))->pluck([$bread]);
36
            if ($permissions) {
37
                foreach ($permissions as $permission) {
38
                    $can[] = $permission;
39
                }
40
            }
41
        }
42
43
        return in_array(1, $can);
44
    }
45
}
46