2012-07-09 19:51:19 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2015-02-26 10:37:37 +00:00
|
|
|
* ownCloud
|
2012-07-09 19:51:19 +00:00
|
|
|
*
|
2015-02-26 10:37:37 +00:00
|
|
|
* @author Georg Ehrke
|
|
|
|
* @copyright 2012 Georg Ehrke
|
2012-07-09 19:51:19 +00:00
|
|
|
*
|
2015-02-26 10:37:37 +00:00
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 3 of the License, or any later version.
|
2012-07-09 19:51:19 +00:00
|
|
|
*
|
2015-02-26 10:37:37 +00:00
|
|
|
* This library is distributed in the hope that it will be useful,
|
2012-07-09 19:51:19 +00:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2015-02-26 10:37:37 +00:00
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU AFFERO GENERAL PUBLIC LICENSE for more details.
|
2012-07-09 19:51:19 +00:00
|
|
|
*
|
2015-02-26 10:37:37 +00:00
|
|
|
* You should have received a copy of the GNU Affero General Public
|
|
|
|
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
|
2012-07-09 19:51:19 +00:00
|
|
|
*
|
|
|
|
*/
|
2012-07-21 14:43:39 +00:00
|
|
|
OC_Hook::connect('OC_User', 'post_deleteUser', 'OC_SubAdmin', 'post_deleteUser');
|
|
|
|
OC_Hook::connect('OC_User', 'post_deleteGroup', 'OC_SubAdmin', 'post_deleteGroup');
|
2012-07-09 19:51:19 +00:00
|
|
|
/**
|
|
|
|
* This class provides all methods needed for managing groups.
|
|
|
|
*
|
|
|
|
* Hooks provided:
|
|
|
|
* post_createSubAdmin($gid)
|
|
|
|
* post_deleteSubAdmin($gid)
|
|
|
|
*/
|
|
|
|
class OC_SubAdmin{
|
|
|
|
|
|
|
|
/**
|
2014-05-19 15:50:53 +00:00
|
|
|
* add a SubAdmin
|
2014-04-19 22:55:01 +00:00
|
|
|
* @param string $uid uid of the SubAdmin
|
|
|
|
* @param string $gid gid of the group
|
2012-07-09 19:51:19 +00:00
|
|
|
* @return boolean
|
|
|
|
*/
|
2012-09-07 13:22:01 +00:00
|
|
|
public static function createSubAdmin($uid, $gid) {
|
2012-08-24 23:52:27 +00:00
|
|
|
$stmt = OC_DB::prepare('INSERT INTO `*PREFIX*group_admin` (`gid`,`uid`) VALUES(?,?)');
|
2012-07-09 19:51:19 +00:00
|
|
|
$result = $stmt->execute(array($gid, $uid));
|
|
|
|
OC_Hook::emit( "OC_SubAdmin", "post_createSubAdmin", array( "gid" => $gid ));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-05-19 15:50:53 +00:00
|
|
|
* delete a SubAdmin
|
2014-04-19 22:55:01 +00:00
|
|
|
* @param string $uid uid of the SubAdmin
|
|
|
|
* @param string $gid gid of the group
|
2012-07-09 19:51:19 +00:00
|
|
|
* @return boolean
|
|
|
|
*/
|
2012-09-07 13:22:01 +00:00
|
|
|
public static function deleteSubAdmin($uid, $gid) {
|
2012-08-24 23:52:27 +00:00
|
|
|
$stmt = OC_DB::prepare('DELETE FROM `*PREFIX*group_admin` WHERE `gid` = ? AND `uid` = ?');
|
2012-07-09 19:51:19 +00:00
|
|
|
$result = $stmt->execute(array($gid, $uid));
|
|
|
|
OC_Hook::emit( "OC_SubAdmin", "post_deleteSubAdmin", array( "gid" => $gid ));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-05-19 15:50:53 +00:00
|
|
|
* get groups of a SubAdmin
|
2014-04-19 22:55:01 +00:00
|
|
|
* @param string $uid uid of the SubAdmin
|
2012-07-09 19:51:19 +00:00
|
|
|
* @return array
|
|
|
|
*/
|
2012-09-07 13:22:01 +00:00
|
|
|
public static function getSubAdminsGroups($uid) {
|
2012-08-24 23:52:27 +00:00
|
|
|
$stmt = OC_DB::prepare('SELECT `gid` FROM `*PREFIX*group_admin` WHERE `uid` = ?');
|
2012-07-15 14:31:28 +00:00
|
|
|
$result = $stmt->execute(array($uid));
|
2012-07-09 19:51:19 +00:00
|
|
|
$gids = array();
|
2012-09-07 13:22:01 +00:00
|
|
|
while($row = $result->fetchRow()) {
|
2012-07-09 19:51:19 +00:00
|
|
|
$gids[] = $row['gid'];
|
|
|
|
}
|
|
|
|
return $gids;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-05-19 15:50:53 +00:00
|
|
|
* get SubAdmins of a group
|
2014-04-19 22:55:01 +00:00
|
|
|
* @param string $gid gid of the group
|
2012-07-09 19:51:19 +00:00
|
|
|
* @return array
|
|
|
|
*/
|
2012-09-07 13:22:01 +00:00
|
|
|
public static function getGroupsSubAdmins($gid) {
|
2012-08-24 23:52:27 +00:00
|
|
|
$stmt = OC_DB::prepare('SELECT `uid` FROM `*PREFIX*group_admin` WHERE `gid` = ?');
|
2012-07-15 14:31:28 +00:00
|
|
|
$result = $stmt->execute(array($gid));
|
2012-07-09 19:51:19 +00:00
|
|
|
$uids = array();
|
2012-09-07 13:22:01 +00:00
|
|
|
while($row = $result->fetchRow()) {
|
2012-07-09 19:51:19 +00:00
|
|
|
$uids[] = $row['uid'];
|
|
|
|
}
|
|
|
|
return $uids;
|
|
|
|
}
|
2012-08-29 06:38:33 +00:00
|
|
|
|
2012-07-09 19:51:19 +00:00
|
|
|
/**
|
2014-05-19 15:50:53 +00:00
|
|
|
* get all SubAdmins
|
2012-07-09 19:51:19 +00:00
|
|
|
* @return array
|
|
|
|
*/
|
2012-09-07 13:22:01 +00:00
|
|
|
public static function getAllSubAdmins() {
|
2012-08-24 23:52:27 +00:00
|
|
|
$stmt = OC_DB::prepare('SELECT * FROM `*PREFIX*group_admin`');
|
2012-07-15 14:31:28 +00:00
|
|
|
$result = $stmt->execute();
|
2012-07-09 19:51:19 +00:00
|
|
|
$subadmins = array();
|
2012-09-07 13:22:01 +00:00
|
|
|
while($row = $result->fetchRow()) {
|
2012-07-09 19:51:19 +00:00
|
|
|
$subadmins[] = $row;
|
|
|
|
}
|
|
|
|
return $subadmins;
|
|
|
|
}
|
2012-08-29 06:38:33 +00:00
|
|
|
|
2012-07-15 14:31:28 +00:00
|
|
|
/**
|
2014-05-19 15:50:53 +00:00
|
|
|
* checks if a user is a SubAdmin of a group
|
2014-04-19 22:55:01 +00:00
|
|
|
* @param string $uid uid of the subadmin
|
|
|
|
* @param string $gid gid of the group
|
2012-07-19 14:43:46 +00:00
|
|
|
* @return bool
|
2012-07-15 14:31:28 +00:00
|
|
|
*/
|
2012-09-07 13:22:01 +00:00
|
|
|
public static function isSubAdminofGroup($uid, $gid) {
|
2012-08-24 23:52:27 +00:00
|
|
|
$stmt = OC_DB::prepare('SELECT COUNT(*) AS `count` FROM `*PREFIX*group_admin` WHERE `uid` = ? AND `gid` = ?');
|
2012-07-15 14:31:28 +00:00
|
|
|
$result = $stmt->execute(array($uid, $gid));
|
|
|
|
$result = $result->fetchRow();
|
2012-09-07 13:22:01 +00:00
|
|
|
if($result['count'] >= 1) {
|
2012-07-15 14:31:28 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2012-08-29 06:38:33 +00:00
|
|
|
|
2012-07-19 14:43:46 +00:00
|
|
|
/**
|
2014-05-19 15:50:53 +00:00
|
|
|
* checks if a user is a SubAdmin
|
2014-04-19 22:55:01 +00:00
|
|
|
* @param string $uid uid of the subadmin
|
2012-07-19 14:43:46 +00:00
|
|
|
* @return bool
|
|
|
|
*/
|
2012-09-07 13:22:01 +00:00
|
|
|
public static function isSubAdmin($uid) {
|
2013-01-14 18:45:17 +00:00
|
|
|
// Check if the user is already an admin
|
|
|
|
if(OC_Group::inGroup($uid, 'admin' )) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-08-24 23:52:27 +00:00
|
|
|
$stmt = OC_DB::prepare('SELECT COUNT(*) AS `count` FROM `*PREFIX*group_admin` WHERE `uid` = ?');
|
2012-07-15 14:31:28 +00:00
|
|
|
$result = $stmt->execute(array($uid));
|
|
|
|
$result = $result->fetchRow();
|
2012-09-07 13:22:01 +00:00
|
|
|
if($result['count'] > 0) {
|
2012-07-15 14:31:28 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2012-08-29 06:38:33 +00:00
|
|
|
|
2012-07-19 14:43:46 +00:00
|
|
|
/**
|
2014-05-19 15:50:53 +00:00
|
|
|
* checks if a user is a accessible by a subadmin
|
2014-04-19 22:55:01 +00:00
|
|
|
* @param string $subadmin uid of the subadmin
|
|
|
|
* @param string $user uid of the user
|
2012-07-19 14:43:46 +00:00
|
|
|
* @return bool
|
|
|
|
*/
|
2012-09-07 13:22:01 +00:00
|
|
|
public static function isUserAccessible($subadmin, $user) {
|
|
|
|
if(!self::isSubAdmin($subadmin)) {
|
2012-07-19 14:30:58 +00:00
|
|
|
return false;
|
|
|
|
}
|
2013-01-14 18:45:17 +00:00
|
|
|
if(OC_User::isAdminUser($user)) {
|
2012-08-26 14:29:10 +00:00
|
|
|
return false;
|
|
|
|
}
|
2012-07-19 14:30:58 +00:00
|
|
|
$accessiblegroups = self::getSubAdminsGroups($subadmin);
|
2012-09-07 13:22:01 +00:00
|
|
|
foreach($accessiblegroups as $accessiblegroup) {
|
|
|
|
if(OC_Group::inGroup($user, $accessiblegroup)) {
|
2012-07-19 14:30:58 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2012-08-29 06:38:33 +00:00
|
|
|
|
2012-07-19 17:17:01 +00:00
|
|
|
/*
|
2014-05-19 15:50:53 +00:00
|
|
|
* alias for self::isSubAdminofGroup()
|
2012-07-19 17:17:01 +00:00
|
|
|
*/
|
2012-09-07 13:22:01 +00:00
|
|
|
public static function isGroupAccessible($subadmin, $group) {
|
2012-07-19 17:17:01 +00:00
|
|
|
return self::isSubAdminofGroup($subadmin, $group);
|
|
|
|
}
|
2012-07-21 14:43:39 +00:00
|
|
|
|
|
|
|
/**
|
2014-05-19 15:50:53 +00:00
|
|
|
* delete all SubAdmins by uid
|
2014-05-11 20:51:30 +00:00
|
|
|
* @param array $parameters
|
2012-07-21 14:43:39 +00:00
|
|
|
* @return boolean
|
|
|
|
*/
|
2012-09-07 13:22:01 +00:00
|
|
|
public static function post_deleteUser($parameters) {
|
2012-08-24 23:52:27 +00:00
|
|
|
$stmt = OC_DB::prepare('DELETE FROM `*PREFIX*group_admin` WHERE `uid` = ?');
|
2012-07-21 14:43:39 +00:00
|
|
|
$result = $stmt->execute(array($parameters['uid']));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-05-19 15:50:53 +00:00
|
|
|
* delete all SubAdmins by gid
|
2014-05-11 20:51:30 +00:00
|
|
|
* @param array $parameters
|
2012-07-21 14:43:39 +00:00
|
|
|
* @return boolean
|
|
|
|
*/
|
2012-09-07 13:22:01 +00:00
|
|
|
public static function post_deleteGroup($parameters) {
|
2012-08-24 23:52:27 +00:00
|
|
|
$stmt = OC_DB::prepare('DELETE FROM `*PREFIX*group_admin` WHERE `gid` = ?');
|
2012-07-21 14:43:39 +00:00
|
|
|
$result = $stmt->execute(array($parameters['gid']));
|
|
|
|
return true;
|
|
|
|
}
|
2012-07-09 19:51:19 +00:00
|
|
|
}
|