1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace crocodicstudio\crudbooster\Modules\PrivilegeModule; |
4
|
|
|
|
5
|
|
|
trait PrivilegeHelpers |
6
|
|
|
{ |
7
|
|
|
public static function isSuperadmin() |
8
|
|
|
{ |
9
|
|
|
return self::findUserRole()->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
|
|
|
return false; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public static function myPrivilegeId() |
71
|
|
|
{ |
72
|
|
|
return session('admin_role_id'); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public static function myPrivilege() |
76
|
|
|
{ |
77
|
|
|
$roles = session('admin_privileges_roles'); |
78
|
|
|
if (! $roles) { |
79
|
|
|
return; |
80
|
|
|
} |
81
|
|
|
foreach ($roles as $role) { |
82
|
|
|
if ($role->path == self::getModulePath()) { |
83
|
|
|
return $role; |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
private static function getModulePath() |
89
|
|
|
{ |
90
|
|
|
$adminPathSegments = count(explode('/',cbConfig('ADMIN_PATH'))); |
91
|
|
|
return Request::segment(1 + $adminPathSegments); |
|
|
|
|
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public static function myPrivilegeName() |
95
|
|
|
{ |
96
|
|
|
return self::findUserRole()->name; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
public static function themeColor() |
100
|
|
|
{ |
101
|
|
|
return self::findUserRole()->theme_color ?: 'skin-blue'; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
public static function denyAccess() |
105
|
|
|
{ |
106
|
|
|
static::redirect(static::adminPath(), cbTrans('denied_access')); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @param $roleId |
111
|
|
|
* @return mixed |
112
|
|
|
*/ |
113
|
|
|
private static function findUserRole() |
114
|
|
|
{ |
115
|
|
|
return \DB::table('cms_privileges')->where('id', self::myPrivilegeId())->first(); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
public static function refreshSessionRoles() |
119
|
|
|
{ |
120
|
|
|
$roles = \DB::table('cms_privileges_roles')->where('id_cms_privileges', self::myPrivilegeId())->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(); |
121
|
|
|
session()->put('admin_privileges_roles', $roles); |
122
|
|
|
} |
123
|
|
|
} |