-
Notifications
You must be signed in to change notification settings - Fork 2.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Continuing the New User management #7151
Changes from all commits
0663d95
43ced8c
b2ca595
b506388
ce46cd3
71160ff
78e1b71
223e342
c7af9cd
8e34316
8e4bbe8
4a30665
dc15233
dc28f58
970f899
57ffadd
3a92d48
f751e0b
e681e1e
478393e
7d3ac19
1734cc0
bf88bd1
9ba4a76
6364546
16fb48f
8a88002
ed1a23c
86d3cf5
f7903e9
1f4bc7c
d650cd6
f946a52
d51b384
ceb6c4d
6526b93
eed108e
b3221f0
004dd73
506e065
17e640a
65aa204
faab6c0
749afe7
330dfeb
4801735
a4cfa95
853ced6
cfba7e2
b6b1332
b3d68fc
7aca126
e159f7e
d3cee79
fe4b1f8
5f57d72
19fd7cd
5b8ba79
68dc665
8df50ac
dbc854d
249e42c
2a8afb0
cbcda49
08ad073
d779db5
3c9788d
d87347e
3e411c8
5444a11
734dd70
7b8935a
dea7f45
3ff123f
a9bd416
b56d142
0067306
59fef54
8141b91
f2fed6d
f9f9550
1ae8ec6
66d00cf
57cc51d
1abf698
f324aa7
747c011
097887a
1115d68
ad1c34d
c38548a
4de14fe
fc7438b
29ef2f7
d030c28
122ebf2
a9e8d53
7b63c2c
ec57260
92b8344
28a0124
2d83c68
75cc16f
b15a5a7
e235de9
39982c2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,188 @@ | ||
<?php | ||
|
||
/** | ||
* Copyright (c) 2014 Arthur Schiwon <[email protected]> | ||
* This file is licensed under the Affero General Public License version 3 or | ||
* later. | ||
* See the COPYING-README file. | ||
*/ | ||
|
||
namespace OC\Group; | ||
|
||
class MetaData { | ||
const SORT_NONE = 0; | ||
const SORT_USERCOUNT = 1; | ||
|
||
/** | ||
* @var string $user | ||
*/ | ||
protected $user; | ||
|
||
/** | ||
* @var bool $isAdmin | ||
*/ | ||
protected $isAdmin; | ||
|
||
/** | ||
* @var string[] $groups | ||
*/ | ||
protected $groups = array(); | ||
|
||
/** | ||
* @var \OC\Group\Manager $groupManager | ||
*/ | ||
protected $groupManager; | ||
|
||
/** | ||
* @var int $sorting | ||
*/ | ||
protected $sorting = false; | ||
|
||
/** | ||
* @var string $lastSearch | ||
*/ | ||
protected $lastSearch; | ||
|
||
/** | ||
* @param string the uid of the current user | ||
* @param bool whether the current users is an admin | ||
* @param \OC\Group\Manager | ||
*/ | ||
public function __construct( | ||
$user, | ||
$isAdmin, | ||
\OC\Group\Manager $groupManager | ||
) { | ||
$this->user = $user; | ||
$this->isAdmin = (bool)$isAdmin; | ||
$this->groupManager = $groupManager; | ||
} | ||
|
||
/** | ||
* returns an array with meta data about all available groups | ||
* the array is structured as follows: | ||
* [0] array containing meta data about admin groups | ||
* [1] array containing meta data about unprivileged groups | ||
* @param string only effective when instance was created with isAdmin being | ||
* true | ||
* @return array | ||
*/ | ||
public function get($search = '') { | ||
if($this->lastSearch !== $search) { | ||
$this->lastSearch = $search; | ||
$this->groups = array(); | ||
} | ||
|
||
$adminGroups = array(); | ||
$groups = array(); | ||
$sortGroupsIndex = 0; | ||
$sortGroupsKeys = array(); | ||
$sortAdminGroupsIndex = 0; | ||
$sortAdminGroupsKeys = array(); | ||
|
||
foreach($this->getGroups($search) as $group) { | ||
$groupMetaData = $this->generateGroupMetaData($group); | ||
if (strtolower($group->getGID()) !== 'admin') { | ||
$this->addEntry( | ||
$groups, | ||
$sortGroupsKeys, | ||
$sortGroupsIndex, | ||
$groupMetaData); | ||
} else { | ||
//admin group is hard coded to 'admin' for now. In future, | ||
//backends may define admin groups too. Then the if statement | ||
//has to be adjusted accordingly. | ||
$this->addEntry( | ||
$adminGroups, | ||
$sortAdminGroupsKeys, | ||
$sortAdminGroupsIndex, | ||
$groupMetaData); | ||
} | ||
} | ||
|
||
//whether sorting is necessary is will be checked in sort() | ||
$this->sort($groups, $sortGroupsKeys); | ||
$this->sort($adminGroups, $sortAdminGroupsKeys); | ||
|
||
return array($adminGroups, $groups); | ||
} | ||
|
||
/** | ||
* @brief sets the sort mode, currently 0 (none) and 1 (user entries, | ||
* descending) are supported | ||
* @param int the sortMode (SORT_NONE, SORT_USERCOUNT) | ||
*/ | ||
public function setSorting($sortMode) { | ||
if($sortMode >= 0 && $sortMode <= 1) { | ||
$this->sorting = $sortMode; | ||
} else { | ||
$this->sorting = 0; | ||
} | ||
} | ||
|
||
/** | ||
* @brief adds an group entry to the resulting array | ||
* @param array the resulting array, by reference | ||
* @param array the sort key array, by reference | ||
* @param array the sort key index, by reference | ||
* @param array the group's meta data as returned by generateGroupMetaData() | ||
* @return null | ||
*/ | ||
private function addEntry(&$entries, &$sortKeys, &$sortIndex, $data) { | ||
$entries[] = $data; | ||
if($this->sorting === 1) { | ||
$sortKeys[$sortIndex] = $data['usercount']; | ||
$sortIndex++; | ||
} | ||
} | ||
|
||
/** | ||
* @brief creates an array containing the group meta data | ||
* @param \OC\Group\Group | ||
* @return array with the keys 'id', 'name' and 'usercount' | ||
*/ | ||
private function generateGroupMetaData(\OC\Group\Group $group) { | ||
return array( | ||
'id' => str_replace(' ','', $group->getGID()), | ||
'name' => $group->getGID(), | ||
'usercount' => $group->count() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. According to the PHPDoc the search parameter is missing here There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. hm? we do not need any search parameter here, PHPDoc also states none. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Then There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @blizzz yes - please assign a meaningful default on the function count() THX There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. now i got it, correct, thank you |
||
); | ||
} | ||
|
||
/** | ||
* @brief sorts the result array, if applicable | ||
* @param array the result array, by reference | ||
* @param array the array containing the sort keys | ||
* @param return null | ||
*/ | ||
private function sort(&$entries, $sortKeys) { | ||
if($this->sorting > 0) { | ||
array_multisort($sortKeys, SORT_DESC, $entries); | ||
} | ||
} | ||
|
||
/** | ||
* @brief returns the available groups | ||
* @param string a search string | ||
* @return \OC\Group\Group[] | ||
*/ | ||
private function getGroups($search = '') { | ||
if(count($this->groups) === 0) { | ||
$this->fetchGroups($search); | ||
} | ||
return $this->groups; | ||
} | ||
|
||
/** | ||
* @brief fetches the group using the group manager or the subAdmin API | ||
* @param string a search string | ||
* @return null | ||
*/ | ||
private function fetchGroups($search = '') { | ||
if($this->isAdmin) { | ||
$this->groups = $this->groupManager->search($search); | ||
} else { | ||
$this->groups = \OC_SubAdmin::getSubAdminsGroups($this->user); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php | ||
/** | ||
* ownCloud | ||
* | ||
* @author Arthur Schiwon | ||
* @copyright 2014 Arthur Schiwon <[email protected]> | ||
* | ||
* 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/>. | ||
* | ||
*/ | ||
|
||
OC_JSON::callCheck(); | ||
OC_JSON::checkSubAdminUser(); | ||
if (isset($_GET['pattern']) && !empty($_GET['pattern'])) { | ||
$pattern = $_GET['pattern']; | ||
} else { | ||
$pattern = ''; | ||
} | ||
$groups = array(); | ||
$adminGroups = array(); | ||
$groupManager = \OC_Group::getManager(); | ||
$isAdmin = OC_User::isAdminUser(OC_User::getUser()); | ||
|
||
//we pass isAdmin as true, because OC_SubAdmin has no search feature, | ||
//groups will be filtered out later | ||
$groupsInfo = new \OC\Group\MetaData(OC_User::getUser(), true, $groupManager); | ||
$groupsInfo->setSorting($groupsInfo::SORT_USERCOUNT); | ||
list($adminGroups, $groups) = $groupsInfo->get($pattern); | ||
|
||
$accessibleGroups = $groupManager->search($pattern); | ||
if(!$isAdmin) { | ||
$subadminGroups = OC_SubAdmin::getSubAdminsGroups(OC_User::getUser()); | ||
$accessibleGroups = array_intersect($groups, $subadminGroups); | ||
} | ||
|
||
OC_JSON::success( | ||
array('data' => array('adminGroups' => $adminGroups, 'groups' => $groups))); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@blizzz Can you please fix PHPDoc of the methods in this file? THX