Skip to content

Commit d18fbf8

Browse files
committed
Permission trait for common permission check methods
1 parent d598b3b commit d18fbf8

File tree

1 file changed

+113
-0
lines changed

1 file changed

+113
-0
lines changed

system/traits/permission.php

+113
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
<?php
2+
3+
/**
4+
* Vvveb
5+
*
6+
* Copyright (C) 2022 Ziadin Givan
7+
*
8+
* This program is free software: you can redistribute it and/or modify
9+
* it under the terms of the GNU Affero General Public License as
10+
* published by the Free Software Foundation, either version 3 of the
11+
* License, or (at your option) any later version.
12+
*
13+
* This program is distributed in the hope that it will be useful,
14+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16+
* GNU Affero General Public License for more details.
17+
*
18+
* You should have received a copy of the GNU Affero General Public License
19+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
20+
*
21+
*/
22+
23+
namespace Vvveb\System\Traits;
24+
25+
use function Vvveb\__;
26+
use Vvveb\System\Core\FrontController;
27+
use Vvveb\System\User\Admin;
28+
29+
trait Permission {
30+
/*
31+
* Permission check for each module/action
32+
*/
33+
protected function permission($permission = null) {
34+
$module = $this->module ?? strtolower(FrontController::getModuleName());
35+
$action = $this->action ?? strtolower(FrontController::getActionName());
36+
$action = ($action && $action != 'index') ? '/' . $action : '';
37+
$permission = $permission ?? ($module . $action);
38+
39+
//if current module/action does not have permission then show permission denied page
40+
if (! Admin::hasPermission($permission)) {
41+
$message = __('Your role does not have permission to access this action!');
42+
$this->view->errors[] = $message;
43+
44+
$adminPath = \Vvveb\adminPath();
45+
$data = ['message' => $message];
46+
47+
if (APP == 'admin') {
48+
$data['adminPath'] = $adminPath;
49+
}
50+
51+
$this->notFound($data, 403);
52+
53+
die(0);
54+
}
55+
}
56+
57+
protected function setPermissions() {
58+
$module = $this->module ?? strtolower(FrontController::getModuleName());
59+
$action = $this->action ?? strtolower(FrontController::getActionName());
60+
$action = ($action && $action != 'index') ? '/' . $action : '';
61+
62+
//get current controller methods to check for permission
63+
$methods = get_class_methods($this);
64+
//$methods = array_map(fn ($value) => "$module/$value", $methods);
65+
$methods = array_map(function ($value) use ($module) {return ($value == 'index') ? $module : "$module/$value"; }, $methods);
66+
67+
//check if controller requires additional permission check
68+
if (isset($this->additionalPermissionCheck)) {
69+
$methods = array_merge($methods, $this->additionalPermissionCheck);
70+
}
71+
72+
$permissions = Admin::hasPermission($methods);
73+
74+
//set a permission array only with action keys for easier permission check in html
75+
$this->modulePermissions = $permissions;
76+
77+
foreach ($permissions as $permission => &$value) {
78+
$key = str_replace("$module/", '', $permission);
79+
$actionPermissions[$key] = $value;
80+
}
81+
$this->actionPermissions = $actionPermissions;
82+
}
83+
84+
protected function getPermissionsFromUrl(&$array, &$permissions) {
85+
foreach ($array as $k => $v) {
86+
if (is_array($v)) {
87+
if (isset($v['url'])) {
88+
if (isset($v['module'])) {
89+
$permissions[$v['url']] = ($v['module'] ?? '') . ((isset($v['action']) && $v['action'] != 'index') ? '/' . $v['action'] : '');
90+
} else {
91+
$permissions[$v['url']] = \Vvveb\pregMatch('/module=([^&$]+)/', $v['url'], 1);
92+
}
93+
}
94+
$this->getPermissionsFromUrl($v, $permissions);
95+
}
96+
}
97+
}
98+
99+
protected function setPermissionsFromUrl(&$array, &$permissions) {
100+
foreach ($array as $k => &$v) {
101+
if (is_array($v)) {
102+
if (isset($v['url'])) {
103+
$url = $v['url'];
104+
105+
if (isset($permissions[$url])) {
106+
$v['permission'] = $permissions[$url];
107+
}
108+
}
109+
$this->setPermissionsFromUrl($v, $permissions);
110+
}
111+
}
112+
}
113+
}

0 commit comments

Comments
 (0)