From d0b625352cc8acc160e22cb2f4e9ae8e10f753f6 Mon Sep 17 00:00:00 2001 From: Georg Ehrke Date: Mon, 9 Jul 2012 21:51:19 +0200 Subject: [PATCH] some work on subadmins --- lib/group.php | 13 +++++ lib/subadmin.php | 107 +++++++++++++++++++++++++++++++++++ lib/util.php | 22 ++++++- settings/templates/users.php | 22 ++++++- settings/users.php | 25 +++++--- 5 files changed, 178 insertions(+), 11 deletions(-) create mode 100644 lib/subadmin.php diff --git a/lib/group.php b/lib/group.php index ceee5fa4ed..fb280c157e 100644 --- a/lib/group.php +++ b/lib/group.php @@ -271,4 +271,17 @@ class OC_Group { } return $users; } + + /** + * @brief get a list of all users in several groups + * @param array $gids + * @returns array with user ids + */ + public static function usersInGroups($gids){ + $users = array(); + foreach($gids as $gid){ + $users = array_merge(array_diff(self::usersInGroup($gid), $users), $users); + } + return $users; + } } diff --git a/lib/subadmin.php b/lib/subadmin.php new file mode 100644 index 0000000000..aad657b024 --- /dev/null +++ b/lib/subadmin.php @@ -0,0 +1,107 @@ +. + * + */ + +/** + * This class provides all methods needed for managing groups. + * + * Hooks provided: + * post_createSubAdmin($gid) + * post_deleteSubAdmin($gid) + */ +class OC_SubAdmin{ + + /** + * @brief add a SubAdmin + * @param $uid uid of the SubAdmin + * @param $gid gid of the group + * @return boolean + */ + public static function createSubAdmin($uid, $gid){ + $stmt = OC_DB::prepare('INSERT INTO *PREFIX*group_admin (gid,uid) VALUES(?,?)'); + $result = $stmt->execute(array($gid, $uid)); + if(OC_DB::isError($result)){ + return false; + } + OC_Hook::emit( "OC_SubAdmin", "post_createSubAdmin", array( "gid" => $gid )); + return true; + } + + /** + * @brief delete a SubAdmin + * @param $uid uid of the SubAdmin + * @param $gid gid of the group + * @return boolean + */ + public static function deleteSubAdmin($uid, $gid){ + $stmt = OC_DB::prepare('DELETE FROM *PREFIX*group_admin WHERE gid = ? AND uid = ?'); + $result = $stmt->execute(array($gid, $uid)); + if(OC_DB::isError($result)){ + return false; + } + OC_Hook::emit( "OC_SubAdmin", "post_deleteSubAdmin", array( "gid" => $gid )); + return true; + } + + /** + * @brief get groups of a SubAdmin + * @param $uid uid of the SubAdmin + * @return array + */ + public static function getSubAdminsGroups($uid){ + $stmt = OC_DB::prepare('SELECT gid FROM *PREFIX*group_admin WHERE uid = ?'); + $result = $stmt->execute(array($gid, $uid)); + $gids = array(); + while($row = $result->fetchRow()){ + $gids[] = $row['gid']; + } + return $gids; + } + + /** + * @brief get SubAdmins of a group + * @param $gid gid of the group + * @return array + */ + public static function getGroupsSubAdmins($gid){ + $stmt = OC_DB::prepare('SELECT uid FROM *PREFIX*group_admin WHERE gid = ?'); + $result = $stmt->execute(array($gid, $uid)); + $uids = array(); + while($row = $result->fetchRow()){ + $uids[] = $row['uid']; + } + return $uids; + } + + /** + * @brief get all SubAdmins + * @return array + */ + public static function getAllSubAdmins(){ + $stmt = OC_DB::prepare('SELECT * FROM *PREFIX*group_admin'); + $result = $stmt->execute(array($gid, $uid)); + $subadmins = array(); + while($row = $result->fetchRow()){ + $subadmins[] = $row; + } + return $subadmins; + } +} diff --git a/lib/util.php b/lib/util.php index 2a7b8a922f..de9171edc8 100755 --- a/lib/util.php +++ b/lib/util.php @@ -66,7 +66,7 @@ class OC_Util { * @return array */ public static function getVersion(){ - return array(4,80,1); + return array(4,81,2); } /** @@ -320,6 +320,26 @@ class OC_Util { } } + /** + * Check if the user is a subadmin, redirects to home if not + * @return array $groups where the current user is subadmin + */ + public static function checkSubAdminUser(){ + // Check if we are a user + self::checkLoggedIn(); + if(OC_Group::inGroup(OC_User::getUser(),'admin')){ + return OC_Group::getGroups(); + } + $stmt = OC_DB::prepare('SELECT COUNT(*) as count FROM *PREFIX*group_admin WHERE uid = ?'); + $result = $stmt->execute(array(OC_User::getUser())); + $result = $result->fetchRow(); + if($result['count'] == 0){ + header( 'Location: '.OC_Helper::linkToAbsolute( '', 'index.php' )); + exit(); + } + return $groups; + } + /** * Redirect to the user default page */ diff --git a/settings/templates/users.php b/settings/templates/users.php index 5511242456..b16aa1ae16 100644 --- a/settings/templates/users.php +++ b/settings/templates/users.php @@ -1,13 +1,14 @@ - * This file is licensed under the Affero General Public License version 3 or later. * See the COPYING-README file. */ - $allGroups=array(); foreach($_["groups"] as $group) { $allGroups[]=$group['name']; } +$_['subadmingroups'] = $_['groups']; ?>
@@ -60,6 +61,9 @@ foreach($_["groups"] as $group) { t('Name')?> t( 'Password' ); ?> t( 'Groups' ); ?> + + t('SubAdmins'); ?> + t( 'Quota' ); ?>   @@ -84,6 +88,20 @@ foreach($_["groups"] as $group) { + + + +