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; |
|
|
|
|
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
private static function getModulePath() |
33
|
|
|
{ |
34
|
|
|
$adminPathSegments = count(explode('/', cbConfig('ADMIN_PATH'))); |
35
|
|
|
|
36
|
|
|
return Request::segment(1 + $adminPathSegments); |
|
|
|
|
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; |
|
|
|
|
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
|
|
|
} |