@@ -52,7 +52,6 @@ |
||
| 52 | 52 | * @param string $user the uid of the current user |
| 53 | 53 | * @param bool $isAdmin whether the current users is an admin |
| 54 | 54 | * @param IGroupManager $groupManager |
| 55 | - * @param IUserManager $userManager |
|
| 56 | 55 | * @param IUserSession $userSession |
| 57 | 56 | */ |
| 58 | 57 | public function __construct( |
@@ -31,174 +31,174 @@ |
||
| 31 | 31 | use OCP\IGroupManager; |
| 32 | 32 | |
| 33 | 33 | class MetaData { |
| 34 | - const SORT_NONE = 0; |
|
| 35 | - const SORT_USERCOUNT = 1; // May have performance issues on LDAP backends |
|
| 36 | - const SORT_GROUPNAME = 2; |
|
| 37 | - |
|
| 38 | - /** @var string */ |
|
| 39 | - protected $user; |
|
| 40 | - /** @var bool */ |
|
| 41 | - protected $isAdmin; |
|
| 42 | - /** @var array */ |
|
| 43 | - protected $metaData = array(); |
|
| 44 | - /** @var IGroupManager */ |
|
| 45 | - protected $groupManager; |
|
| 46 | - /** @var bool */ |
|
| 47 | - protected $sorting = false; |
|
| 48 | - /** @var IUserSession */ |
|
| 49 | - protected $userSession; |
|
| 50 | - |
|
| 51 | - /** |
|
| 52 | - * @param string $user the uid of the current user |
|
| 53 | - * @param bool $isAdmin whether the current users is an admin |
|
| 54 | - * @param IGroupManager $groupManager |
|
| 55 | - * @param IUserManager $userManager |
|
| 56 | - * @param IUserSession $userSession |
|
| 57 | - */ |
|
| 58 | - public function __construct( |
|
| 59 | - $user, |
|
| 60 | - $isAdmin, |
|
| 61 | - IGroupManager $groupManager, |
|
| 62 | - IUserSession $userSession |
|
| 63 | - ) { |
|
| 64 | - $this->user = $user; |
|
| 65 | - $this->isAdmin = (bool)$isAdmin; |
|
| 66 | - $this->groupManager = $groupManager; |
|
| 67 | - $this->userSession = $userSession; |
|
| 68 | - } |
|
| 69 | - |
|
| 70 | - /** |
|
| 71 | - * returns an array with meta data about all available groups |
|
| 72 | - * the array is structured as follows: |
|
| 73 | - * [0] array containing meta data about admin groups |
|
| 74 | - * [1] array containing meta data about unprivileged groups |
|
| 75 | - * @param string $groupSearch only effective when instance was created with |
|
| 76 | - * isAdmin being true |
|
| 77 | - * @param string $userSearch the pattern users are search for |
|
| 78 | - * @return array |
|
| 79 | - */ |
|
| 80 | - public function get($groupSearch = '', $userSearch = '') { |
|
| 81 | - $key = $groupSearch . '::' . $userSearch; |
|
| 82 | - if(isset($this->metaData[$key])) { |
|
| 83 | - return $this->metaData[$key]; |
|
| 84 | - } |
|
| 85 | - |
|
| 86 | - $adminGroups = array(); |
|
| 87 | - $groups = array(); |
|
| 88 | - $sortGroupsIndex = 0; |
|
| 89 | - $sortGroupsKeys = array(); |
|
| 90 | - $sortAdminGroupsIndex = 0; |
|
| 91 | - $sortAdminGroupsKeys = array(); |
|
| 92 | - |
|
| 93 | - foreach($this->getGroups($groupSearch) as $group) { |
|
| 94 | - $groupMetaData = $this->generateGroupMetaData($group, $userSearch); |
|
| 95 | - if (strtolower($group->getGID()) !== 'admin') { |
|
| 96 | - $this->addEntry( |
|
| 97 | - $groups, |
|
| 98 | - $sortGroupsKeys, |
|
| 99 | - $sortGroupsIndex, |
|
| 100 | - $groupMetaData); |
|
| 101 | - } else { |
|
| 102 | - //admin group is hard coded to 'admin' for now. In future, |
|
| 103 | - //backends may define admin groups too. Then the if statement |
|
| 104 | - //has to be adjusted accordingly. |
|
| 105 | - $this->addEntry( |
|
| 106 | - $adminGroups, |
|
| 107 | - $sortAdminGroupsKeys, |
|
| 108 | - $sortAdminGroupsIndex, |
|
| 109 | - $groupMetaData); |
|
| 110 | - } |
|
| 111 | - } |
|
| 112 | - |
|
| 113 | - //whether sorting is necessary is will be checked in sort() |
|
| 114 | - $this->sort($groups, $sortGroupsKeys); |
|
| 115 | - $this->sort($adminGroups, $sortAdminGroupsKeys); |
|
| 116 | - |
|
| 117 | - $this->metaData[$key] = array($adminGroups, $groups); |
|
| 118 | - return $this->metaData[$key]; |
|
| 119 | - } |
|
| 120 | - |
|
| 121 | - /** |
|
| 122 | - * sets the sort mode, see SORT_* constants for supported modes |
|
| 123 | - * |
|
| 124 | - * @param int $sortMode |
|
| 125 | - */ |
|
| 126 | - public function setSorting($sortMode) { |
|
| 127 | - switch ($sortMode) { |
|
| 128 | - case self::SORT_USERCOUNT: |
|
| 129 | - case self::SORT_GROUPNAME: |
|
| 130 | - $this->sorting = $sortMode; |
|
| 131 | - break; |
|
| 132 | - |
|
| 133 | - default: |
|
| 134 | - $this->sorting = self::SORT_NONE; |
|
| 135 | - } |
|
| 136 | - } |
|
| 137 | - |
|
| 138 | - /** |
|
| 139 | - * adds an group entry to the resulting array |
|
| 140 | - * @param array $entries the resulting array, by reference |
|
| 141 | - * @param array $sortKeys the sort key array, by reference |
|
| 142 | - * @param int $sortIndex the sort key index, by reference |
|
| 143 | - * @param array $data the group's meta data as returned by generateGroupMetaData() |
|
| 144 | - */ |
|
| 145 | - private function addEntry(&$entries, &$sortKeys, &$sortIndex, $data) { |
|
| 146 | - $entries[] = $data; |
|
| 147 | - if ($this->sorting === self::SORT_USERCOUNT) { |
|
| 148 | - $sortKeys[$sortIndex] = $data['usercount']; |
|
| 149 | - $sortIndex++; |
|
| 150 | - } else if ($this->sorting === self::SORT_GROUPNAME) { |
|
| 151 | - $sortKeys[$sortIndex] = $data['name']; |
|
| 152 | - $sortIndex++; |
|
| 153 | - } |
|
| 154 | - } |
|
| 155 | - |
|
| 156 | - /** |
|
| 157 | - * creates an array containing the group meta data |
|
| 158 | - * @param \OCP\IGroup $group |
|
| 159 | - * @param string $userSearch |
|
| 160 | - * @return array with the keys 'id', 'name', 'usercount' and 'disabled' |
|
| 161 | - */ |
|
| 162 | - private function generateGroupMetaData(\OCP\IGroup $group, $userSearch) { |
|
| 163 | - return array( |
|
| 164 | - 'id' => $group->getGID(), |
|
| 165 | - 'name' => $group->getDisplayName(), |
|
| 166 | - 'usercount' => $this->sorting === self::SORT_USERCOUNT ? $group->count($userSearch) : 0, |
|
| 167 | - 'disabled' => $group->countDisabled() |
|
| 168 | - ); |
|
| 169 | - } |
|
| 170 | - |
|
| 171 | - /** |
|
| 172 | - * sorts the result array, if applicable |
|
| 173 | - * @param array $entries the result array, by reference |
|
| 174 | - * @param array $sortKeys the array containing the sort keys |
|
| 175 | - * @param return null |
|
| 176 | - */ |
|
| 177 | - private function sort(&$entries, $sortKeys) { |
|
| 178 | - if ($this->sorting === self::SORT_USERCOUNT) { |
|
| 179 | - array_multisort($sortKeys, SORT_DESC, $entries); |
|
| 180 | - } else if ($this->sorting === self::SORT_GROUPNAME) { |
|
| 181 | - array_multisort($sortKeys, SORT_ASC, $entries); |
|
| 182 | - } |
|
| 183 | - } |
|
| 184 | - |
|
| 185 | - /** |
|
| 186 | - * returns the available groups |
|
| 187 | - * @param string $search a search string |
|
| 188 | - * @return \OCP\IGroup[] |
|
| 189 | - */ |
|
| 190 | - protected function getGroups($search = '') { |
|
| 191 | - if($this->isAdmin) { |
|
| 192 | - return $this->groupManager->search($search); |
|
| 193 | - } else { |
|
| 194 | - $userObject = $this->userSession->getUser(); |
|
| 195 | - if($userObject !== null) { |
|
| 196 | - $groups = $this->groupManager->getSubAdmin()->getSubAdminsGroups($userObject); |
|
| 197 | - } else { |
|
| 198 | - $groups = []; |
|
| 199 | - } |
|
| 200 | - |
|
| 201 | - return $groups; |
|
| 202 | - } |
|
| 203 | - } |
|
| 34 | + const SORT_NONE = 0; |
|
| 35 | + const SORT_USERCOUNT = 1; // May have performance issues on LDAP backends |
|
| 36 | + const SORT_GROUPNAME = 2; |
|
| 37 | + |
|
| 38 | + /** @var string */ |
|
| 39 | + protected $user; |
|
| 40 | + /** @var bool */ |
|
| 41 | + protected $isAdmin; |
|
| 42 | + /** @var array */ |
|
| 43 | + protected $metaData = array(); |
|
| 44 | + /** @var IGroupManager */ |
|
| 45 | + protected $groupManager; |
|
| 46 | + /** @var bool */ |
|
| 47 | + protected $sorting = false; |
|
| 48 | + /** @var IUserSession */ |
|
| 49 | + protected $userSession; |
|
| 50 | + |
|
| 51 | + /** |
|
| 52 | + * @param string $user the uid of the current user |
|
| 53 | + * @param bool $isAdmin whether the current users is an admin |
|
| 54 | + * @param IGroupManager $groupManager |
|
| 55 | + * @param IUserManager $userManager |
|
| 56 | + * @param IUserSession $userSession |
|
| 57 | + */ |
|
| 58 | + public function __construct( |
|
| 59 | + $user, |
|
| 60 | + $isAdmin, |
|
| 61 | + IGroupManager $groupManager, |
|
| 62 | + IUserSession $userSession |
|
| 63 | + ) { |
|
| 64 | + $this->user = $user; |
|
| 65 | + $this->isAdmin = (bool)$isAdmin; |
|
| 66 | + $this->groupManager = $groupManager; |
|
| 67 | + $this->userSession = $userSession; |
|
| 68 | + } |
|
| 69 | + |
|
| 70 | + /** |
|
| 71 | + * returns an array with meta data about all available groups |
|
| 72 | + * the array is structured as follows: |
|
| 73 | + * [0] array containing meta data about admin groups |
|
| 74 | + * [1] array containing meta data about unprivileged groups |
|
| 75 | + * @param string $groupSearch only effective when instance was created with |
|
| 76 | + * isAdmin being true |
|
| 77 | + * @param string $userSearch the pattern users are search for |
|
| 78 | + * @return array |
|
| 79 | + */ |
|
| 80 | + public function get($groupSearch = '', $userSearch = '') { |
|
| 81 | + $key = $groupSearch . '::' . $userSearch; |
|
| 82 | + if(isset($this->metaData[$key])) { |
|
| 83 | + return $this->metaData[$key]; |
|
| 84 | + } |
|
| 85 | + |
|
| 86 | + $adminGroups = array(); |
|
| 87 | + $groups = array(); |
|
| 88 | + $sortGroupsIndex = 0; |
|
| 89 | + $sortGroupsKeys = array(); |
|
| 90 | + $sortAdminGroupsIndex = 0; |
|
| 91 | + $sortAdminGroupsKeys = array(); |
|
| 92 | + |
|
| 93 | + foreach($this->getGroups($groupSearch) as $group) { |
|
| 94 | + $groupMetaData = $this->generateGroupMetaData($group, $userSearch); |
|
| 95 | + if (strtolower($group->getGID()) !== 'admin') { |
|
| 96 | + $this->addEntry( |
|
| 97 | + $groups, |
|
| 98 | + $sortGroupsKeys, |
|
| 99 | + $sortGroupsIndex, |
|
| 100 | + $groupMetaData); |
|
| 101 | + } else { |
|
| 102 | + //admin group is hard coded to 'admin' for now. In future, |
|
| 103 | + //backends may define admin groups too. Then the if statement |
|
| 104 | + //has to be adjusted accordingly. |
|
| 105 | + $this->addEntry( |
|
| 106 | + $adminGroups, |
|
| 107 | + $sortAdminGroupsKeys, |
|
| 108 | + $sortAdminGroupsIndex, |
|
| 109 | + $groupMetaData); |
|
| 110 | + } |
|
| 111 | + } |
|
| 112 | + |
|
| 113 | + //whether sorting is necessary is will be checked in sort() |
|
| 114 | + $this->sort($groups, $sortGroupsKeys); |
|
| 115 | + $this->sort($adminGroups, $sortAdminGroupsKeys); |
|
| 116 | + |
|
| 117 | + $this->metaData[$key] = array($adminGroups, $groups); |
|
| 118 | + return $this->metaData[$key]; |
|
| 119 | + } |
|
| 120 | + |
|
| 121 | + /** |
|
| 122 | + * sets the sort mode, see SORT_* constants for supported modes |
|
| 123 | + * |
|
| 124 | + * @param int $sortMode |
|
| 125 | + */ |
|
| 126 | + public function setSorting($sortMode) { |
|
| 127 | + switch ($sortMode) { |
|
| 128 | + case self::SORT_USERCOUNT: |
|
| 129 | + case self::SORT_GROUPNAME: |
|
| 130 | + $this->sorting = $sortMode; |
|
| 131 | + break; |
|
| 132 | + |
|
| 133 | + default: |
|
| 134 | + $this->sorting = self::SORT_NONE; |
|
| 135 | + } |
|
| 136 | + } |
|
| 137 | + |
|
| 138 | + /** |
|
| 139 | + * adds an group entry to the resulting array |
|
| 140 | + * @param array $entries the resulting array, by reference |
|
| 141 | + * @param array $sortKeys the sort key array, by reference |
|
| 142 | + * @param int $sortIndex the sort key index, by reference |
|
| 143 | + * @param array $data the group's meta data as returned by generateGroupMetaData() |
|
| 144 | + */ |
|
| 145 | + private function addEntry(&$entries, &$sortKeys, &$sortIndex, $data) { |
|
| 146 | + $entries[] = $data; |
|
| 147 | + if ($this->sorting === self::SORT_USERCOUNT) { |
|
| 148 | + $sortKeys[$sortIndex] = $data['usercount']; |
|
| 149 | + $sortIndex++; |
|
| 150 | + } else if ($this->sorting === self::SORT_GROUPNAME) { |
|
| 151 | + $sortKeys[$sortIndex] = $data['name']; |
|
| 152 | + $sortIndex++; |
|
| 153 | + } |
|
| 154 | + } |
|
| 155 | + |
|
| 156 | + /** |
|
| 157 | + * creates an array containing the group meta data |
|
| 158 | + * @param \OCP\IGroup $group |
|
| 159 | + * @param string $userSearch |
|
| 160 | + * @return array with the keys 'id', 'name', 'usercount' and 'disabled' |
|
| 161 | + */ |
|
| 162 | + private function generateGroupMetaData(\OCP\IGroup $group, $userSearch) { |
|
| 163 | + return array( |
|
| 164 | + 'id' => $group->getGID(), |
|
| 165 | + 'name' => $group->getDisplayName(), |
|
| 166 | + 'usercount' => $this->sorting === self::SORT_USERCOUNT ? $group->count($userSearch) : 0, |
|
| 167 | + 'disabled' => $group->countDisabled() |
|
| 168 | + ); |
|
| 169 | + } |
|
| 170 | + |
|
| 171 | + /** |
|
| 172 | + * sorts the result array, if applicable |
|
| 173 | + * @param array $entries the result array, by reference |
|
| 174 | + * @param array $sortKeys the array containing the sort keys |
|
| 175 | + * @param return null |
|
| 176 | + */ |
|
| 177 | + private function sort(&$entries, $sortKeys) { |
|
| 178 | + if ($this->sorting === self::SORT_USERCOUNT) { |
|
| 179 | + array_multisort($sortKeys, SORT_DESC, $entries); |
|
| 180 | + } else if ($this->sorting === self::SORT_GROUPNAME) { |
|
| 181 | + array_multisort($sortKeys, SORT_ASC, $entries); |
|
| 182 | + } |
|
| 183 | + } |
|
| 184 | + |
|
| 185 | + /** |
|
| 186 | + * returns the available groups |
|
| 187 | + * @param string $search a search string |
|
| 188 | + * @return \OCP\IGroup[] |
|
| 189 | + */ |
|
| 190 | + protected function getGroups($search = '') { |
|
| 191 | + if($this->isAdmin) { |
|
| 192 | + return $this->groupManager->search($search); |
|
| 193 | + } else { |
|
| 194 | + $userObject = $this->userSession->getUser(); |
|
| 195 | + if($userObject !== null) { |
|
| 196 | + $groups = $this->groupManager->getSubAdmin()->getSubAdminsGroups($userObject); |
|
| 197 | + } else { |
|
| 198 | + $groups = []; |
|
| 199 | + } |
|
| 200 | + |
|
| 201 | + return $groups; |
|
| 202 | + } |
|
| 203 | + } |
|
| 204 | 204 | } |
@@ -380,7 +380,6 @@ |
||
| 380 | 380 | /** |
| 381 | 381 | * get the number of disabled users in a group |
| 382 | 382 | * |
| 383 | - * @param string $search |
|
| 384 | 383 | * @return int|bool |
| 385 | 384 | */ |
| 386 | 385 | public function countDisabledInGroup(string $gid): int { |
@@ -55,357 +55,357 @@ |
||
| 55 | 55 | * Class for group management in a SQL Database (e.g. MySQL, SQLite) |
| 56 | 56 | */ |
| 57 | 57 | class Database extends ABackend |
| 58 | - implements IAddToGroupBackend, |
|
| 59 | - ICountDisabledInGroup, |
|
| 60 | - ICountUsersBackend, |
|
| 61 | - ICreateGroupBackend, |
|
| 62 | - IDeleteGroupBackend, |
|
| 63 | - IRemoveFromGroupBackend { |
|
| 64 | - |
|
| 65 | - /** @var string[] */ |
|
| 66 | - private $groupCache = []; |
|
| 67 | - |
|
| 68 | - /** @var IDBConnection */ |
|
| 69 | - private $dbConn; |
|
| 70 | - |
|
| 71 | - /** |
|
| 72 | - * \OC\Group\Database constructor. |
|
| 73 | - * |
|
| 74 | - * @param IDBConnection|null $dbConn |
|
| 75 | - */ |
|
| 76 | - public function __construct(IDBConnection $dbConn = null) { |
|
| 77 | - $this->dbConn = $dbConn; |
|
| 78 | - } |
|
| 79 | - |
|
| 80 | - /** |
|
| 81 | - * FIXME: This function should not be required! |
|
| 82 | - */ |
|
| 83 | - private function fixDI() { |
|
| 84 | - if ($this->dbConn === null) { |
|
| 85 | - $this->dbConn = \OC::$server->getDatabaseConnection(); |
|
| 86 | - } |
|
| 87 | - } |
|
| 88 | - |
|
| 89 | - /** |
|
| 90 | - * Try to create a new group |
|
| 91 | - * @param string $gid The name of the group to create |
|
| 92 | - * @return bool |
|
| 93 | - * |
|
| 94 | - * Tries to create a new group. If the group name already exists, false will |
|
| 95 | - * be returned. |
|
| 96 | - */ |
|
| 97 | - public function createGroup(string $gid): bool { |
|
| 98 | - $this->fixDI(); |
|
| 99 | - |
|
| 100 | - // Add group |
|
| 101 | - $result = $this->dbConn->insertIfNotExist('*PREFIX*groups', [ |
|
| 102 | - 'gid' => $gid, |
|
| 103 | - ]); |
|
| 104 | - |
|
| 105 | - // Add to cache |
|
| 106 | - $this->groupCache[$gid] = $gid; |
|
| 107 | - |
|
| 108 | - return $result === 1; |
|
| 109 | - } |
|
| 110 | - |
|
| 111 | - /** |
|
| 112 | - * delete a group |
|
| 113 | - * @param string $gid gid of the group to delete |
|
| 114 | - * @return bool |
|
| 115 | - * |
|
| 116 | - * Deletes a group and removes it from the group_user-table |
|
| 117 | - */ |
|
| 118 | - public function deleteGroup(string $gid): bool { |
|
| 119 | - $this->fixDI(); |
|
| 120 | - |
|
| 121 | - // Delete the group |
|
| 122 | - $qb = $this->dbConn->getQueryBuilder(); |
|
| 123 | - $qb->delete('groups') |
|
| 124 | - ->where($qb->expr()->eq('gid', $qb->createNamedParameter($gid))) |
|
| 125 | - ->execute(); |
|
| 126 | - |
|
| 127 | - // Delete the group-user relation |
|
| 128 | - $qb = $this->dbConn->getQueryBuilder(); |
|
| 129 | - $qb->delete('group_user') |
|
| 130 | - ->where($qb->expr()->eq('gid', $qb->createNamedParameter($gid))) |
|
| 131 | - ->execute(); |
|
| 132 | - |
|
| 133 | - // Delete the group-groupadmin relation |
|
| 134 | - $qb = $this->dbConn->getQueryBuilder(); |
|
| 135 | - $qb->delete('group_admin') |
|
| 136 | - ->where($qb->expr()->eq('gid', $qb->createNamedParameter($gid))) |
|
| 137 | - ->execute(); |
|
| 138 | - |
|
| 139 | - // Delete from cache |
|
| 140 | - unset($this->groupCache[$gid]); |
|
| 141 | - |
|
| 142 | - return true; |
|
| 143 | - } |
|
| 144 | - |
|
| 145 | - /** |
|
| 146 | - * is user in group? |
|
| 147 | - * @param string $uid uid of the user |
|
| 148 | - * @param string $gid gid of the group |
|
| 149 | - * @return bool |
|
| 150 | - * |
|
| 151 | - * Checks whether the user is member of a group or not. |
|
| 152 | - */ |
|
| 153 | - public function inGroup( $uid, $gid ) { |
|
| 154 | - $this->fixDI(); |
|
| 155 | - |
|
| 156 | - // check |
|
| 157 | - $qb = $this->dbConn->getQueryBuilder(); |
|
| 158 | - $cursor = $qb->select('uid') |
|
| 159 | - ->from('group_user') |
|
| 160 | - ->where($qb->expr()->eq('gid', $qb->createNamedParameter($gid))) |
|
| 161 | - ->andWhere($qb->expr()->eq('uid', $qb->createNamedParameter($uid))) |
|
| 162 | - ->execute(); |
|
| 163 | - |
|
| 164 | - $result = $cursor->fetch(); |
|
| 165 | - $cursor->closeCursor(); |
|
| 166 | - |
|
| 167 | - return $result ? true : false; |
|
| 168 | - } |
|
| 169 | - |
|
| 170 | - /** |
|
| 171 | - * Add a user to a group |
|
| 172 | - * @param string $uid Name of the user to add to group |
|
| 173 | - * @param string $gid Name of the group in which add the user |
|
| 174 | - * @return bool |
|
| 175 | - * |
|
| 176 | - * Adds a user to a group. |
|
| 177 | - */ |
|
| 178 | - public function addToGroup(string $uid, string $gid): bool { |
|
| 179 | - $this->fixDI(); |
|
| 180 | - |
|
| 181 | - // No duplicate entries! |
|
| 182 | - if( !$this->inGroup( $uid, $gid )) { |
|
| 183 | - $qb = $this->dbConn->getQueryBuilder(); |
|
| 184 | - $qb->insert('group_user') |
|
| 185 | - ->setValue('uid', $qb->createNamedParameter($uid)) |
|
| 186 | - ->setValue('gid', $qb->createNamedParameter($gid)) |
|
| 187 | - ->execute(); |
|
| 188 | - return true; |
|
| 189 | - }else{ |
|
| 190 | - return false; |
|
| 191 | - } |
|
| 192 | - } |
|
| 193 | - |
|
| 194 | - /** |
|
| 195 | - * Removes a user from a group |
|
| 196 | - * @param string $uid Name of the user to remove from group |
|
| 197 | - * @param string $gid Name of the group from which remove the user |
|
| 198 | - * @return bool |
|
| 199 | - * |
|
| 200 | - * removes the user from a group. |
|
| 201 | - */ |
|
| 202 | - public function removeFromGroup(string $uid, string $gid): bool { |
|
| 203 | - $this->fixDI(); |
|
| 204 | - |
|
| 205 | - $qb = $this->dbConn->getQueryBuilder(); |
|
| 206 | - $qb->delete('group_user') |
|
| 207 | - ->where($qb->expr()->eq('uid', $qb->createNamedParameter($uid))) |
|
| 208 | - ->andWhere($qb->expr()->eq('gid', $qb->createNamedParameter($gid))) |
|
| 209 | - ->execute(); |
|
| 210 | - |
|
| 211 | - return true; |
|
| 212 | - } |
|
| 213 | - |
|
| 214 | - /** |
|
| 215 | - * Get all groups a user belongs to |
|
| 216 | - * @param string $uid Name of the user |
|
| 217 | - * @return array an array of group names |
|
| 218 | - * |
|
| 219 | - * This function fetches all groups a user belongs to. It does not check |
|
| 220 | - * if the user exists at all. |
|
| 221 | - */ |
|
| 222 | - public function getUserGroups( $uid ) { |
|
| 223 | - //guests has empty or null $uid |
|
| 224 | - if ($uid === null || $uid === '') { |
|
| 225 | - return []; |
|
| 226 | - } |
|
| 227 | - |
|
| 228 | - $this->fixDI(); |
|
| 229 | - |
|
| 230 | - // No magic! |
|
| 231 | - $qb = $this->dbConn->getQueryBuilder(); |
|
| 232 | - $cursor = $qb->select('gid') |
|
| 233 | - ->from('group_user') |
|
| 234 | - ->where($qb->expr()->eq('uid', $qb->createNamedParameter($uid))) |
|
| 235 | - ->execute(); |
|
| 236 | - |
|
| 237 | - $groups = []; |
|
| 238 | - while( $row = $cursor->fetch()) { |
|
| 239 | - $groups[] = $row['gid']; |
|
| 240 | - $this->groupCache[$row['gid']] = $row['gid']; |
|
| 241 | - } |
|
| 242 | - $cursor->closeCursor(); |
|
| 243 | - |
|
| 244 | - return $groups; |
|
| 245 | - } |
|
| 246 | - |
|
| 247 | - /** |
|
| 248 | - * get a list of all groups |
|
| 249 | - * @param string $search |
|
| 250 | - * @param int $limit |
|
| 251 | - * @param int $offset |
|
| 252 | - * @return array an array of group names |
|
| 253 | - * |
|
| 254 | - * Returns a list with all groups |
|
| 255 | - */ |
|
| 256 | - public function getGroups($search = '', $limit = null, $offset = null) { |
|
| 257 | - $this->fixDI(); |
|
| 258 | - |
|
| 259 | - $query = $this->dbConn->getQueryBuilder(); |
|
| 260 | - $query->select('gid') |
|
| 261 | - ->from('groups') |
|
| 262 | - ->orderBy('gid', 'ASC'); |
|
| 263 | - |
|
| 264 | - if ($search !== '') { |
|
| 265 | - $query->where($query->expr()->iLike('gid', $query->createNamedParameter( |
|
| 266 | - '%' . $this->dbConn->escapeLikeParameter($search) . '%' |
|
| 267 | - ))); |
|
| 268 | - } |
|
| 269 | - |
|
| 270 | - $query->setMaxResults($limit) |
|
| 271 | - ->setFirstResult($offset); |
|
| 272 | - $result = $query->execute(); |
|
| 273 | - |
|
| 274 | - $groups = []; |
|
| 275 | - while ($row = $result->fetch()) { |
|
| 276 | - $groups[] = $row['gid']; |
|
| 277 | - } |
|
| 278 | - $result->closeCursor(); |
|
| 279 | - |
|
| 280 | - return $groups; |
|
| 281 | - } |
|
| 282 | - |
|
| 283 | - /** |
|
| 284 | - * check if a group exists |
|
| 285 | - * @param string $gid |
|
| 286 | - * @return bool |
|
| 287 | - */ |
|
| 288 | - public function groupExists($gid) { |
|
| 289 | - $this->fixDI(); |
|
| 290 | - |
|
| 291 | - // Check cache first |
|
| 292 | - if (isset($this->groupCache[$gid])) { |
|
| 293 | - return true; |
|
| 294 | - } |
|
| 295 | - |
|
| 296 | - $qb = $this->dbConn->getQueryBuilder(); |
|
| 297 | - $cursor = $qb->select('gid') |
|
| 298 | - ->from('groups') |
|
| 299 | - ->where($qb->expr()->eq('gid', $qb->createNamedParameter($gid))) |
|
| 300 | - ->execute(); |
|
| 301 | - $result = $cursor->fetch(); |
|
| 302 | - $cursor->closeCursor(); |
|
| 303 | - |
|
| 304 | - if ($result !== false) { |
|
| 305 | - $this->groupCache[$gid] = $gid; |
|
| 306 | - return true; |
|
| 307 | - } |
|
| 308 | - return false; |
|
| 309 | - } |
|
| 310 | - |
|
| 311 | - /** |
|
| 312 | - * get a list of all users in a group |
|
| 313 | - * @param string $gid |
|
| 314 | - * @param string $search |
|
| 315 | - * @param int $limit |
|
| 316 | - * @param int $offset |
|
| 317 | - * @return array an array of user ids |
|
| 318 | - */ |
|
| 319 | - public function usersInGroup($gid, $search = '', $limit = null, $offset = null) { |
|
| 320 | - $this->fixDI(); |
|
| 321 | - |
|
| 322 | - $query = $this->dbConn->getQueryBuilder(); |
|
| 323 | - $query->select('uid') |
|
| 324 | - ->from('group_user') |
|
| 325 | - ->where($query->expr()->eq('gid', $query->createNamedParameter($gid))) |
|
| 326 | - ->orderBy('uid', 'ASC'); |
|
| 327 | - |
|
| 328 | - if ($search !== '') { |
|
| 329 | - $query->andWhere($query->expr()->like('uid', $query->createNamedParameter( |
|
| 330 | - '%' . $this->dbConn->escapeLikeParameter($search) . '%' |
|
| 331 | - ))); |
|
| 332 | - } |
|
| 333 | - |
|
| 334 | - $query->setMaxResults($limit) |
|
| 335 | - ->setFirstResult($offset); |
|
| 336 | - $result = $query->execute(); |
|
| 337 | - |
|
| 338 | - $users = []; |
|
| 339 | - while ($row = $result->fetch()) { |
|
| 340 | - $users[] = $row['uid']; |
|
| 341 | - } |
|
| 342 | - $result->closeCursor(); |
|
| 343 | - |
|
| 344 | - return $users; |
|
| 345 | - } |
|
| 346 | - |
|
| 347 | - /** |
|
| 348 | - * get the number of all users matching the search string in a group |
|
| 349 | - * @param string $gid |
|
| 350 | - * @param string $search |
|
| 351 | - * @return int |
|
| 352 | - */ |
|
| 353 | - public function countUsersInGroup(string $gid, string $search = ''): int { |
|
| 354 | - $this->fixDI(); |
|
| 355 | - |
|
| 356 | - $query = $this->dbConn->getQueryBuilder(); |
|
| 357 | - $query->selectAlias($query->createFunction('COUNT(*)'), 'num_users') |
|
| 358 | - ->from('group_user') |
|
| 359 | - ->where($query->expr()->eq('gid', $query->createNamedParameter($gid))); |
|
| 360 | - |
|
| 361 | - if ($search !== '') { |
|
| 362 | - $query->andWhere($query->expr()->like('uid', $query->createNamedParameter( |
|
| 363 | - '%' . $this->dbConn->escapeLikeParameter($search) . '%' |
|
| 364 | - ))); |
|
| 365 | - } |
|
| 366 | - |
|
| 367 | - $result = $query->execute(); |
|
| 368 | - $count = $result->fetchColumn(); |
|
| 369 | - $result->closeCursor(); |
|
| 370 | - |
|
| 371 | - if ($count !== false) { |
|
| 372 | - $count = (int)$count; |
|
| 373 | - } else { |
|
| 374 | - $count = 0; |
|
| 375 | - } |
|
| 376 | - |
|
| 377 | - return $count; |
|
| 378 | - } |
|
| 379 | - |
|
| 380 | - /** |
|
| 381 | - * get the number of disabled users in a group |
|
| 382 | - * |
|
| 383 | - * @param string $search |
|
| 384 | - * @return int|bool |
|
| 385 | - */ |
|
| 386 | - public function countDisabledInGroup(string $gid): int { |
|
| 387 | - $this->fixDI(); |
|
| 58 | + implements IAddToGroupBackend, |
|
| 59 | + ICountDisabledInGroup, |
|
| 60 | + ICountUsersBackend, |
|
| 61 | + ICreateGroupBackend, |
|
| 62 | + IDeleteGroupBackend, |
|
| 63 | + IRemoveFromGroupBackend { |
|
| 64 | + |
|
| 65 | + /** @var string[] */ |
|
| 66 | + private $groupCache = []; |
|
| 67 | + |
|
| 68 | + /** @var IDBConnection */ |
|
| 69 | + private $dbConn; |
|
| 70 | + |
|
| 71 | + /** |
|
| 72 | + * \OC\Group\Database constructor. |
|
| 73 | + * |
|
| 74 | + * @param IDBConnection|null $dbConn |
|
| 75 | + */ |
|
| 76 | + public function __construct(IDBConnection $dbConn = null) { |
|
| 77 | + $this->dbConn = $dbConn; |
|
| 78 | + } |
|
| 79 | + |
|
| 80 | + /** |
|
| 81 | + * FIXME: This function should not be required! |
|
| 82 | + */ |
|
| 83 | + private function fixDI() { |
|
| 84 | + if ($this->dbConn === null) { |
|
| 85 | + $this->dbConn = \OC::$server->getDatabaseConnection(); |
|
| 86 | + } |
|
| 87 | + } |
|
| 88 | + |
|
| 89 | + /** |
|
| 90 | + * Try to create a new group |
|
| 91 | + * @param string $gid The name of the group to create |
|
| 92 | + * @return bool |
|
| 93 | + * |
|
| 94 | + * Tries to create a new group. If the group name already exists, false will |
|
| 95 | + * be returned. |
|
| 96 | + */ |
|
| 97 | + public function createGroup(string $gid): bool { |
|
| 98 | + $this->fixDI(); |
|
| 99 | + |
|
| 100 | + // Add group |
|
| 101 | + $result = $this->dbConn->insertIfNotExist('*PREFIX*groups', [ |
|
| 102 | + 'gid' => $gid, |
|
| 103 | + ]); |
|
| 104 | + |
|
| 105 | + // Add to cache |
|
| 106 | + $this->groupCache[$gid] = $gid; |
|
| 107 | + |
|
| 108 | + return $result === 1; |
|
| 109 | + } |
|
| 110 | + |
|
| 111 | + /** |
|
| 112 | + * delete a group |
|
| 113 | + * @param string $gid gid of the group to delete |
|
| 114 | + * @return bool |
|
| 115 | + * |
|
| 116 | + * Deletes a group and removes it from the group_user-table |
|
| 117 | + */ |
|
| 118 | + public function deleteGroup(string $gid): bool { |
|
| 119 | + $this->fixDI(); |
|
| 120 | + |
|
| 121 | + // Delete the group |
|
| 122 | + $qb = $this->dbConn->getQueryBuilder(); |
|
| 123 | + $qb->delete('groups') |
|
| 124 | + ->where($qb->expr()->eq('gid', $qb->createNamedParameter($gid))) |
|
| 125 | + ->execute(); |
|
| 126 | + |
|
| 127 | + // Delete the group-user relation |
|
| 128 | + $qb = $this->dbConn->getQueryBuilder(); |
|
| 129 | + $qb->delete('group_user') |
|
| 130 | + ->where($qb->expr()->eq('gid', $qb->createNamedParameter($gid))) |
|
| 131 | + ->execute(); |
|
| 132 | + |
|
| 133 | + // Delete the group-groupadmin relation |
|
| 134 | + $qb = $this->dbConn->getQueryBuilder(); |
|
| 135 | + $qb->delete('group_admin') |
|
| 136 | + ->where($qb->expr()->eq('gid', $qb->createNamedParameter($gid))) |
|
| 137 | + ->execute(); |
|
| 138 | + |
|
| 139 | + // Delete from cache |
|
| 140 | + unset($this->groupCache[$gid]); |
|
| 141 | + |
|
| 142 | + return true; |
|
| 143 | + } |
|
| 144 | + |
|
| 145 | + /** |
|
| 146 | + * is user in group? |
|
| 147 | + * @param string $uid uid of the user |
|
| 148 | + * @param string $gid gid of the group |
|
| 149 | + * @return bool |
|
| 150 | + * |
|
| 151 | + * Checks whether the user is member of a group or not. |
|
| 152 | + */ |
|
| 153 | + public function inGroup( $uid, $gid ) { |
|
| 154 | + $this->fixDI(); |
|
| 155 | + |
|
| 156 | + // check |
|
| 157 | + $qb = $this->dbConn->getQueryBuilder(); |
|
| 158 | + $cursor = $qb->select('uid') |
|
| 159 | + ->from('group_user') |
|
| 160 | + ->where($qb->expr()->eq('gid', $qb->createNamedParameter($gid))) |
|
| 161 | + ->andWhere($qb->expr()->eq('uid', $qb->createNamedParameter($uid))) |
|
| 162 | + ->execute(); |
|
| 163 | + |
|
| 164 | + $result = $cursor->fetch(); |
|
| 165 | + $cursor->closeCursor(); |
|
| 166 | + |
|
| 167 | + return $result ? true : false; |
|
| 168 | + } |
|
| 169 | + |
|
| 170 | + /** |
|
| 171 | + * Add a user to a group |
|
| 172 | + * @param string $uid Name of the user to add to group |
|
| 173 | + * @param string $gid Name of the group in which add the user |
|
| 174 | + * @return bool |
|
| 175 | + * |
|
| 176 | + * Adds a user to a group. |
|
| 177 | + */ |
|
| 178 | + public function addToGroup(string $uid, string $gid): bool { |
|
| 179 | + $this->fixDI(); |
|
| 180 | + |
|
| 181 | + // No duplicate entries! |
|
| 182 | + if( !$this->inGroup( $uid, $gid )) { |
|
| 183 | + $qb = $this->dbConn->getQueryBuilder(); |
|
| 184 | + $qb->insert('group_user') |
|
| 185 | + ->setValue('uid', $qb->createNamedParameter($uid)) |
|
| 186 | + ->setValue('gid', $qb->createNamedParameter($gid)) |
|
| 187 | + ->execute(); |
|
| 188 | + return true; |
|
| 189 | + }else{ |
|
| 190 | + return false; |
|
| 191 | + } |
|
| 192 | + } |
|
| 193 | + |
|
| 194 | + /** |
|
| 195 | + * Removes a user from a group |
|
| 196 | + * @param string $uid Name of the user to remove from group |
|
| 197 | + * @param string $gid Name of the group from which remove the user |
|
| 198 | + * @return bool |
|
| 199 | + * |
|
| 200 | + * removes the user from a group. |
|
| 201 | + */ |
|
| 202 | + public function removeFromGroup(string $uid, string $gid): bool { |
|
| 203 | + $this->fixDI(); |
|
| 204 | + |
|
| 205 | + $qb = $this->dbConn->getQueryBuilder(); |
|
| 206 | + $qb->delete('group_user') |
|
| 207 | + ->where($qb->expr()->eq('uid', $qb->createNamedParameter($uid))) |
|
| 208 | + ->andWhere($qb->expr()->eq('gid', $qb->createNamedParameter($gid))) |
|
| 209 | + ->execute(); |
|
| 210 | + |
|
| 211 | + return true; |
|
| 212 | + } |
|
| 213 | + |
|
| 214 | + /** |
|
| 215 | + * Get all groups a user belongs to |
|
| 216 | + * @param string $uid Name of the user |
|
| 217 | + * @return array an array of group names |
|
| 218 | + * |
|
| 219 | + * This function fetches all groups a user belongs to. It does not check |
|
| 220 | + * if the user exists at all. |
|
| 221 | + */ |
|
| 222 | + public function getUserGroups( $uid ) { |
|
| 223 | + //guests has empty or null $uid |
|
| 224 | + if ($uid === null || $uid === '') { |
|
| 225 | + return []; |
|
| 226 | + } |
|
| 227 | + |
|
| 228 | + $this->fixDI(); |
|
| 229 | + |
|
| 230 | + // No magic! |
|
| 231 | + $qb = $this->dbConn->getQueryBuilder(); |
|
| 232 | + $cursor = $qb->select('gid') |
|
| 233 | + ->from('group_user') |
|
| 234 | + ->where($qb->expr()->eq('uid', $qb->createNamedParameter($uid))) |
|
| 235 | + ->execute(); |
|
| 236 | + |
|
| 237 | + $groups = []; |
|
| 238 | + while( $row = $cursor->fetch()) { |
|
| 239 | + $groups[] = $row['gid']; |
|
| 240 | + $this->groupCache[$row['gid']] = $row['gid']; |
|
| 241 | + } |
|
| 242 | + $cursor->closeCursor(); |
|
| 243 | + |
|
| 244 | + return $groups; |
|
| 245 | + } |
|
| 246 | + |
|
| 247 | + /** |
|
| 248 | + * get a list of all groups |
|
| 249 | + * @param string $search |
|
| 250 | + * @param int $limit |
|
| 251 | + * @param int $offset |
|
| 252 | + * @return array an array of group names |
|
| 253 | + * |
|
| 254 | + * Returns a list with all groups |
|
| 255 | + */ |
|
| 256 | + public function getGroups($search = '', $limit = null, $offset = null) { |
|
| 257 | + $this->fixDI(); |
|
| 258 | + |
|
| 259 | + $query = $this->dbConn->getQueryBuilder(); |
|
| 260 | + $query->select('gid') |
|
| 261 | + ->from('groups') |
|
| 262 | + ->orderBy('gid', 'ASC'); |
|
| 263 | + |
|
| 264 | + if ($search !== '') { |
|
| 265 | + $query->where($query->expr()->iLike('gid', $query->createNamedParameter( |
|
| 266 | + '%' . $this->dbConn->escapeLikeParameter($search) . '%' |
|
| 267 | + ))); |
|
| 268 | + } |
|
| 269 | + |
|
| 270 | + $query->setMaxResults($limit) |
|
| 271 | + ->setFirstResult($offset); |
|
| 272 | + $result = $query->execute(); |
|
| 273 | + |
|
| 274 | + $groups = []; |
|
| 275 | + while ($row = $result->fetch()) { |
|
| 276 | + $groups[] = $row['gid']; |
|
| 277 | + } |
|
| 278 | + $result->closeCursor(); |
|
| 279 | + |
|
| 280 | + return $groups; |
|
| 281 | + } |
|
| 282 | + |
|
| 283 | + /** |
|
| 284 | + * check if a group exists |
|
| 285 | + * @param string $gid |
|
| 286 | + * @return bool |
|
| 287 | + */ |
|
| 288 | + public function groupExists($gid) { |
|
| 289 | + $this->fixDI(); |
|
| 290 | + |
|
| 291 | + // Check cache first |
|
| 292 | + if (isset($this->groupCache[$gid])) { |
|
| 293 | + return true; |
|
| 294 | + } |
|
| 295 | + |
|
| 296 | + $qb = $this->dbConn->getQueryBuilder(); |
|
| 297 | + $cursor = $qb->select('gid') |
|
| 298 | + ->from('groups') |
|
| 299 | + ->where($qb->expr()->eq('gid', $qb->createNamedParameter($gid))) |
|
| 300 | + ->execute(); |
|
| 301 | + $result = $cursor->fetch(); |
|
| 302 | + $cursor->closeCursor(); |
|
| 303 | + |
|
| 304 | + if ($result !== false) { |
|
| 305 | + $this->groupCache[$gid] = $gid; |
|
| 306 | + return true; |
|
| 307 | + } |
|
| 308 | + return false; |
|
| 309 | + } |
|
| 310 | + |
|
| 311 | + /** |
|
| 312 | + * get a list of all users in a group |
|
| 313 | + * @param string $gid |
|
| 314 | + * @param string $search |
|
| 315 | + * @param int $limit |
|
| 316 | + * @param int $offset |
|
| 317 | + * @return array an array of user ids |
|
| 318 | + */ |
|
| 319 | + public function usersInGroup($gid, $search = '', $limit = null, $offset = null) { |
|
| 320 | + $this->fixDI(); |
|
| 321 | + |
|
| 322 | + $query = $this->dbConn->getQueryBuilder(); |
|
| 323 | + $query->select('uid') |
|
| 324 | + ->from('group_user') |
|
| 325 | + ->where($query->expr()->eq('gid', $query->createNamedParameter($gid))) |
|
| 326 | + ->orderBy('uid', 'ASC'); |
|
| 327 | + |
|
| 328 | + if ($search !== '') { |
|
| 329 | + $query->andWhere($query->expr()->like('uid', $query->createNamedParameter( |
|
| 330 | + '%' . $this->dbConn->escapeLikeParameter($search) . '%' |
|
| 331 | + ))); |
|
| 332 | + } |
|
| 333 | + |
|
| 334 | + $query->setMaxResults($limit) |
|
| 335 | + ->setFirstResult($offset); |
|
| 336 | + $result = $query->execute(); |
|
| 337 | + |
|
| 338 | + $users = []; |
|
| 339 | + while ($row = $result->fetch()) { |
|
| 340 | + $users[] = $row['uid']; |
|
| 341 | + } |
|
| 342 | + $result->closeCursor(); |
|
| 343 | + |
|
| 344 | + return $users; |
|
| 345 | + } |
|
| 346 | + |
|
| 347 | + /** |
|
| 348 | + * get the number of all users matching the search string in a group |
|
| 349 | + * @param string $gid |
|
| 350 | + * @param string $search |
|
| 351 | + * @return int |
|
| 352 | + */ |
|
| 353 | + public function countUsersInGroup(string $gid, string $search = ''): int { |
|
| 354 | + $this->fixDI(); |
|
| 355 | + |
|
| 356 | + $query = $this->dbConn->getQueryBuilder(); |
|
| 357 | + $query->selectAlias($query->createFunction('COUNT(*)'), 'num_users') |
|
| 358 | + ->from('group_user') |
|
| 359 | + ->where($query->expr()->eq('gid', $query->createNamedParameter($gid))); |
|
| 360 | + |
|
| 361 | + if ($search !== '') { |
|
| 362 | + $query->andWhere($query->expr()->like('uid', $query->createNamedParameter( |
|
| 363 | + '%' . $this->dbConn->escapeLikeParameter($search) . '%' |
|
| 364 | + ))); |
|
| 365 | + } |
|
| 366 | + |
|
| 367 | + $result = $query->execute(); |
|
| 368 | + $count = $result->fetchColumn(); |
|
| 369 | + $result->closeCursor(); |
|
| 370 | + |
|
| 371 | + if ($count !== false) { |
|
| 372 | + $count = (int)$count; |
|
| 373 | + } else { |
|
| 374 | + $count = 0; |
|
| 375 | + } |
|
| 376 | + |
|
| 377 | + return $count; |
|
| 378 | + } |
|
| 379 | + |
|
| 380 | + /** |
|
| 381 | + * get the number of disabled users in a group |
|
| 382 | + * |
|
| 383 | + * @param string $search |
|
| 384 | + * @return int|bool |
|
| 385 | + */ |
|
| 386 | + public function countDisabledInGroup(string $gid): int { |
|
| 387 | + $this->fixDI(); |
|
| 388 | 388 | |
| 389 | - $query = $this->dbConn->getQueryBuilder(); |
|
| 390 | - $query->select($query->createFunction('COUNT(Distinct uid)')) |
|
| 391 | - ->from('preferences', 'p') |
|
| 392 | - ->innerJoin('p', 'group_user', 'g', 'p.userid = g.uid') |
|
| 393 | - ->where($query->expr()->eq('appid', $query->createNamedParameter('core'))) |
|
| 394 | - ->andWhere($query->expr()->eq('configkey', $query->createNamedParameter('enabled'))) |
|
| 395 | - ->andWhere($query->expr()->eq('configvalue', $query->createNamedParameter('false'), IQueryBuilder::PARAM_STR)) |
|
| 396 | - ->andWhere($query->expr()->eq('gid', $query->createNamedParameter($gid), IQueryBuilder::PARAM_STR)); |
|
| 389 | + $query = $this->dbConn->getQueryBuilder(); |
|
| 390 | + $query->select($query->createFunction('COUNT(Distinct uid)')) |
|
| 391 | + ->from('preferences', 'p') |
|
| 392 | + ->innerJoin('p', 'group_user', 'g', 'p.userid = g.uid') |
|
| 393 | + ->where($query->expr()->eq('appid', $query->createNamedParameter('core'))) |
|
| 394 | + ->andWhere($query->expr()->eq('configkey', $query->createNamedParameter('enabled'))) |
|
| 395 | + ->andWhere($query->expr()->eq('configvalue', $query->createNamedParameter('false'), IQueryBuilder::PARAM_STR)) |
|
| 396 | + ->andWhere($query->expr()->eq('gid', $query->createNamedParameter($gid), IQueryBuilder::PARAM_STR)); |
|
| 397 | 397 | |
| 398 | - $result = $query->execute(); |
|
| 399 | - $count = $result->fetchColumn(); |
|
| 400 | - $result->closeCursor(); |
|
| 398 | + $result = $query->execute(); |
|
| 399 | + $count = $result->fetchColumn(); |
|
| 400 | + $result->closeCursor(); |
|
| 401 | 401 | |
| 402 | - if ($count !== false) { |
|
| 403 | - $count = (int)$count; |
|
| 404 | - } else { |
|
| 405 | - $count = 0; |
|
| 406 | - } |
|
| 407 | - |
|
| 408 | - return $count; |
|
| 409 | - } |
|
| 402 | + if ($count !== false) { |
|
| 403 | + $count = (int)$count; |
|
| 404 | + } else { |
|
| 405 | + $count = 0; |
|
| 406 | + } |
|
| 407 | + |
|
| 408 | + return $count; |
|
| 409 | + } |
|
| 410 | 410 | |
| 411 | 411 | } |
@@ -150,7 +150,7 @@ discard block |
||
| 150 | 150 | * |
| 151 | 151 | * Checks whether the user is member of a group or not. |
| 152 | 152 | */ |
| 153 | - public function inGroup( $uid, $gid ) { |
|
| 153 | + public function inGroup($uid, $gid) { |
|
| 154 | 154 | $this->fixDI(); |
| 155 | 155 | |
| 156 | 156 | // check |
@@ -179,14 +179,14 @@ discard block |
||
| 179 | 179 | $this->fixDI(); |
| 180 | 180 | |
| 181 | 181 | // No duplicate entries! |
| 182 | - if( !$this->inGroup( $uid, $gid )) { |
|
| 182 | + if (!$this->inGroup($uid, $gid)) { |
|
| 183 | 183 | $qb = $this->dbConn->getQueryBuilder(); |
| 184 | 184 | $qb->insert('group_user') |
| 185 | 185 | ->setValue('uid', $qb->createNamedParameter($uid)) |
| 186 | 186 | ->setValue('gid', $qb->createNamedParameter($gid)) |
| 187 | 187 | ->execute(); |
| 188 | 188 | return true; |
| 189 | - }else{ |
|
| 189 | + } else { |
|
| 190 | 190 | return false; |
| 191 | 191 | } |
| 192 | 192 | } |
@@ -219,7 +219,7 @@ discard block |
||
| 219 | 219 | * This function fetches all groups a user belongs to. It does not check |
| 220 | 220 | * if the user exists at all. |
| 221 | 221 | */ |
| 222 | - public function getUserGroups( $uid ) { |
|
| 222 | + public function getUserGroups($uid) { |
|
| 223 | 223 | //guests has empty or null $uid |
| 224 | 224 | if ($uid === null || $uid === '') { |
| 225 | 225 | return []; |
@@ -235,7 +235,7 @@ discard block |
||
| 235 | 235 | ->execute(); |
| 236 | 236 | |
| 237 | 237 | $groups = []; |
| 238 | - while( $row = $cursor->fetch()) { |
|
| 238 | + while ($row = $cursor->fetch()) { |
|
| 239 | 239 | $groups[] = $row['gid']; |
| 240 | 240 | $this->groupCache[$row['gid']] = $row['gid']; |
| 241 | 241 | } |
@@ -263,7 +263,7 @@ discard block |
||
| 263 | 263 | |
| 264 | 264 | if ($search !== '') { |
| 265 | 265 | $query->where($query->expr()->iLike('gid', $query->createNamedParameter( |
| 266 | - '%' . $this->dbConn->escapeLikeParameter($search) . '%' |
|
| 266 | + '%'.$this->dbConn->escapeLikeParameter($search).'%' |
|
| 267 | 267 | ))); |
| 268 | 268 | } |
| 269 | 269 | |
@@ -327,7 +327,7 @@ discard block |
||
| 327 | 327 | |
| 328 | 328 | if ($search !== '') { |
| 329 | 329 | $query->andWhere($query->expr()->like('uid', $query->createNamedParameter( |
| 330 | - '%' . $this->dbConn->escapeLikeParameter($search) . '%' |
|
| 330 | + '%'.$this->dbConn->escapeLikeParameter($search).'%' |
|
| 331 | 331 | ))); |
| 332 | 332 | } |
| 333 | 333 | |
@@ -360,7 +360,7 @@ discard block |
||
| 360 | 360 | |
| 361 | 361 | if ($search !== '') { |
| 362 | 362 | $query->andWhere($query->expr()->like('uid', $query->createNamedParameter( |
| 363 | - '%' . $this->dbConn->escapeLikeParameter($search) . '%' |
|
| 363 | + '%'.$this->dbConn->escapeLikeParameter($search).'%' |
|
| 364 | 364 | ))); |
| 365 | 365 | } |
| 366 | 366 | |
@@ -369,7 +369,7 @@ discard block |
||
| 369 | 369 | $result->closeCursor(); |
| 370 | 370 | |
| 371 | 371 | if ($count !== false) { |
| 372 | - $count = (int)$count; |
|
| 372 | + $count = (int) $count; |
|
| 373 | 373 | } else { |
| 374 | 374 | $count = 0; |
| 375 | 375 | } |
@@ -400,7 +400,7 @@ discard block |
||
| 400 | 400 | $result->closeCursor(); |
| 401 | 401 | |
| 402 | 402 | if ($count !== false) { |
| 403 | - $count = (int)$count; |
|
| 403 | + $count = (int) $count; |
|
| 404 | 404 | } else { |
| 405 | 405 | $count = 0; |
| 406 | 406 | } |
@@ -35,292 +35,292 @@ |
||
| 35 | 35 | use OCP\Group\Backend\ICountDisabledInGroup; |
| 36 | 36 | |
| 37 | 37 | class Group implements IGroup { |
| 38 | - /** @var null|string */ |
|
| 39 | - protected $displayName; |
|
| 38 | + /** @var null|string */ |
|
| 39 | + protected $displayName; |
|
| 40 | 40 | |
| 41 | - /** |
|
| 42 | - * @var string $id |
|
| 43 | - */ |
|
| 44 | - private $gid; |
|
| 41 | + /** |
|
| 42 | + * @var string $id |
|
| 43 | + */ |
|
| 44 | + private $gid; |
|
| 45 | 45 | |
| 46 | - /** |
|
| 47 | - * @var \OC\User\User[] $users |
|
| 48 | - */ |
|
| 49 | - private $users = array(); |
|
| 46 | + /** |
|
| 47 | + * @var \OC\User\User[] $users |
|
| 48 | + */ |
|
| 49 | + private $users = array(); |
|
| 50 | 50 | |
| 51 | - /** |
|
| 52 | - * @var bool $usersLoaded |
|
| 53 | - */ |
|
| 54 | - private $usersLoaded; |
|
| 51 | + /** |
|
| 52 | + * @var bool $usersLoaded |
|
| 53 | + */ |
|
| 54 | + private $usersLoaded; |
|
| 55 | 55 | |
| 56 | - /** |
|
| 57 | - * @var \OC\Group\Backend[]|\OC\Group\Database[] $backend |
|
| 58 | - */ |
|
| 59 | - private $backends; |
|
| 56 | + /** |
|
| 57 | + * @var \OC\Group\Backend[]|\OC\Group\Database[] $backend |
|
| 58 | + */ |
|
| 59 | + private $backends; |
|
| 60 | 60 | |
| 61 | - /** |
|
| 62 | - * @var \OC\Hooks\PublicEmitter $emitter |
|
| 63 | - */ |
|
| 64 | - private $emitter; |
|
| 61 | + /** |
|
| 62 | + * @var \OC\Hooks\PublicEmitter $emitter |
|
| 63 | + */ |
|
| 64 | + private $emitter; |
|
| 65 | 65 | |
| 66 | - /** |
|
| 67 | - * @var \OC\User\Manager $userManager |
|
| 68 | - */ |
|
| 69 | - private $userManager; |
|
| 66 | + /** |
|
| 67 | + * @var \OC\User\Manager $userManager |
|
| 68 | + */ |
|
| 69 | + private $userManager; |
|
| 70 | 70 | |
| 71 | - /** |
|
| 72 | - * @param string $gid |
|
| 73 | - * @param \OC\Group\Backend[] $backends |
|
| 74 | - * @param \OC\User\Manager $userManager |
|
| 75 | - * @param \OC\Hooks\PublicEmitter $emitter |
|
| 76 | - * @param string $displayName |
|
| 77 | - */ |
|
| 78 | - public function __construct($gid, $backends, $userManager, $emitter = null, $displayName = null) { |
|
| 79 | - $this->gid = $gid; |
|
| 80 | - $this->backends = $backends; |
|
| 81 | - $this->userManager = $userManager; |
|
| 82 | - $this->emitter = $emitter; |
|
| 83 | - $this->displayName = $displayName; |
|
| 84 | - } |
|
| 71 | + /** |
|
| 72 | + * @param string $gid |
|
| 73 | + * @param \OC\Group\Backend[] $backends |
|
| 74 | + * @param \OC\User\Manager $userManager |
|
| 75 | + * @param \OC\Hooks\PublicEmitter $emitter |
|
| 76 | + * @param string $displayName |
|
| 77 | + */ |
|
| 78 | + public function __construct($gid, $backends, $userManager, $emitter = null, $displayName = null) { |
|
| 79 | + $this->gid = $gid; |
|
| 80 | + $this->backends = $backends; |
|
| 81 | + $this->userManager = $userManager; |
|
| 82 | + $this->emitter = $emitter; |
|
| 83 | + $this->displayName = $displayName; |
|
| 84 | + } |
|
| 85 | 85 | |
| 86 | - public function getGID() { |
|
| 87 | - return $this->gid; |
|
| 88 | - } |
|
| 86 | + public function getGID() { |
|
| 87 | + return $this->gid; |
|
| 88 | + } |
|
| 89 | 89 | |
| 90 | - public function getDisplayName() { |
|
| 91 | - if (is_null($this->displayName)) { |
|
| 92 | - return $this->gid; |
|
| 93 | - } |
|
| 94 | - return $this->displayName; |
|
| 95 | - } |
|
| 90 | + public function getDisplayName() { |
|
| 91 | + if (is_null($this->displayName)) { |
|
| 92 | + return $this->gid; |
|
| 93 | + } |
|
| 94 | + return $this->displayName; |
|
| 95 | + } |
|
| 96 | 96 | |
| 97 | - /** |
|
| 98 | - * get all users in the group |
|
| 99 | - * |
|
| 100 | - * @return \OC\User\User[] |
|
| 101 | - */ |
|
| 102 | - public function getUsers() { |
|
| 103 | - if ($this->usersLoaded) { |
|
| 104 | - return $this->users; |
|
| 105 | - } |
|
| 97 | + /** |
|
| 98 | + * get all users in the group |
|
| 99 | + * |
|
| 100 | + * @return \OC\User\User[] |
|
| 101 | + */ |
|
| 102 | + public function getUsers() { |
|
| 103 | + if ($this->usersLoaded) { |
|
| 104 | + return $this->users; |
|
| 105 | + } |
|
| 106 | 106 | |
| 107 | - $userIds = array(); |
|
| 108 | - foreach ($this->backends as $backend) { |
|
| 109 | - $diff = array_diff( |
|
| 110 | - $backend->usersInGroup($this->gid), |
|
| 111 | - $userIds |
|
| 112 | - ); |
|
| 113 | - if ($diff) { |
|
| 114 | - $userIds = array_merge($userIds, $diff); |
|
| 115 | - } |
|
| 116 | - } |
|
| 107 | + $userIds = array(); |
|
| 108 | + foreach ($this->backends as $backend) { |
|
| 109 | + $diff = array_diff( |
|
| 110 | + $backend->usersInGroup($this->gid), |
|
| 111 | + $userIds |
|
| 112 | + ); |
|
| 113 | + if ($diff) { |
|
| 114 | + $userIds = array_merge($userIds, $diff); |
|
| 115 | + } |
|
| 116 | + } |
|
| 117 | 117 | |
| 118 | - $this->users = $this->getVerifiedUsers($userIds); |
|
| 119 | - $this->usersLoaded = true; |
|
| 120 | - return $this->users; |
|
| 121 | - } |
|
| 118 | + $this->users = $this->getVerifiedUsers($userIds); |
|
| 119 | + $this->usersLoaded = true; |
|
| 120 | + return $this->users; |
|
| 121 | + } |
|
| 122 | 122 | |
| 123 | - /** |
|
| 124 | - * check if a user is in the group |
|
| 125 | - * |
|
| 126 | - * @param IUser $user |
|
| 127 | - * @return bool |
|
| 128 | - */ |
|
| 129 | - public function inGroup(IUser $user) { |
|
| 130 | - if (isset($this->users[$user->getUID()])) { |
|
| 131 | - return true; |
|
| 132 | - } |
|
| 133 | - foreach ($this->backends as $backend) { |
|
| 134 | - if ($backend->inGroup($user->getUID(), $this->gid)) { |
|
| 135 | - $this->users[$user->getUID()] = $user; |
|
| 136 | - return true; |
|
| 137 | - } |
|
| 138 | - } |
|
| 139 | - return false; |
|
| 140 | - } |
|
| 123 | + /** |
|
| 124 | + * check if a user is in the group |
|
| 125 | + * |
|
| 126 | + * @param IUser $user |
|
| 127 | + * @return bool |
|
| 128 | + */ |
|
| 129 | + public function inGroup(IUser $user) { |
|
| 130 | + if (isset($this->users[$user->getUID()])) { |
|
| 131 | + return true; |
|
| 132 | + } |
|
| 133 | + foreach ($this->backends as $backend) { |
|
| 134 | + if ($backend->inGroup($user->getUID(), $this->gid)) { |
|
| 135 | + $this->users[$user->getUID()] = $user; |
|
| 136 | + return true; |
|
| 137 | + } |
|
| 138 | + } |
|
| 139 | + return false; |
|
| 140 | + } |
|
| 141 | 141 | |
| 142 | - /** |
|
| 143 | - * add a user to the group |
|
| 144 | - * |
|
| 145 | - * @param IUser $user |
|
| 146 | - */ |
|
| 147 | - public function addUser(IUser $user) { |
|
| 148 | - if ($this->inGroup($user)) { |
|
| 149 | - return; |
|
| 150 | - } |
|
| 142 | + /** |
|
| 143 | + * add a user to the group |
|
| 144 | + * |
|
| 145 | + * @param IUser $user |
|
| 146 | + */ |
|
| 147 | + public function addUser(IUser $user) { |
|
| 148 | + if ($this->inGroup($user)) { |
|
| 149 | + return; |
|
| 150 | + } |
|
| 151 | 151 | |
| 152 | - if ($this->emitter) { |
|
| 153 | - $this->emitter->emit('\OC\Group', 'preAddUser', array($this, $user)); |
|
| 154 | - } |
|
| 155 | - foreach ($this->backends as $backend) { |
|
| 156 | - if ($backend->implementsActions(\OC\Group\Backend::ADD_TO_GROUP)) { |
|
| 157 | - $backend->addToGroup($user->getUID(), $this->gid); |
|
| 158 | - if ($this->users) { |
|
| 159 | - $this->users[$user->getUID()] = $user; |
|
| 160 | - } |
|
| 161 | - if ($this->emitter) { |
|
| 162 | - $this->emitter->emit('\OC\Group', 'postAddUser', array($this, $user)); |
|
| 163 | - } |
|
| 164 | - return; |
|
| 165 | - } |
|
| 166 | - } |
|
| 167 | - } |
|
| 152 | + if ($this->emitter) { |
|
| 153 | + $this->emitter->emit('\OC\Group', 'preAddUser', array($this, $user)); |
|
| 154 | + } |
|
| 155 | + foreach ($this->backends as $backend) { |
|
| 156 | + if ($backend->implementsActions(\OC\Group\Backend::ADD_TO_GROUP)) { |
|
| 157 | + $backend->addToGroup($user->getUID(), $this->gid); |
|
| 158 | + if ($this->users) { |
|
| 159 | + $this->users[$user->getUID()] = $user; |
|
| 160 | + } |
|
| 161 | + if ($this->emitter) { |
|
| 162 | + $this->emitter->emit('\OC\Group', 'postAddUser', array($this, $user)); |
|
| 163 | + } |
|
| 164 | + return; |
|
| 165 | + } |
|
| 166 | + } |
|
| 167 | + } |
|
| 168 | 168 | |
| 169 | - /** |
|
| 170 | - * remove a user from the group |
|
| 171 | - * |
|
| 172 | - * @param \OC\User\User $user |
|
| 173 | - */ |
|
| 174 | - public function removeUser($user) { |
|
| 175 | - $result = false; |
|
| 176 | - if ($this->emitter) { |
|
| 177 | - $this->emitter->emit('\OC\Group', 'preRemoveUser', array($this, $user)); |
|
| 178 | - } |
|
| 179 | - foreach ($this->backends as $backend) { |
|
| 180 | - if ($backend->implementsActions(\OC\Group\Backend::REMOVE_FROM_GOUP) and $backend->inGroup($user->getUID(), $this->gid)) { |
|
| 181 | - $backend->removeFromGroup($user->getUID(), $this->gid); |
|
| 182 | - $result = true; |
|
| 183 | - } |
|
| 184 | - } |
|
| 185 | - if ($result) { |
|
| 186 | - if ($this->emitter) { |
|
| 187 | - $this->emitter->emit('\OC\Group', 'postRemoveUser', array($this, $user)); |
|
| 188 | - } |
|
| 189 | - if ($this->users) { |
|
| 190 | - foreach ($this->users as $index => $groupUser) { |
|
| 191 | - if ($groupUser->getUID() === $user->getUID()) { |
|
| 192 | - unset($this->users[$index]); |
|
| 193 | - return; |
|
| 194 | - } |
|
| 195 | - } |
|
| 196 | - } |
|
| 197 | - } |
|
| 198 | - } |
|
| 169 | + /** |
|
| 170 | + * remove a user from the group |
|
| 171 | + * |
|
| 172 | + * @param \OC\User\User $user |
|
| 173 | + */ |
|
| 174 | + public function removeUser($user) { |
|
| 175 | + $result = false; |
|
| 176 | + if ($this->emitter) { |
|
| 177 | + $this->emitter->emit('\OC\Group', 'preRemoveUser', array($this, $user)); |
|
| 178 | + } |
|
| 179 | + foreach ($this->backends as $backend) { |
|
| 180 | + if ($backend->implementsActions(\OC\Group\Backend::REMOVE_FROM_GOUP) and $backend->inGroup($user->getUID(), $this->gid)) { |
|
| 181 | + $backend->removeFromGroup($user->getUID(), $this->gid); |
|
| 182 | + $result = true; |
|
| 183 | + } |
|
| 184 | + } |
|
| 185 | + if ($result) { |
|
| 186 | + if ($this->emitter) { |
|
| 187 | + $this->emitter->emit('\OC\Group', 'postRemoveUser', array($this, $user)); |
|
| 188 | + } |
|
| 189 | + if ($this->users) { |
|
| 190 | + foreach ($this->users as $index => $groupUser) { |
|
| 191 | + if ($groupUser->getUID() === $user->getUID()) { |
|
| 192 | + unset($this->users[$index]); |
|
| 193 | + return; |
|
| 194 | + } |
|
| 195 | + } |
|
| 196 | + } |
|
| 197 | + } |
|
| 198 | + } |
|
| 199 | 199 | |
| 200 | - /** |
|
| 201 | - * search for users in the group by userid |
|
| 202 | - * |
|
| 203 | - * @param string $search |
|
| 204 | - * @param int $limit |
|
| 205 | - * @param int $offset |
|
| 206 | - * @return \OC\User\User[] |
|
| 207 | - */ |
|
| 208 | - public function searchUsers($search, $limit = null, $offset = null) { |
|
| 209 | - $users = array(); |
|
| 210 | - foreach ($this->backends as $backend) { |
|
| 211 | - $userIds = $backend->usersInGroup($this->gid, $search, $limit, $offset); |
|
| 212 | - $users += $this->getVerifiedUsers($userIds); |
|
| 213 | - if (!is_null($limit) and $limit <= 0) { |
|
| 214 | - return array_values($users); |
|
| 215 | - } |
|
| 216 | - } |
|
| 217 | - return array_values($users); |
|
| 218 | - } |
|
| 200 | + /** |
|
| 201 | + * search for users in the group by userid |
|
| 202 | + * |
|
| 203 | + * @param string $search |
|
| 204 | + * @param int $limit |
|
| 205 | + * @param int $offset |
|
| 206 | + * @return \OC\User\User[] |
|
| 207 | + */ |
|
| 208 | + public function searchUsers($search, $limit = null, $offset = null) { |
|
| 209 | + $users = array(); |
|
| 210 | + foreach ($this->backends as $backend) { |
|
| 211 | + $userIds = $backend->usersInGroup($this->gid, $search, $limit, $offset); |
|
| 212 | + $users += $this->getVerifiedUsers($userIds); |
|
| 213 | + if (!is_null($limit) and $limit <= 0) { |
|
| 214 | + return array_values($users); |
|
| 215 | + } |
|
| 216 | + } |
|
| 217 | + return array_values($users); |
|
| 218 | + } |
|
| 219 | 219 | |
| 220 | - /** |
|
| 221 | - * returns the number of users matching the search string |
|
| 222 | - * |
|
| 223 | - * @param string $search |
|
| 224 | - * @return int|bool |
|
| 225 | - */ |
|
| 226 | - public function count($search = '') { |
|
| 227 | - $users = false; |
|
| 228 | - foreach ($this->backends as $backend) { |
|
| 229 | - if($backend->implementsActions(\OC\Group\Backend::COUNT_USERS)) { |
|
| 230 | - if($users === false) { |
|
| 231 | - //we could directly add to a bool variable, but this would |
|
| 232 | - //be ugly |
|
| 233 | - $users = 0; |
|
| 234 | - } |
|
| 235 | - $users += $backend->countUsersInGroup($this->gid, $search); |
|
| 236 | - } |
|
| 237 | - } |
|
| 238 | - return $users; |
|
| 239 | - } |
|
| 220 | + /** |
|
| 221 | + * returns the number of users matching the search string |
|
| 222 | + * |
|
| 223 | + * @param string $search |
|
| 224 | + * @return int|bool |
|
| 225 | + */ |
|
| 226 | + public function count($search = '') { |
|
| 227 | + $users = false; |
|
| 228 | + foreach ($this->backends as $backend) { |
|
| 229 | + if($backend->implementsActions(\OC\Group\Backend::COUNT_USERS)) { |
|
| 230 | + if($users === false) { |
|
| 231 | + //we could directly add to a bool variable, but this would |
|
| 232 | + //be ugly |
|
| 233 | + $users = 0; |
|
| 234 | + } |
|
| 235 | + $users += $backend->countUsersInGroup($this->gid, $search); |
|
| 236 | + } |
|
| 237 | + } |
|
| 238 | + return $users; |
|
| 239 | + } |
|
| 240 | 240 | |
| 241 | - /** |
|
| 242 | - * returns the number of disabled users |
|
| 243 | - * |
|
| 244 | - * @return int|bool |
|
| 245 | - */ |
|
| 246 | - public function countDisabled() { |
|
| 247 | - $users = false; |
|
| 248 | - foreach ($this->backends as $backend) { |
|
| 249 | - if($backend instanceOf ICountDisabledInGroup) { |
|
| 250 | - if($users === false) { |
|
| 251 | - //we could directly add to a bool variable, but this would |
|
| 252 | - //be ugly |
|
| 253 | - $users = 0; |
|
| 254 | - } |
|
| 255 | - $users += $backend->countDisabledInGroup($this->gid); |
|
| 256 | - } |
|
| 257 | - } |
|
| 258 | - return $users; |
|
| 259 | - } |
|
| 241 | + /** |
|
| 242 | + * returns the number of disabled users |
|
| 243 | + * |
|
| 244 | + * @return int|bool |
|
| 245 | + */ |
|
| 246 | + public function countDisabled() { |
|
| 247 | + $users = false; |
|
| 248 | + foreach ($this->backends as $backend) { |
|
| 249 | + if($backend instanceOf ICountDisabledInGroup) { |
|
| 250 | + if($users === false) { |
|
| 251 | + //we could directly add to a bool variable, but this would |
|
| 252 | + //be ugly |
|
| 253 | + $users = 0; |
|
| 254 | + } |
|
| 255 | + $users += $backend->countDisabledInGroup($this->gid); |
|
| 256 | + } |
|
| 257 | + } |
|
| 258 | + return $users; |
|
| 259 | + } |
|
| 260 | 260 | |
| 261 | - /** |
|
| 262 | - * search for users in the group by displayname |
|
| 263 | - * |
|
| 264 | - * @param string $search |
|
| 265 | - * @param int $limit |
|
| 266 | - * @param int $offset |
|
| 267 | - * @return \OC\User\User[] |
|
| 268 | - */ |
|
| 269 | - public function searchDisplayName($search, $limit = null, $offset = null) { |
|
| 270 | - $users = array(); |
|
| 271 | - foreach ($this->backends as $backend) { |
|
| 272 | - $userIds = $backend->usersInGroup($this->gid, $search, $limit, $offset); |
|
| 273 | - $users = $this->getVerifiedUsers($userIds); |
|
| 274 | - if (!is_null($limit) and $limit <= 0) { |
|
| 275 | - return array_values($users); |
|
| 276 | - } |
|
| 277 | - } |
|
| 278 | - return array_values($users); |
|
| 279 | - } |
|
| 261 | + /** |
|
| 262 | + * search for users in the group by displayname |
|
| 263 | + * |
|
| 264 | + * @param string $search |
|
| 265 | + * @param int $limit |
|
| 266 | + * @param int $offset |
|
| 267 | + * @return \OC\User\User[] |
|
| 268 | + */ |
|
| 269 | + public function searchDisplayName($search, $limit = null, $offset = null) { |
|
| 270 | + $users = array(); |
|
| 271 | + foreach ($this->backends as $backend) { |
|
| 272 | + $userIds = $backend->usersInGroup($this->gid, $search, $limit, $offset); |
|
| 273 | + $users = $this->getVerifiedUsers($userIds); |
|
| 274 | + if (!is_null($limit) and $limit <= 0) { |
|
| 275 | + return array_values($users); |
|
| 276 | + } |
|
| 277 | + } |
|
| 278 | + return array_values($users); |
|
| 279 | + } |
|
| 280 | 280 | |
| 281 | - /** |
|
| 282 | - * delete the group |
|
| 283 | - * |
|
| 284 | - * @return bool |
|
| 285 | - */ |
|
| 286 | - public function delete() { |
|
| 287 | - // Prevent users from deleting group admin |
|
| 288 | - if ($this->getGID() === 'admin') { |
|
| 289 | - return false; |
|
| 290 | - } |
|
| 281 | + /** |
|
| 282 | + * delete the group |
|
| 283 | + * |
|
| 284 | + * @return bool |
|
| 285 | + */ |
|
| 286 | + public function delete() { |
|
| 287 | + // Prevent users from deleting group admin |
|
| 288 | + if ($this->getGID() === 'admin') { |
|
| 289 | + return false; |
|
| 290 | + } |
|
| 291 | 291 | |
| 292 | - $result = false; |
|
| 293 | - if ($this->emitter) { |
|
| 294 | - $this->emitter->emit('\OC\Group', 'preDelete', array($this)); |
|
| 295 | - } |
|
| 296 | - foreach ($this->backends as $backend) { |
|
| 297 | - if ($backend->implementsActions(\OC\Group\Backend::DELETE_GROUP)) { |
|
| 298 | - $result = true; |
|
| 299 | - $backend->deleteGroup($this->gid); |
|
| 300 | - } |
|
| 301 | - } |
|
| 302 | - if ($result and $this->emitter) { |
|
| 303 | - $this->emitter->emit('\OC\Group', 'postDelete', array($this)); |
|
| 304 | - } |
|
| 305 | - return $result; |
|
| 306 | - } |
|
| 292 | + $result = false; |
|
| 293 | + if ($this->emitter) { |
|
| 294 | + $this->emitter->emit('\OC\Group', 'preDelete', array($this)); |
|
| 295 | + } |
|
| 296 | + foreach ($this->backends as $backend) { |
|
| 297 | + if ($backend->implementsActions(\OC\Group\Backend::DELETE_GROUP)) { |
|
| 298 | + $result = true; |
|
| 299 | + $backend->deleteGroup($this->gid); |
|
| 300 | + } |
|
| 301 | + } |
|
| 302 | + if ($result and $this->emitter) { |
|
| 303 | + $this->emitter->emit('\OC\Group', 'postDelete', array($this)); |
|
| 304 | + } |
|
| 305 | + return $result; |
|
| 306 | + } |
|
| 307 | 307 | |
| 308 | - /** |
|
| 309 | - * returns all the Users from an array that really exists |
|
| 310 | - * @param string[] $userIds an array containing user IDs |
|
| 311 | - * @return \OC\User\User[] an Array with the userId as Key and \OC\User\User as value |
|
| 312 | - */ |
|
| 313 | - private function getVerifiedUsers($userIds) { |
|
| 314 | - if (!is_array($userIds)) { |
|
| 315 | - return array(); |
|
| 316 | - } |
|
| 317 | - $users = array(); |
|
| 318 | - foreach ($userIds as $userId) { |
|
| 319 | - $user = $this->userManager->get($userId); |
|
| 320 | - if (!is_null($user)) { |
|
| 321 | - $users[$userId] = $user; |
|
| 322 | - } |
|
| 323 | - } |
|
| 324 | - return $users; |
|
| 325 | - } |
|
| 308 | + /** |
|
| 309 | + * returns all the Users from an array that really exists |
|
| 310 | + * @param string[] $userIds an array containing user IDs |
|
| 311 | + * @return \OC\User\User[] an Array with the userId as Key and \OC\User\User as value |
|
| 312 | + */ |
|
| 313 | + private function getVerifiedUsers($userIds) { |
|
| 314 | + if (!is_array($userIds)) { |
|
| 315 | + return array(); |
|
| 316 | + } |
|
| 317 | + $users = array(); |
|
| 318 | + foreach ($userIds as $userId) { |
|
| 319 | + $user = $this->userManager->get($userId); |
|
| 320 | + if (!is_null($user)) { |
|
| 321 | + $users[$userId] = $user; |
|
| 322 | + } |
|
| 323 | + } |
|
| 324 | + return $users; |
|
| 325 | + } |
|
| 326 | 326 | } |
@@ -226,8 +226,8 @@ discard block |
||
| 226 | 226 | public function count($search = '') { |
| 227 | 227 | $users = false; |
| 228 | 228 | foreach ($this->backends as $backend) { |
| 229 | - if($backend->implementsActions(\OC\Group\Backend::COUNT_USERS)) { |
|
| 230 | - if($users === false) { |
|
| 229 | + if ($backend->implementsActions(\OC\Group\Backend::COUNT_USERS)) { |
|
| 230 | + if ($users === false) { |
|
| 231 | 231 | //we could directly add to a bool variable, but this would |
| 232 | 232 | //be ugly |
| 233 | 233 | $users = 0; |
@@ -246,8 +246,8 @@ discard block |
||
| 246 | 246 | public function countDisabled() { |
| 247 | 247 | $users = false; |
| 248 | 248 | foreach ($this->backends as $backend) { |
| 249 | - if($backend instanceOf ICountDisabledInGroup) { |
|
| 250 | - if($users === false) { |
|
| 249 | + if ($backend instanceOf ICountDisabledInGroup) { |
|
| 250 | + if ($users === false) { |
|
| 251 | 251 | //we could directly add to a bool variable, but this would |
| 252 | 252 | //be ugly |
| 253 | 253 | $users = 0; |
@@ -57,523 +57,523 @@ |
||
| 57 | 57 | * @package OC\User |
| 58 | 58 | */ |
| 59 | 59 | class Manager extends PublicEmitter implements IUserManager { |
| 60 | - /** |
|
| 61 | - * @var \OCP\UserInterface[] $backends |
|
| 62 | - */ |
|
| 63 | - private $backends = array(); |
|
| 64 | - |
|
| 65 | - /** |
|
| 66 | - * @var \OC\User\User[] $cachedUsers |
|
| 67 | - */ |
|
| 68 | - private $cachedUsers = array(); |
|
| 69 | - |
|
| 70 | - /** |
|
| 71 | - * @var \OCP\IConfig $config |
|
| 72 | - */ |
|
| 73 | - private $config; |
|
| 74 | - |
|
| 75 | - /** |
|
| 76 | - * @param \OCP\IConfig $config |
|
| 77 | - */ |
|
| 78 | - public function __construct(IConfig $config) { |
|
| 79 | - $this->config = $config; |
|
| 80 | - $cachedUsers = &$this->cachedUsers; |
|
| 81 | - $this->listen('\OC\User', 'postDelete', function ($user) use (&$cachedUsers) { |
|
| 82 | - /** @var \OC\User\User $user */ |
|
| 83 | - unset($cachedUsers[$user->getUID()]); |
|
| 84 | - }); |
|
| 85 | - } |
|
| 86 | - |
|
| 87 | - /** |
|
| 88 | - * Get the active backends |
|
| 89 | - * @return \OCP\UserInterface[] |
|
| 90 | - */ |
|
| 91 | - public function getBackends() { |
|
| 92 | - return $this->backends; |
|
| 93 | - } |
|
| 94 | - |
|
| 95 | - /** |
|
| 96 | - * register a user backend |
|
| 97 | - * |
|
| 98 | - * @param \OCP\UserInterface $backend |
|
| 99 | - */ |
|
| 100 | - public function registerBackend($backend) { |
|
| 101 | - $this->backends[] = $backend; |
|
| 102 | - } |
|
| 103 | - |
|
| 104 | - /** |
|
| 105 | - * remove a user backend |
|
| 106 | - * |
|
| 107 | - * @param \OCP\UserInterface $backend |
|
| 108 | - */ |
|
| 109 | - public function removeBackend($backend) { |
|
| 110 | - $this->cachedUsers = array(); |
|
| 111 | - if (($i = array_search($backend, $this->backends)) !== false) { |
|
| 112 | - unset($this->backends[$i]); |
|
| 113 | - } |
|
| 114 | - } |
|
| 115 | - |
|
| 116 | - /** |
|
| 117 | - * remove all user backends |
|
| 118 | - */ |
|
| 119 | - public function clearBackends() { |
|
| 120 | - $this->cachedUsers = array(); |
|
| 121 | - $this->backends = array(); |
|
| 122 | - } |
|
| 123 | - |
|
| 124 | - /** |
|
| 125 | - * get a user by user id |
|
| 126 | - * |
|
| 127 | - * @param string $uid |
|
| 128 | - * @return \OC\User\User|null Either the user or null if the specified user does not exist |
|
| 129 | - */ |
|
| 130 | - public function get($uid) { |
|
| 131 | - if (is_null($uid) || $uid === '' || $uid === false) { |
|
| 132 | - return null; |
|
| 133 | - } |
|
| 134 | - if (isset($this->cachedUsers[$uid])) { //check the cache first to prevent having to loop over the backends |
|
| 135 | - return $this->cachedUsers[$uid]; |
|
| 136 | - } |
|
| 137 | - foreach ($this->backends as $backend) { |
|
| 138 | - if ($backend->userExists($uid)) { |
|
| 139 | - return $this->getUserObject($uid, $backend); |
|
| 140 | - } |
|
| 141 | - } |
|
| 142 | - return null; |
|
| 143 | - } |
|
| 144 | - |
|
| 145 | - /** |
|
| 146 | - * get or construct the user object |
|
| 147 | - * |
|
| 148 | - * @param string $uid |
|
| 149 | - * @param \OCP\UserInterface $backend |
|
| 150 | - * @param bool $cacheUser If false the newly created user object will not be cached |
|
| 151 | - * @return \OC\User\User |
|
| 152 | - */ |
|
| 153 | - protected function getUserObject($uid, $backend, $cacheUser = true) { |
|
| 154 | - if (isset($this->cachedUsers[$uid])) { |
|
| 155 | - return $this->cachedUsers[$uid]; |
|
| 156 | - } |
|
| 157 | - |
|
| 158 | - $user = new User($uid, $backend, $this, $this->config); |
|
| 159 | - if ($cacheUser) { |
|
| 160 | - $this->cachedUsers[$uid] = $user; |
|
| 161 | - } |
|
| 162 | - return $user; |
|
| 163 | - } |
|
| 164 | - |
|
| 165 | - /** |
|
| 166 | - * check if a user exists |
|
| 167 | - * |
|
| 168 | - * @param string $uid |
|
| 169 | - * @return bool |
|
| 170 | - */ |
|
| 171 | - public function userExists($uid) { |
|
| 172 | - $user = $this->get($uid); |
|
| 173 | - return ($user !== null); |
|
| 174 | - } |
|
| 175 | - |
|
| 176 | - /** |
|
| 177 | - * Check if the password is valid for the user |
|
| 178 | - * |
|
| 179 | - * @param string $loginName |
|
| 180 | - * @param string $password |
|
| 181 | - * @return mixed the User object on success, false otherwise |
|
| 182 | - */ |
|
| 183 | - public function checkPassword($loginName, $password) { |
|
| 184 | - $result = $this->checkPasswordNoLogging($loginName, $password); |
|
| 185 | - |
|
| 186 | - if ($result === false) { |
|
| 187 | - \OC::$server->getLogger()->warning('Login failed: \''. $loginName .'\' (Remote IP: \''. \OC::$server->getRequest()->getRemoteAddress(). '\')', ['app' => 'core']); |
|
| 188 | - } |
|
| 189 | - |
|
| 190 | - return $result; |
|
| 191 | - } |
|
| 192 | - |
|
| 193 | - /** |
|
| 194 | - * Check if the password is valid for the user |
|
| 195 | - * |
|
| 196 | - * @internal |
|
| 197 | - * @param string $loginName |
|
| 198 | - * @param string $password |
|
| 199 | - * @return mixed the User object on success, false otherwise |
|
| 200 | - */ |
|
| 201 | - public function checkPasswordNoLogging($loginName, $password) { |
|
| 202 | - $loginName = str_replace("\0", '', $loginName); |
|
| 203 | - $password = str_replace("\0", '', $password); |
|
| 204 | - |
|
| 205 | - foreach ($this->backends as $backend) { |
|
| 206 | - if ($backend->implementsActions(Backend::CHECK_PASSWORD)) { |
|
| 207 | - $uid = $backend->checkPassword($loginName, $password); |
|
| 208 | - if ($uid !== false) { |
|
| 209 | - return $this->getUserObject($uid, $backend); |
|
| 210 | - } |
|
| 211 | - } |
|
| 212 | - } |
|
| 213 | - |
|
| 214 | - return false; |
|
| 215 | - } |
|
| 216 | - |
|
| 217 | - /** |
|
| 218 | - * search by user id |
|
| 219 | - * |
|
| 220 | - * @param string $pattern |
|
| 221 | - * @param int $limit |
|
| 222 | - * @param int $offset |
|
| 223 | - * @return \OC\User\User[] |
|
| 224 | - */ |
|
| 225 | - public function search($pattern, $limit = null, $offset = null) { |
|
| 226 | - $users = array(); |
|
| 227 | - foreach ($this->backends as $backend) { |
|
| 228 | - $backendUsers = $backend->getUsers($pattern, $limit, $offset); |
|
| 229 | - if (is_array($backendUsers)) { |
|
| 230 | - foreach ($backendUsers as $uid) { |
|
| 231 | - $users[$uid] = $this->getUserObject($uid, $backend); |
|
| 232 | - } |
|
| 233 | - } |
|
| 234 | - } |
|
| 235 | - |
|
| 236 | - uasort($users, function ($a, $b) { |
|
| 237 | - /** |
|
| 238 | - * @var \OC\User\User $a |
|
| 239 | - * @var \OC\User\User $b |
|
| 240 | - */ |
|
| 241 | - return strcmp($a->getUID(), $b->getUID()); |
|
| 242 | - }); |
|
| 243 | - return $users; |
|
| 244 | - } |
|
| 245 | - |
|
| 246 | - /** |
|
| 247 | - * search by displayName |
|
| 248 | - * |
|
| 249 | - * @param string $pattern |
|
| 250 | - * @param int $limit |
|
| 251 | - * @param int $offset |
|
| 252 | - * @return \OC\User\User[] |
|
| 253 | - */ |
|
| 254 | - public function searchDisplayName($pattern, $limit = null, $offset = null) { |
|
| 255 | - $users = array(); |
|
| 256 | - foreach ($this->backends as $backend) { |
|
| 257 | - $backendUsers = $backend->getDisplayNames($pattern, $limit, $offset); |
|
| 258 | - if (is_array($backendUsers)) { |
|
| 259 | - foreach ($backendUsers as $uid => $displayName) { |
|
| 260 | - $users[] = $this->getUserObject($uid, $backend); |
|
| 261 | - } |
|
| 262 | - } |
|
| 263 | - } |
|
| 264 | - |
|
| 265 | - usort($users, function ($a, $b) { |
|
| 266 | - /** |
|
| 267 | - * @var \OC\User\User $a |
|
| 268 | - * @var \OC\User\User $b |
|
| 269 | - */ |
|
| 270 | - return strcmp(strtolower($a->getDisplayName()), strtolower($b->getDisplayName())); |
|
| 271 | - }); |
|
| 272 | - return $users; |
|
| 273 | - } |
|
| 274 | - |
|
| 275 | - /** |
|
| 276 | - * @param string $uid |
|
| 277 | - * @param string $password |
|
| 278 | - * @throws \InvalidArgumentException |
|
| 279 | - * @return bool|IUser the created user or false |
|
| 280 | - */ |
|
| 281 | - public function createUser($uid, $password) { |
|
| 282 | - $localBackends = []; |
|
| 283 | - foreach ($this->backends as $backend) { |
|
| 284 | - if ($backend instanceof Database) { |
|
| 285 | - // First check if there is another user backend |
|
| 286 | - $localBackends[] = $backend; |
|
| 287 | - continue; |
|
| 288 | - } |
|
| 289 | - |
|
| 290 | - if ($backend->implementsActions(Backend::CREATE_USER)) { |
|
| 291 | - return $this->createUserFromBackend($uid, $password, $backend); |
|
| 292 | - } |
|
| 293 | - } |
|
| 294 | - |
|
| 295 | - foreach ($localBackends as $backend) { |
|
| 296 | - if ($backend->implementsActions(Backend::CREATE_USER)) { |
|
| 297 | - return $this->createUserFromBackend($uid, $password, $backend); |
|
| 298 | - } |
|
| 299 | - } |
|
| 300 | - |
|
| 301 | - return false; |
|
| 302 | - } |
|
| 303 | - |
|
| 304 | - /** |
|
| 305 | - * @param string $uid |
|
| 306 | - * @param string $password |
|
| 307 | - * @param UserInterface $backend |
|
| 308 | - * @return IUser|null |
|
| 309 | - * @throws \InvalidArgumentException |
|
| 310 | - */ |
|
| 311 | - public function createUserFromBackend($uid, $password, UserInterface $backend) { |
|
| 312 | - $l = \OC::$server->getL10N('lib'); |
|
| 313 | - |
|
| 314 | - // Check the name for bad characters |
|
| 315 | - // Allowed are: "a-z", "A-Z", "0-9" and "_.@-'" |
|
| 316 | - if (preg_match('/[^a-zA-Z0-9 _\.@\-\']/', $uid)) { |
|
| 317 | - throw new \InvalidArgumentException($l->t('Only the following characters are allowed in a username:' |
|
| 318 | - . ' "a-z", "A-Z", "0-9", and "_.@-\'"')); |
|
| 319 | - } |
|
| 320 | - // No empty username |
|
| 321 | - if (trim($uid) === '') { |
|
| 322 | - throw new \InvalidArgumentException($l->t('A valid username must be provided')); |
|
| 323 | - } |
|
| 324 | - // No whitespace at the beginning or at the end |
|
| 325 | - if (trim($uid) !== $uid) { |
|
| 326 | - throw new \InvalidArgumentException($l->t('Username contains whitespace at the beginning or at the end')); |
|
| 327 | - } |
|
| 328 | - // Username only consists of 1 or 2 dots (directory traversal) |
|
| 329 | - if ($uid === '.' || $uid === '..') { |
|
| 330 | - throw new \InvalidArgumentException($l->t('Username must not consist of dots only')); |
|
| 331 | - } |
|
| 332 | - // No empty password |
|
| 333 | - if (trim($password) === '') { |
|
| 334 | - throw new \InvalidArgumentException($l->t('A valid password must be provided')); |
|
| 335 | - } |
|
| 336 | - |
|
| 337 | - // Check if user already exists |
|
| 338 | - if ($this->userExists($uid)) { |
|
| 339 | - throw new \InvalidArgumentException($l->t('The username is already being used')); |
|
| 340 | - } |
|
| 341 | - |
|
| 342 | - $this->emit('\OC\User', 'preCreateUser', [$uid, $password]); |
|
| 343 | - $state = $backend->createUser($uid, $password); |
|
| 344 | - if($state === false) { |
|
| 345 | - throw new \InvalidArgumentException($l->t('Could not create user')); |
|
| 346 | - } |
|
| 347 | - $user = $this->getUserObject($uid, $backend); |
|
| 348 | - if ($user instanceof IUser) { |
|
| 349 | - $this->emit('\OC\User', 'postCreateUser', [$user, $password]); |
|
| 350 | - } |
|
| 351 | - return $user; |
|
| 352 | - } |
|
| 353 | - |
|
| 354 | - /** |
|
| 355 | - * returns how many users per backend exist (if supported by backend) |
|
| 356 | - * |
|
| 357 | - * @param boolean $hasLoggedIn when true only users that have a lastLogin |
|
| 358 | - * entry in the preferences table will be affected |
|
| 359 | - * @return array|int an array of backend class as key and count number as value |
|
| 360 | - * if $hasLoggedIn is true only an int is returned |
|
| 361 | - */ |
|
| 362 | - public function countUsers($hasLoggedIn = false) { |
|
| 363 | - if ($hasLoggedIn) { |
|
| 364 | - return $this->countSeenUsers(); |
|
| 365 | - } |
|
| 366 | - $userCountStatistics = []; |
|
| 367 | - foreach ($this->backends as $backend) { |
|
| 368 | - if ($backend->implementsActions(Backend::COUNT_USERS)) { |
|
| 369 | - $backendUsers = $backend->countUsers(); |
|
| 370 | - if($backendUsers !== false) { |
|
| 371 | - if($backend instanceof IUserBackend) { |
|
| 372 | - $name = $backend->getBackendName(); |
|
| 373 | - } else { |
|
| 374 | - $name = get_class($backend); |
|
| 375 | - } |
|
| 376 | - if(isset($userCountStatistics[$name])) { |
|
| 377 | - $userCountStatistics[$name] += $backendUsers; |
|
| 378 | - } else { |
|
| 379 | - $userCountStatistics[$name] = $backendUsers; |
|
| 380 | - } |
|
| 381 | - } |
|
| 382 | - } |
|
| 383 | - } |
|
| 384 | - return $userCountStatistics; |
|
| 385 | - } |
|
| 386 | - |
|
| 387 | - /** |
|
| 388 | - * The callback is executed for each user on each backend. |
|
| 389 | - * If the callback returns false no further users will be retrieved. |
|
| 390 | - * |
|
| 391 | - * @param \Closure $callback |
|
| 392 | - * @param string $search |
|
| 393 | - * @param boolean $onlySeen when true only users that have a lastLogin entry |
|
| 394 | - * in the preferences table will be affected |
|
| 395 | - * @since 9.0.0 |
|
| 396 | - */ |
|
| 397 | - public function callForAllUsers(\Closure $callback, $search = '', $onlySeen = false) { |
|
| 398 | - if ($onlySeen) { |
|
| 399 | - $this->callForSeenUsers($callback); |
|
| 400 | - } else { |
|
| 401 | - foreach ($this->getBackends() as $backend) { |
|
| 402 | - $limit = 500; |
|
| 403 | - $offset = 0; |
|
| 404 | - do { |
|
| 405 | - $users = $backend->getUsers($search, $limit, $offset); |
|
| 406 | - foreach ($users as $uid) { |
|
| 407 | - if (!$backend->userExists($uid)) { |
|
| 408 | - continue; |
|
| 409 | - } |
|
| 410 | - $user = $this->getUserObject($uid, $backend, false); |
|
| 411 | - $return = $callback($user); |
|
| 412 | - if ($return === false) { |
|
| 413 | - break; |
|
| 414 | - } |
|
| 415 | - } |
|
| 416 | - $offset += $limit; |
|
| 417 | - } while (count($users) >= $limit); |
|
| 418 | - } |
|
| 419 | - } |
|
| 420 | - } |
|
| 421 | - |
|
| 422 | - /** |
|
| 423 | - * returns how many users are disabled |
|
| 424 | - * |
|
| 425 | - * @return int |
|
| 426 | - * @since 12.0.0 |
|
| 427 | - */ |
|
| 428 | - public function countDisabledUsers(): int { |
|
| 429 | - $queryBuilder = \OC::$server->getDatabaseConnection()->getQueryBuilder(); |
|
| 430 | - $queryBuilder->select($queryBuilder->createFunction('COUNT(*)')) |
|
| 431 | - ->from('preferences') |
|
| 432 | - ->where($queryBuilder->expr()->eq('appid', $queryBuilder->createNamedParameter('core'))) |
|
| 433 | - ->andWhere($queryBuilder->expr()->eq('configkey', $queryBuilder->createNamedParameter('enabled'))) |
|
| 434 | - ->andWhere($queryBuilder->expr()->eq('configvalue', $queryBuilder->createNamedParameter('false'), IQueryBuilder::PARAM_STR)); |
|
| 60 | + /** |
|
| 61 | + * @var \OCP\UserInterface[] $backends |
|
| 62 | + */ |
|
| 63 | + private $backends = array(); |
|
| 64 | + |
|
| 65 | + /** |
|
| 66 | + * @var \OC\User\User[] $cachedUsers |
|
| 67 | + */ |
|
| 68 | + private $cachedUsers = array(); |
|
| 69 | + |
|
| 70 | + /** |
|
| 71 | + * @var \OCP\IConfig $config |
|
| 72 | + */ |
|
| 73 | + private $config; |
|
| 74 | + |
|
| 75 | + /** |
|
| 76 | + * @param \OCP\IConfig $config |
|
| 77 | + */ |
|
| 78 | + public function __construct(IConfig $config) { |
|
| 79 | + $this->config = $config; |
|
| 80 | + $cachedUsers = &$this->cachedUsers; |
|
| 81 | + $this->listen('\OC\User', 'postDelete', function ($user) use (&$cachedUsers) { |
|
| 82 | + /** @var \OC\User\User $user */ |
|
| 83 | + unset($cachedUsers[$user->getUID()]); |
|
| 84 | + }); |
|
| 85 | + } |
|
| 86 | + |
|
| 87 | + /** |
|
| 88 | + * Get the active backends |
|
| 89 | + * @return \OCP\UserInterface[] |
|
| 90 | + */ |
|
| 91 | + public function getBackends() { |
|
| 92 | + return $this->backends; |
|
| 93 | + } |
|
| 94 | + |
|
| 95 | + /** |
|
| 96 | + * register a user backend |
|
| 97 | + * |
|
| 98 | + * @param \OCP\UserInterface $backend |
|
| 99 | + */ |
|
| 100 | + public function registerBackend($backend) { |
|
| 101 | + $this->backends[] = $backend; |
|
| 102 | + } |
|
| 103 | + |
|
| 104 | + /** |
|
| 105 | + * remove a user backend |
|
| 106 | + * |
|
| 107 | + * @param \OCP\UserInterface $backend |
|
| 108 | + */ |
|
| 109 | + public function removeBackend($backend) { |
|
| 110 | + $this->cachedUsers = array(); |
|
| 111 | + if (($i = array_search($backend, $this->backends)) !== false) { |
|
| 112 | + unset($this->backends[$i]); |
|
| 113 | + } |
|
| 114 | + } |
|
| 115 | + |
|
| 116 | + /** |
|
| 117 | + * remove all user backends |
|
| 118 | + */ |
|
| 119 | + public function clearBackends() { |
|
| 120 | + $this->cachedUsers = array(); |
|
| 121 | + $this->backends = array(); |
|
| 122 | + } |
|
| 123 | + |
|
| 124 | + /** |
|
| 125 | + * get a user by user id |
|
| 126 | + * |
|
| 127 | + * @param string $uid |
|
| 128 | + * @return \OC\User\User|null Either the user or null if the specified user does not exist |
|
| 129 | + */ |
|
| 130 | + public function get($uid) { |
|
| 131 | + if (is_null($uid) || $uid === '' || $uid === false) { |
|
| 132 | + return null; |
|
| 133 | + } |
|
| 134 | + if (isset($this->cachedUsers[$uid])) { //check the cache first to prevent having to loop over the backends |
|
| 135 | + return $this->cachedUsers[$uid]; |
|
| 136 | + } |
|
| 137 | + foreach ($this->backends as $backend) { |
|
| 138 | + if ($backend->userExists($uid)) { |
|
| 139 | + return $this->getUserObject($uid, $backend); |
|
| 140 | + } |
|
| 141 | + } |
|
| 142 | + return null; |
|
| 143 | + } |
|
| 144 | + |
|
| 145 | + /** |
|
| 146 | + * get or construct the user object |
|
| 147 | + * |
|
| 148 | + * @param string $uid |
|
| 149 | + * @param \OCP\UserInterface $backend |
|
| 150 | + * @param bool $cacheUser If false the newly created user object will not be cached |
|
| 151 | + * @return \OC\User\User |
|
| 152 | + */ |
|
| 153 | + protected function getUserObject($uid, $backend, $cacheUser = true) { |
|
| 154 | + if (isset($this->cachedUsers[$uid])) { |
|
| 155 | + return $this->cachedUsers[$uid]; |
|
| 156 | + } |
|
| 157 | + |
|
| 158 | + $user = new User($uid, $backend, $this, $this->config); |
|
| 159 | + if ($cacheUser) { |
|
| 160 | + $this->cachedUsers[$uid] = $user; |
|
| 161 | + } |
|
| 162 | + return $user; |
|
| 163 | + } |
|
| 164 | + |
|
| 165 | + /** |
|
| 166 | + * check if a user exists |
|
| 167 | + * |
|
| 168 | + * @param string $uid |
|
| 169 | + * @return bool |
|
| 170 | + */ |
|
| 171 | + public function userExists($uid) { |
|
| 172 | + $user = $this->get($uid); |
|
| 173 | + return ($user !== null); |
|
| 174 | + } |
|
| 175 | + |
|
| 176 | + /** |
|
| 177 | + * Check if the password is valid for the user |
|
| 178 | + * |
|
| 179 | + * @param string $loginName |
|
| 180 | + * @param string $password |
|
| 181 | + * @return mixed the User object on success, false otherwise |
|
| 182 | + */ |
|
| 183 | + public function checkPassword($loginName, $password) { |
|
| 184 | + $result = $this->checkPasswordNoLogging($loginName, $password); |
|
| 185 | + |
|
| 186 | + if ($result === false) { |
|
| 187 | + \OC::$server->getLogger()->warning('Login failed: \''. $loginName .'\' (Remote IP: \''. \OC::$server->getRequest()->getRemoteAddress(). '\')', ['app' => 'core']); |
|
| 188 | + } |
|
| 189 | + |
|
| 190 | + return $result; |
|
| 191 | + } |
|
| 192 | + |
|
| 193 | + /** |
|
| 194 | + * Check if the password is valid for the user |
|
| 195 | + * |
|
| 196 | + * @internal |
|
| 197 | + * @param string $loginName |
|
| 198 | + * @param string $password |
|
| 199 | + * @return mixed the User object on success, false otherwise |
|
| 200 | + */ |
|
| 201 | + public function checkPasswordNoLogging($loginName, $password) { |
|
| 202 | + $loginName = str_replace("\0", '', $loginName); |
|
| 203 | + $password = str_replace("\0", '', $password); |
|
| 204 | + |
|
| 205 | + foreach ($this->backends as $backend) { |
|
| 206 | + if ($backend->implementsActions(Backend::CHECK_PASSWORD)) { |
|
| 207 | + $uid = $backend->checkPassword($loginName, $password); |
|
| 208 | + if ($uid !== false) { |
|
| 209 | + return $this->getUserObject($uid, $backend); |
|
| 210 | + } |
|
| 211 | + } |
|
| 212 | + } |
|
| 213 | + |
|
| 214 | + return false; |
|
| 215 | + } |
|
| 216 | + |
|
| 217 | + /** |
|
| 218 | + * search by user id |
|
| 219 | + * |
|
| 220 | + * @param string $pattern |
|
| 221 | + * @param int $limit |
|
| 222 | + * @param int $offset |
|
| 223 | + * @return \OC\User\User[] |
|
| 224 | + */ |
|
| 225 | + public function search($pattern, $limit = null, $offset = null) { |
|
| 226 | + $users = array(); |
|
| 227 | + foreach ($this->backends as $backend) { |
|
| 228 | + $backendUsers = $backend->getUsers($pattern, $limit, $offset); |
|
| 229 | + if (is_array($backendUsers)) { |
|
| 230 | + foreach ($backendUsers as $uid) { |
|
| 231 | + $users[$uid] = $this->getUserObject($uid, $backend); |
|
| 232 | + } |
|
| 233 | + } |
|
| 234 | + } |
|
| 235 | + |
|
| 236 | + uasort($users, function ($a, $b) { |
|
| 237 | + /** |
|
| 238 | + * @var \OC\User\User $a |
|
| 239 | + * @var \OC\User\User $b |
|
| 240 | + */ |
|
| 241 | + return strcmp($a->getUID(), $b->getUID()); |
|
| 242 | + }); |
|
| 243 | + return $users; |
|
| 244 | + } |
|
| 245 | + |
|
| 246 | + /** |
|
| 247 | + * search by displayName |
|
| 248 | + * |
|
| 249 | + * @param string $pattern |
|
| 250 | + * @param int $limit |
|
| 251 | + * @param int $offset |
|
| 252 | + * @return \OC\User\User[] |
|
| 253 | + */ |
|
| 254 | + public function searchDisplayName($pattern, $limit = null, $offset = null) { |
|
| 255 | + $users = array(); |
|
| 256 | + foreach ($this->backends as $backend) { |
|
| 257 | + $backendUsers = $backend->getDisplayNames($pattern, $limit, $offset); |
|
| 258 | + if (is_array($backendUsers)) { |
|
| 259 | + foreach ($backendUsers as $uid => $displayName) { |
|
| 260 | + $users[] = $this->getUserObject($uid, $backend); |
|
| 261 | + } |
|
| 262 | + } |
|
| 263 | + } |
|
| 264 | + |
|
| 265 | + usort($users, function ($a, $b) { |
|
| 266 | + /** |
|
| 267 | + * @var \OC\User\User $a |
|
| 268 | + * @var \OC\User\User $b |
|
| 269 | + */ |
|
| 270 | + return strcmp(strtolower($a->getDisplayName()), strtolower($b->getDisplayName())); |
|
| 271 | + }); |
|
| 272 | + return $users; |
|
| 273 | + } |
|
| 274 | + |
|
| 275 | + /** |
|
| 276 | + * @param string $uid |
|
| 277 | + * @param string $password |
|
| 278 | + * @throws \InvalidArgumentException |
|
| 279 | + * @return bool|IUser the created user or false |
|
| 280 | + */ |
|
| 281 | + public function createUser($uid, $password) { |
|
| 282 | + $localBackends = []; |
|
| 283 | + foreach ($this->backends as $backend) { |
|
| 284 | + if ($backend instanceof Database) { |
|
| 285 | + // First check if there is another user backend |
|
| 286 | + $localBackends[] = $backend; |
|
| 287 | + continue; |
|
| 288 | + } |
|
| 289 | + |
|
| 290 | + if ($backend->implementsActions(Backend::CREATE_USER)) { |
|
| 291 | + return $this->createUserFromBackend($uid, $password, $backend); |
|
| 292 | + } |
|
| 293 | + } |
|
| 294 | + |
|
| 295 | + foreach ($localBackends as $backend) { |
|
| 296 | + if ($backend->implementsActions(Backend::CREATE_USER)) { |
|
| 297 | + return $this->createUserFromBackend($uid, $password, $backend); |
|
| 298 | + } |
|
| 299 | + } |
|
| 300 | + |
|
| 301 | + return false; |
|
| 302 | + } |
|
| 303 | + |
|
| 304 | + /** |
|
| 305 | + * @param string $uid |
|
| 306 | + * @param string $password |
|
| 307 | + * @param UserInterface $backend |
|
| 308 | + * @return IUser|null |
|
| 309 | + * @throws \InvalidArgumentException |
|
| 310 | + */ |
|
| 311 | + public function createUserFromBackend($uid, $password, UserInterface $backend) { |
|
| 312 | + $l = \OC::$server->getL10N('lib'); |
|
| 313 | + |
|
| 314 | + // Check the name for bad characters |
|
| 315 | + // Allowed are: "a-z", "A-Z", "0-9" and "_.@-'" |
|
| 316 | + if (preg_match('/[^a-zA-Z0-9 _\.@\-\']/', $uid)) { |
|
| 317 | + throw new \InvalidArgumentException($l->t('Only the following characters are allowed in a username:' |
|
| 318 | + . ' "a-z", "A-Z", "0-9", and "_.@-\'"')); |
|
| 319 | + } |
|
| 320 | + // No empty username |
|
| 321 | + if (trim($uid) === '') { |
|
| 322 | + throw new \InvalidArgumentException($l->t('A valid username must be provided')); |
|
| 323 | + } |
|
| 324 | + // No whitespace at the beginning or at the end |
|
| 325 | + if (trim($uid) !== $uid) { |
|
| 326 | + throw new \InvalidArgumentException($l->t('Username contains whitespace at the beginning or at the end')); |
|
| 327 | + } |
|
| 328 | + // Username only consists of 1 or 2 dots (directory traversal) |
|
| 329 | + if ($uid === '.' || $uid === '..') { |
|
| 330 | + throw new \InvalidArgumentException($l->t('Username must not consist of dots only')); |
|
| 331 | + } |
|
| 332 | + // No empty password |
|
| 333 | + if (trim($password) === '') { |
|
| 334 | + throw new \InvalidArgumentException($l->t('A valid password must be provided')); |
|
| 335 | + } |
|
| 336 | + |
|
| 337 | + // Check if user already exists |
|
| 338 | + if ($this->userExists($uid)) { |
|
| 339 | + throw new \InvalidArgumentException($l->t('The username is already being used')); |
|
| 340 | + } |
|
| 341 | + |
|
| 342 | + $this->emit('\OC\User', 'preCreateUser', [$uid, $password]); |
|
| 343 | + $state = $backend->createUser($uid, $password); |
|
| 344 | + if($state === false) { |
|
| 345 | + throw new \InvalidArgumentException($l->t('Could not create user')); |
|
| 346 | + } |
|
| 347 | + $user = $this->getUserObject($uid, $backend); |
|
| 348 | + if ($user instanceof IUser) { |
|
| 349 | + $this->emit('\OC\User', 'postCreateUser', [$user, $password]); |
|
| 350 | + } |
|
| 351 | + return $user; |
|
| 352 | + } |
|
| 353 | + |
|
| 354 | + /** |
|
| 355 | + * returns how many users per backend exist (if supported by backend) |
|
| 356 | + * |
|
| 357 | + * @param boolean $hasLoggedIn when true only users that have a lastLogin |
|
| 358 | + * entry in the preferences table will be affected |
|
| 359 | + * @return array|int an array of backend class as key and count number as value |
|
| 360 | + * if $hasLoggedIn is true only an int is returned |
|
| 361 | + */ |
|
| 362 | + public function countUsers($hasLoggedIn = false) { |
|
| 363 | + if ($hasLoggedIn) { |
|
| 364 | + return $this->countSeenUsers(); |
|
| 365 | + } |
|
| 366 | + $userCountStatistics = []; |
|
| 367 | + foreach ($this->backends as $backend) { |
|
| 368 | + if ($backend->implementsActions(Backend::COUNT_USERS)) { |
|
| 369 | + $backendUsers = $backend->countUsers(); |
|
| 370 | + if($backendUsers !== false) { |
|
| 371 | + if($backend instanceof IUserBackend) { |
|
| 372 | + $name = $backend->getBackendName(); |
|
| 373 | + } else { |
|
| 374 | + $name = get_class($backend); |
|
| 375 | + } |
|
| 376 | + if(isset($userCountStatistics[$name])) { |
|
| 377 | + $userCountStatistics[$name] += $backendUsers; |
|
| 378 | + } else { |
|
| 379 | + $userCountStatistics[$name] = $backendUsers; |
|
| 380 | + } |
|
| 381 | + } |
|
| 382 | + } |
|
| 383 | + } |
|
| 384 | + return $userCountStatistics; |
|
| 385 | + } |
|
| 386 | + |
|
| 387 | + /** |
|
| 388 | + * The callback is executed for each user on each backend. |
|
| 389 | + * If the callback returns false no further users will be retrieved. |
|
| 390 | + * |
|
| 391 | + * @param \Closure $callback |
|
| 392 | + * @param string $search |
|
| 393 | + * @param boolean $onlySeen when true only users that have a lastLogin entry |
|
| 394 | + * in the preferences table will be affected |
|
| 395 | + * @since 9.0.0 |
|
| 396 | + */ |
|
| 397 | + public function callForAllUsers(\Closure $callback, $search = '', $onlySeen = false) { |
|
| 398 | + if ($onlySeen) { |
|
| 399 | + $this->callForSeenUsers($callback); |
|
| 400 | + } else { |
|
| 401 | + foreach ($this->getBackends() as $backend) { |
|
| 402 | + $limit = 500; |
|
| 403 | + $offset = 0; |
|
| 404 | + do { |
|
| 405 | + $users = $backend->getUsers($search, $limit, $offset); |
|
| 406 | + foreach ($users as $uid) { |
|
| 407 | + if (!$backend->userExists($uid)) { |
|
| 408 | + continue; |
|
| 409 | + } |
|
| 410 | + $user = $this->getUserObject($uid, $backend, false); |
|
| 411 | + $return = $callback($user); |
|
| 412 | + if ($return === false) { |
|
| 413 | + break; |
|
| 414 | + } |
|
| 415 | + } |
|
| 416 | + $offset += $limit; |
|
| 417 | + } while (count($users) >= $limit); |
|
| 418 | + } |
|
| 419 | + } |
|
| 420 | + } |
|
| 421 | + |
|
| 422 | + /** |
|
| 423 | + * returns how many users are disabled |
|
| 424 | + * |
|
| 425 | + * @return int |
|
| 426 | + * @since 12.0.0 |
|
| 427 | + */ |
|
| 428 | + public function countDisabledUsers(): int { |
|
| 429 | + $queryBuilder = \OC::$server->getDatabaseConnection()->getQueryBuilder(); |
|
| 430 | + $queryBuilder->select($queryBuilder->createFunction('COUNT(*)')) |
|
| 431 | + ->from('preferences') |
|
| 432 | + ->where($queryBuilder->expr()->eq('appid', $queryBuilder->createNamedParameter('core'))) |
|
| 433 | + ->andWhere($queryBuilder->expr()->eq('configkey', $queryBuilder->createNamedParameter('enabled'))) |
|
| 434 | + ->andWhere($queryBuilder->expr()->eq('configvalue', $queryBuilder->createNamedParameter('false'), IQueryBuilder::PARAM_STR)); |
|
| 435 | 435 | |
| 436 | 436 | |
| 437 | - $result = $queryBuilder->execute(); |
|
| 438 | - $count = $result->fetchColumn(); |
|
| 439 | - $result->closeCursor(); |
|
| 437 | + $result = $queryBuilder->execute(); |
|
| 438 | + $count = $result->fetchColumn(); |
|
| 439 | + $result->closeCursor(); |
|
| 440 | 440 | |
| 441 | - if ($count !== false) { |
|
| 442 | - $count = (int)$count; |
|
| 443 | - } else { |
|
| 444 | - $count = 0; |
|
| 445 | - } |
|
| 446 | - |
|
| 447 | - return $count; |
|
| 448 | - } |
|
| 449 | - |
|
| 450 | - /** |
|
| 451 | - * returns how many users are disabled in the requested groups |
|
| 452 | - * |
|
| 453 | - * @param array $groups groupids to search |
|
| 454 | - * @return int |
|
| 455 | - * @since 14.0.0 |
|
| 456 | - */ |
|
| 457 | - public function countDisabledUsersOfGroups(array $groups): int { |
|
| 458 | - $queryBuilder = \OC::$server->getDatabaseConnection()->getQueryBuilder(); |
|
| 459 | - $queryBuilder->select($queryBuilder->createFunction('COUNT(Distinct uid)')) |
|
| 460 | - ->from('preferences', 'p') |
|
| 461 | - ->innerJoin('p', 'group_user', 'g', 'p.userid = g.uid') |
|
| 462 | - ->where($queryBuilder->expr()->eq('appid', $queryBuilder->createNamedParameter('core'))) |
|
| 463 | - ->andWhere($queryBuilder->expr()->eq('configkey', $queryBuilder->createNamedParameter('enabled'))) |
|
| 464 | - ->andWhere($queryBuilder->expr()->eq('configvalue', $queryBuilder->createNamedParameter('false'), IQueryBuilder::PARAM_STR)) |
|
| 465 | - ->andWhere($queryBuilder->expr()->in('gid', $queryBuilder->createNamedParameter($groups, IQueryBuilder::PARAM_STR_ARRAY))); |
|
| 466 | - |
|
| 467 | - $result = $queryBuilder->execute(); |
|
| 468 | - $count = $result->fetchColumn(); |
|
| 469 | - $result->closeCursor(); |
|
| 441 | + if ($count !== false) { |
|
| 442 | + $count = (int)$count; |
|
| 443 | + } else { |
|
| 444 | + $count = 0; |
|
| 445 | + } |
|
| 446 | + |
|
| 447 | + return $count; |
|
| 448 | + } |
|
| 449 | + |
|
| 450 | + /** |
|
| 451 | + * returns how many users are disabled in the requested groups |
|
| 452 | + * |
|
| 453 | + * @param array $groups groupids to search |
|
| 454 | + * @return int |
|
| 455 | + * @since 14.0.0 |
|
| 456 | + */ |
|
| 457 | + public function countDisabledUsersOfGroups(array $groups): int { |
|
| 458 | + $queryBuilder = \OC::$server->getDatabaseConnection()->getQueryBuilder(); |
|
| 459 | + $queryBuilder->select($queryBuilder->createFunction('COUNT(Distinct uid)')) |
|
| 460 | + ->from('preferences', 'p') |
|
| 461 | + ->innerJoin('p', 'group_user', 'g', 'p.userid = g.uid') |
|
| 462 | + ->where($queryBuilder->expr()->eq('appid', $queryBuilder->createNamedParameter('core'))) |
|
| 463 | + ->andWhere($queryBuilder->expr()->eq('configkey', $queryBuilder->createNamedParameter('enabled'))) |
|
| 464 | + ->andWhere($queryBuilder->expr()->eq('configvalue', $queryBuilder->createNamedParameter('false'), IQueryBuilder::PARAM_STR)) |
|
| 465 | + ->andWhere($queryBuilder->expr()->in('gid', $queryBuilder->createNamedParameter($groups, IQueryBuilder::PARAM_STR_ARRAY))); |
|
| 466 | + |
|
| 467 | + $result = $queryBuilder->execute(); |
|
| 468 | + $count = $result->fetchColumn(); |
|
| 469 | + $result->closeCursor(); |
|
| 470 | 470 | |
| 471 | - if ($count !== false) { |
|
| 472 | - $count = (int)$count; |
|
| 473 | - } else { |
|
| 474 | - $count = 0; |
|
| 475 | - } |
|
| 476 | - |
|
| 477 | - return $count; |
|
| 478 | - } |
|
| 479 | - |
|
| 480 | - /** |
|
| 481 | - * returns how many users have logged in once |
|
| 482 | - * |
|
| 483 | - * @return int |
|
| 484 | - * @since 11.0.0 |
|
| 485 | - */ |
|
| 486 | - public function countSeenUsers() { |
|
| 487 | - $queryBuilder = \OC::$server->getDatabaseConnection()->getQueryBuilder(); |
|
| 488 | - $queryBuilder->select($queryBuilder->createFunction('COUNT(*)')) |
|
| 489 | - ->from('preferences') |
|
| 490 | - ->where($queryBuilder->expr()->eq('appid', $queryBuilder->createNamedParameter('login'))) |
|
| 491 | - ->andWhere($queryBuilder->expr()->eq('configkey', $queryBuilder->createNamedParameter('lastLogin'))) |
|
| 492 | - ->andWhere($queryBuilder->expr()->isNotNull('configvalue')); |
|
| 493 | - |
|
| 494 | - $query = $queryBuilder->execute(); |
|
| 495 | - |
|
| 496 | - $result = (int)$query->fetchColumn(); |
|
| 497 | - $query->closeCursor(); |
|
| 498 | - |
|
| 499 | - return $result; |
|
| 500 | - } |
|
| 501 | - |
|
| 502 | - /** |
|
| 503 | - * @param \Closure $callback |
|
| 504 | - * @since 11.0.0 |
|
| 505 | - */ |
|
| 506 | - public function callForSeenUsers(\Closure $callback) { |
|
| 507 | - $limit = 1000; |
|
| 508 | - $offset = 0; |
|
| 509 | - do { |
|
| 510 | - $userIds = $this->getSeenUserIds($limit, $offset); |
|
| 511 | - $offset += $limit; |
|
| 512 | - foreach ($userIds as $userId) { |
|
| 513 | - foreach ($this->backends as $backend) { |
|
| 514 | - if ($backend->userExists($userId)) { |
|
| 515 | - $user = $this->getUserObject($userId, $backend, false); |
|
| 516 | - $return = $callback($user); |
|
| 517 | - if ($return === false) { |
|
| 518 | - return; |
|
| 519 | - } |
|
| 520 | - break; |
|
| 521 | - } |
|
| 522 | - } |
|
| 523 | - } |
|
| 524 | - } while (count($userIds) >= $limit); |
|
| 525 | - } |
|
| 526 | - |
|
| 527 | - /** |
|
| 528 | - * Getting all userIds that have a listLogin value requires checking the |
|
| 529 | - * value in php because on oracle you cannot use a clob in a where clause, |
|
| 530 | - * preventing us from doing a not null or length(value) > 0 check. |
|
| 531 | - * |
|
| 532 | - * @param int $limit |
|
| 533 | - * @param int $offset |
|
| 534 | - * @return string[] with user ids |
|
| 535 | - */ |
|
| 536 | - private function getSeenUserIds($limit = null, $offset = null) { |
|
| 537 | - $queryBuilder = \OC::$server->getDatabaseConnection()->getQueryBuilder(); |
|
| 538 | - $queryBuilder->select(['userid']) |
|
| 539 | - ->from('preferences') |
|
| 540 | - ->where($queryBuilder->expr()->eq( |
|
| 541 | - 'appid', $queryBuilder->createNamedParameter('login')) |
|
| 542 | - ) |
|
| 543 | - ->andWhere($queryBuilder->expr()->eq( |
|
| 544 | - 'configkey', $queryBuilder->createNamedParameter('lastLogin')) |
|
| 545 | - ) |
|
| 546 | - ->andWhere($queryBuilder->expr()->isNotNull('configvalue') |
|
| 547 | - ); |
|
| 548 | - |
|
| 549 | - if ($limit !== null) { |
|
| 550 | - $queryBuilder->setMaxResults($limit); |
|
| 551 | - } |
|
| 552 | - if ($offset !== null) { |
|
| 553 | - $queryBuilder->setFirstResult($offset); |
|
| 554 | - } |
|
| 555 | - $query = $queryBuilder->execute(); |
|
| 556 | - $result = []; |
|
| 557 | - |
|
| 558 | - while ($row = $query->fetch()) { |
|
| 559 | - $result[] = $row['userid']; |
|
| 560 | - } |
|
| 561 | - |
|
| 562 | - $query->closeCursor(); |
|
| 563 | - |
|
| 564 | - return $result; |
|
| 565 | - } |
|
| 566 | - |
|
| 567 | - /** |
|
| 568 | - * @param string $email |
|
| 569 | - * @return IUser[] |
|
| 570 | - * @since 9.1.0 |
|
| 571 | - */ |
|
| 572 | - public function getByEmail($email) { |
|
| 573 | - $userIds = $this->config->getUsersForUserValue('settings', 'email', $email); |
|
| 574 | - |
|
| 575 | - return array_map(function($uid) { |
|
| 576 | - return $this->get($uid); |
|
| 577 | - }, $userIds); |
|
| 578 | - } |
|
| 471 | + if ($count !== false) { |
|
| 472 | + $count = (int)$count; |
|
| 473 | + } else { |
|
| 474 | + $count = 0; |
|
| 475 | + } |
|
| 476 | + |
|
| 477 | + return $count; |
|
| 478 | + } |
|
| 479 | + |
|
| 480 | + /** |
|
| 481 | + * returns how many users have logged in once |
|
| 482 | + * |
|
| 483 | + * @return int |
|
| 484 | + * @since 11.0.0 |
|
| 485 | + */ |
|
| 486 | + public function countSeenUsers() { |
|
| 487 | + $queryBuilder = \OC::$server->getDatabaseConnection()->getQueryBuilder(); |
|
| 488 | + $queryBuilder->select($queryBuilder->createFunction('COUNT(*)')) |
|
| 489 | + ->from('preferences') |
|
| 490 | + ->where($queryBuilder->expr()->eq('appid', $queryBuilder->createNamedParameter('login'))) |
|
| 491 | + ->andWhere($queryBuilder->expr()->eq('configkey', $queryBuilder->createNamedParameter('lastLogin'))) |
|
| 492 | + ->andWhere($queryBuilder->expr()->isNotNull('configvalue')); |
|
| 493 | + |
|
| 494 | + $query = $queryBuilder->execute(); |
|
| 495 | + |
|
| 496 | + $result = (int)$query->fetchColumn(); |
|
| 497 | + $query->closeCursor(); |
|
| 498 | + |
|
| 499 | + return $result; |
|
| 500 | + } |
|
| 501 | + |
|
| 502 | + /** |
|
| 503 | + * @param \Closure $callback |
|
| 504 | + * @since 11.0.0 |
|
| 505 | + */ |
|
| 506 | + public function callForSeenUsers(\Closure $callback) { |
|
| 507 | + $limit = 1000; |
|
| 508 | + $offset = 0; |
|
| 509 | + do { |
|
| 510 | + $userIds = $this->getSeenUserIds($limit, $offset); |
|
| 511 | + $offset += $limit; |
|
| 512 | + foreach ($userIds as $userId) { |
|
| 513 | + foreach ($this->backends as $backend) { |
|
| 514 | + if ($backend->userExists($userId)) { |
|
| 515 | + $user = $this->getUserObject($userId, $backend, false); |
|
| 516 | + $return = $callback($user); |
|
| 517 | + if ($return === false) { |
|
| 518 | + return; |
|
| 519 | + } |
|
| 520 | + break; |
|
| 521 | + } |
|
| 522 | + } |
|
| 523 | + } |
|
| 524 | + } while (count($userIds) >= $limit); |
|
| 525 | + } |
|
| 526 | + |
|
| 527 | + /** |
|
| 528 | + * Getting all userIds that have a listLogin value requires checking the |
|
| 529 | + * value in php because on oracle you cannot use a clob in a where clause, |
|
| 530 | + * preventing us from doing a not null or length(value) > 0 check. |
|
| 531 | + * |
|
| 532 | + * @param int $limit |
|
| 533 | + * @param int $offset |
|
| 534 | + * @return string[] with user ids |
|
| 535 | + */ |
|
| 536 | + private function getSeenUserIds($limit = null, $offset = null) { |
|
| 537 | + $queryBuilder = \OC::$server->getDatabaseConnection()->getQueryBuilder(); |
|
| 538 | + $queryBuilder->select(['userid']) |
|
| 539 | + ->from('preferences') |
|
| 540 | + ->where($queryBuilder->expr()->eq( |
|
| 541 | + 'appid', $queryBuilder->createNamedParameter('login')) |
|
| 542 | + ) |
|
| 543 | + ->andWhere($queryBuilder->expr()->eq( |
|
| 544 | + 'configkey', $queryBuilder->createNamedParameter('lastLogin')) |
|
| 545 | + ) |
|
| 546 | + ->andWhere($queryBuilder->expr()->isNotNull('configvalue') |
|
| 547 | + ); |
|
| 548 | + |
|
| 549 | + if ($limit !== null) { |
|
| 550 | + $queryBuilder->setMaxResults($limit); |
|
| 551 | + } |
|
| 552 | + if ($offset !== null) { |
|
| 553 | + $queryBuilder->setFirstResult($offset); |
|
| 554 | + } |
|
| 555 | + $query = $queryBuilder->execute(); |
|
| 556 | + $result = []; |
|
| 557 | + |
|
| 558 | + while ($row = $query->fetch()) { |
|
| 559 | + $result[] = $row['userid']; |
|
| 560 | + } |
|
| 561 | + |
|
| 562 | + $query->closeCursor(); |
|
| 563 | + |
|
| 564 | + return $result; |
|
| 565 | + } |
|
| 566 | + |
|
| 567 | + /** |
|
| 568 | + * @param string $email |
|
| 569 | + * @return IUser[] |
|
| 570 | + * @since 9.1.0 |
|
| 571 | + */ |
|
| 572 | + public function getByEmail($email) { |
|
| 573 | + $userIds = $this->config->getUsersForUserValue('settings', 'email', $email); |
|
| 574 | + |
|
| 575 | + return array_map(function($uid) { |
|
| 576 | + return $this->get($uid); |
|
| 577 | + }, $userIds); |
|
| 578 | + } |
|
| 579 | 579 | } |
@@ -78,7 +78,7 @@ discard block |
||
| 78 | 78 | public function __construct(IConfig $config) { |
| 79 | 79 | $this->config = $config; |
| 80 | 80 | $cachedUsers = &$this->cachedUsers; |
| 81 | - $this->listen('\OC\User', 'postDelete', function ($user) use (&$cachedUsers) { |
|
| 81 | + $this->listen('\OC\User', 'postDelete', function($user) use (&$cachedUsers) { |
|
| 82 | 82 | /** @var \OC\User\User $user */ |
| 83 | 83 | unset($cachedUsers[$user->getUID()]); |
| 84 | 84 | }); |
@@ -184,7 +184,7 @@ discard block |
||
| 184 | 184 | $result = $this->checkPasswordNoLogging($loginName, $password); |
| 185 | 185 | |
| 186 | 186 | if ($result === false) { |
| 187 | - \OC::$server->getLogger()->warning('Login failed: \''. $loginName .'\' (Remote IP: \''. \OC::$server->getRequest()->getRemoteAddress(). '\')', ['app' => 'core']); |
|
| 187 | + \OC::$server->getLogger()->warning('Login failed: \''.$loginName.'\' (Remote IP: \''.\OC::$server->getRequest()->getRemoteAddress().'\')', ['app' => 'core']); |
|
| 188 | 188 | } |
| 189 | 189 | |
| 190 | 190 | return $result; |
@@ -233,7 +233,7 @@ discard block |
||
| 233 | 233 | } |
| 234 | 234 | } |
| 235 | 235 | |
| 236 | - uasort($users, function ($a, $b) { |
|
| 236 | + uasort($users, function($a, $b) { |
|
| 237 | 237 | /** |
| 238 | 238 | * @var \OC\User\User $a |
| 239 | 239 | * @var \OC\User\User $b |
@@ -262,7 +262,7 @@ discard block |
||
| 262 | 262 | } |
| 263 | 263 | } |
| 264 | 264 | |
| 265 | - usort($users, function ($a, $b) { |
|
| 265 | + usort($users, function($a, $b) { |
|
| 266 | 266 | /** |
| 267 | 267 | * @var \OC\User\User $a |
| 268 | 268 | * @var \OC\User\User $b |
@@ -341,7 +341,7 @@ discard block |
||
| 341 | 341 | |
| 342 | 342 | $this->emit('\OC\User', 'preCreateUser', [$uid, $password]); |
| 343 | 343 | $state = $backend->createUser($uid, $password); |
| 344 | - if($state === false) { |
|
| 344 | + if ($state === false) { |
|
| 345 | 345 | throw new \InvalidArgumentException($l->t('Could not create user')); |
| 346 | 346 | } |
| 347 | 347 | $user = $this->getUserObject($uid, $backend); |
@@ -367,13 +367,13 @@ discard block |
||
| 367 | 367 | foreach ($this->backends as $backend) { |
| 368 | 368 | if ($backend->implementsActions(Backend::COUNT_USERS)) { |
| 369 | 369 | $backendUsers = $backend->countUsers(); |
| 370 | - if($backendUsers !== false) { |
|
| 371 | - if($backend instanceof IUserBackend) { |
|
| 370 | + if ($backendUsers !== false) { |
|
| 371 | + if ($backend instanceof IUserBackend) { |
|
| 372 | 372 | $name = $backend->getBackendName(); |
| 373 | 373 | } else { |
| 374 | 374 | $name = get_class($backend); |
| 375 | 375 | } |
| 376 | - if(isset($userCountStatistics[$name])) { |
|
| 376 | + if (isset($userCountStatistics[$name])) { |
|
| 377 | 377 | $userCountStatistics[$name] += $backendUsers; |
| 378 | 378 | } else { |
| 379 | 379 | $userCountStatistics[$name] = $backendUsers; |
@@ -439,7 +439,7 @@ discard block |
||
| 439 | 439 | $result->closeCursor(); |
| 440 | 440 | |
| 441 | 441 | if ($count !== false) { |
| 442 | - $count = (int)$count; |
|
| 442 | + $count = (int) $count; |
|
| 443 | 443 | } else { |
| 444 | 444 | $count = 0; |
| 445 | 445 | } |
@@ -469,7 +469,7 @@ discard block |
||
| 469 | 469 | $result->closeCursor(); |
| 470 | 470 | |
| 471 | 471 | if ($count !== false) { |
| 472 | - $count = (int)$count; |
|
| 472 | + $count = (int) $count; |
|
| 473 | 473 | } else { |
| 474 | 474 | $count = 0; |
| 475 | 475 | } |
@@ -493,7 +493,7 @@ discard block |
||
| 493 | 493 | |
| 494 | 494 | $query = $queryBuilder->execute(); |
| 495 | 495 | |
| 496 | - $result = (int)$query->fetchColumn(); |
|
| 496 | + $result = (int) $query->fetchColumn(); |
|
| 497 | 497 | $query->closeCursor(); |
| 498 | 498 | |
| 499 | 499 | return $result; |
@@ -29,8 +29,8 @@ |
||
| 29 | 29 | */ |
| 30 | 30 | interface ICountDisabledInGroup { |
| 31 | 31 | |
| 32 | - /** |
|
| 33 | - * @since 14.0.0 |
|
| 34 | - */ |
|
| 35 | - public function countDisabledInGroup(string $gid): int; |
|
| 32 | + /** |
|
| 33 | + * @since 14.0.0 |
|
| 34 | + */ |
|
| 35 | + public function countDisabledInGroup(string $gid): int; |
|
| 36 | 36 | } |
@@ -44,240 +44,240 @@ |
||
| 44 | 44 | |
| 45 | 45 | class GroupsController extends AUserData { |
| 46 | 46 | |
| 47 | - /** @var ILogger */ |
|
| 48 | - private $logger; |
|
| 47 | + /** @var ILogger */ |
|
| 48 | + private $logger; |
|
| 49 | 49 | |
| 50 | - /** |
|
| 51 | - * @param string $appName |
|
| 52 | - * @param IRequest $request |
|
| 53 | - * @param IUserManager $userManager |
|
| 54 | - * @param IConfig $config |
|
| 55 | - * @param IGroupManager $groupManager |
|
| 56 | - * @param IUserSession $userSession |
|
| 57 | - * @param AccountManager $accountManager |
|
| 58 | - * @param ILogger $logger |
|
| 59 | - * @param UsersController $userController |
|
| 60 | - */ |
|
| 61 | - public function __construct(string $appName, |
|
| 62 | - IRequest $request, |
|
| 63 | - IUserManager $userManager, |
|
| 64 | - IConfig $config, |
|
| 65 | - IGroupManager $groupManager, |
|
| 66 | - IUserSession $userSession, |
|
| 67 | - AccountManager $accountManager, |
|
| 68 | - ILogger $logger) { |
|
| 69 | - parent::__construct($appName, |
|
| 70 | - $request, |
|
| 71 | - $userManager, |
|
| 72 | - $config, |
|
| 73 | - $groupManager, |
|
| 74 | - $userSession, |
|
| 75 | - $accountManager); |
|
| 50 | + /** |
|
| 51 | + * @param string $appName |
|
| 52 | + * @param IRequest $request |
|
| 53 | + * @param IUserManager $userManager |
|
| 54 | + * @param IConfig $config |
|
| 55 | + * @param IGroupManager $groupManager |
|
| 56 | + * @param IUserSession $userSession |
|
| 57 | + * @param AccountManager $accountManager |
|
| 58 | + * @param ILogger $logger |
|
| 59 | + * @param UsersController $userController |
|
| 60 | + */ |
|
| 61 | + public function __construct(string $appName, |
|
| 62 | + IRequest $request, |
|
| 63 | + IUserManager $userManager, |
|
| 64 | + IConfig $config, |
|
| 65 | + IGroupManager $groupManager, |
|
| 66 | + IUserSession $userSession, |
|
| 67 | + AccountManager $accountManager, |
|
| 68 | + ILogger $logger) { |
|
| 69 | + parent::__construct($appName, |
|
| 70 | + $request, |
|
| 71 | + $userManager, |
|
| 72 | + $config, |
|
| 73 | + $groupManager, |
|
| 74 | + $userSession, |
|
| 75 | + $accountManager); |
|
| 76 | 76 | |
| 77 | - $this->logger = $logger; |
|
| 78 | - } |
|
| 77 | + $this->logger = $logger; |
|
| 78 | + } |
|
| 79 | 79 | |
| 80 | - /** |
|
| 81 | - * returns a list of groups |
|
| 82 | - * |
|
| 83 | - * @NoAdminRequired |
|
| 84 | - * |
|
| 85 | - * @param string $search |
|
| 86 | - * @param int $limit |
|
| 87 | - * @param int $offset |
|
| 88 | - * @return DataResponse |
|
| 89 | - */ |
|
| 90 | - public function getGroups(string $search = '', int $limit = null, int $offset = 0): DataResponse { |
|
| 91 | - $groups = $this->groupManager->search($search, $limit, $offset); |
|
| 92 | - $groups = array_map(function($group) { |
|
| 93 | - /** @var IGroup $group */ |
|
| 94 | - return $group->getGID(); |
|
| 95 | - }, $groups); |
|
| 80 | + /** |
|
| 81 | + * returns a list of groups |
|
| 82 | + * |
|
| 83 | + * @NoAdminRequired |
|
| 84 | + * |
|
| 85 | + * @param string $search |
|
| 86 | + * @param int $limit |
|
| 87 | + * @param int $offset |
|
| 88 | + * @return DataResponse |
|
| 89 | + */ |
|
| 90 | + public function getGroups(string $search = '', int $limit = null, int $offset = 0): DataResponse { |
|
| 91 | + $groups = $this->groupManager->search($search, $limit, $offset); |
|
| 92 | + $groups = array_map(function($group) { |
|
| 93 | + /** @var IGroup $group */ |
|
| 94 | + return $group->getGID(); |
|
| 95 | + }, $groups); |
|
| 96 | 96 | |
| 97 | - return new DataResponse(['groups' => $groups]); |
|
| 98 | - } |
|
| 97 | + return new DataResponse(['groups' => $groups]); |
|
| 98 | + } |
|
| 99 | 99 | |
| 100 | - /** |
|
| 101 | - * returns a list of groups details with ids and displaynames |
|
| 102 | - * |
|
| 103 | - * @NoAdminRequired |
|
| 104 | - * |
|
| 105 | - * @param string $search |
|
| 106 | - * @param int $limit |
|
| 107 | - * @param int $offset |
|
| 108 | - * @return DataResponse |
|
| 109 | - */ |
|
| 110 | - public function getGroupsDetails(string $search = '', int $limit = null, int $offset = 0): DataResponse { |
|
| 111 | - $groups = $this->groupManager->search($search, $limit, $offset); |
|
| 112 | - $groups = array_map(function($group) { |
|
| 113 | - /** @var IGroup $group */ |
|
| 114 | - return [ |
|
| 115 | - 'id' => $group->getGID(), |
|
| 116 | - 'displayname' => $group->getDisplayName(), |
|
| 117 | - 'usercount' => $group->count(), |
|
| 118 | - 'disabled' => $group->countDisabled() |
|
| 119 | - ]; |
|
| 120 | - }, $groups); |
|
| 100 | + /** |
|
| 101 | + * returns a list of groups details with ids and displaynames |
|
| 102 | + * |
|
| 103 | + * @NoAdminRequired |
|
| 104 | + * |
|
| 105 | + * @param string $search |
|
| 106 | + * @param int $limit |
|
| 107 | + * @param int $offset |
|
| 108 | + * @return DataResponse |
|
| 109 | + */ |
|
| 110 | + public function getGroupsDetails(string $search = '', int $limit = null, int $offset = 0): DataResponse { |
|
| 111 | + $groups = $this->groupManager->search($search, $limit, $offset); |
|
| 112 | + $groups = array_map(function($group) { |
|
| 113 | + /** @var IGroup $group */ |
|
| 114 | + return [ |
|
| 115 | + 'id' => $group->getGID(), |
|
| 116 | + 'displayname' => $group->getDisplayName(), |
|
| 117 | + 'usercount' => $group->count(), |
|
| 118 | + 'disabled' => $group->countDisabled() |
|
| 119 | + ]; |
|
| 120 | + }, $groups); |
|
| 121 | 121 | |
| 122 | - return new DataResponse(['groups' => $groups]); |
|
| 123 | - } |
|
| 122 | + return new DataResponse(['groups' => $groups]); |
|
| 123 | + } |
|
| 124 | 124 | |
| 125 | - /** |
|
| 126 | - * @NoAdminRequired |
|
| 127 | - * |
|
| 128 | - * @param string $groupId |
|
| 129 | - * @return DataResponse |
|
| 130 | - * @throws OCSException |
|
| 131 | - * |
|
| 132 | - * @deprecated 14 Use getGroupUsers |
|
| 133 | - */ |
|
| 134 | - public function getGroup(string $groupId): DataResponse { |
|
| 135 | - return $this->getGroupUsers($groupId); |
|
| 136 | - } |
|
| 125 | + /** |
|
| 126 | + * @NoAdminRequired |
|
| 127 | + * |
|
| 128 | + * @param string $groupId |
|
| 129 | + * @return DataResponse |
|
| 130 | + * @throws OCSException |
|
| 131 | + * |
|
| 132 | + * @deprecated 14 Use getGroupUsers |
|
| 133 | + */ |
|
| 134 | + public function getGroup(string $groupId): DataResponse { |
|
| 135 | + return $this->getGroupUsers($groupId); |
|
| 136 | + } |
|
| 137 | 137 | |
| 138 | - /** |
|
| 139 | - * returns an array of users in the specified group |
|
| 140 | - * |
|
| 141 | - * @NoAdminRequired |
|
| 142 | - * |
|
| 143 | - * @param string $groupId |
|
| 144 | - * @return DataResponse |
|
| 145 | - * @throws OCSException |
|
| 146 | - */ |
|
| 147 | - public function getGroupUsers(string $groupId): DataResponse { |
|
| 148 | - $user = $this->userSession->getUser(); |
|
| 149 | - $isSubadminOfGroup = false; |
|
| 138 | + /** |
|
| 139 | + * returns an array of users in the specified group |
|
| 140 | + * |
|
| 141 | + * @NoAdminRequired |
|
| 142 | + * |
|
| 143 | + * @param string $groupId |
|
| 144 | + * @return DataResponse |
|
| 145 | + * @throws OCSException |
|
| 146 | + */ |
|
| 147 | + public function getGroupUsers(string $groupId): DataResponse { |
|
| 148 | + $user = $this->userSession->getUser(); |
|
| 149 | + $isSubadminOfGroup = false; |
|
| 150 | 150 | |
| 151 | - // Check the group exists |
|
| 152 | - $group = $this->groupManager->get($groupId); |
|
| 153 | - if ($group !== null) { |
|
| 154 | - $isSubadminOfGroup =$this->groupManager->getSubAdmin()->isSubAdminOfGroup($user, $group); |
|
| 155 | - } else { |
|
| 156 | - throw new OCSNotFoundException('The requested group could not be found'); |
|
| 157 | - } |
|
| 151 | + // Check the group exists |
|
| 152 | + $group = $this->groupManager->get($groupId); |
|
| 153 | + if ($group !== null) { |
|
| 154 | + $isSubadminOfGroup =$this->groupManager->getSubAdmin()->isSubAdminOfGroup($user, $group); |
|
| 155 | + } else { |
|
| 156 | + throw new OCSNotFoundException('The requested group could not be found'); |
|
| 157 | + } |
|
| 158 | 158 | |
| 159 | - // Check subadmin has access to this group |
|
| 160 | - if($this->groupManager->isAdmin($user->getUID()) |
|
| 161 | - || $isSubadminOfGroup) { |
|
| 162 | - $users = $this->groupManager->get($groupId)->getUsers(); |
|
| 163 | - $users = array_map(function($user) { |
|
| 164 | - /** @var IUser $user */ |
|
| 165 | - return $user->getUID(); |
|
| 166 | - }, $users); |
|
| 167 | - $users = array_values($users); |
|
| 168 | - return new DataResponse(['users' => $users]); |
|
| 169 | - } |
|
| 159 | + // Check subadmin has access to this group |
|
| 160 | + if($this->groupManager->isAdmin($user->getUID()) |
|
| 161 | + || $isSubadminOfGroup) { |
|
| 162 | + $users = $this->groupManager->get($groupId)->getUsers(); |
|
| 163 | + $users = array_map(function($user) { |
|
| 164 | + /** @var IUser $user */ |
|
| 165 | + return $user->getUID(); |
|
| 166 | + }, $users); |
|
| 167 | + $users = array_values($users); |
|
| 168 | + return new DataResponse(['users' => $users]); |
|
| 169 | + } |
|
| 170 | 170 | |
| 171 | - throw new OCSForbiddenException(); |
|
| 172 | - } |
|
| 171 | + throw new OCSForbiddenException(); |
|
| 172 | + } |
|
| 173 | 173 | |
| 174 | - /** |
|
| 175 | - * returns an array of users details in the specified group |
|
| 176 | - * |
|
| 177 | - * @NoAdminRequired |
|
| 178 | - * |
|
| 179 | - * @param string $groupId |
|
| 180 | - * @param int $limit |
|
| 181 | - * @param int $offset |
|
| 182 | - * @return DataResponse |
|
| 183 | - * @throws OCSException |
|
| 184 | - */ |
|
| 185 | - public function getGroupUsersDetails(string $groupId, int $limit = null, int $offset = 0): DataResponse { |
|
| 186 | - $user = $this->userSession->getUser(); |
|
| 187 | - $isSubadminOfGroup = false; |
|
| 174 | + /** |
|
| 175 | + * returns an array of users details in the specified group |
|
| 176 | + * |
|
| 177 | + * @NoAdminRequired |
|
| 178 | + * |
|
| 179 | + * @param string $groupId |
|
| 180 | + * @param int $limit |
|
| 181 | + * @param int $offset |
|
| 182 | + * @return DataResponse |
|
| 183 | + * @throws OCSException |
|
| 184 | + */ |
|
| 185 | + public function getGroupUsersDetails(string $groupId, int $limit = null, int $offset = 0): DataResponse { |
|
| 186 | + $user = $this->userSession->getUser(); |
|
| 187 | + $isSubadminOfGroup = false; |
|
| 188 | 188 | |
| 189 | - // Check the group exists |
|
| 190 | - $group = $this->groupManager->get($groupId); |
|
| 191 | - if ($group !== null) { |
|
| 192 | - $isSubadminOfGroup =$this->groupManager->getSubAdmin()->isSubAdminOfGroup($user, $group); |
|
| 193 | - } else { |
|
| 194 | - throw new OCSException('The requested group could not be found', \OCP\API::RESPOND_NOT_FOUND); |
|
| 195 | - } |
|
| 189 | + // Check the group exists |
|
| 190 | + $group = $this->groupManager->get($groupId); |
|
| 191 | + if ($group !== null) { |
|
| 192 | + $isSubadminOfGroup =$this->groupManager->getSubAdmin()->isSubAdminOfGroup($user, $group); |
|
| 193 | + } else { |
|
| 194 | + throw new OCSException('The requested group could not be found', \OCP\API::RESPOND_NOT_FOUND); |
|
| 195 | + } |
|
| 196 | 196 | |
| 197 | - // Check subadmin has access to this group |
|
| 198 | - if($this->groupManager->isAdmin($user->getUID()) |
|
| 199 | - || $isSubadminOfGroup) { |
|
| 200 | - $users = $this->groupManager->get($groupId)->getUsers(); |
|
| 201 | - // Extract required number |
|
| 202 | - $users = array_slice($users, $offset, $limit); |
|
| 203 | - $users = array_keys($users); |
|
| 204 | - $usersDetails = []; |
|
| 205 | - foreach ($users as $userId) { |
|
| 206 | - $userData = $this->getUserData($userId); |
|
| 207 | - // Do not insert empty entry |
|
| 208 | - if(!empty($userData)) { |
|
| 209 | - $usersDetails[$userId] = $userData; |
|
| 210 | - } |
|
| 211 | - } |
|
| 212 | - return new DataResponse(['users' => $usersDetails]); |
|
| 213 | - } |
|
| 197 | + // Check subadmin has access to this group |
|
| 198 | + if($this->groupManager->isAdmin($user->getUID()) |
|
| 199 | + || $isSubadminOfGroup) { |
|
| 200 | + $users = $this->groupManager->get($groupId)->getUsers(); |
|
| 201 | + // Extract required number |
|
| 202 | + $users = array_slice($users, $offset, $limit); |
|
| 203 | + $users = array_keys($users); |
|
| 204 | + $usersDetails = []; |
|
| 205 | + foreach ($users as $userId) { |
|
| 206 | + $userData = $this->getUserData($userId); |
|
| 207 | + // Do not insert empty entry |
|
| 208 | + if(!empty($userData)) { |
|
| 209 | + $usersDetails[$userId] = $userData; |
|
| 210 | + } |
|
| 211 | + } |
|
| 212 | + return new DataResponse(['users' => $usersDetails]); |
|
| 213 | + } |
|
| 214 | 214 | |
| 215 | - throw new OCSException('User does not have access to specified group', \OCP\API::RESPOND_UNAUTHORISED); |
|
| 216 | - } |
|
| 215 | + throw new OCSException('User does not have access to specified group', \OCP\API::RESPOND_UNAUTHORISED); |
|
| 216 | + } |
|
| 217 | 217 | |
| 218 | - /** |
|
| 219 | - * creates a new group |
|
| 220 | - * |
|
| 221 | - * @PasswordConfirmationRequired |
|
| 222 | - * |
|
| 223 | - * @param string $groupid |
|
| 224 | - * @return DataResponse |
|
| 225 | - * @throws OCSException |
|
| 226 | - */ |
|
| 227 | - public function addGroup(string $groupid): DataResponse { |
|
| 228 | - // Validate name |
|
| 229 | - if(empty($groupid)) { |
|
| 230 | - $this->logger->error('Group name not supplied', ['app' => 'provisioning_api']); |
|
| 231 | - throw new OCSException('Invalid group name', 101); |
|
| 232 | - } |
|
| 233 | - // Check if it exists |
|
| 234 | - if($this->groupManager->groupExists($groupid)){ |
|
| 235 | - throw new OCSException('', 102); |
|
| 236 | - } |
|
| 237 | - $this->groupManager->createGroup($groupid); |
|
| 238 | - return new DataResponse(); |
|
| 239 | - } |
|
| 218 | + /** |
|
| 219 | + * creates a new group |
|
| 220 | + * |
|
| 221 | + * @PasswordConfirmationRequired |
|
| 222 | + * |
|
| 223 | + * @param string $groupid |
|
| 224 | + * @return DataResponse |
|
| 225 | + * @throws OCSException |
|
| 226 | + */ |
|
| 227 | + public function addGroup(string $groupid): DataResponse { |
|
| 228 | + // Validate name |
|
| 229 | + if(empty($groupid)) { |
|
| 230 | + $this->logger->error('Group name not supplied', ['app' => 'provisioning_api']); |
|
| 231 | + throw new OCSException('Invalid group name', 101); |
|
| 232 | + } |
|
| 233 | + // Check if it exists |
|
| 234 | + if($this->groupManager->groupExists($groupid)){ |
|
| 235 | + throw new OCSException('', 102); |
|
| 236 | + } |
|
| 237 | + $this->groupManager->createGroup($groupid); |
|
| 238 | + return new DataResponse(); |
|
| 239 | + } |
|
| 240 | 240 | |
| 241 | - /** |
|
| 242 | - * @PasswordConfirmationRequired |
|
| 243 | - * |
|
| 244 | - * @param string $groupId |
|
| 245 | - * @return DataResponse |
|
| 246 | - * @throws OCSException |
|
| 247 | - */ |
|
| 248 | - public function deleteGroup(string $groupId): DataResponse { |
|
| 249 | - // Check it exists |
|
| 250 | - if(!$this->groupManager->groupExists($groupId)){ |
|
| 251 | - throw new OCSException('', 101); |
|
| 252 | - } else if($groupId === 'admin' || !$this->groupManager->get($groupId)->delete()){ |
|
| 253 | - // Cannot delete admin group |
|
| 254 | - throw new OCSException('', 102); |
|
| 255 | - } |
|
| 241 | + /** |
|
| 242 | + * @PasswordConfirmationRequired |
|
| 243 | + * |
|
| 244 | + * @param string $groupId |
|
| 245 | + * @return DataResponse |
|
| 246 | + * @throws OCSException |
|
| 247 | + */ |
|
| 248 | + public function deleteGroup(string $groupId): DataResponse { |
|
| 249 | + // Check it exists |
|
| 250 | + if(!$this->groupManager->groupExists($groupId)){ |
|
| 251 | + throw new OCSException('', 101); |
|
| 252 | + } else if($groupId === 'admin' || !$this->groupManager->get($groupId)->delete()){ |
|
| 253 | + // Cannot delete admin group |
|
| 254 | + throw new OCSException('', 102); |
|
| 255 | + } |
|
| 256 | 256 | |
| 257 | - return new DataResponse(); |
|
| 258 | - } |
|
| 257 | + return new DataResponse(); |
|
| 258 | + } |
|
| 259 | 259 | |
| 260 | - /** |
|
| 261 | - * @param string $groupId |
|
| 262 | - * @return DataResponse |
|
| 263 | - * @throws OCSException |
|
| 264 | - */ |
|
| 265 | - public function getSubAdminsOfGroup(string $groupId): DataResponse { |
|
| 266 | - // Check group exists |
|
| 267 | - $targetGroup = $this->groupManager->get($groupId); |
|
| 268 | - if($targetGroup === null) { |
|
| 269 | - throw new OCSException('Group does not exist', 101); |
|
| 270 | - } |
|
| 260 | + /** |
|
| 261 | + * @param string $groupId |
|
| 262 | + * @return DataResponse |
|
| 263 | + * @throws OCSException |
|
| 264 | + */ |
|
| 265 | + public function getSubAdminsOfGroup(string $groupId): DataResponse { |
|
| 266 | + // Check group exists |
|
| 267 | + $targetGroup = $this->groupManager->get($groupId); |
|
| 268 | + if($targetGroup === null) { |
|
| 269 | + throw new OCSException('Group does not exist', 101); |
|
| 270 | + } |
|
| 271 | 271 | |
| 272 | - /** @var IUser[] $subadmins */ |
|
| 273 | - $subadmins = $this->groupManager->getSubAdmin()->getGroupsSubAdmins($targetGroup); |
|
| 274 | - // New class returns IUser[] so convert back |
|
| 275 | - $uids = []; |
|
| 276 | - foreach ($subadmins as $user) { |
|
| 277 | - $uids[] = $user->getUID(); |
|
| 278 | - } |
|
| 272 | + /** @var IUser[] $subadmins */ |
|
| 273 | + $subadmins = $this->groupManager->getSubAdmin()->getGroupsSubAdmins($targetGroup); |
|
| 274 | + // New class returns IUser[] so convert back |
|
| 275 | + $uids = []; |
|
| 276 | + foreach ($subadmins as $user) { |
|
| 277 | + $uids[] = $user->getUID(); |
|
| 278 | + } |
|
| 279 | 279 | |
| 280 | - return new DataResponse($uids); |
|
| 281 | - } |
|
| 280 | + return new DataResponse($uids); |
|
| 281 | + } |
|
| 282 | 282 | |
| 283 | 283 | } |
@@ -33,97 +33,97 @@ |
||
| 33 | 33 | * @since 8.0.0 |
| 34 | 34 | */ |
| 35 | 35 | interface IGroup { |
| 36 | - /** |
|
| 37 | - * @return string |
|
| 38 | - * @since 8.0.0 |
|
| 39 | - */ |
|
| 40 | - public function getGID(); |
|
| 36 | + /** |
|
| 37 | + * @return string |
|
| 38 | + * @since 8.0.0 |
|
| 39 | + */ |
|
| 40 | + public function getGID(); |
|
| 41 | 41 | |
| 42 | - /** |
|
| 43 | - * Returns the group display name |
|
| 44 | - * |
|
| 45 | - * @return string |
|
| 46 | - * @since 12.0.0 |
|
| 47 | - */ |
|
| 48 | - public function getDisplayName(); |
|
| 42 | + /** |
|
| 43 | + * Returns the group display name |
|
| 44 | + * |
|
| 45 | + * @return string |
|
| 46 | + * @since 12.0.0 |
|
| 47 | + */ |
|
| 48 | + public function getDisplayName(); |
|
| 49 | 49 | |
| 50 | - /** |
|
| 51 | - * get all users in the group |
|
| 52 | - * |
|
| 53 | - * @return \OCP\IUser[] |
|
| 54 | - * @since 8.0.0 |
|
| 55 | - */ |
|
| 56 | - public function getUsers(); |
|
| 50 | + /** |
|
| 51 | + * get all users in the group |
|
| 52 | + * |
|
| 53 | + * @return \OCP\IUser[] |
|
| 54 | + * @since 8.0.0 |
|
| 55 | + */ |
|
| 56 | + public function getUsers(); |
|
| 57 | 57 | |
| 58 | - /** |
|
| 59 | - * check if a user is in the group |
|
| 60 | - * |
|
| 61 | - * @param \OCP\IUser $user |
|
| 62 | - * @return bool |
|
| 63 | - * @since 8.0.0 |
|
| 64 | - */ |
|
| 65 | - public function inGroup(IUser $user); |
|
| 58 | + /** |
|
| 59 | + * check if a user is in the group |
|
| 60 | + * |
|
| 61 | + * @param \OCP\IUser $user |
|
| 62 | + * @return bool |
|
| 63 | + * @since 8.0.0 |
|
| 64 | + */ |
|
| 65 | + public function inGroup(IUser $user); |
|
| 66 | 66 | |
| 67 | - /** |
|
| 68 | - * add a user to the group |
|
| 69 | - * |
|
| 70 | - * @param \OCP\IUser $user |
|
| 71 | - * @since 8.0.0 |
|
| 72 | - */ |
|
| 73 | - public function addUser(IUser $user); |
|
| 67 | + /** |
|
| 68 | + * add a user to the group |
|
| 69 | + * |
|
| 70 | + * @param \OCP\IUser $user |
|
| 71 | + * @since 8.0.0 |
|
| 72 | + */ |
|
| 73 | + public function addUser(IUser $user); |
|
| 74 | 74 | |
| 75 | - /** |
|
| 76 | - * remove a user from the group |
|
| 77 | - * |
|
| 78 | - * @param \OCP\IUser $user |
|
| 79 | - * @since 8.0.0 |
|
| 80 | - */ |
|
| 81 | - public function removeUser($user); |
|
| 75 | + /** |
|
| 76 | + * remove a user from the group |
|
| 77 | + * |
|
| 78 | + * @param \OCP\IUser $user |
|
| 79 | + * @since 8.0.0 |
|
| 80 | + */ |
|
| 81 | + public function removeUser($user); |
|
| 82 | 82 | |
| 83 | - /** |
|
| 84 | - * search for users in the group by userid |
|
| 85 | - * |
|
| 86 | - * @param string $search |
|
| 87 | - * @param int $limit |
|
| 88 | - * @param int $offset |
|
| 89 | - * @return \OCP\IUser[] |
|
| 90 | - * @since 8.0.0 |
|
| 91 | - */ |
|
| 92 | - public function searchUsers($search, $limit = null, $offset = null); |
|
| 83 | + /** |
|
| 84 | + * search for users in the group by userid |
|
| 85 | + * |
|
| 86 | + * @param string $search |
|
| 87 | + * @param int $limit |
|
| 88 | + * @param int $offset |
|
| 89 | + * @return \OCP\IUser[] |
|
| 90 | + * @since 8.0.0 |
|
| 91 | + */ |
|
| 92 | + public function searchUsers($search, $limit = null, $offset = null); |
|
| 93 | 93 | |
| 94 | - /** |
|
| 95 | - * returns the number of users matching the search string |
|
| 96 | - * |
|
| 97 | - * @param string $search |
|
| 98 | - * @return int|bool |
|
| 99 | - * @since 8.0.0 |
|
| 100 | - */ |
|
| 101 | - public function count($search = ''); |
|
| 94 | + /** |
|
| 95 | + * returns the number of users matching the search string |
|
| 96 | + * |
|
| 97 | + * @param string $search |
|
| 98 | + * @return int|bool |
|
| 99 | + * @since 8.0.0 |
|
| 100 | + */ |
|
| 101 | + public function count($search = ''); |
|
| 102 | 102 | |
| 103 | - /** |
|
| 104 | - * returns the number of disabled users |
|
| 105 | - * |
|
| 106 | - * @return int|bool |
|
| 107 | - * @since 14.0.0 |
|
| 108 | - */ |
|
| 109 | - public function countDisabled(); |
|
| 103 | + /** |
|
| 104 | + * returns the number of disabled users |
|
| 105 | + * |
|
| 106 | + * @return int|bool |
|
| 107 | + * @since 14.0.0 |
|
| 108 | + */ |
|
| 109 | + public function countDisabled(); |
|
| 110 | 110 | |
| 111 | - /** |
|
| 112 | - * search for users in the group by displayname |
|
| 113 | - * |
|
| 114 | - * @param string $search |
|
| 115 | - * @param int $limit |
|
| 116 | - * @param int $offset |
|
| 117 | - * @return \OCP\IUser[] |
|
| 118 | - * @since 8.0.0 |
|
| 119 | - */ |
|
| 120 | - public function searchDisplayName($search, $limit = null, $offset = null); |
|
| 111 | + /** |
|
| 112 | + * search for users in the group by displayname |
|
| 113 | + * |
|
| 114 | + * @param string $search |
|
| 115 | + * @param int $limit |
|
| 116 | + * @param int $offset |
|
| 117 | + * @return \OCP\IUser[] |
|
| 118 | + * @since 8.0.0 |
|
| 119 | + */ |
|
| 120 | + public function searchDisplayName($search, $limit = null, $offset = null); |
|
| 121 | 121 | |
| 122 | - /** |
|
| 123 | - * delete the group |
|
| 124 | - * |
|
| 125 | - * @return bool |
|
| 126 | - * @since 8.0.0 |
|
| 127 | - */ |
|
| 128 | - public function delete(); |
|
| 122 | + /** |
|
| 123 | + * delete the group |
|
| 124 | + * |
|
| 125 | + * @return bool |
|
| 126 | + * @since 8.0.0 |
|
| 127 | + */ |
|
| 128 | + public function delete(); |
|
| 129 | 129 | } |
@@ -65,397 +65,397 @@ |
||
| 65 | 65 | * @package OC\Settings\Controller |
| 66 | 66 | */ |
| 67 | 67 | class UsersController extends Controller { |
| 68 | - /** @var IUserManager */ |
|
| 69 | - private $userManager; |
|
| 70 | - /** @var IGroupManager */ |
|
| 71 | - private $groupManager; |
|
| 72 | - /** @var IUserSession */ |
|
| 73 | - private $userSession; |
|
| 74 | - /** @var IConfig */ |
|
| 75 | - private $config; |
|
| 76 | - /** @var bool */ |
|
| 77 | - private $isAdmin; |
|
| 78 | - /** @var IL10N */ |
|
| 79 | - private $l10n; |
|
| 80 | - /** @var IMailer */ |
|
| 81 | - private $mailer; |
|
| 82 | - /** @var IFactory */ |
|
| 83 | - private $l10nFactory; |
|
| 84 | - /** @var IAppManager */ |
|
| 85 | - private $appManager; |
|
| 86 | - /** @var AccountManager */ |
|
| 87 | - private $accountManager; |
|
| 88 | - /** @var Manager */ |
|
| 89 | - private $keyManager; |
|
| 90 | - /** @var IJobList */ |
|
| 91 | - private $jobList; |
|
| 92 | - /** @var IManager */ |
|
| 93 | - private $encryptionManager; |
|
| 68 | + /** @var IUserManager */ |
|
| 69 | + private $userManager; |
|
| 70 | + /** @var IGroupManager */ |
|
| 71 | + private $groupManager; |
|
| 72 | + /** @var IUserSession */ |
|
| 73 | + private $userSession; |
|
| 74 | + /** @var IConfig */ |
|
| 75 | + private $config; |
|
| 76 | + /** @var bool */ |
|
| 77 | + private $isAdmin; |
|
| 78 | + /** @var IL10N */ |
|
| 79 | + private $l10n; |
|
| 80 | + /** @var IMailer */ |
|
| 81 | + private $mailer; |
|
| 82 | + /** @var IFactory */ |
|
| 83 | + private $l10nFactory; |
|
| 84 | + /** @var IAppManager */ |
|
| 85 | + private $appManager; |
|
| 86 | + /** @var AccountManager */ |
|
| 87 | + private $accountManager; |
|
| 88 | + /** @var Manager */ |
|
| 89 | + private $keyManager; |
|
| 90 | + /** @var IJobList */ |
|
| 91 | + private $jobList; |
|
| 92 | + /** @var IManager */ |
|
| 93 | + private $encryptionManager; |
|
| 94 | 94 | |
| 95 | 95 | |
| 96 | - public function __construct(string $appName, |
|
| 97 | - IRequest $request, |
|
| 98 | - IUserManager $userManager, |
|
| 99 | - IGroupManager $groupManager, |
|
| 100 | - IUserSession $userSession, |
|
| 101 | - IConfig $config, |
|
| 102 | - bool $isAdmin, |
|
| 103 | - IL10N $l10n, |
|
| 104 | - IMailer $mailer, |
|
| 105 | - IFactory $l10nFactory, |
|
| 106 | - IAppManager $appManager, |
|
| 107 | - AccountManager $accountManager, |
|
| 108 | - Manager $keyManager, |
|
| 109 | - IJobList $jobList, |
|
| 110 | - IManager $encryptionManager) { |
|
| 111 | - parent::__construct($appName, $request); |
|
| 112 | - $this->userManager = $userManager; |
|
| 113 | - $this->groupManager = $groupManager; |
|
| 114 | - $this->userSession = $userSession; |
|
| 115 | - $this->config = $config; |
|
| 116 | - $this->isAdmin = $isAdmin; |
|
| 117 | - $this->l10n = $l10n; |
|
| 118 | - $this->mailer = $mailer; |
|
| 119 | - $this->l10nFactory = $l10nFactory; |
|
| 120 | - $this->appManager = $appManager; |
|
| 121 | - $this->accountManager = $accountManager; |
|
| 122 | - $this->keyManager = $keyManager; |
|
| 123 | - $this->jobList = $jobList; |
|
| 124 | - $this->encryptionManager = $encryptionManager; |
|
| 125 | - } |
|
| 96 | + public function __construct(string $appName, |
|
| 97 | + IRequest $request, |
|
| 98 | + IUserManager $userManager, |
|
| 99 | + IGroupManager $groupManager, |
|
| 100 | + IUserSession $userSession, |
|
| 101 | + IConfig $config, |
|
| 102 | + bool $isAdmin, |
|
| 103 | + IL10N $l10n, |
|
| 104 | + IMailer $mailer, |
|
| 105 | + IFactory $l10nFactory, |
|
| 106 | + IAppManager $appManager, |
|
| 107 | + AccountManager $accountManager, |
|
| 108 | + Manager $keyManager, |
|
| 109 | + IJobList $jobList, |
|
| 110 | + IManager $encryptionManager) { |
|
| 111 | + parent::__construct($appName, $request); |
|
| 112 | + $this->userManager = $userManager; |
|
| 113 | + $this->groupManager = $groupManager; |
|
| 114 | + $this->userSession = $userSession; |
|
| 115 | + $this->config = $config; |
|
| 116 | + $this->isAdmin = $isAdmin; |
|
| 117 | + $this->l10n = $l10n; |
|
| 118 | + $this->mailer = $mailer; |
|
| 119 | + $this->l10nFactory = $l10nFactory; |
|
| 120 | + $this->appManager = $appManager; |
|
| 121 | + $this->accountManager = $accountManager; |
|
| 122 | + $this->keyManager = $keyManager; |
|
| 123 | + $this->jobList = $jobList; |
|
| 124 | + $this->encryptionManager = $encryptionManager; |
|
| 125 | + } |
|
| 126 | 126 | |
| 127 | 127 | |
| 128 | - /** |
|
| 129 | - * @NoCSRFRequired |
|
| 130 | - * @NoAdminRequired |
|
| 131 | - * |
|
| 132 | - * Display users list template |
|
| 133 | - * |
|
| 134 | - * @return TemplateResponse |
|
| 135 | - */ |
|
| 136 | - public function usersListByGroup() { |
|
| 137 | - return $this->usersList(); |
|
| 138 | - } |
|
| 128 | + /** |
|
| 129 | + * @NoCSRFRequired |
|
| 130 | + * @NoAdminRequired |
|
| 131 | + * |
|
| 132 | + * Display users list template |
|
| 133 | + * |
|
| 134 | + * @return TemplateResponse |
|
| 135 | + */ |
|
| 136 | + public function usersListByGroup() { |
|
| 137 | + return $this->usersList(); |
|
| 138 | + } |
|
| 139 | 139 | |
| 140 | - /** |
|
| 141 | - * @NoCSRFRequired |
|
| 142 | - * @NoAdminRequired |
|
| 143 | - * |
|
| 144 | - * Display users list template |
|
| 145 | - * |
|
| 146 | - * @return TemplateResponse |
|
| 147 | - */ |
|
| 148 | - public function usersList() { |
|
| 149 | - $user = $this->userSession->getUser(); |
|
| 150 | - $uid = $user->getUID(); |
|
| 140 | + /** |
|
| 141 | + * @NoCSRFRequired |
|
| 142 | + * @NoAdminRequired |
|
| 143 | + * |
|
| 144 | + * Display users list template |
|
| 145 | + * |
|
| 146 | + * @return TemplateResponse |
|
| 147 | + */ |
|
| 148 | + public function usersList() { |
|
| 149 | + $user = $this->userSession->getUser(); |
|
| 150 | + $uid = $user->getUID(); |
|
| 151 | 151 | |
| 152 | - \OC::$server->getNavigationManager()->setActiveEntry('core_users'); |
|
| 152 | + \OC::$server->getNavigationManager()->setActiveEntry('core_users'); |
|
| 153 | 153 | |
| 154 | - /* SORT OPTION: SORT_USERCOUNT or SORT_GROUPNAME */ |
|
| 155 | - $sortGroupsBy = \OC\Group\MetaData::SORT_USERCOUNT; |
|
| 156 | - if ($this->config->getSystemValue('sort_groups_by_name', false)) { |
|
| 157 | - $sortGroupsBy = \OC\Group\MetaData::SORT_GROUPNAME; |
|
| 158 | - } else { |
|
| 159 | - $isLDAPUsed = false; |
|
| 160 | - if ($this->appManager->isEnabledForUser('user_ldap')) { |
|
| 161 | - $isLDAPUsed = |
|
| 162 | - $this->groupManager->isBackendUsed('\OCA\User_LDAP\Group_LDAP') |
|
| 163 | - || $this->groupManager->isBackendUsed('\OCA\User_LDAP\Group_Proxy'); |
|
| 164 | - if ($isLDAPUsed) { |
|
| 165 | - // LDAP user count can be slow, so we sort by group name here |
|
| 166 | - $sortGroupsBy = \OC\Group\MetaData::SORT_GROUPNAME; |
|
| 167 | - } |
|
| 168 | - } |
|
| 169 | - } |
|
| 154 | + /* SORT OPTION: SORT_USERCOUNT or SORT_GROUPNAME */ |
|
| 155 | + $sortGroupsBy = \OC\Group\MetaData::SORT_USERCOUNT; |
|
| 156 | + if ($this->config->getSystemValue('sort_groups_by_name', false)) { |
|
| 157 | + $sortGroupsBy = \OC\Group\MetaData::SORT_GROUPNAME; |
|
| 158 | + } else { |
|
| 159 | + $isLDAPUsed = false; |
|
| 160 | + if ($this->appManager->isEnabledForUser('user_ldap')) { |
|
| 161 | + $isLDAPUsed = |
|
| 162 | + $this->groupManager->isBackendUsed('\OCA\User_LDAP\Group_LDAP') |
|
| 163 | + || $this->groupManager->isBackendUsed('\OCA\User_LDAP\Group_Proxy'); |
|
| 164 | + if ($isLDAPUsed) { |
|
| 165 | + // LDAP user count can be slow, so we sort by group name here |
|
| 166 | + $sortGroupsBy = \OC\Group\MetaData::SORT_GROUPNAME; |
|
| 167 | + } |
|
| 168 | + } |
|
| 169 | + } |
|
| 170 | 170 | |
| 171 | - /* ENCRYPTION CONFIG */ |
|
| 172 | - $isEncryptionEnabled = $this->encryptionManager->isEnabled(); |
|
| 173 | - $useMasterKey = $this->config->getAppValue('encryption', 'useMasterKey', true); |
|
| 174 | - // If masterKey enabled, then you can change password. This is to avoid data loss! |
|
| 175 | - $canChangePassword = ($isEncryptionEnabled && $useMasterKey) || $useMasterKey; |
|
| 171 | + /* ENCRYPTION CONFIG */ |
|
| 172 | + $isEncryptionEnabled = $this->encryptionManager->isEnabled(); |
|
| 173 | + $useMasterKey = $this->config->getAppValue('encryption', 'useMasterKey', true); |
|
| 174 | + // If masterKey enabled, then you can change password. This is to avoid data loss! |
|
| 175 | + $canChangePassword = ($isEncryptionEnabled && $useMasterKey) || $useMasterKey; |
|
| 176 | 176 | |
| 177 | 177 | |
| 178 | - /* GROUPS */ |
|
| 179 | - $groupsInfo = new \OC\Group\MetaData( |
|
| 180 | - $uid, |
|
| 181 | - $this->isAdmin, |
|
| 182 | - $this->groupManager, |
|
| 183 | - $this->userSession |
|
| 184 | - ); |
|
| 178 | + /* GROUPS */ |
|
| 179 | + $groupsInfo = new \OC\Group\MetaData( |
|
| 180 | + $uid, |
|
| 181 | + $this->isAdmin, |
|
| 182 | + $this->groupManager, |
|
| 183 | + $this->userSession |
|
| 184 | + ); |
|
| 185 | 185 | |
| 186 | - $groupsInfo->setSorting($sortGroupsBy); |
|
| 187 | - list($adminGroup, $groups) = $groupsInfo->get(); |
|
| 186 | + $groupsInfo->setSorting($sortGroupsBy); |
|
| 187 | + list($adminGroup, $groups) = $groupsInfo->get(); |
|
| 188 | 188 | |
| 189 | 189 | |
| 190 | - if ($this->isAdmin) { |
|
| 191 | - $disabledUsers = $isLDAPUsed ? 0 : $this->userManager->countDisabledUsers(); |
|
| 192 | - $userCount = array_reduce($this->userManager->countUsers(), function($v, $w) { |
|
| 193 | - return $v + (int)$w; |
|
| 194 | - }, 0); |
|
| 195 | - } else { |
|
| 196 | - // User is subadmin ! |
|
| 197 | - // Map group list to names to retrieve the countDisabledUsersOfGroups |
|
| 198 | - $userCount = 0; |
|
| 199 | - $groupsNames = []; |
|
| 200 | - foreach($groups as $group) { |
|
| 201 | - $userCount += (int)['usercount']; |
|
| 202 | - $groupsName[] = ['name']; |
|
| 203 | - }; |
|
| 204 | - $disabledUsers = $isLDAPUsed ? 0 : $this->userManager->countDisabledUsersOfGroups($groupsNames); |
|
| 205 | - } |
|
| 206 | - $disabledUsersGroup = [ |
|
| 207 | - 'id' => 'disabled', |
|
| 208 | - 'name' => 'Disabled users', |
|
| 209 | - 'usercount' => $disabledUsers |
|
| 210 | - ]; |
|
| 211 | - $allGroups = array_merge_recursive($adminGroup, $groups); |
|
| 190 | + if ($this->isAdmin) { |
|
| 191 | + $disabledUsers = $isLDAPUsed ? 0 : $this->userManager->countDisabledUsers(); |
|
| 192 | + $userCount = array_reduce($this->userManager->countUsers(), function($v, $w) { |
|
| 193 | + return $v + (int)$w; |
|
| 194 | + }, 0); |
|
| 195 | + } else { |
|
| 196 | + // User is subadmin ! |
|
| 197 | + // Map group list to names to retrieve the countDisabledUsersOfGroups |
|
| 198 | + $userCount = 0; |
|
| 199 | + $groupsNames = []; |
|
| 200 | + foreach($groups as $group) { |
|
| 201 | + $userCount += (int)['usercount']; |
|
| 202 | + $groupsName[] = ['name']; |
|
| 203 | + }; |
|
| 204 | + $disabledUsers = $isLDAPUsed ? 0 : $this->userManager->countDisabledUsersOfGroups($groupsNames); |
|
| 205 | + } |
|
| 206 | + $disabledUsersGroup = [ |
|
| 207 | + 'id' => 'disabled', |
|
| 208 | + 'name' => 'Disabled users', |
|
| 209 | + 'usercount' => $disabledUsers |
|
| 210 | + ]; |
|
| 211 | + $allGroups = array_merge_recursive($adminGroup, $groups); |
|
| 212 | 212 | |
| 213 | - /* QUOTAS PRESETS */ |
|
| 214 | - $quotaPreset = $this->config->getAppValue('files', 'quota_preset', '1 GB, 5 GB, 10 GB'); |
|
| 215 | - $quotaPreset = explode(',', $quotaPreset); |
|
| 216 | - foreach ($quotaPreset as &$preset) { |
|
| 217 | - $preset = trim($preset); |
|
| 218 | - } |
|
| 219 | - $quotaPreset = array_diff($quotaPreset, array('default', 'none')); |
|
| 220 | - $defaultQuota = $this->config->getAppValue('files', 'default_quota', 'none'); |
|
| 213 | + /* QUOTAS PRESETS */ |
|
| 214 | + $quotaPreset = $this->config->getAppValue('files', 'quota_preset', '1 GB, 5 GB, 10 GB'); |
|
| 215 | + $quotaPreset = explode(',', $quotaPreset); |
|
| 216 | + foreach ($quotaPreset as &$preset) { |
|
| 217 | + $preset = trim($preset); |
|
| 218 | + } |
|
| 219 | + $quotaPreset = array_diff($quotaPreset, array('default', 'none')); |
|
| 220 | + $defaultQuota = $this->config->getAppValue('files', 'default_quota', 'none'); |
|
| 221 | 221 | |
| 222 | - \OC::$server->getEventDispatcher()->dispatch('OC\Settings\Users::loadAdditionalScripts'); |
|
| 222 | + \OC::$server->getEventDispatcher()->dispatch('OC\Settings\Users::loadAdditionalScripts'); |
|
| 223 | 223 | |
| 224 | - /* TOTAL USERS COUNT */ |
|
| 224 | + /* TOTAL USERS COUNT */ |
|
| 225 | 225 | |
| 226 | - /* LANGUAGES */ |
|
| 227 | - $languages = $this->l10nFactory->getLanguages(); |
|
| 226 | + /* LANGUAGES */ |
|
| 227 | + $languages = $this->l10nFactory->getLanguages(); |
|
| 228 | 228 | |
| 229 | - /* FINAL DATA */ |
|
| 230 | - $serverData = array(); |
|
| 231 | - // groups |
|
| 232 | - $serverData['groups'] = array_merge_recursive($adminGroup, [$disabledUsersGroup], $groups); |
|
| 233 | - // Various data |
|
| 234 | - $serverData['isAdmin'] = $this->isAdmin; |
|
| 235 | - $serverData['sortGroups'] = $sortGroupsBy; |
|
| 236 | - $serverData['quotaPreset'] = $quotaPreset; |
|
| 237 | - $serverData['userCount'] = $userCount - $disabledUsers; |
|
| 238 | - $serverData['languages'] = $languages; |
|
| 239 | - $serverData['defaultLanguage'] = $this->config->getSystemValue('default_language', 'en'); |
|
| 240 | - // Settings |
|
| 241 | - $serverData['defaultQuota'] = $defaultQuota; |
|
| 242 | - $serverData['canChangePassword'] = $canChangePassword; |
|
| 229 | + /* FINAL DATA */ |
|
| 230 | + $serverData = array(); |
|
| 231 | + // groups |
|
| 232 | + $serverData['groups'] = array_merge_recursive($adminGroup, [$disabledUsersGroup], $groups); |
|
| 233 | + // Various data |
|
| 234 | + $serverData['isAdmin'] = $this->isAdmin; |
|
| 235 | + $serverData['sortGroups'] = $sortGroupsBy; |
|
| 236 | + $serverData['quotaPreset'] = $quotaPreset; |
|
| 237 | + $serverData['userCount'] = $userCount - $disabledUsers; |
|
| 238 | + $serverData['languages'] = $languages; |
|
| 239 | + $serverData['defaultLanguage'] = $this->config->getSystemValue('default_language', 'en'); |
|
| 240 | + // Settings |
|
| 241 | + $serverData['defaultQuota'] = $defaultQuota; |
|
| 242 | + $serverData['canChangePassword'] = $canChangePassword; |
|
| 243 | 243 | |
| 244 | - return new TemplateResponse('settings', 'settings', ['serverData' => $serverData]); |
|
| 245 | - } |
|
| 244 | + return new TemplateResponse('settings', 'settings', ['serverData' => $serverData]); |
|
| 245 | + } |
|
| 246 | 246 | |
| 247 | - /** |
|
| 248 | - * @NoAdminRequired |
|
| 249 | - * @NoSubadminRequired |
|
| 250 | - * @PasswordConfirmationRequired |
|
| 251 | - * |
|
| 252 | - * @param string $avatarScope |
|
| 253 | - * @param string $displayname |
|
| 254 | - * @param string $displaynameScope |
|
| 255 | - * @param string $phone |
|
| 256 | - * @param string $phoneScope |
|
| 257 | - * @param string $email |
|
| 258 | - * @param string $emailScope |
|
| 259 | - * @param string $website |
|
| 260 | - * @param string $websiteScope |
|
| 261 | - * @param string $address |
|
| 262 | - * @param string $addressScope |
|
| 263 | - * @param string $twitter |
|
| 264 | - * @param string $twitterScope |
|
| 265 | - * @return DataResponse |
|
| 266 | - */ |
|
| 267 | - public function setUserSettings($avatarScope, |
|
| 268 | - $displayname, |
|
| 269 | - $displaynameScope, |
|
| 270 | - $phone, |
|
| 271 | - $phoneScope, |
|
| 272 | - $email, |
|
| 273 | - $emailScope, |
|
| 274 | - $website, |
|
| 275 | - $websiteScope, |
|
| 276 | - $address, |
|
| 277 | - $addressScope, |
|
| 278 | - $twitter, |
|
| 279 | - $twitterScope |
|
| 280 | - ) { |
|
| 281 | - if (!empty($email) && !$this->mailer->validateMailAddress($email)) { |
|
| 282 | - return new DataResponse( |
|
| 283 | - [ |
|
| 284 | - 'status' => 'error', |
|
| 285 | - 'data' => [ |
|
| 286 | - 'message' => $this->l10n->t('Invalid mail address') |
|
| 287 | - ] |
|
| 288 | - ], |
|
| 289 | - Http::STATUS_UNPROCESSABLE_ENTITY |
|
| 290 | - ); |
|
| 291 | - } |
|
| 292 | - $user = $this->userSession->getUser(); |
|
| 293 | - $data = $this->accountManager->getUser($user); |
|
| 294 | - $data[AccountManager::PROPERTY_AVATAR] = ['scope' => $avatarScope]; |
|
| 295 | - if ($this->config->getSystemValue('allow_user_to_change_display_name', true) !== false) { |
|
| 296 | - $data[AccountManager::PROPERTY_DISPLAYNAME] = ['value' => $displayname, 'scope' => $displaynameScope]; |
|
| 297 | - $data[AccountManager::PROPERTY_EMAIL] = ['value' => $email, 'scope' => $emailScope]; |
|
| 298 | - } |
|
| 299 | - if ($this->appManager->isEnabledForUser('federatedfilesharing')) { |
|
| 300 | - $federatedFileSharing = new \OCA\FederatedFileSharing\AppInfo\Application(); |
|
| 301 | - $shareProvider = $federatedFileSharing->getFederatedShareProvider(); |
|
| 302 | - if ($shareProvider->isLookupServerUploadEnabled()) { |
|
| 303 | - $data[AccountManager::PROPERTY_WEBSITE] = ['value' => $website, 'scope' => $websiteScope]; |
|
| 304 | - $data[AccountManager::PROPERTY_ADDRESS] = ['value' => $address, 'scope' => $addressScope]; |
|
| 305 | - $data[AccountManager::PROPERTY_PHONE] = ['value' => $phone, 'scope' => $phoneScope]; |
|
| 306 | - $data[AccountManager::PROPERTY_TWITTER] = ['value' => $twitter, 'scope' => $twitterScope]; |
|
| 307 | - } |
|
| 308 | - } |
|
| 309 | - try { |
|
| 310 | - $this->saveUserSettings($user, $data); |
|
| 311 | - return new DataResponse( |
|
| 312 | - [ |
|
| 313 | - 'status' => 'success', |
|
| 314 | - 'data' => [ |
|
| 315 | - 'userId' => $user->getUID(), |
|
| 316 | - 'avatarScope' => $data[AccountManager::PROPERTY_AVATAR]['scope'], |
|
| 317 | - 'displayname' => $data[AccountManager::PROPERTY_DISPLAYNAME]['value'], |
|
| 318 | - 'displaynameScope' => $data[AccountManager::PROPERTY_DISPLAYNAME]['scope'], |
|
| 319 | - 'email' => $data[AccountManager::PROPERTY_EMAIL]['value'], |
|
| 320 | - 'emailScope' => $data[AccountManager::PROPERTY_EMAIL]['scope'], |
|
| 321 | - 'website' => $data[AccountManager::PROPERTY_WEBSITE]['value'], |
|
| 322 | - 'websiteScope' => $data[AccountManager::PROPERTY_WEBSITE]['scope'], |
|
| 323 | - 'address' => $data[AccountManager::PROPERTY_ADDRESS]['value'], |
|
| 324 | - 'addressScope' => $data[AccountManager::PROPERTY_ADDRESS]['scope'], |
|
| 325 | - 'message' => $this->l10n->t('Settings saved') |
|
| 326 | - ] |
|
| 327 | - ], |
|
| 328 | - Http::STATUS_OK |
|
| 329 | - ); |
|
| 330 | - } catch (ForbiddenException $e) { |
|
| 331 | - return new DataResponse([ |
|
| 332 | - 'status' => 'error', |
|
| 333 | - 'data' => [ |
|
| 334 | - 'message' => $e->getMessage() |
|
| 335 | - ], |
|
| 336 | - ]); |
|
| 337 | - } |
|
| 338 | - } |
|
| 339 | - /** |
|
| 340 | - * update account manager with new user data |
|
| 341 | - * |
|
| 342 | - * @param IUser $user |
|
| 343 | - * @param array $data |
|
| 344 | - * @throws ForbiddenException |
|
| 345 | - */ |
|
| 346 | - protected function saveUserSettings(IUser $user, array $data) { |
|
| 347 | - // keep the user back-end up-to-date with the latest display name and email |
|
| 348 | - // address |
|
| 349 | - $oldDisplayName = $user->getDisplayName(); |
|
| 350 | - $oldDisplayName = is_null($oldDisplayName) ? '' : $oldDisplayName; |
|
| 351 | - if (isset($data[AccountManager::PROPERTY_DISPLAYNAME]['value']) |
|
| 352 | - && $oldDisplayName !== $data[AccountManager::PROPERTY_DISPLAYNAME]['value'] |
|
| 353 | - ) { |
|
| 354 | - $result = $user->setDisplayName($data[AccountManager::PROPERTY_DISPLAYNAME]['value']); |
|
| 355 | - if ($result === false) { |
|
| 356 | - throw new ForbiddenException($this->l10n->t('Unable to change full name')); |
|
| 357 | - } |
|
| 358 | - } |
|
| 359 | - $oldEmailAddress = $user->getEMailAddress(); |
|
| 360 | - $oldEmailAddress = is_null($oldEmailAddress) ? '' : $oldEmailAddress; |
|
| 361 | - if (isset($data[AccountManager::PROPERTY_EMAIL]['value']) |
|
| 362 | - && $oldEmailAddress !== $data[AccountManager::PROPERTY_EMAIL]['value'] |
|
| 363 | - ) { |
|
| 364 | - // this is the only permission a backend provides and is also used |
|
| 365 | - // for the permission of setting a email address |
|
| 366 | - if (!$user->canChangeDisplayName()) { |
|
| 367 | - throw new ForbiddenException($this->l10n->t('Unable to change email address')); |
|
| 368 | - } |
|
| 369 | - $user->setEMailAddress($data[AccountManager::PROPERTY_EMAIL]['value']); |
|
| 370 | - } |
|
| 371 | - $this->accountManager->updateUser($user, $data); |
|
| 372 | - } |
|
| 247 | + /** |
|
| 248 | + * @NoAdminRequired |
|
| 249 | + * @NoSubadminRequired |
|
| 250 | + * @PasswordConfirmationRequired |
|
| 251 | + * |
|
| 252 | + * @param string $avatarScope |
|
| 253 | + * @param string $displayname |
|
| 254 | + * @param string $displaynameScope |
|
| 255 | + * @param string $phone |
|
| 256 | + * @param string $phoneScope |
|
| 257 | + * @param string $email |
|
| 258 | + * @param string $emailScope |
|
| 259 | + * @param string $website |
|
| 260 | + * @param string $websiteScope |
|
| 261 | + * @param string $address |
|
| 262 | + * @param string $addressScope |
|
| 263 | + * @param string $twitter |
|
| 264 | + * @param string $twitterScope |
|
| 265 | + * @return DataResponse |
|
| 266 | + */ |
|
| 267 | + public function setUserSettings($avatarScope, |
|
| 268 | + $displayname, |
|
| 269 | + $displaynameScope, |
|
| 270 | + $phone, |
|
| 271 | + $phoneScope, |
|
| 272 | + $email, |
|
| 273 | + $emailScope, |
|
| 274 | + $website, |
|
| 275 | + $websiteScope, |
|
| 276 | + $address, |
|
| 277 | + $addressScope, |
|
| 278 | + $twitter, |
|
| 279 | + $twitterScope |
|
| 280 | + ) { |
|
| 281 | + if (!empty($email) && !$this->mailer->validateMailAddress($email)) { |
|
| 282 | + return new DataResponse( |
|
| 283 | + [ |
|
| 284 | + 'status' => 'error', |
|
| 285 | + 'data' => [ |
|
| 286 | + 'message' => $this->l10n->t('Invalid mail address') |
|
| 287 | + ] |
|
| 288 | + ], |
|
| 289 | + Http::STATUS_UNPROCESSABLE_ENTITY |
|
| 290 | + ); |
|
| 291 | + } |
|
| 292 | + $user = $this->userSession->getUser(); |
|
| 293 | + $data = $this->accountManager->getUser($user); |
|
| 294 | + $data[AccountManager::PROPERTY_AVATAR] = ['scope' => $avatarScope]; |
|
| 295 | + if ($this->config->getSystemValue('allow_user_to_change_display_name', true) !== false) { |
|
| 296 | + $data[AccountManager::PROPERTY_DISPLAYNAME] = ['value' => $displayname, 'scope' => $displaynameScope]; |
|
| 297 | + $data[AccountManager::PROPERTY_EMAIL] = ['value' => $email, 'scope' => $emailScope]; |
|
| 298 | + } |
|
| 299 | + if ($this->appManager->isEnabledForUser('federatedfilesharing')) { |
|
| 300 | + $federatedFileSharing = new \OCA\FederatedFileSharing\AppInfo\Application(); |
|
| 301 | + $shareProvider = $federatedFileSharing->getFederatedShareProvider(); |
|
| 302 | + if ($shareProvider->isLookupServerUploadEnabled()) { |
|
| 303 | + $data[AccountManager::PROPERTY_WEBSITE] = ['value' => $website, 'scope' => $websiteScope]; |
|
| 304 | + $data[AccountManager::PROPERTY_ADDRESS] = ['value' => $address, 'scope' => $addressScope]; |
|
| 305 | + $data[AccountManager::PROPERTY_PHONE] = ['value' => $phone, 'scope' => $phoneScope]; |
|
| 306 | + $data[AccountManager::PROPERTY_TWITTER] = ['value' => $twitter, 'scope' => $twitterScope]; |
|
| 307 | + } |
|
| 308 | + } |
|
| 309 | + try { |
|
| 310 | + $this->saveUserSettings($user, $data); |
|
| 311 | + return new DataResponse( |
|
| 312 | + [ |
|
| 313 | + 'status' => 'success', |
|
| 314 | + 'data' => [ |
|
| 315 | + 'userId' => $user->getUID(), |
|
| 316 | + 'avatarScope' => $data[AccountManager::PROPERTY_AVATAR]['scope'], |
|
| 317 | + 'displayname' => $data[AccountManager::PROPERTY_DISPLAYNAME]['value'], |
|
| 318 | + 'displaynameScope' => $data[AccountManager::PROPERTY_DISPLAYNAME]['scope'], |
|
| 319 | + 'email' => $data[AccountManager::PROPERTY_EMAIL]['value'], |
|
| 320 | + 'emailScope' => $data[AccountManager::PROPERTY_EMAIL]['scope'], |
|
| 321 | + 'website' => $data[AccountManager::PROPERTY_WEBSITE]['value'], |
|
| 322 | + 'websiteScope' => $data[AccountManager::PROPERTY_WEBSITE]['scope'], |
|
| 323 | + 'address' => $data[AccountManager::PROPERTY_ADDRESS]['value'], |
|
| 324 | + 'addressScope' => $data[AccountManager::PROPERTY_ADDRESS]['scope'], |
|
| 325 | + 'message' => $this->l10n->t('Settings saved') |
|
| 326 | + ] |
|
| 327 | + ], |
|
| 328 | + Http::STATUS_OK |
|
| 329 | + ); |
|
| 330 | + } catch (ForbiddenException $e) { |
|
| 331 | + return new DataResponse([ |
|
| 332 | + 'status' => 'error', |
|
| 333 | + 'data' => [ |
|
| 334 | + 'message' => $e->getMessage() |
|
| 335 | + ], |
|
| 336 | + ]); |
|
| 337 | + } |
|
| 338 | + } |
|
| 339 | + /** |
|
| 340 | + * update account manager with new user data |
|
| 341 | + * |
|
| 342 | + * @param IUser $user |
|
| 343 | + * @param array $data |
|
| 344 | + * @throws ForbiddenException |
|
| 345 | + */ |
|
| 346 | + protected function saveUserSettings(IUser $user, array $data) { |
|
| 347 | + // keep the user back-end up-to-date with the latest display name and email |
|
| 348 | + // address |
|
| 349 | + $oldDisplayName = $user->getDisplayName(); |
|
| 350 | + $oldDisplayName = is_null($oldDisplayName) ? '' : $oldDisplayName; |
|
| 351 | + if (isset($data[AccountManager::PROPERTY_DISPLAYNAME]['value']) |
|
| 352 | + && $oldDisplayName !== $data[AccountManager::PROPERTY_DISPLAYNAME]['value'] |
|
| 353 | + ) { |
|
| 354 | + $result = $user->setDisplayName($data[AccountManager::PROPERTY_DISPLAYNAME]['value']); |
|
| 355 | + if ($result === false) { |
|
| 356 | + throw new ForbiddenException($this->l10n->t('Unable to change full name')); |
|
| 357 | + } |
|
| 358 | + } |
|
| 359 | + $oldEmailAddress = $user->getEMailAddress(); |
|
| 360 | + $oldEmailAddress = is_null($oldEmailAddress) ? '' : $oldEmailAddress; |
|
| 361 | + if (isset($data[AccountManager::PROPERTY_EMAIL]['value']) |
|
| 362 | + && $oldEmailAddress !== $data[AccountManager::PROPERTY_EMAIL]['value'] |
|
| 363 | + ) { |
|
| 364 | + // this is the only permission a backend provides and is also used |
|
| 365 | + // for the permission of setting a email address |
|
| 366 | + if (!$user->canChangeDisplayName()) { |
|
| 367 | + throw new ForbiddenException($this->l10n->t('Unable to change email address')); |
|
| 368 | + } |
|
| 369 | + $user->setEMailAddress($data[AccountManager::PROPERTY_EMAIL]['value']); |
|
| 370 | + } |
|
| 371 | + $this->accountManager->updateUser($user, $data); |
|
| 372 | + } |
|
| 373 | 373 | |
| 374 | - /** |
|
| 375 | - * Set the mail address of a user |
|
| 376 | - * |
|
| 377 | - * @NoAdminRequired |
|
| 378 | - * @NoSubadminRequired |
|
| 379 | - * @PasswordConfirmationRequired |
|
| 380 | - * |
|
| 381 | - * @param string $account |
|
| 382 | - * @param bool $onlyVerificationCode only return verification code without updating the data |
|
| 383 | - * @return DataResponse |
|
| 384 | - */ |
|
| 385 | - public function getVerificationCode(string $account, bool $onlyVerificationCode): DataResponse { |
|
| 374 | + /** |
|
| 375 | + * Set the mail address of a user |
|
| 376 | + * |
|
| 377 | + * @NoAdminRequired |
|
| 378 | + * @NoSubadminRequired |
|
| 379 | + * @PasswordConfirmationRequired |
|
| 380 | + * |
|
| 381 | + * @param string $account |
|
| 382 | + * @param bool $onlyVerificationCode only return verification code without updating the data |
|
| 383 | + * @return DataResponse |
|
| 384 | + */ |
|
| 385 | + public function getVerificationCode(string $account, bool $onlyVerificationCode): DataResponse { |
|
| 386 | 386 | |
| 387 | - $user = $this->userSession->getUser(); |
|
| 387 | + $user = $this->userSession->getUser(); |
|
| 388 | 388 | |
| 389 | - if ($user === null) { |
|
| 390 | - return new DataResponse([], Http::STATUS_BAD_REQUEST); |
|
| 391 | - } |
|
| 389 | + if ($user === null) { |
|
| 390 | + return new DataResponse([], Http::STATUS_BAD_REQUEST); |
|
| 391 | + } |
|
| 392 | 392 | |
| 393 | - $accountData = $this->accountManager->getUser($user); |
|
| 394 | - $cloudId = $user->getCloudId(); |
|
| 395 | - $message = 'Use my Federated Cloud ID to share with me: ' . $cloudId; |
|
| 396 | - $signature = $this->signMessage($user, $message); |
|
| 393 | + $accountData = $this->accountManager->getUser($user); |
|
| 394 | + $cloudId = $user->getCloudId(); |
|
| 395 | + $message = 'Use my Federated Cloud ID to share with me: ' . $cloudId; |
|
| 396 | + $signature = $this->signMessage($user, $message); |
|
| 397 | 397 | |
| 398 | - $code = $message . ' ' . $signature; |
|
| 399 | - $codeMd5 = $message . ' ' . md5($signature); |
|
| 398 | + $code = $message . ' ' . $signature; |
|
| 399 | + $codeMd5 = $message . ' ' . md5($signature); |
|
| 400 | 400 | |
| 401 | - switch ($account) { |
|
| 402 | - case 'verify-twitter': |
|
| 403 | - $accountData[AccountManager::PROPERTY_TWITTER]['verified'] = AccountManager::VERIFICATION_IN_PROGRESS; |
|
| 404 | - $msg = $this->l10n->t('In order to verify your Twitter account, post the following tweet on Twitter (please make sure to post it without any line breaks):'); |
|
| 405 | - $code = $codeMd5; |
|
| 406 | - $type = AccountManager::PROPERTY_TWITTER; |
|
| 407 | - $data = $accountData[AccountManager::PROPERTY_TWITTER]['value']; |
|
| 408 | - $accountData[AccountManager::PROPERTY_TWITTER]['signature'] = $signature; |
|
| 409 | - break; |
|
| 410 | - case 'verify-website': |
|
| 411 | - $accountData[AccountManager::PROPERTY_WEBSITE]['verified'] = AccountManager::VERIFICATION_IN_PROGRESS; |
|
| 412 | - $msg = $this->l10n->t('In order to verify your Website, store the following content in your web-root at \'.well-known/CloudIdVerificationCode.txt\' (please make sure that the complete text is in one line):'); |
|
| 413 | - $type = AccountManager::PROPERTY_WEBSITE; |
|
| 414 | - $data = $accountData[AccountManager::PROPERTY_WEBSITE]['value']; |
|
| 415 | - $accountData[AccountManager::PROPERTY_WEBSITE]['signature'] = $signature; |
|
| 416 | - break; |
|
| 417 | - default: |
|
| 418 | - return new DataResponse([], Http::STATUS_BAD_REQUEST); |
|
| 419 | - } |
|
| 401 | + switch ($account) { |
|
| 402 | + case 'verify-twitter': |
|
| 403 | + $accountData[AccountManager::PROPERTY_TWITTER]['verified'] = AccountManager::VERIFICATION_IN_PROGRESS; |
|
| 404 | + $msg = $this->l10n->t('In order to verify your Twitter account, post the following tweet on Twitter (please make sure to post it without any line breaks):'); |
|
| 405 | + $code = $codeMd5; |
|
| 406 | + $type = AccountManager::PROPERTY_TWITTER; |
|
| 407 | + $data = $accountData[AccountManager::PROPERTY_TWITTER]['value']; |
|
| 408 | + $accountData[AccountManager::PROPERTY_TWITTER]['signature'] = $signature; |
|
| 409 | + break; |
|
| 410 | + case 'verify-website': |
|
| 411 | + $accountData[AccountManager::PROPERTY_WEBSITE]['verified'] = AccountManager::VERIFICATION_IN_PROGRESS; |
|
| 412 | + $msg = $this->l10n->t('In order to verify your Website, store the following content in your web-root at \'.well-known/CloudIdVerificationCode.txt\' (please make sure that the complete text is in one line):'); |
|
| 413 | + $type = AccountManager::PROPERTY_WEBSITE; |
|
| 414 | + $data = $accountData[AccountManager::PROPERTY_WEBSITE]['value']; |
|
| 415 | + $accountData[AccountManager::PROPERTY_WEBSITE]['signature'] = $signature; |
|
| 416 | + break; |
|
| 417 | + default: |
|
| 418 | + return new DataResponse([], Http::STATUS_BAD_REQUEST); |
|
| 419 | + } |
|
| 420 | 420 | |
| 421 | - if ($onlyVerificationCode === false) { |
|
| 422 | - $this->accountManager->updateUser($user, $accountData); |
|
| 421 | + if ($onlyVerificationCode === false) { |
|
| 422 | + $this->accountManager->updateUser($user, $accountData); |
|
| 423 | 423 | |
| 424 | - $this->jobList->add(VerifyUserData::class, |
|
| 425 | - [ |
|
| 426 | - 'verificationCode' => $code, |
|
| 427 | - 'data' => $data, |
|
| 428 | - 'type' => $type, |
|
| 429 | - 'uid' => $user->getUID(), |
|
| 430 | - 'try' => 0, |
|
| 431 | - 'lastRun' => $this->getCurrentTime() |
|
| 432 | - ] |
|
| 433 | - ); |
|
| 434 | - } |
|
| 424 | + $this->jobList->add(VerifyUserData::class, |
|
| 425 | + [ |
|
| 426 | + 'verificationCode' => $code, |
|
| 427 | + 'data' => $data, |
|
| 428 | + 'type' => $type, |
|
| 429 | + 'uid' => $user->getUID(), |
|
| 430 | + 'try' => 0, |
|
| 431 | + 'lastRun' => $this->getCurrentTime() |
|
| 432 | + ] |
|
| 433 | + ); |
|
| 434 | + } |
|
| 435 | 435 | |
| 436 | - return new DataResponse(['msg' => $msg, 'code' => $code]); |
|
| 437 | - } |
|
| 436 | + return new DataResponse(['msg' => $msg, 'code' => $code]); |
|
| 437 | + } |
|
| 438 | 438 | |
| 439 | - /** |
|
| 440 | - * get current timestamp |
|
| 441 | - * |
|
| 442 | - * @return int |
|
| 443 | - */ |
|
| 444 | - protected function getCurrentTime(): int { |
|
| 445 | - return time(); |
|
| 446 | - } |
|
| 439 | + /** |
|
| 440 | + * get current timestamp |
|
| 441 | + * |
|
| 442 | + * @return int |
|
| 443 | + */ |
|
| 444 | + protected function getCurrentTime(): int { |
|
| 445 | + return time(); |
|
| 446 | + } |
|
| 447 | 447 | |
| 448 | - /** |
|
| 449 | - * sign message with users private key |
|
| 450 | - * |
|
| 451 | - * @param IUser $user |
|
| 452 | - * @param string $message |
|
| 453 | - * |
|
| 454 | - * @return string base64 encoded signature |
|
| 455 | - */ |
|
| 456 | - protected function signMessage(IUser $user, string $message): string { |
|
| 457 | - $privateKey = $this->keyManager->getKey($user)->getPrivate(); |
|
| 458 | - openssl_sign(json_encode($message), $signature, $privateKey, OPENSSL_ALGO_SHA512); |
|
| 459 | - return base64_encode($signature); |
|
| 460 | - } |
|
| 448 | + /** |
|
| 449 | + * sign message with users private key |
|
| 450 | + * |
|
| 451 | + * @param IUser $user |
|
| 452 | + * @param string $message |
|
| 453 | + * |
|
| 454 | + * @return string base64 encoded signature |
|
| 455 | + */ |
|
| 456 | + protected function signMessage(IUser $user, string $message): string { |
|
| 457 | + $privateKey = $this->keyManager->getKey($user)->getPrivate(); |
|
| 458 | + openssl_sign(json_encode($message), $signature, $privateKey, OPENSSL_ALGO_SHA512); |
|
| 459 | + return base64_encode($signature); |
|
| 460 | + } |
|
| 461 | 461 | } |
@@ -190,15 +190,15 @@ discard block |
||
| 190 | 190 | if ($this->isAdmin) { |
| 191 | 191 | $disabledUsers = $isLDAPUsed ? 0 : $this->userManager->countDisabledUsers(); |
| 192 | 192 | $userCount = array_reduce($this->userManager->countUsers(), function($v, $w) { |
| 193 | - return $v + (int)$w; |
|
| 193 | + return $v + (int) $w; |
|
| 194 | 194 | }, 0); |
| 195 | 195 | } else { |
| 196 | 196 | // User is subadmin ! |
| 197 | 197 | // Map group list to names to retrieve the countDisabledUsersOfGroups |
| 198 | 198 | $userCount = 0; |
| 199 | 199 | $groupsNames = []; |
| 200 | - foreach($groups as $group) { |
|
| 201 | - $userCount += (int)['usercount']; |
|
| 200 | + foreach ($groups as $group) { |
|
| 201 | + $userCount += (int) ['usercount']; |
|
| 202 | 202 | $groupsName[] = ['name']; |
| 203 | 203 | }; |
| 204 | 204 | $disabledUsers = $isLDAPUsed ? 0 : $this->userManager->countDisabledUsersOfGroups($groupsNames); |
@@ -392,11 +392,11 @@ discard block |
||
| 392 | 392 | |
| 393 | 393 | $accountData = $this->accountManager->getUser($user); |
| 394 | 394 | $cloudId = $user->getCloudId(); |
| 395 | - $message = 'Use my Federated Cloud ID to share with me: ' . $cloudId; |
|
| 395 | + $message = 'Use my Federated Cloud ID to share with me: '.$cloudId; |
|
| 396 | 396 | $signature = $this->signMessage($user, $message); |
| 397 | 397 | |
| 398 | - $code = $message . ' ' . $signature; |
|
| 399 | - $codeMd5 = $message . ' ' . md5($signature); |
|
| 398 | + $code = $message.' '.$signature; |
|
| 399 | + $codeMd5 = $message.' '.md5($signature); |
|
| 400 | 400 | |
| 401 | 401 | switch ($account) { |
| 402 | 402 | case 'verify-twitter': |