Passed
Pull Request — master (#1111)
by Iman
04:07
created

PrivilegeHelpers::denyAccess()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace crocodicstudio\crudbooster\Modules\PrivilegeModule;
4
5
trait PrivilegeHelpers
6
{
7
    public static function isSuperadmin()
8
    {
9
        return session('admin_is_superadmin');
10
    }
11
12
    public static function canView()
13
    {
14
        return self::canDo('is_visible');
15
    }
16
17
    public static function canUpdate()
18
    {
19
        return self::canDo('is_edit');
20
    }
21
22
    public static function canCreate()
23
    {
24
        return self::canDo('is_create');
25
    }
26
27
    public static function canRead()
28
    {
29
        return self::canDo('is_read');
30
    }
31
32
    public static function canDelete()
33
    {
34
        return self::canDo('is_delete');
35
    }
36
37
    public static function canCRUD()
38
    {
39
        if (self::isSuperadmin()) {
40
            return true;
41
        }
42
43
        $session = session('admin_privileges_roles');
44
        foreach ($session as $v) {
45
            if ($v->path !== self::getModulePath()) {
46
                continue;
47
            }
48
            if ($v->is_visible && $v->is_create && $v->is_read && $v->is_edit && $v->is_delete) {
49
                return true;
50
            }
51
52
            return false;
53
        }
54
    }
55
56
    private static function canDo($verb)
57
    {
58
        if (self::isSuperadmin()) {
59
            return true;
60
        }
61
62
        foreach (session('admin_privileges_roles') as $role) {
63
            if ($role->path == self::getModulePath()) {
64
                return (bool) $role->{$verb};
65
            }
66
        }
67
    }
68
69
    public static function myPrivilegeId()
70
    {
71
        return session('admin_privileges');
72
    }
73
74
    public static function myPrivilege()
75
    {
76
        $roles = session('admin_privileges_roles');
77
        if (! $roles) {
78
            return;
79
        }
80
        foreach ($roles as $role) {
81
            if ($role->path == self::getModulePath()) {
82
                return $role;
83
            }
84
        }
85
    }
86
87
    private static function getModulePath()
88
    {
89
        $adminPathSegments = count(explode('/',cbConfig('ADMIN_PATH')));
90
        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...
91
    }
92
93
    public static function myPrivilegeName()
94
    {
95
        return session('admin_privileges_name');
96
    }
97
98
    public static function denyAccess()
99
    {
100
        static::redirect(static::adminPath(), cbTrans('denied_access'));
101
    }
102
}