25
25
use function Vvveb \__ ;
26
26
use Vvveb \Controller \Base ;
27
27
use Vvveb \Sql \RoleSQL ;
28
+ use Vvveb \System \Cache ;
28
29
use Vvveb \System \User \Role as RoleList ;
29
30
30
31
class Role extends Base {
31
32
protected $ type = 'role ' ;
32
33
34
+ protected $ app = 'admin ' ;
35
+
36
+ protected $ apps = [
37
+ 'admin ' => ['permissions ' => ['controllers ' ]],
38
+ 'rest ' => ['permissions ' => ['routes ' , 'controllers ' ]],
39
+ 'graphql ' => ['permissions ' => ['controllers ' , 'models ' ]],
40
+ ];
41
+
42
+ function init () {
43
+ $ this ->app = $ this ->request ->get ['app ' ] ?? 'admin ' ;
44
+
45
+ if (! isset ($ this ->apps [$ this ->app ])) {
46
+ //$this->view->errors[] = __('Invalid app!');
47
+ return $ this ->notFound (__ ('Invalid app! ' ));
48
+ }
49
+
50
+ parent ::init ();
51
+ }
52
+
33
53
function index () {
34
54
$ role_id = $ this ->request ->get ['role_id ' ] ?? false ;
35
55
36
- $ tree = [];
37
- RoleList::mkmap (DIR_APP . 'controller ' , $ tree );
38
- $ this ->view ->tree = $ tree ['controller ' ];
56
+ $ cache = Cache::getInstance ();
57
+ $ tree = $ cache ->cache (APP , $ this ->app . '-permissions ' ,function () {
58
+ $ tree = [];
59
+ //$this->view->tree = RoleList::controllers($this->app);
60
+ foreach ($ this ->apps [$ this ->app ]['permissions ' ] as $ method ) {
61
+ $ data = RoleList::$ method ($ this ->app );
62
+
63
+ if ($ data ) {
64
+ $ tree += RoleList::$ method ($ this ->app );
65
+ }
66
+ ////$this->view->tree = $tree['controller'];
67
+ }
39
68
40
- $ controllers = RoleList::getControllerList ();
41
- $ this ->view ->controllers = $ controllers ;
42
- $ this ->view ->capabilities = RoleList::getCapabilitiesList ();
69
+ return $ tree ;
70
+ }, 259200 );
71
+
72
+ $ this ->view ->tree = $ tree ;
73
+
74
+ //\Vvveb\dd($this->view->tree);
75
+
76
+ //$this->view->controllers = RoleList::getControllerList($this->app);
77
+ $ this ->view ->capabilities = RoleList::getCapabilitiesList ($ this ->app );
78
+ $ this ->view ->apps = $ this ->apps ;
79
+ $ this ->view ->app = $ this ->app ;
43
80
44
81
$ role = new RoleSQL ();
45
82
$ this ->view ->role = $ role ->get (['role_id ' => $ role_id ]);
46
83
47
84
if ($ this ->view ->role ) {
48
- $ this ->view ->role ['permissions ' ] = json_decode ($ this ->view ->role ['permissions ' ], true );
85
+ $ permissions = json_decode ($ this ->view ->role ['permissions ' ], true );
86
+
87
+ if (isset ($ permissions [$ this ->app ])) {
88
+ $ permissions = $ permissions [$ this ->app ];
89
+ } else {
90
+ //backward compatibility for admin app
91
+ if ($ this ->app !== 'admin ' ) {
92
+ $ permissions = [];
93
+ }
94
+ }
95
+
96
+ $ this ->view ->role ['permissions ' ] = $ permissions ;
49
97
$ this ->view ->role ['permissions ' ]['deny ' ] = $ this ->view ->role ['permissions ' ]['deny ' ] ?? [];
50
98
$ this ->view ->role ['permissions ' ]['allow ' ] = $ this ->view ->role ['permissions ' ]['allow ' ] ?? [];
51
99
$ this ->view ->role ['permissions ' ]['capabilities ' ] = $ this ->view ->role ['permissions ' ]['capabilities ' ] ?? [];
@@ -57,11 +105,24 @@ function save() {
57
105
$ allow = $ this ->request ->post ['allow ' ] ?? [];
58
106
$ deny = $ this ->request ->post ['deny ' ] ?? [];
59
107
$ capabilities = $ this ->request ->post ['capabilities ' ] ?? [];
60
- $ permissions = ['deny ' => $ deny , 'allow ' => $ allow , 'capabilities ' => $ capabilities ];
108
+ $ permissions = [$ this -> app => [ 'deny ' => $ deny , 'allow ' => $ allow , 'capabilities ' => $ capabilities] ];
61
109
62
110
$ role_id = $ this ->request ->get ['role_id ' ] ?? false ;
63
111
64
- $ role = new RoleSQL ();
112
+ $ role = new RoleSQL ();
113
+ $ this ->view ->role = $ role ->get (['role_id ' => $ role_id ]);
114
+ $ currentPermissions = json_decode ($ this ->view ->role ['permissions ' ], true );
115
+
116
+ if ($ currentPermissions ) {
117
+ //backward compatibility
118
+ if (isset ($ currentPermissions ['allow ' ])) {
119
+ $ currentPermissions ['admin ' ] = $ currentPermissions ;
120
+ unset($ currentPermissions ['allow ' ], $ currentPermissions ['deny ' ], $ currentPermissions ['capabilities ' ]);
121
+ }
122
+
123
+ $ currentPermissions [$ this ->app ] = $ permissions [$ this ->app ];
124
+ $ permissions = $ currentPermissions ;
125
+ }
65
126
66
127
if ($ role_id ) {
67
128
$ result = $ role ->edit (['role_id ' => $ role_id , 'role ' => $ data + ['permissions ' => json_encode ($ permissions )]]);
0 commit comments