Passed
Push — master ( 8b5564...e666cf )
by Iman
08:54
created

PrivilegeHelpers::getPrivileges()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 1
nop 0
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace crocodicstudio\crudbooster\Modules\PrivilegeModule;
4
5
trait PrivilegeHelpers
6
{
7
    public static function canView()
8
    {
9
        return self::canDo('is_visible');
10
    }
11
12
    private static function canDo($verb)
13
    {
14
        if (self::isSuperadmin()) {
15
            return true;
16
        }
17
18
        foreach (self::getPrivileges() as $role) {
19
            if ($role->path == self::getModulePath()) {
20
                return (bool) $role->{$verb};
21
            }
22
        }
23
24
        return false;
25
    }
26
27
    public static function isSuperadmin()
28
    {
29
        return auth('cbAdmin')->user()->role()->is_superadmin;
0 ignored issues
show
Bug introduced by
The method role() does not exist on Illuminate\Contracts\Auth\Authenticatable. It seems like you code against a sub-type of Illuminate\Contracts\Auth\Authenticatable such as Illuminate\Foundation\Auth\User. ( Ignorable by Annotation )

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

29
        return auth('cbAdmin')->user()->/** @scrutinizer ignore-call */ role()->is_superadmin;
Loading history...
30
    }
31
32
    private static function getModulePath()
33
    {
34
        $adminPathSegments = count(explode('/', cbConfig('ADMIN_PATH')));
35
36
        return Request::segment(1 + $adminPathSegments);
0 ignored issues
show
Bug introduced by
The type crocodicstudio\crudboost...PrivilegeModule\Request was not found. Did you mean Request? If so, make sure to prefix the type with \.
Loading history...
37
    }
38
39
    public static function canUpdate()
40
    {
41
        return self::canDo('is_edit');
42
    }
43
44
    public static function canCreate()
45
    {
46
        return self::canDo('is_create');
47
    }
48
49
    public static function canRead()
50
    {
51
        return self::canDo('is_read');
52
    }
53
54
    public static function canDelete()
55
    {
56
        return self::canDo('is_delete');
57
    }
58
59
    public static function canCRUD()
60
    {
61
        if (self::isSuperadmin()) {
62
            return true;
63
        }
64
65
        foreach (self::getPrivileges() as $role) {
66
            if ($role->path !== self::getModulePath()) {
67
                continue;
68
            }
69
            if ($role->is_visible && $role->is_create && $role->is_read && $role->is_edit && $role->is_delete) {
70
                return true;
71
            }
72
73
            return false;
74
        }
75
    }
76
77
    public static function myPrivilege()
78
    {
79
        $roles = self::getPrivileges();
80
81
        foreach ($roles as $role) {
82
            if ($role->path == self::getModulePath()) {
83
                return $role;
84
            }
85
        }
86
    }
87
88
    public static function themeColor()
89
    {
90
        return auth('cbAdmin')->user()->role()->theme_color ?: 'skin-blue';
91
    }
92
93
    public static function denyAccess()
94
    {
95
        static::redirect(static::adminPath(), cbTrans('denied_access'));
96
    }
97
98
    public static function refreshSessionRoles()
99
    {
100
        return cache()->forget('cb_admin_privileges_roles');
101
    }
102
103
    public static function getPrivileges()
104
    {
105
        $uid = auth('cbAdmin')->user()->id_cms_privileges;
0 ignored issues
show
Bug introduced by
Accessing id_cms_privileges on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
106
107
        return cache()->rememberForever('cb_admin_privileges_roles', function () use ($uid) {
108
            return \DB::table('cms_privileges_roles')->where('id_cms_privileges', $uid)->join('cms_moduls', 'cms_moduls.id', '=', 'id_cms_moduls')->select('cms_moduls.name', 'cms_moduls.path', 'is_visible', 'is_create', 'is_read', 'is_edit', 'is_delete')->get() ?: [];
109
        });
110
    }
111
}