|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Crocodicstudio\Crudbooster\Modules\PrivilegeModule; |
|
4
|
|
|
|
|
5
|
|
|
trait PrivilegeHelpers |
|
6
|
|
|
{ |
|
7
|
|
|
public static function canView() |
|
8
|
|
|
{ |
|
9
|
|
|
return self::canDo('can_see_module'); |
|
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 cbUser()->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('can_edit'); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
public static function canCreate() |
|
45
|
|
|
{ |
|
46
|
|
|
return self::canDo('can_create'); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
public static function canRead() |
|
50
|
|
|
{ |
|
51
|
|
|
return self::canDo('can_read'); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
public static function canDelete() |
|
55
|
|
|
{ |
|
56
|
|
|
return self::canDo('can_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->can_see_module && $role->can_create && $role->can_read && $role->can_edit && $role->can_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 cbUser()->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
|
|
|
$roleId = cbUser()->cms_roles_id; |
|
|
|
|
|
|
106
|
|
|
|
|
107
|
|
|
return cache()->rememberForever('cb_admin_privileges_roles'. $roleId, function () use ($roleId) { |
|
108
|
|
|
return \DB::table('cms_roles_privileges')->where('cms_roles_id', $roleId)->join('cms_modules', 'cms_modules.id', '=', 'cms_roles_privileges.cms_modules_id')->select('cms_modules.name', 'cms_modules.path', 'can_see_module', 'can_create', 'can_read', 'can_edit', 'can_delete')->get() ?: []; |
|
109
|
|
|
}); |
|
110
|
|
|
} |
|
111
|
|
|
} |