Passed
Push — master ( e065bd...c15bec )
by Iman
04:29
created

PrivilegeHelpers   A

Complexity

Total Complexity 28

Size/Duplication

Total Lines 100
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 100
rs 10
c 0
b 0
f 0
wmc 28

13 Methods

Rating   Name   Duplication   Size   Complexity  
A canRead() 0 3 1
A themeColor() 0 3 2
A canView() 0 3 1
A getModulePath() 0 5 1
A canUpdate() 0 3 1
A myPrivilege() 0 9 4
A canDelete() 0 3 1
A denyAccess() 0 3 1
B canCRUD() 0 16 9
A canDo() 0 13 4
A refreshSessionRoles() 0 4 1
A isSuperadmin() 0 3 1
A canCreate() 0 3 1
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 (session('admin_privileges_roles') 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
        $session = session('admin_privileges_roles');
66
        foreach ($session as $v) {
67
            if ($v->path !== self::getModulePath()) {
68
                continue;
69
            }
70
            if ($v->is_visible && $v->is_create && $v->is_read && $v->is_edit && $v->is_delete) {
71
                return true;
72
            }
73
74
            return false;
75
        }
76
    }
77
78
    public static function myPrivilege()
79
    {
80
        $roles = session('admin_privileges_roles');
81
        if (! $roles) {
82
            return;
83
        }
84
        foreach ($roles as $role) {
85
            if ($role->path == self::getModulePath()) {
86
                return $role;
87
            }
88
        }
89
    }
90
91
    public static function themeColor()
92
    {
93
        return auth('cbAdmin')->user()->role()->theme_color ?: 'skin-blue';
94
    }
95
96
    public static function denyAccess()
97
    {
98
        static::redirect(static::adminPath(), cbTrans('denied_access'));
99
    }
100
101
    public static function refreshSessionRoles()
102
    {
103
        $roles = \DB::table('cms_privileges_roles')->where('id_cms_privileges', auth('cbAdmin')->user()->id_cms_privileges)->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();
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...
104
        session()->put('admin_privileges_roles', $roles);
105
    }
106
}