Skip to content

armgit / Vvveb

Public Imported from GitHub
0

Powerful and easy to use cms to build websites, blogs or ecommerce stores.

GraphQL and Rest role permissions

givanz79f6255a7542
diff --git a/admin/controller/admin/role.php b/admin/controller/admin/role.php
index 1a65939..2c6b071 100755
--- a/admin/controller/admin/role.php
+++ b/admin/controller/admin/role.php
@@ -25,27 +25,75 @@ namespace Vvveb\Controller\Admin;
use function Vvveb\__;
use Vvveb\Controller\Base;
use Vvveb\Sql\RoleSQL;
+use Vvveb\System\Cache;
use Vvveb\System\User\Role as RoleList;
class Role extends Base {
protected $type = 'role';
+ protected $app = 'admin';
+
+ protected $apps = [
+ 'admin' => ['permissions' => ['controllers']],
+ 'rest' => ['permissions' => ['routes', 'controllers']],
+ 'graphql' => ['permissions' => ['controllers', 'models']],
+ ];
+
+ function init() {
+ $this->app = $this->request->get['app'] ?? 'admin';
+
+ if (! isset($this->apps[$this->app])) {
+ //$this->view->errors[] = __('Invalid app!');
+ return $this->notFound(__('Invalid app!'));
+ }
+
+ parent::init();
+ }
+
function index() {
$role_id = $this->request->get['role_id'] ?? false;
- $tree = [];
- RoleList::mkmap(DIR_APP . 'controller', $tree);
- $this->view->tree = $tree['controller'];
+ $cache = Cache::getInstance();
+ $tree = $cache->cache(APP, $this->app . '-permissions',function () {
+ $tree = [];
+ //$this->view->tree = RoleList::controllers($this->app);
+ foreach ($this->apps[$this->app]['permissions'] as $method) {
+ $data = RoleList::$method($this->app);
+
+ if ($data) {
+ $tree += RoleList::$method($this->app);
+ }
+ ////$this->view->tree = $tree['controller'];
+ }
- $controllers = RoleList::getControllerList();
- $this->view->controllers = $controllers;
- $this->view->capabilities = RoleList::getCapabilitiesList();
+ return $tree;
+ }, 259200);
+
+ $this->view->tree = $tree;
+
+ //\Vvveb\dd($this->view->tree);
+
+ //$this->view->controllers = RoleList::getControllerList($this->app);
+ $this->view->capabilities = RoleList::getCapabilitiesList($this->app);
+ $this->view->apps = $this->apps;
+ $this->view->app = $this->app;
$role = new RoleSQL();
$this->view->role = $role->get(['role_id' => $role_id]);
if ($this->view->role) {
- $this->view->role['permissions'] = json_decode($this->view->role['permissions'], true);
+ $permissions = json_decode($this->view->role['permissions'], true);
+
+ if (isset($permissions[$this->app])) {
+ $permissions = $permissions[$this->app];
+ } else {
+ //backward compatibility for admin app
+ if ($this->app !== 'admin') {
+ $permissions = [];
+ }
+ }
+
+ $this->view->role['permissions'] = $permissions;
$this->view->role['permissions']['deny'] = $this->view->role['permissions']['deny'] ?? [];
$this->view->role['permissions']['allow'] = $this->view->role['permissions']['allow'] ?? [];
$this->view->role['permissions']['capabilities'] = $this->view->role['permissions']['capabilities'] ?? [];
@@ -57,11 +105,24 @@ class Role extends Base {
$allow = $this->request->post['allow'] ?? [];
$deny = $this->request->post['deny'] ?? [];
$capabilities = $this->request->post['capabilities'] ?? [];
- $permissions = ['deny' => $deny, 'allow' => $allow, 'capabilities' => $capabilities];
+ $permissions = [$this->app => ['deny' => $deny, 'allow' => $allow, 'capabilities' => $capabilities]];
$role_id = $this->request->get['role_id'] ?? false;
- $role = new RoleSQL();
+ $role = new RoleSQL();
+ $this->view->role = $role->get(['role_id' => $role_id]);
+ $currentPermissions = json_decode($this->view->role['permissions'], true);
+
+ if ($currentPermissions) {
+ //backward compatibility
+ if (isset($currentPermissions['allow'])) {
+ $currentPermissions['admin'] = $currentPermissions;
+ unset($currentPermissions['allow'], $currentPermissions['deny'], $currentPermissions['capabilities']);
+ }
+
+ $currentPermissions[$this->app] = $permissions[$this->app];
+ $permissions = $currentPermissions;
+ }
if ($role_id) {
$result = $role->edit(['role_id' => $role_id, 'role' => $data + ['permissions' => json_encode($permissions)]]);
diff --git a/admin/controller/admin/roles.php b/admin/controller/admin/roles.php
index bd7f9d0..6265958 100755
--- a/admin/controller/admin/roles.php
+++ b/admin/controller/admin/roles.php
@@ -22,24 +22,16 @@
namespace Vvveb\Controller\Admin;
-use Vvveb\Controller\Base;
+use Vvveb\Controller\Listing;
-class Roles extends Base {
- private function save() {
- }
+class Roles extends Listing {
+ protected $type = 'role';
- function index() {
- $view = $this->view;
- $roles = new \Vvveb\Sql\RoleSQL();
+ protected $controller = 'role';
- $options = [
- 'type' => 'admin', //$this->type,
- ] + $this->global;
+ protected $listController = 'roles';
- $results = $roles->getAll($options);
+ protected $list = 'role';
- $view->roles = $results['role'];
- $view->count = $results['count'];
- $view->limit = $options['limit'];
- }
+ protected $module = 'admin';
}
diff --git a/admin/template/admin/role.tpl b/admin/template/admin/role.tpl
index d373951..7d73902 100755
--- a/admin/template/admin/role.tpl
+++ b/admin/template/admin/role.tpl
@@ -85,13 +85,24 @@ if ($tree) {
foreach($parent as $id => $permission) {
$uniq = Vvveb\System\Functions\Str::random(5);
$hasChildren = is_array($permission) && count($permission);
+ $rule = $path . ($path && $id != 'index' ? '/' : '') . ($id != 'index' ? $id : '') . ($hasChildren ? '/*' : '');
?>
//catch all data attributes
@permission [data-v-permission-*] = $permission['@@__data-v-permission-(*)__@@']
@permission [data-v-name] = $id
- @permission input[type="hidden"] = <?php echo ($path ? $path . '/' : $path) . $id;?>
+ @permission [data-v-rule] = $rule
+ @permission [data-v-name]|addClass = <?php
+ if ($this->app == 'rest' && !$hasChildren) {
+ if ($id == 'get') echo 'badge bg-success';
+ if ($id == 'post') echo 'badge bg-primary';
+ if ($id == 'put') echo 'badge bg-warning';
+ if ($id == 'patch') echo 'badge bg-info';
+ if ($id == 'delete') echo 'badge bg-danger';
+ }
+ ?>
+ @permission input[type="hidden"] = <?php echo $rule;?>
@permission input[type="checkbox"]|id = $uniq
- @permission|class = <?php if ($hasChildren) echo 'folder'; else 'file';?>
+ @permission|class = <?php if ($hasChildren) echo 'folder'; else echo 'file';?>
@permission|append = <?php
@@ -127,3 +138,22 @@ if(isset($this->capabilities) && is_array($this->capabilities)) {
@capability|after = <?php
}
}?>
+
+
+@app = [data-v-apps] [data-v-app]
+@app|deleteAllButFirstChild
+
+@app|before = <?php
+if(isset($this->apps) && is_array($this->apps)) {
+ foreach ($this->apps as $app => $options) {?>
+
+
+ @app [data-v-app-url]|innerText = $app
+ @app a[data-v-app-url]|href = <?php echo Vvveb\url('', ['app' => $app]);?>
+ @app [data-v-app-url]|addClass = <?php
+ if ($this->app == $app) echo 'active';
+ ?>
+
+ @app|after = <?php
+ }
+}?>
diff --git a/admin/template/admin/roles.tpl b/admin/template/admin/roles.tpl
index 29c621d..b654828 100755
--- a/admin/template/admin/roles.tpl
+++ b/admin/template/admin/roles.tpl
@@ -1,17 +1,2 @@
-import(common.tpl)
-import(pagination.tpl)
+import(listing.tpl, {"type":"role", "list": "roles"})
-[data-v-roles] [data-v-role]|deleteAllButFirstChild
-
-[data-v-roles] [data-v-role]|before = <?php
-if(isset($this->roles) && is_array($this->roles)) {
- //$pagination = $this->roles[$_roles_idx]['pagination'];
- foreach ($this->roles as $index => $role) {?>
-
- [data-v-roles] [data-v-role] [data-v-role-url]|href = <?php echo Vvveb\url(['module' => 'admin/role', 'role_id' => $role['role_id']]);?>
-
- [data-v-roles] [data-v-role] [data-v-*]|innerText = $role['@@__data-v-(*)__@@']
-
- [data-v-roles] [data-v-role]|after = <?php
- }
-}?>
\ No newline at end of file
diff --git a/system/user/admin.php b/system/user/admin.php
index 669ac20..212238b 100755
--- a/system/user/admin.php
+++ b/system/user/admin.php
@@ -22,10 +22,9 @@
namespace Vvveb\System\User;
+use function Vvveb\session as sess;
use Vvveb\Sql\AdminSQL;
use Vvveb\System\PageCache;
-use function Vvveb\session as sess;
-
class Admin extends Auth {
private static $namespace = 'admin';
@@ -61,9 +60,9 @@ class Admin extends Auth {
return $admin->add([self :: $namespace => $data]);
}
- public static function hasCapability($capability) {
+ public static function hasCapability($capability, $app = APP) {
$admin = sess(self :: $namespace, false);
- $capabilities = $admin['permissions']['capabilities'] ?? [];
+ $capabilities = $admin['permissions'][$app]['capabilities'] ?? $admin['permissions']['capabilities'] ?? [];
return in_array($capability, $capabilities);
}
@@ -81,9 +80,14 @@ class Admin extends Auth {
return in_array($site_id, $site_access);
}
- public static function hasPermission($permission) {
+ public static function hasPermission($permission, $app = APP) {
$admin = sess(self :: $namespace, false);
- $permissions = $admin['permissions'] ?: [];
+
+ if (! $admin) {
+ return false;
+ }
+
+ $permissions = ($admin['permissions'][$app] ?? $admin['permissions']) ?: [];
$allow = $permissions['allow'] ?? [];
$deny = $permissions['deny'] ?? [];
diff --git a/system/user/role.php b/system/user/role.php
index 5654af6..854e88a 100755
--- a/system/user/role.php
+++ b/system/user/role.php
@@ -25,6 +25,7 @@ namespace Vvveb\System\User;
use function Vvveb\pregMatch;
use function Vvveb\pregMatchAll;
use Vvveb\System\Event;
+use Vvveb\System\Sqlp\Sqlp;
class Role {
static private $data = null;
@@ -68,7 +69,6 @@ class Role {
$hasPermission = $allow && ! $deny;
$result[$perm] = $hasPermission;
}*/
-
$list = implode("\n", $permissions);
$allowList = [];
@@ -105,7 +105,7 @@ class Role {
}
}
- public static function mkmap($dir, &$path = []) {
+ public static function mkmap($dir, &$path = [], $app = 'admin') {
if ($dir[0] == '.') {
return;
}
@@ -123,56 +123,77 @@ class Role {
if (is_dir($dir . '/' . $file)) {
self::mkmap($dir . '/' . $file, $path[$name]);
} else {
- $path[$name][$filename] = self::getActions($dir . '/' . $file);
+ if (! in_array($filename, ['base', 'crud', 'listing', 'error404', 'error403', 'error500'])) {
+ //$path[$name][$filename] = self::getActions2($dir . '/' . $file);
+ $class = str_replace(DIR_ROOT . $app . DS . 'controller' . DS, '', $dir . DS . $filename);
+ $class = str_replace('/', '\\', $class);
+ //echo $class;
+ $actions = self::getActions($class, $filename);
+
+ if ($actions) {
+ $path[$name][$filename] = $actions;
+ }
+ }
}
}
closedir($dh);
}
}
- return;
+ return [];
+ }
+
+ public static function controllers($app = 'admin') {
+ $tree = [];
+ self::mkmap(DIR_ROOT . $app . DS . 'controller', $tree);
+
+ return $tree['controller'] ?? [];
}
public static function getActions($file) {
- $permission = substr($file, strpos($file, '/controller/') + 12);
- //remove extension
- $permission = substr($permission, 0, strrpos($permission, '.'));
+ //echo $file;
+
+ $permission = str_replace('\\', '/', $file);
+ $class = '\\Vvveb\\Controller\\' . $file;
//if plugin add namespace
if (strpos($file, 'plugins/')) {
$pluginName = pregMatch('@/plugins/(.+?)/@', $file, 1);
$permission = "plugins/$pluginName$permission";
}
- $data = [];
- //$data[$permission] = $permission;
+ if (class_exists($class)) {
+ $methods = get_class_methods($class);
- $controllerCode = file_get_contents($file);
- //get all public methods
- $methods = pregMatchAll('/(?<!private|protected)\s+function.+?(\w+)\(/', $controllerCode, 1);
+ $data = [];
+ //add index by default
+ $data['index'] = $permission;
- if ($methods) {
- foreach ($methods as $method) {
- //ignore constructor
- if ($method[0] != '_') {
- if ($method == 'index') {
- $data[$method] = $permission;
- } else {
- $data[$method] = "$permission/$method";
+ if ($methods) {
+ foreach ($methods as $method) {
+ //ignore constructor
+ if ($method[0] != '_' && $method != 'init' && $method != 'goToHelp') {
+ if ($method == 'index') {
+ $data[$method] = $permission;
+ } else {
+ $data[$method] = "$permission/$method";
+ }
}
}
}
+
+ return $data;
}
- return $data;
+ return [];
}
- public static function getControllerList() {
+ public static function getControllerList($app = 'admin') {
if (self :: $data) {
return self :: $data;
}
$files = [];
- $path = [DIR_APP . '/controller/*', DIR_PLUGINS . '*/admin/controller/'];
+ $path = [DIR_APP . '/controller/*', DIR_PLUGINS . '*/' . $app . '/controller/'];
while (count($path) > 0) {
$next = array_shift($path);
@@ -183,7 +204,9 @@ class Role {
}
if (is_file($file)) {
- $files[] = $file;
+ if (! in_array(basename($file, '.php'), ['base', 'crud', 'listing', 'error404', 'error403', 'error500'])) {
+ $files[] = $file;
+ }
}
}
}
@@ -210,6 +233,9 @@ class Role {
//get all public methods
$methods = pregMatchAll('/(?<!private)\s+function.+?(\w+)\(/', $controllerCode, 1);
+ //add index by default
+ $data['permissions'][] = $permission;
+
if ($methods) {
foreach ($methods as $method) {
//ignore constructor
@@ -229,10 +255,79 @@ class Role {
return $data;
}
- public static function getCapabilitiesList() {
+ public static function getCapabilitiesList($app = 'admin') {
$capabilities = include DIR_SYSTEM . 'data' . DS . 'capabilities.php';
list($capabilities) = Event :: trigger(__CLASS__,__FUNCTION__, $capabilities);
return $capabilities;
}
+
+ public static function routes($app = 'app') {
+ $routes = \Vvveb\config("$app-routes");
+ $routeSchemas = [];
+
+ foreach ($routes as $route => $options) {
+ //rest
+
+ //$path = $options['module'];//str_replace('/rest/', '/', $route);
+ $path = str_replace('/rest/', '', $route);
+
+ if (! $path) {
+ $path = 'index';
+ }
+
+ if (isset($options['schema'])) {
+ //$path = $options['schema'];
+ }
+
+ if (strpos($path, '/')) {
+ $slash = explode('/', $path);
+
+ if ($slash) {
+ $current = &$routeSchemas;
+
+ foreach ($slash as $name) {
+ if (! isset($current[$name])) {
+ $current[$name] = [];
+ }
+ $current = &$current[$name];
+ }
+ }
+ } else {
+ $current = &$routeSchemas[$path];
+ }
+
+ if (isset($options['methods'])) {
+ foreach ($options['methods'] as $method) {
+ $current[$method] = $path . '/' . $method;
+ }
+ } else {
+ //$routeSchemas[$options['module']] = $options['module'];
+ }
+ }
+
+ return $routeSchemas;
+ }
+
+ public static function models($app = 'admin') {
+ $dirSQL = DIR_ROOT . 'admin' . DS . 'sql' . DS . DB_ENGINE . DS;
+ $sqlp = new Sqlp();
+ $models = [];
+
+ $files = glob("$dirSQL*.sql");
+ //var_dump($files);
+
+ foreach ($files as $file) {
+ $sqlp->parseSqlPfile($file);
+ $tree = $sqlp->getModel();
+ $model = basename($file, '.sql');
+
+ foreach ($tree as $method => $options) {
+ $methodName = $model . '/' . $method;
+ $models[$model][$method] = $methodName;
+ }
+ }
+
+ return $models;
+ }
}