some work on subadmins

This commit is contained in:
Georg Ehrke 2012-07-09 21:51:19 +02:00
parent acb196e17f
commit d0b625352c
5 changed files with 178 additions and 11 deletions

View File

@ -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;
}
}

107
lib/subadmin.php Normal file
View File

@ -0,0 +1,107 @@
<?php
/**
* ownCloud
*
* @author Georg Ehrke
* @copyright 2012 Georg Ehrke
*
* 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.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU AFFERO GENERAL PUBLIC LICENSE for more details.
*
* 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/>.
*
*/
/**
* 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;
}
}

View File

@ -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
*/

View File

@ -1,13 +1,14 @@
<?php /**
<?php
/**
* Copyright (c) 2011, Robin Appelman <icewind1991@gmail.com>
* 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'];
?>
<div id="controls">
@ -60,6 +61,9 @@ foreach($_["groups"] as $group) {
<th id='headerName'><?php echo $l->t('Name')?></th>
<th id="headerPassword"><?php echo $l->t( 'Password' ); ?></th>
<th id="headerGroups"><?php echo $l->t( 'Groups' ); ?></th>
<?php if(is_array($_['subadmins']) || $_['subadmins']): ?>
<th id="headerSubAdmins"><?php echo $l->t('SubAdmins'); ?></th>
<?php endif;?>
<th id="headerQuota"><?php echo $l->t( 'Quota' ); ?></th>
<th id="headerRemove">&nbsp;</th>
</tr>
@ -84,6 +88,20 @@ foreach($_["groups"] as $group) {
<?php endforeach;?>
</select>
</td>
<?php if(is_array($_['subadmins']) || $_['subadmins']): ?>
<td class="subadmins"><select
data-username="<?php echo $user['name'] ;?>"
data-user-groups="<?php echo $user['groups'] ;?>"
data-placeholder="subadmins" title="<?php echo $l->t('SubAdmin for ...')?>"
multiple="multiple">
<?php foreach($_["subadmingroups"] as $group): ?>
<option value="<?php echo $group['name'];?>">
<?php echo $group['name'];?>
</option>
<?php endforeach;?>
</select>
</td>
<?php endif;?>
<td class="quota">
<div class="quota-select-wrapper">
<select class='quota-user'>

View File

@ -6,7 +6,7 @@
*/
require_once('../lib/base.php');
OC_Util::checkAdminUser();
OC_Util::checkSubAdminUser();
// We have some javascript foo!
OC_Util::addScript( 'settings', 'users' );
@ -17,11 +17,22 @@ OC_App::setActiveNavigationEntry( 'core_users' );
$users = array();
$groups = array();
foreach( OC_User::getUsers() as $i ){
$users[] = array( "name" => $i, "groups" => join( ", ", OC_Group::getUserGroups( $i ) ),'quota'=>OC_Preferences::getValue($i,'files','quota','default'));
$isadmin = OC_Group::inGroup(OC_User::getUser(),'admin')?true:false;
if($isadmin){
$groups = OC_Group::getGroups();
$accessibleusers = OC_User::getUsers();
$subadmins = OC_SubAdmin::getAllSubAdmins();
}else{
$groups = OC_SubAdmin::getSubAdminsGroups(OC_User::getUser());
$accessibleusers = OC_Group::usersInGroups($groups);
$subadmins = false;
}
foreach( OC_Group::getGroups() as $i ){
foreach($accessibleusers as $i){
$users[] = array( "name" => $i, "groups" => join( ", ", /*array_intersect(*/OC_Group::getUserGroups($i)/*, OC_SubAdmin::getSubAdminsGroups(OC_User::getUser()))*/),'quota'=>OC_Preferences::getValue($i,'files','quota','default'));
}
foreach( $groups as $i ){
// Do some more work here soon
$groups[] = array( "name" => $i );
}
@ -44,10 +55,8 @@ if (\OC_App::isEnabled( "files_sharing" ) ) {
$tmpl = new OC_Template( "settings", "users", "user" );
$tmpl->assign( "users", $users );
$tmpl->assign( "groups", $groups );
$tmpl->assign( 'subadmins', $subadmins);
$tmpl->assign( 'quota_preset', $quotaPreset);
$tmpl->assign( 'default_quota', $defaultQuota);
$tmpl->assign( 'share_notice', $shareNotice);
$tmpl->printPage();
?>
$tmpl->printPage();