From d033483d3ed9e05f1109a3713ec6a3df883a1dae Mon Sep 17 00:00:00 2001 From: Udo Walter Date: Fri, 7 Apr 2017 15:49:54 +0200 Subject: [permission-manager] renamed some files, moved html generation to templates, using bind variables in mysql code, changed order of modules in role editor --- .../permissionmanager/inc/dbupdate.inc.php | 54 ------- .../permissionmanager/inc/getdata.inc.php | 97 ------------- .../inc/getpermissiondata.inc.php | 97 +++++++++++++ .../inc/permissiondbupdate.inc.php | 57 ++++++++ .../permissionmanager/inc/permissionutil.inc.php | 10 +- .../permissionmanager/install.inc.php | 6 +- modules-available/permissionmanager/page.inc.php | 67 ++++----- .../templates/locationsTable.html | 25 ---- .../templates/locationstable.html | 25 ++++ .../templates/modulepermissionbox.html | 11 ++ .../permissionmanager/templates/permission.html | 6 + .../templates/permissiontreenode.html | 11 ++ .../permissionmanager/templates/roleEditor.html | 148 ------------------- .../permissionmanager/templates/roleeditor.html | 147 +++++++++++++++++++ .../permissionmanager/templates/rolesTable.html | 65 --------- .../permissionmanager/templates/rolestable.html | 65 +++++++++ .../permissionmanager/templates/usersTable.html | 160 --------------------- .../permissionmanager/templates/userstable.html | 160 +++++++++++++++++++++ 18 files changed, 618 insertions(+), 593 deletions(-) delete mode 100644 modules-available/permissionmanager/inc/dbupdate.inc.php delete mode 100644 modules-available/permissionmanager/inc/getdata.inc.php create mode 100644 modules-available/permissionmanager/inc/getpermissiondata.inc.php create mode 100644 modules-available/permissionmanager/inc/permissiondbupdate.inc.php delete mode 100644 modules-available/permissionmanager/templates/locationsTable.html create mode 100644 modules-available/permissionmanager/templates/locationstable.html create mode 100644 modules-available/permissionmanager/templates/modulepermissionbox.html create mode 100644 modules-available/permissionmanager/templates/permission.html create mode 100644 modules-available/permissionmanager/templates/permissiontreenode.html delete mode 100644 modules-available/permissionmanager/templates/roleEditor.html create mode 100644 modules-available/permissionmanager/templates/roleeditor.html delete mode 100644 modules-available/permissionmanager/templates/rolesTable.html create mode 100644 modules-available/permissionmanager/templates/rolestable.html delete mode 100644 modules-available/permissionmanager/templates/usersTable.html create mode 100644 modules-available/permissionmanager/templates/userstable.html (limited to 'modules-available/permissionmanager') diff --git a/modules-available/permissionmanager/inc/dbupdate.inc.php b/modules-available/permissionmanager/inc/dbupdate.inc.php deleted file mode 100644 index 1101e4f7..00000000 --- a/modules-available/permissionmanager/inc/dbupdate.inc.php +++ /dev/null @@ -1,54 +0,0 @@ -fetch(PDO::FETCH_ASSOC)) { - $data[] = array( - 'userid' => $row['userid'], - 'name' => $row['login'], - 'role' => explode(",",$row['role']) - ); - } - return $data; - } - - // get LocationIDs, Location Names, Roles of each Location - public static function getLocationData() { - $res = self::queryLocationData(); - $data = array(); - while ($row = $res->fetch(PDO::FETCH_ASSOC)) { - $data[] = array( - 'locid' => $row['locid'], - 'name' => $row['locname'], - 'role' => explode(",",$row['role']) - ); - } - return $data; - } - - // get all roles from database (id and name) - public static function getRoles() { - $res = Database::simpleQuery("SELECT id, name FROM role ORDER BY name ASC"); - $data = array(); - while ($row = $res->fetch(PDO::FETCH_ASSOC)) { - $data[] = array( - 'roleId' => $row['id'], - 'roleName' => $row['name'] - ); - } - return $data; - } - - public static function getLocations($selected) { - $res = Database::simplequery("SELECT locationid, locationname FROM location"); - $data = array(); - while ($row = $res->fetch(PDO::FETCH_ASSOC)) { - $data[] = array('locid' => $row['locationid'], 'locName' => $row['locationname'], - 'selected' => in_array($row['locationid'], $selected) ? "selected" : ""); - } - return $data; - } - - public static function getRoleData($roleID) { - $query = "SELECT id, name FROM role WHERE id = $roleID"; - $data = Database::queryFirst($query); - $query = "SELECT roleid, locid FROM roleXlocation WHERE roleid = $roleID"; - $res = Database::simpleQuery($query); - $data["locations"] = array(); - while ($row = $res->fetch(PDO::FETCH_ASSOC)) { - $data["locations"][] = $row['locid']; - } - $query = "SELECT roleid, permissionid FROM roleXpermission WHERE roleid = $roleID"; - $res = Database::simpleQuery($query); - $data["permissions"] = array(); - while ($row = $res->fetch(PDO::FETCH_ASSOC)) { - $data["permissions"][] = $row['permissionid']; - } - return $data; - } - - // UserID, User Login Name, Roles of each User - private static function queryUserData() { - $res = Database::simpleQuery("SELECT user.userid AS userid, user.login AS login, GROUP_CONCAT(role.name ORDER BY role.name ASC) AS role - FROM user - LEFT JOIN userXrole ON user.userid = userXrole.userid - LEFT JOIN role ON userXrole.roleid = role.id - GROUP BY user.userid - "); - return $res; - } - - // LocationID, Location Name, Roles of each Location - private static function queryLocationData() { - $res = Database::simpleQuery("SELECT location.locationid AS locid, location.locationname AS locname, GROUP_CONCAT(role.name ORDER BY role.name ASC) AS role - FROM location - LEFT JOIN roleXlocation ON location.locationid = roleXlocation.locid - LEFT JOIN role ON roleXlocation.roleid = role.id - GROUP BY location.locationid - ORDER BY location.locationname - "); - return $res; - } - -} \ No newline at end of file diff --git a/modules-available/permissionmanager/inc/getpermissiondata.inc.php b/modules-available/permissionmanager/inc/getpermissiondata.inc.php new file mode 100644 index 00000000..be7ddb1c --- /dev/null +++ b/modules-available/permissionmanager/inc/getpermissiondata.inc.php @@ -0,0 +1,97 @@ +fetch(PDO::FETCH_ASSOC)) { + $data[] = array( + 'userid' => $row['userid'], + 'name' => $row['login'], + 'role' => explode(",",$row['role']) + ); + } + return $data; + } + + // get LocationIDs, Location Names, Roles of each Location + public static function getLocationData() { + $res = self::queryLocationData(); + $data = array(); + while ($row = $res->fetch(PDO::FETCH_ASSOC)) { + $data[] = array( + 'locid' => $row['locid'], + 'name' => $row['locname'], + 'role' => explode(",",$row['role']) + ); + } + return $data; + } + + // get all roles from database (id and name) + public static function getRoles() { + $res = Database::simpleQuery("SELECT id, name FROM role ORDER BY name ASC"); + $data = array(); + while ($row = $res->fetch(PDO::FETCH_ASSOC)) { + $data[] = array( + 'roleId' => $row['id'], + 'roleName' => $row['name'] + ); + } + return $data; + } + + public static function getLocations($selected) { + $res = Database::simplequery("SELECT locationid, locationname FROM location"); + $data = array(); + while ($row = $res->fetch(PDO::FETCH_ASSOC)) { + $data[] = array('locid' => $row['locationid'], 'locName' => $row['locationname'], + 'selected' => in_array($row['locationid'], $selected) ? "selected" : ""); + } + return $data; + } + + public static function getRoleData($roleId) { + $query = "SELECT id, name FROM role WHERE id = :roleId"; + $data = Database::queryFirst($query, array("roleId" => $roleId)); + $query = "SELECT roleid, locid FROM role_x_location WHERE roleid = :roleId"; + $res = Database::simpleQuery($query, array("roleId" => $roleId)); + $data["locations"] = array(); + while ($row = $res->fetch(PDO::FETCH_ASSOC)) { + $data["locations"][] = $row['locid']; + } + $query = "SELECT roleid, permissionid FROM role_x_permission WHERE roleid = :roleId"; + $res = Database::simpleQuery($query, array("roleId" => $roleId)); + $data["permissions"] = array(); + while ($row = $res->fetch(PDO::FETCH_ASSOC)) { + $data["permissions"][] = $row['permissionid']; + } + return $data; + } + + // UserID, User Login Name, Roles of each User + private static function queryUserData() { + $res = Database::simpleQuery("SELECT user.userid AS userid, user.login AS login, GROUP_CONCAT(role.name ORDER BY role.name ASC) AS role + FROM user + LEFT JOIN user_x_role ON user.userid = user_x_role.userid + LEFT JOIN role ON user_x_role.roleid = role.id + GROUP BY user.userid + "); + return $res; + } + + // LocationID, Location Name, Roles of each Location + private static function queryLocationData() { + $res = Database::simpleQuery("SELECT location.locationid AS locid, location.locationname AS locname, GROUP_CONCAT(role.name ORDER BY role.name ASC) AS role + FROM location + LEFT JOIN role_x_location ON location.locationid = role_x_location.locid + LEFT JOIN role ON role_x_location.roleid = role.id + GROUP BY location.locationid + ORDER BY location.locationname + "); + return $res; + } + +} \ No newline at end of file diff --git a/modules-available/permissionmanager/inc/permissiondbupdate.inc.php b/modules-available/permissionmanager/inc/permissiondbupdate.inc.php new file mode 100644 index 00000000..87c989fa --- /dev/null +++ b/modules-available/permissionmanager/inc/permissiondbupdate.inc.php @@ -0,0 +1,57 @@ + $user, "role" => $role)); + } + } + } + + // remove user_x_role entry from database + public static function removeRoleFromUser($users, $roles) { + foreach($users AS $user) { + foreach ($roles AS $role) { + $query = "DELETE FROM user_x_role WHERE userid = :user AND roleid = :role"; + Database::exec($query, array("user" => $user, "role" => $role)); + } + } + } + + // delete role, delete user_x_role relationships, delete role_x_location relationships, delete role_x_permission relationships + public static function deleteRole($id) { + $query = "DELETE FROM role WHERE id = :id"; + Database::exec($query, array("id" => $id)); + $query = "DELETE FROM user_x_role WHERE roleid = :id"; + Database::exec($query, array("id" => $id)); + $query = "DELETE FROM role_x_location WHERE roleid = :id"; + Database::exec($query, array("id" => $id)); + $query = "DELETE FROM role_x_permission WHERE roleid = :id"; + Database::exec($query, array("id" => $id)); + } + + public static function saveRole($roleName, $locations, $permissions, $role = NULL) { + if ($role) { + Database::exec("UPDATE role SET name = :roleName WHERE id = :role", + array("roleName" => $roleName, "role" => $role)); + Database::exec("DELETE FROM role_x_location WHERE roleid = :role", array("role" => $role)); + Database::exec("DELETE FROM role_x_permission WHERE roleid = :role", array("role" => $role)); + } else { + Database::exec("INSERT INTO role (name) VALUES (:roleName)", array("roleName" => $roleName)); + $role = Database::lastInsertId(); + } + foreach ($locations as $locID) { + Database::exec("INSERT INTO role_x_location (roleid, locid) VALUES (:role, :locid)", + array("role" => $role, "locid" => $locID)); + } + foreach ($permissions as $permission) { + Database::exec("INSERT INTO role_x_permission (roleid, permissionid) VALUES (:role, :permission)", + array("role" => $role, "permission" => $permission)); + } + } + +} diff --git a/modules-available/permissionmanager/inc/permissionutil.inc.php b/modules-available/permissionmanager/inc/permissionutil.inc.php index 10f2a61a..d6adf2bf 100644 --- a/modules-available/permissionmanager/inc/permissionutil.inc.php +++ b/modules-available/permissionmanager/inc/permissionutil.inc.php @@ -16,10 +16,18 @@ class PermissionUtil $permissions = self::putInPermissionTree($out[1].".".$k, $v, $permissions); } } + ksort($permissions); + global $MENU_CAT_OVERRIDE; + $sortingOrder = $MENU_CAT_OVERRIDE; + foreach ($permissions as $module => $v) $sortingOrder[Module::get($module)->getCategory()][] = $module; + $permissions = array_replace(array_flip(call_user_func_array('array_merge', $sortingOrder)), $permissions); + foreach ($permissions as $module => $v) if (is_int($v)) unset($permissions[$module]); + + return $permissions; } - private function putInPermissionTree($permission, $description, $tree) + private static function putInPermissionTree($permission, $description, $tree) { $subPermissions = explode('.', $permission); $original =& $tree; diff --git a/modules-available/permissionmanager/install.inc.php b/modules-available/permissionmanager/install.inc.php index 2d408f38..8c882498 100644 --- a/modules-available/permissionmanager/install.inc.php +++ b/modules-available/permissionmanager/install.inc.php @@ -8,19 +8,19 @@ $res[] = tableCreate('role', " PRIMARY KEY (`id`) "); -$res[] = tableCreate('userXrole', " +$res[] = tableCreate('user_x_role', " `userid` int(10) unsigned NOT NULL, `roleid` int(10) unsigned NOT NULL, PRIMARY KEY (`userid`, `roleid`) "); -$res[] = tableCreate('roleXlocation', " +$res[] = tableCreate('role_x_location', " `roleid` int(10) unsigned NOT NULL, `locid` int(10) unsigned NOT NULL, PRIMARY KEY (`roleid`, `locid`) "); -$res[] = tableCreate('roleXpermission', " +$res[] = tableCreate('role_x_permission', " `roleid` int(10) unsigned NOT NULL, `permissionid` varchar(200) NOT NULL, PRIMARY KEY (`roleid`, `permissionid`) diff --git a/modules-available/permissionmanager/page.inc.php b/modules-available/permissionmanager/page.inc.php index 15d2fb73..3c72cfe4 100644 --- a/modules-available/permissionmanager/page.inc.php +++ b/modules-available/permissionmanager/page.inc.php @@ -19,20 +19,20 @@ class Page_PermissionManager extends Page if ($action === 'addRoleToUser') { $users = Request::post('users', ''); $roles = Request::post('roles', ''); - DbUpdate::addRoleToUser($users, $roles); + PermissionDbUpdate::addRoleToUser($users, $roles); } elseif ($action === 'removeRoleFromUser') { $users = Request::post('users', ''); $roles = Request::post('roles', ''); - DbUpdate::removeRoleFromUser($users, $roles); + PermissionDbUpdate::removeRoleFromUser($users, $roles); } elseif ($action === 'deleteRole') { $id = Request::post('deleteId', false, 'string'); - DbUpdate::deleteRole($id); + PermissionDbUpdate::deleteRole($id); } elseif ($action === 'saveRole') { $roleID = Request::post("roleid", false); $roleName = Request::post("roleName"); $locations = Request::post("allLocations", "off") == "on" ? array(0) : Request::post("locations"); $permissions = Request::post("allPermissions", "off") == "on" ? array("*") : Request::post("permissions");; - DbUpdate::saveRole($roleName, $locations, $permissions, $roleID); + PermissionDbUpdate::saveRole($roleName, $locations, $permissions, $roleID); } } @@ -55,14 +55,14 @@ class Page_PermissionManager extends Page Render::closeTag('div'); if ($show === "roles") { - $data = array("roles" => GetData::getRoles()); - Render::addTemplate('rolesTable', $data); + $data = array("roles" => GetPermissionData::getRoles()); + Render::addTemplate('rolestable', $data); } elseif ($show === "users") { - $data = array("user" => GetData::getUserData(), "roles" => GetData::getRoles()); - Render::addTemplate('usersTable', $data); + $data = array("user" => GetPermissionData::getUserData(), "roles" => GetPermissionData::getRoles()); + Render::addTemplate('userstable', $data); } elseif ($show === "locations") { - $data = array("location" => GetData::getLocationData()); - Render::addTemplate('locationsTable', $data); + $data = array("location" => GetPermissionData::getLocationData()); + Render::addTemplate('locationstable', $data); } } elseif ($show === "roleEditor") { $data = array(); @@ -70,7 +70,7 @@ class Page_PermissionManager extends Page $roleID = Request::get("roleid", false); $selectedLocations = array(); if ($roleID) { - $roleData = GetData::getRoleData($roleID); + $roleData = GetPermissionData::getRoleData($roleID); $data["roleid"] = $roleID; $data["roleName"] = $roleData["name"]; if (count($roleData["locations"]) == 1 && $roleData["locations"][0] == 0) { @@ -94,7 +94,7 @@ class Page_PermissionManager extends Page $permissions = PermissionUtil::getPermissions(); $permissionHTML = ""; foreach ($permissions as $k => $v) { - $name = Dictionary::translateFileModule($k, "module", "module_name"); + $name = Module::get($k)->getDisplayName(); $permissionHTML .= "
- "; - $permissionHTML .= self::generateSubPermissionHTML($v, $k); + "; + $permissionHTML .= self::generatePermissionHTML($v, $k); $permissionHTML .= "
"; } - $data["locations"] = GetData::getLocations($selectedLocations); + $data["locations"] = GetPermissionData::getLocations($selectedLocations); $data["moduleNames"] = array(); foreach (array_keys($permissions) as $moduleid) { - $data["moduleNames"][] = array("id" => $moduleid, - "name" => Dictionary::translateFileModule($moduleid, "module", "module_name")); + $data["moduleNames"][] = array("id" => $moduleid, "name" => Module::get($moduleid)->getDisplayName()); } $data["permissionHTML"] = $permissionHTML; - Render::addTemplate('roleEditor', $data); + Render::addTemplate('roleeditor', $data); + } } @@ -143,31 +143,18 @@ class Page_PermissionManager extends Page return $buttonColors; } - private static function generateSubPermissionHTML($subPermissions, $permissionString) + private static function generatePermissionHTML($subPermissions, $permString) { - $html = ""; - return $html; + return $res; } } diff --git a/modules-available/permissionmanager/templates/locationsTable.html b/modules-available/permissionmanager/templates/locationsTable.html deleted file mode 100644 index e170cd02..00000000 --- a/modules-available/permissionmanager/templates/locationsTable.html +++ /dev/null @@ -1,25 +0,0 @@ -
-
- - - - - - - - - - {{#location}} - - - - - {{/location}} - -
{{lang_Locations}}{{lang_Roles}}
{{name}} - {{#role}} - {{.}} - {{/role}} -
-
-
\ No newline at end of file diff --git a/modules-available/permissionmanager/templates/locationstable.html b/modules-available/permissionmanager/templates/locationstable.html new file mode 100644 index 00000000..e170cd02 --- /dev/null +++ b/modules-available/permissionmanager/templates/locationstable.html @@ -0,0 +1,25 @@ +
+
+ + + + + + + + + + {{#location}} + + + + + {{/location}} + +
{{lang_Locations}}{{lang_Roles}}
{{name}} + {{#role}} + {{.}} + {{/role}} +
+
+
\ No newline at end of file diff --git a/modules-available/permissionmanager/templates/modulepermissionbox.html b/modules-available/permissionmanager/templates/modulepermissionbox.html new file mode 100644 index 00000000..d298409b --- /dev/null +++ b/modules-available/permissionmanager/templates/modulepermissionbox.html @@ -0,0 +1,11 @@ + \ No newline at end of file diff --git a/modules-available/permissionmanager/templates/permission.html b/modules-available/permissionmanager/templates/permission.html new file mode 100644 index 00000000..3cc0bf97 --- /dev/null +++ b/modules-available/permissionmanager/templates/permission.html @@ -0,0 +1,6 @@ +
  • +
    + + +
    +
  • \ No newline at end of file diff --git a/modules-available/permissionmanager/templates/permissiontreenode.html b/modules-available/permissionmanager/templates/permissiontreenode.html new file mode 100644 index 00000000..e9656902 --- /dev/null +++ b/modules-available/permissionmanager/templates/permissiontreenode.html @@ -0,0 +1,11 @@ + \ No newline at end of file diff --git a/modules-available/permissionmanager/templates/roleEditor.html b/modules-available/permissionmanager/templates/roleEditor.html deleted file mode 100644 index ee717fe6..00000000 --- a/modules-available/permissionmanager/templates/roleEditor.html +++ /dev/null @@ -1,148 +0,0 @@ -
    - - - -
    -
    - {{lang_Name}}: - - - -
    -
    -
    -
    - {{lang_Locations}}: -
    -
    -
    - -
    -
    - {{abc}} -
    -
    - {{lang_Permissions}}: -
    -
    - {{#moduleNames}} - - {{/moduleNames}} -
    -
    -
    - {{{permissionHTML}}} -
    -
    -
    - - \ No newline at end of file diff --git a/modules-available/permissionmanager/templates/roleeditor.html b/modules-available/permissionmanager/templates/roleeditor.html new file mode 100644 index 00000000..7de8eaf3 --- /dev/null +++ b/modules-available/permissionmanager/templates/roleeditor.html @@ -0,0 +1,147 @@ +
    + + + +
    +
    + {{lang_Name}}: + + + +
    +
    +
    +
    + {{lang_Locations}}: +
    +
    +
    + +
    +
    +
    +
    + {{lang_Permissions}}: +
    +
    + {{#moduleNames}} + + {{/moduleNames}} +
    +
    +
    + {{{permissionHTML}}} +
    +
    +
    + + \ No newline at end of file diff --git a/modules-available/permissionmanager/templates/rolesTable.html b/modules-available/permissionmanager/templates/rolesTable.html deleted file mode 100644 index 40cdda96..00000000 --- a/modules-available/permissionmanager/templates/rolesTable.html +++ /dev/null @@ -1,65 +0,0 @@ -
    - - - - -
    -
    - - - - - - - - - - - {{#roles}} - - - - - - {{/roles}} - -
    {{lang_Roles}}{{lang_Edit}}{{lang_Delete}}
    {{roleName}} - {{lang_Edit}} - - {{lang_Delete}} -
    -
    -
    - - - - - -
    - - \ No newline at end of file diff --git a/modules-available/permissionmanager/templates/rolestable.html b/modules-available/permissionmanager/templates/rolestable.html new file mode 100644 index 00000000..40cdda96 --- /dev/null +++ b/modules-available/permissionmanager/templates/rolestable.html @@ -0,0 +1,65 @@ +
    + + + + +
    +
    + + + + + + + + + + + {{#roles}} + + + + + + {{/roles}} + +
    {{lang_Roles}}{{lang_Edit}}{{lang_Delete}}
    {{roleName}} + {{lang_Edit}} + + {{lang_Delete}} +
    +
    +
    + + + + + +
    + + \ No newline at end of file diff --git a/modules-available/permissionmanager/templates/usersTable.html b/modules-available/permissionmanager/templates/usersTable.html deleted file mode 100644 index 3db1ebdb..00000000 --- a/modules-available/permissionmanager/templates/usersTable.html +++ /dev/null @@ -1,160 +0,0 @@ -
    - - - - -
    -
    - - - - - - - - - - - {{#user}} - - - - - - {{/user}} - -
    {{lang_Users}}{{lang_Roles}}{{lang_Selected}}
    {{name}} - {{#role}} - {{.}} - {{/role}} - -
    - - -
    -
    -
    -
    - - - - - -
    - - \ No newline at end of file diff --git a/modules-available/permissionmanager/templates/userstable.html b/modules-available/permissionmanager/templates/userstable.html new file mode 100644 index 00000000..3db1ebdb --- /dev/null +++ b/modules-available/permissionmanager/templates/userstable.html @@ -0,0 +1,160 @@ +
    + + + + +
    +
    + + + + + + + + + + + {{#user}} + + + + + + {{/user}} + +
    {{lang_Users}}{{lang_Roles}}{{lang_Selected}}
    {{name}} + {{#role}} + {{.}} + {{/role}} + +
    + + +
    +
    +
    +
    + + + + + +
    + + \ No newline at end of file -- cgit v1.2.3-55-g7522