diff options
Diffstat (limited to 'modules-available/permissionmanager/inc/getpermissiondata.inc.php')
-rw-r--r-- | modules-available/permissionmanager/inc/getpermissiondata.inc.php | 55 |
1 files changed, 35 insertions, 20 deletions
diff --git a/modules-available/permissionmanager/inc/getpermissiondata.inc.php b/modules-available/permissionmanager/inc/getpermissiondata.inc.php index 982fa0b7..660c94ae 100644 --- a/modules-available/permissionmanager/inc/getpermissiondata.inc.php +++ b/modules-available/permissionmanager/inc/getpermissiondata.inc.php @@ -1,27 +1,32 @@ <?php -class GetPermissionData { +class GetPermissionData +{ + + const WITH_USER_COUNT = 1; + const WITH_LOCATION_COUNT = 2; /** * Get data for all users. * * @return array array of users (each with userid, username and roles (each with roleid and rolename)) */ - public static function getUserData() { + public static function getUserData() + { $res = Database::simpleQuery("SELECT user.userid AS userid, user.login AS login, role.rolename AS rolename, role.roleid AS roleid FROM user - LEFT JOIN user_x_role ON user.userid = user_x_role.userid - LEFT JOIN role ON user_x_role.roleid = role.roleid + LEFT JOIN role_x_user ON user.userid = role_x_user.userid + LEFT JOIN role ON role_x_user.roleid = role.roleid "); - $userdata= array(); + $userdata = array(); while ($row = $res->fetch(PDO::FETCH_ASSOC)) { - $userdata[$row['userid'].' '.$row['login']][] = array( + $userdata[$row['userid'] . ' ' . $row['login']][] = array( 'roleid' => $row['roleid'], 'rolename' => $row['rolename'] ); } $data = array(); - foreach($userdata AS $user => $roles) { + foreach ($userdata AS $user => $roles) { $user = explode(" ", $user, 2); $data[] = array( 'userid' => $user[0], @@ -37,8 +42,9 @@ class GetPermissionData { * * @return array array of locations (each including the roles that have permissions for them) */ - public static function getLocationData() { - $res = Database::simpleQuery("SELECT role.roleid as roleid, rolename, GROUP_CONCAT(COALESCE(locationid, 0)) AS locationids FROM role + public static function getLocationData() + { + $res = Database::simpleQuery("SELECT role.roleid AS roleid, rolename, GROUP_CONCAT(COALESCE(locationid, 0)) AS locationids FROM role INNER JOIN role_x_location ON role.roleid = role_x_location.roleid GROUP BY roleid ORDER BY rolename ASC"); $locations = Location::getLocations(0, 0, false, true); while ($row = $res->fetch(PDO::FETCH_ASSOC)) { @@ -61,18 +67,26 @@ class GetPermissionData { /** * Get all roles. * + * @param int $flags Bitmask specifying additional data to fetch (WITH_* constants of this class) * @return array array roles (each with roleid and rolename) */ - public static function getRoles() { - $res = Database::simpleQuery("SELECT roleid, rolename FROM role ORDER BY rolename ASC"); - $data = array(); - while ($row = $res->fetch(PDO::FETCH_ASSOC)) { - $data[] = array( - 'roleid' => $row['roleid'], - 'rolename' => $row['rolename'] - ); + public static function getRoles($flags = 0) + { + $cols = $joins = ''; + if ($flags & self::WITH_USER_COUNT) { + $cols .= ', Count(DISTINCT rxu.userid) AS users'; + $joins .= ' LEFT JOIN role_x_user rxu ON (r.roleid = rxu.roleid)'; } - return $data; + if ($flags & self::WITH_LOCATION_COUNT) { + $cols .= ', Count(DISTINCT rxl.locationid) AS locations'; + $joins .= ' LEFT JOIN role_x_location rxl ON (r.roleid = rxl.roleid)'; + } + if (!empty($joins)) { + $joins .= ' GROUP BY r.roleid'; + } + return Database::queryAll("SELECT r.roleid, r.rolename, r.roledescription $cols FROM role r + $joins + ORDER BY rolename ASC"); } /** @@ -81,8 +95,9 @@ class GetPermissionData { * @param string $roleid id of the role * @return array array containing an array of permissions and an array of locations */ - public static function getRoleData($roleid) { - $query = "SELECT roleid, rolename FROM role WHERE roleid = :roleid"; + public static function getRoleData($roleid) + { + $query = "SELECT roleid, rolename, roledescription FROM role WHERE roleid = :roleid"; $data = Database::queryFirst($query, array("roleid" => $roleid)); $query = "SELECT roleid, locationid FROM role_x_location WHERE roleid = :roleid"; $res = Database::simpleQuery($query, array("roleid" => $roleid)); |