40
40
use Vvveb \System \PageCache ;
41
41
use Vvveb \System \Session ;
42
42
use Vvveb \System \Sites ;
43
+ use Vvveb \System \Traits \Permission ;
43
44
use Vvveb \System \User \Admin ;
44
45
45
46
#[\AllowDynamicProperties]
46
47
class Base {
48
+ use Permission;
49
+
47
50
public $ view ;
48
51
49
52
public $ request ;
@@ -218,75 +221,6 @@ protected function customProduct() {
218
221
return $ products_menu ;
219
222
}
220
223
221
- /*
222
- * Permission check for each module/action
223
- */
224
- protected function permissions () {
225
- $ module = strtolower (FrontController::getModuleName ());
226
- $ action = strtolower (FrontController::getActionName ());
227
- $ action = ($ action && $ action != 'index ' ) ? '/ ' . $ action : '' ;
228
- $ permission = $ module . $ action ;
229
-
230
- //if current module/action does not have permission then show permission denied page
231
- if (! Admin::hasPermission ($ permission )) {
232
- $ message = __ ('Your role does not have permission to access this action! ' );
233
- $ this ->view ->errors [] = $ message ;
234
-
235
- die ($ this ->notFound (true , $ message , 403 ));
236
- }
237
-
238
- //get current controller methods to check for permission
239
- $ methods = get_class_methods ($ this );
240
- //$methods = array_map(fn ($value) => "$module/$value", $methods);
241
- $ methods = array_map (function ($ value ) use ($ module ) {return ($ value == 'index ' ) ? $ module : "$ module/ $ value " ; }, $ methods );
242
-
243
- //check if controller requires additional permission check
244
- if (isset ($ this ->additionalPermissionCheck )) {
245
- $ methods = array_merge ($ methods , $ this ->additionalPermissionCheck );
246
- }
247
-
248
- $ permissions = Admin::hasPermission ($ methods );
249
-
250
- //set a permission array only with action keys for easier permission check in html
251
- $ this ->modulePermissions = $ permissions ;
252
-
253
- foreach ($ permissions as $ permission => &$ value ) {
254
- $ key = str_replace ("$ module/ " , '' , $ permission );
255
- $ actionPermissions [$ key ] = $ value ;
256
- }
257
- $ this ->actionPermissions = $ actionPermissions ;
258
- }
259
-
260
- protected function getPermissionsFromUrl (&$ array , &$ permissions ) {
261
- foreach ($ array as $ k => $ v ) {
262
- if (is_array ($ v )) {
263
- if (isset ($ v ['url ' ])) {
264
- if (isset ($ v ['module ' ])) {
265
- $ permissions [$ v ['url ' ]] = ($ v ['module ' ] ?? '' ) . ((isset ($ v ['action ' ]) && $ v ['action ' ] != 'index ' ) ? '/ ' . $ v ['action ' ] : '' );
266
- } else {
267
- $ permissions [$ v ['url ' ]] = \Vvveb \pregMatch ('/module=([^&$]+)/ ' , $ v ['url ' ], 1 );
268
- }
269
- }
270
- $ this ->getPermissionsFromUrl ($ v , $ permissions );
271
- }
272
- }
273
- }
274
-
275
- protected function setPermissionsFromUrl (&$ array , &$ permissions ) {
276
- foreach ($ array as $ k => &$ v ) {
277
- if (is_array ($ v )) {
278
- if (isset ($ v ['url ' ])) {
279
- $ url = $ v ['url ' ];
280
-
281
- if (isset ($ permissions [$ url ])) {
282
- $ v ['permission ' ] = $ permissions [$ url ];
283
- }
284
- }
285
- $ this ->setPermissionsFromUrl ($ v , $ permissions );
286
- }
287
- }
288
- }
289
-
290
224
protected function language ($ defaultLanguage = false , $ defaultLanguageId = false , $ defaultLocale = false ) {
291
225
$ languages = availableLanguages ();
292
226
@@ -353,10 +287,13 @@ protected function language($defaultLanguage = false, $defaultLanguageId = false
353
287
//if no default language configured then set first language as current language
354
288
if (! isset ($ languages [$ language ])) {
355
289
$ default_language = key ($ languages );
356
- $ lang = $ languages [$ default_language ];
357
- $ default_language_id = $ lang ['language_id ' ] ?? $ defaultLanguageId ;
358
- $ default_locale = $ lang ['locale ' ] ?? $ defaultLocale ;
359
- $ default_rtl = $ lang ['rtl ' ] ?? false ;
290
+ $ lang = $ languages [$ default_language ] ?? [];
291
+
292
+ if ($ lang ) {
293
+ $ default_language_id = $ lang ['language_id ' ] ?? $ defaultLanguageId ;
294
+ $ default_locale = $ lang ['locale ' ] ?? $ defaultLocale ;
295
+ $ default_rtl = $ lang ['rtl ' ] ?? false ;
296
+ }
360
297
}
361
298
362
299
//if no language configured then set default language as current language
@@ -474,6 +411,7 @@ function init() {
474
411
475
412
$ this ->language ();
476
413
$ this ->currency ();
414
+ $ adminPath = \Vvveb \adminPath ();
477
415
478
416
//change site status (live, under maintenance etc)
479
417
if ($ state = ($ this ->request ->post ['state ' ] ?? false )) {
@@ -483,8 +421,9 @@ function init() {
483
421
PageCache::getInstance ()->purge ();
484
422
}
485
423
} else {
486
- $ message = __ ('Your role does not have permission to access this action! ' );
487
- $ this ->view ->errors [] = $ message ;
424
+ $ message = __ ('Your role does not have permission to access this action! ' );
425
+ $ this ->view ->errors [] = $ message ;
426
+ $ this ->view ->adminPath = $ adminPath ;
488
427
}
489
428
}
490
429
@@ -504,7 +443,8 @@ function init() {
504
443
$ className = get_class ($ this );
505
444
506
445
if ($ className != 'Vvveb\Controller\Error403 ' ) {
507
- $ this ->permissions ();
446
+ $ this ->permission ();
447
+ $ this ->setPermissions ();
508
448
}
509
449
510
450
//load plugins for active site if safe mode is not selected
@@ -530,18 +470,11 @@ function init() {
530
470
$ this ->session ->delete ('success ' );
531
471
}
532
472
533
- $ menu = \Vvveb \config ('admin-menu ' , []);
534
-
535
473
//don't initialize menu items for CLI
536
474
if (defined ('CLI ' )) {
537
475
return ;
538
476
}
539
-
540
- $ this ->view ->global = $ this ->global ;
541
-
542
- //send to view for button visibillity check
543
- $ this ->view ->actionPermissions = $ this ->actionPermissions ?? [];
544
- $ this ->view ->modulePermissions = $ this ->modulePermissions ?? [];
477
+ $ menu = \Vvveb \config ('admin-menu ' , []);
545
478
546
479
//custom posts -- add to menu
547
480
$ this ->taxonomies = $ this ->getTaxonomies ();
@@ -561,9 +494,13 @@ function init() {
561
494
$ urls = array_map (function ($ value ) use ($ permissions ) { return $ value ? ($ permissions [$ value ] ?? false ) : false ; }, $ urls );
562
495
$ this ->setPermissionsFromUrl ($ menu , $ urls );
563
496
564
- $ view ->menu = $ menu ;
497
+ $ view ->menu = $ menu ;
498
+ $ view ->global = $ this ->global ;
499
+
500
+ //send to view for button visibillity check
501
+ $ this ->view ->actionPermissions = $ this ->actionPermissions ?? [];
502
+ $ this ->view ->modulePermissions = $ this ->modulePermissions ?? [];
565
503
566
- $ adminPath = \Vvveb \adminPath ();
567
504
$ view ->adminPath = $ adminPath ;
568
505
$ view ->mediaPath = PUBLIC_PATH . 'media ' ;
569
506
$ view ->publicPath = PUBLIC_PATH . 'media ' ;
@@ -600,7 +537,9 @@ protected function requireLogin() {
600
537
$ this ->view ->action = "{$ admin_path }index.php?module=user/login " ;
601
538
$ this ->view ->template ('user/login.html ' );
602
539
603
- die ($ this ->view ->render ());
540
+ $ this ->view ->render ();
541
+
542
+ die (0 );
604
543
}
605
544
606
545
/**
@@ -611,7 +550,7 @@ protected function requireLogin() {
611
550
* @param mixed $service
612
551
* @param mixed $message
613
552
*/
614
- protected function notFound ($ service = false , $ message = false , $ statusCode = 404 ) {
553
+ protected function notFound ($ message = false , $ statusCode = 404 , $ service = false ) {
615
554
return FrontController::notFound ($ service , $ message , $ statusCode );
616
555
}
617
556
@@ -650,7 +589,5 @@ function goToHelp() {
650
589
$ url = $ this ->getDocUrlForPage ();
651
590
652
591
return header ("Location: $ url " );
653
-
654
- die ($ url );
655
592
}
656
593
}
0 commit comments