Skip to content

Commit 27fb8e4

Browse files
committed
Permissions code moved to trait to share code for GraphQL and Rest
1 parent 79f6255 commit 27fb8e4

File tree

1 file changed

+27
-90
lines changed

1 file changed

+27
-90
lines changed

admin/controller/base.php

+27-90
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,13 @@
4040
use Vvveb\System\PageCache;
4141
use Vvveb\System\Session;
4242
use Vvveb\System\Sites;
43+
use Vvveb\System\Traits\Permission;
4344
use Vvveb\System\User\Admin;
4445

4546
#[\AllowDynamicProperties]
4647
class Base {
48+
use Permission;
49+
4750
public $view;
4851

4952
public $request;
@@ -218,75 +221,6 @@ protected function customProduct() {
218221
return $products_menu;
219222
}
220223

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-
290224
protected function language($defaultLanguage = false, $defaultLanguageId = false, $defaultLocale = false) {
291225
$languages = availableLanguages();
292226

@@ -353,10 +287,13 @@ protected function language($defaultLanguage = false, $defaultLanguageId = false
353287
//if no default language configured then set first language as current language
354288
if (! isset($languages[$language])) {
355289
$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+
}
360297
}
361298

362299
//if no language configured then set default language as current language
@@ -474,6 +411,7 @@ function init() {
474411

475412
$this->language();
476413
$this->currency();
414+
$adminPath = \Vvveb\adminPath();
477415

478416
//change site status (live, under maintenance etc)
479417
if ($state = ($this->request->post['state'] ?? false)) {
@@ -483,8 +421,9 @@ function init() {
483421
PageCache::getInstance()->purge();
484422
}
485423
} 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;
488427
}
489428
}
490429

@@ -504,7 +443,8 @@ function init() {
504443
$className = get_class($this);
505444

506445
if ($className != 'Vvveb\Controller\Error403') {
507-
$this->permissions();
446+
$this->permission();
447+
$this->setPermissions();
508448
}
509449

510450
//load plugins for active site if safe mode is not selected
@@ -530,18 +470,11 @@ function init() {
530470
$this->session->delete('success');
531471
}
532472

533-
$menu = \Vvveb\config('admin-menu', []);
534-
535473
//don't initialize menu items for CLI
536474
if (defined('CLI')) {
537475
return;
538476
}
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', []);
545478

546479
//custom posts -- add to menu
547480
$this->taxonomies = $this->getTaxonomies();
@@ -561,9 +494,13 @@ function init() {
561494
$urls = array_map(function ($value) use ($permissions) { return $value ? ($permissions[$value] ?? false) : false; }, $urls);
562495
$this->setPermissionsFromUrl($menu, $urls);
563496

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 ?? [];
565503

566-
$adminPath = \Vvveb\adminPath();
567504
$view->adminPath = $adminPath;
568505
$view->mediaPath = PUBLIC_PATH . 'media';
569506
$view->publicPath = PUBLIC_PATH . 'media';
@@ -600,7 +537,9 @@ protected function requireLogin() {
600537
$this->view->action = "{$admin_path}index.php?module=user/login";
601538
$this->view->template('user/login.html');
602539

603-
die($this->view->render());
540+
$this->view->render();
541+
542+
die(0);
604543
}
605544

606545
/**
@@ -611,7 +550,7 @@ protected function requireLogin() {
611550
* @param mixed $service
612551
* @param mixed $message
613552
*/
614-
protected function notFound($service = false, $message = false, $statusCode = 404) {
553+
protected function notFound($message = false, $statusCode = 404, $service = false) {
615554
return FrontController::notFound($service, $message, $statusCode);
616555
}
617556

@@ -650,7 +589,5 @@ function goToHelp() {
650589
$url = $this->getDocUrlForPage();
651590

652591
return header("Location: $url");
653-
654-
die($url);
655592
}
656593
}

0 commit comments

Comments
 (0)