@@ -56,362 +56,362 @@ |
||
| 56 | 56 | * Class for group management in a SQL Database (e.g. MySQL, SQLite) |
| 57 | 57 | */ |
| 58 | 58 | class Database extends ABackend |
| 59 | - implements IAddToGroupBackend, |
|
| 60 | - ICountDisabledInGroup, |
|
| 61 | - ICountUsersBackend, |
|
| 62 | - ICreateGroupBackend, |
|
| 63 | - IDeleteGroupBackend, |
|
| 64 | - IRemoveFromGroupBackend { |
|
| 65 | - |
|
| 66 | - /** @var string[] */ |
|
| 67 | - private $groupCache = []; |
|
| 68 | - |
|
| 69 | - /** @var IDBConnection */ |
|
| 70 | - private $dbConn; |
|
| 71 | - |
|
| 72 | - /** |
|
| 73 | - * \OC\Group\Database constructor. |
|
| 74 | - * |
|
| 75 | - * @param IDBConnection|null $dbConn |
|
| 76 | - */ |
|
| 77 | - public function __construct(IDBConnection $dbConn = null) { |
|
| 78 | - $this->dbConn = $dbConn; |
|
| 79 | - } |
|
| 80 | - |
|
| 81 | - /** |
|
| 82 | - * FIXME: This function should not be required! |
|
| 83 | - */ |
|
| 84 | - private function fixDI() { |
|
| 85 | - if ($this->dbConn === null) { |
|
| 86 | - $this->dbConn = \OC::$server->getDatabaseConnection(); |
|
| 87 | - } |
|
| 88 | - } |
|
| 89 | - |
|
| 90 | - /** |
|
| 91 | - * Try to create a new group |
|
| 92 | - * @param string $gid The name of the group to create |
|
| 93 | - * @return bool |
|
| 94 | - * |
|
| 95 | - * Tries to create a new group. If the group name already exists, false will |
|
| 96 | - * be returned. |
|
| 97 | - */ |
|
| 98 | - public function createGroup(string $gid): bool { |
|
| 99 | - $this->fixDI(); |
|
| 100 | - |
|
| 101 | - try { |
|
| 102 | - // Add group |
|
| 103 | - $builder = $this->dbConn->getQueryBuilder(); |
|
| 104 | - $result = $builder->insert('groups') |
|
| 105 | - ->setValue('gid', $builder->createNamedParameter($gid)) |
|
| 106 | - ->execute(); |
|
| 107 | - } catch(UniqueConstraintViolationException $e) { |
|
| 108 | - $result = 0; |
|
| 109 | - } |
|
| 110 | - |
|
| 111 | - // Add to cache |
|
| 112 | - $this->groupCache[$gid] = $gid; |
|
| 113 | - |
|
| 114 | - return $result === 1; |
|
| 115 | - } |
|
| 116 | - |
|
| 117 | - /** |
|
| 118 | - * delete a group |
|
| 119 | - * @param string $gid gid of the group to delete |
|
| 120 | - * @return bool |
|
| 121 | - * |
|
| 122 | - * Deletes a group and removes it from the group_user-table |
|
| 123 | - */ |
|
| 124 | - public function deleteGroup(string $gid): bool { |
|
| 125 | - $this->fixDI(); |
|
| 126 | - |
|
| 127 | - // Delete the group |
|
| 128 | - $qb = $this->dbConn->getQueryBuilder(); |
|
| 129 | - $qb->delete('groups') |
|
| 130 | - ->where($qb->expr()->eq('gid', $qb->createNamedParameter($gid))) |
|
| 131 | - ->execute(); |
|
| 132 | - |
|
| 133 | - // Delete the group-user relation |
|
| 134 | - $qb = $this->dbConn->getQueryBuilder(); |
|
| 135 | - $qb->delete('group_user') |
|
| 136 | - ->where($qb->expr()->eq('gid', $qb->createNamedParameter($gid))) |
|
| 137 | - ->execute(); |
|
| 138 | - |
|
| 139 | - // Delete the group-groupadmin relation |
|
| 140 | - $qb = $this->dbConn->getQueryBuilder(); |
|
| 141 | - $qb->delete('group_admin') |
|
| 142 | - ->where($qb->expr()->eq('gid', $qb->createNamedParameter($gid))) |
|
| 143 | - ->execute(); |
|
| 144 | - |
|
| 145 | - // Delete from cache |
|
| 146 | - unset($this->groupCache[$gid]); |
|
| 147 | - |
|
| 148 | - return true; |
|
| 149 | - } |
|
| 150 | - |
|
| 151 | - /** |
|
| 152 | - * is user in group? |
|
| 153 | - * @param string $uid uid of the user |
|
| 154 | - * @param string $gid gid of the group |
|
| 155 | - * @return bool |
|
| 156 | - * |
|
| 157 | - * Checks whether the user is member of a group or not. |
|
| 158 | - */ |
|
| 159 | - public function inGroup( $uid, $gid ) { |
|
| 160 | - $this->fixDI(); |
|
| 161 | - |
|
| 162 | - // check |
|
| 163 | - $qb = $this->dbConn->getQueryBuilder(); |
|
| 164 | - $cursor = $qb->select('uid') |
|
| 165 | - ->from('group_user') |
|
| 166 | - ->where($qb->expr()->eq('gid', $qb->createNamedParameter($gid))) |
|
| 167 | - ->andWhere($qb->expr()->eq('uid', $qb->createNamedParameter($uid))) |
|
| 168 | - ->execute(); |
|
| 169 | - |
|
| 170 | - $result = $cursor->fetch(); |
|
| 171 | - $cursor->closeCursor(); |
|
| 172 | - |
|
| 173 | - return $result ? true : false; |
|
| 174 | - } |
|
| 175 | - |
|
| 176 | - /** |
|
| 177 | - * Add a user to a group |
|
| 178 | - * @param string $uid Name of the user to add to group |
|
| 179 | - * @param string $gid Name of the group in which add the user |
|
| 180 | - * @return bool |
|
| 181 | - * |
|
| 182 | - * Adds a user to a group. |
|
| 183 | - */ |
|
| 184 | - public function addToGroup(string $uid, string $gid): bool { |
|
| 185 | - $this->fixDI(); |
|
| 186 | - |
|
| 187 | - // No duplicate entries! |
|
| 188 | - if( !$this->inGroup( $uid, $gid )) { |
|
| 189 | - $qb = $this->dbConn->getQueryBuilder(); |
|
| 190 | - $qb->insert('group_user') |
|
| 191 | - ->setValue('uid', $qb->createNamedParameter($uid)) |
|
| 192 | - ->setValue('gid', $qb->createNamedParameter($gid)) |
|
| 193 | - ->execute(); |
|
| 194 | - return true; |
|
| 195 | - }else{ |
|
| 196 | - return false; |
|
| 197 | - } |
|
| 198 | - } |
|
| 199 | - |
|
| 200 | - /** |
|
| 201 | - * Removes a user from a group |
|
| 202 | - * @param string $uid Name of the user to remove from group |
|
| 203 | - * @param string $gid Name of the group from which remove the user |
|
| 204 | - * @return bool |
|
| 205 | - * |
|
| 206 | - * removes the user from a group. |
|
| 207 | - */ |
|
| 208 | - public function removeFromGroup(string $uid, string $gid): bool { |
|
| 209 | - $this->fixDI(); |
|
| 210 | - |
|
| 211 | - $qb = $this->dbConn->getQueryBuilder(); |
|
| 212 | - $qb->delete('group_user') |
|
| 213 | - ->where($qb->expr()->eq('uid', $qb->createNamedParameter($uid))) |
|
| 214 | - ->andWhere($qb->expr()->eq('gid', $qb->createNamedParameter($gid))) |
|
| 215 | - ->execute(); |
|
| 216 | - |
|
| 217 | - return true; |
|
| 218 | - } |
|
| 219 | - |
|
| 220 | - /** |
|
| 221 | - * Get all groups a user belongs to |
|
| 222 | - * @param string $uid Name of the user |
|
| 223 | - * @return array an array of group names |
|
| 224 | - * |
|
| 225 | - * This function fetches all groups a user belongs to. It does not check |
|
| 226 | - * if the user exists at all. |
|
| 227 | - */ |
|
| 228 | - public function getUserGroups( $uid ) { |
|
| 229 | - //guests has empty or null $uid |
|
| 230 | - if ($uid === null || $uid === '') { |
|
| 231 | - return []; |
|
| 232 | - } |
|
| 233 | - |
|
| 234 | - $this->fixDI(); |
|
| 235 | - |
|
| 236 | - // No magic! |
|
| 237 | - $qb = $this->dbConn->getQueryBuilder(); |
|
| 238 | - $cursor = $qb->select('gid') |
|
| 239 | - ->from('group_user') |
|
| 240 | - ->where($qb->expr()->eq('uid', $qb->createNamedParameter($uid))) |
|
| 241 | - ->execute(); |
|
| 242 | - |
|
| 243 | - $groups = []; |
|
| 244 | - while( $row = $cursor->fetch()) { |
|
| 245 | - $groups[] = $row['gid']; |
|
| 246 | - $this->groupCache[$row['gid']] = $row['gid']; |
|
| 247 | - } |
|
| 248 | - $cursor->closeCursor(); |
|
| 249 | - |
|
| 250 | - return $groups; |
|
| 251 | - } |
|
| 252 | - |
|
| 253 | - /** |
|
| 254 | - * get a list of all groups |
|
| 255 | - * @param string $search |
|
| 256 | - * @param int $limit |
|
| 257 | - * @param int $offset |
|
| 258 | - * @return array an array of group names |
|
| 259 | - * |
|
| 260 | - * Returns a list with all groups |
|
| 261 | - */ |
|
| 262 | - public function getGroups($search = '', $limit = null, $offset = null) { |
|
| 263 | - $this->fixDI(); |
|
| 264 | - |
|
| 265 | - $query = $this->dbConn->getQueryBuilder(); |
|
| 266 | - $query->select('gid') |
|
| 267 | - ->from('groups') |
|
| 268 | - ->orderBy('gid', 'ASC'); |
|
| 269 | - |
|
| 270 | - if ($search !== '') { |
|
| 271 | - $query->where($query->expr()->iLike('gid', $query->createNamedParameter( |
|
| 272 | - '%' . $this->dbConn->escapeLikeParameter($search) . '%' |
|
| 273 | - ))); |
|
| 274 | - } |
|
| 275 | - |
|
| 276 | - $query->setMaxResults($limit) |
|
| 277 | - ->setFirstResult($offset); |
|
| 278 | - $result = $query->execute(); |
|
| 279 | - |
|
| 280 | - $groups = []; |
|
| 281 | - while ($row = $result->fetch()) { |
|
| 282 | - $groups[] = $row['gid']; |
|
| 283 | - } |
|
| 284 | - $result->closeCursor(); |
|
| 285 | - |
|
| 286 | - return $groups; |
|
| 287 | - } |
|
| 288 | - |
|
| 289 | - /** |
|
| 290 | - * check if a group exists |
|
| 291 | - * @param string $gid |
|
| 292 | - * @return bool |
|
| 293 | - */ |
|
| 294 | - public function groupExists($gid) { |
|
| 295 | - $this->fixDI(); |
|
| 296 | - |
|
| 297 | - // Check cache first |
|
| 298 | - if (isset($this->groupCache[$gid])) { |
|
| 299 | - return true; |
|
| 300 | - } |
|
| 301 | - |
|
| 302 | - $qb = $this->dbConn->getQueryBuilder(); |
|
| 303 | - $cursor = $qb->select('gid') |
|
| 304 | - ->from('groups') |
|
| 305 | - ->where($qb->expr()->eq('gid', $qb->createNamedParameter($gid))) |
|
| 306 | - ->execute(); |
|
| 307 | - $result = $cursor->fetch(); |
|
| 308 | - $cursor->closeCursor(); |
|
| 309 | - |
|
| 310 | - if ($result !== false) { |
|
| 311 | - $this->groupCache[$gid] = $gid; |
|
| 312 | - return true; |
|
| 313 | - } |
|
| 314 | - return false; |
|
| 315 | - } |
|
| 316 | - |
|
| 317 | - /** |
|
| 318 | - * get a list of all users in a group |
|
| 319 | - * @param string $gid |
|
| 320 | - * @param string $search |
|
| 321 | - * @param int $limit |
|
| 322 | - * @param int $offset |
|
| 323 | - * @return array an array of user ids |
|
| 324 | - */ |
|
| 325 | - public function usersInGroup($gid, $search = '', $limit = null, $offset = null) { |
|
| 326 | - $this->fixDI(); |
|
| 327 | - |
|
| 328 | - $query = $this->dbConn->getQueryBuilder(); |
|
| 329 | - $query->select('uid') |
|
| 330 | - ->from('group_user') |
|
| 331 | - ->where($query->expr()->eq('gid', $query->createNamedParameter($gid))) |
|
| 332 | - ->orderBy('uid', 'ASC'); |
|
| 333 | - |
|
| 334 | - if ($search !== '') { |
|
| 335 | - $query->andWhere($query->expr()->like('uid', $query->createNamedParameter( |
|
| 336 | - '%' . $this->dbConn->escapeLikeParameter($search) . '%' |
|
| 337 | - ))); |
|
| 338 | - } |
|
| 339 | - |
|
| 340 | - $query->setMaxResults($limit) |
|
| 341 | - ->setFirstResult($offset); |
|
| 342 | - $result = $query->execute(); |
|
| 343 | - |
|
| 344 | - $users = []; |
|
| 345 | - while ($row = $result->fetch()) { |
|
| 346 | - $users[] = $row['uid']; |
|
| 347 | - } |
|
| 348 | - $result->closeCursor(); |
|
| 349 | - |
|
| 350 | - return $users; |
|
| 351 | - } |
|
| 352 | - |
|
| 353 | - /** |
|
| 354 | - * get the number of all users matching the search string in a group |
|
| 355 | - * @param string $gid |
|
| 356 | - * @param string $search |
|
| 357 | - * @return int |
|
| 358 | - */ |
|
| 359 | - public function countUsersInGroup(string $gid, string $search = ''): int { |
|
| 360 | - $this->fixDI(); |
|
| 361 | - |
|
| 362 | - $query = $this->dbConn->getQueryBuilder(); |
|
| 363 | - $query->select($query->func()->count('*', 'num_users')) |
|
| 364 | - ->from('group_user') |
|
| 365 | - ->where($query->expr()->eq('gid', $query->createNamedParameter($gid))); |
|
| 366 | - |
|
| 367 | - if ($search !== '') { |
|
| 368 | - $query->andWhere($query->expr()->like('uid', $query->createNamedParameter( |
|
| 369 | - '%' . $this->dbConn->escapeLikeParameter($search) . '%' |
|
| 370 | - ))); |
|
| 371 | - } |
|
| 372 | - |
|
| 373 | - $result = $query->execute(); |
|
| 374 | - $count = $result->fetchColumn(); |
|
| 375 | - $result->closeCursor(); |
|
| 376 | - |
|
| 377 | - if ($count !== false) { |
|
| 378 | - $count = (int)$count; |
|
| 379 | - } else { |
|
| 380 | - $count = 0; |
|
| 381 | - } |
|
| 382 | - |
|
| 383 | - return $count; |
|
| 384 | - } |
|
| 385 | - |
|
| 386 | - /** |
|
| 387 | - * get the number of disabled users in a group |
|
| 388 | - * |
|
| 389 | - * @param string $search |
|
| 390 | - * @return int|bool |
|
| 391 | - */ |
|
| 392 | - public function countDisabledInGroup(string $gid): int { |
|
| 393 | - $this->fixDI(); |
|
| 59 | + implements IAddToGroupBackend, |
|
| 60 | + ICountDisabledInGroup, |
|
| 61 | + ICountUsersBackend, |
|
| 62 | + ICreateGroupBackend, |
|
| 63 | + IDeleteGroupBackend, |
|
| 64 | + IRemoveFromGroupBackend { |
|
| 65 | + |
|
| 66 | + /** @var string[] */ |
|
| 67 | + private $groupCache = []; |
|
| 68 | + |
|
| 69 | + /** @var IDBConnection */ |
|
| 70 | + private $dbConn; |
|
| 71 | + |
|
| 72 | + /** |
|
| 73 | + * \OC\Group\Database constructor. |
|
| 74 | + * |
|
| 75 | + * @param IDBConnection|null $dbConn |
|
| 76 | + */ |
|
| 77 | + public function __construct(IDBConnection $dbConn = null) { |
|
| 78 | + $this->dbConn = $dbConn; |
|
| 79 | + } |
|
| 80 | + |
|
| 81 | + /** |
|
| 82 | + * FIXME: This function should not be required! |
|
| 83 | + */ |
|
| 84 | + private function fixDI() { |
|
| 85 | + if ($this->dbConn === null) { |
|
| 86 | + $this->dbConn = \OC::$server->getDatabaseConnection(); |
|
| 87 | + } |
|
| 88 | + } |
|
| 89 | + |
|
| 90 | + /** |
|
| 91 | + * Try to create a new group |
|
| 92 | + * @param string $gid The name of the group to create |
|
| 93 | + * @return bool |
|
| 94 | + * |
|
| 95 | + * Tries to create a new group. If the group name already exists, false will |
|
| 96 | + * be returned. |
|
| 97 | + */ |
|
| 98 | + public function createGroup(string $gid): bool { |
|
| 99 | + $this->fixDI(); |
|
| 100 | + |
|
| 101 | + try { |
|
| 102 | + // Add group |
|
| 103 | + $builder = $this->dbConn->getQueryBuilder(); |
|
| 104 | + $result = $builder->insert('groups') |
|
| 105 | + ->setValue('gid', $builder->createNamedParameter($gid)) |
|
| 106 | + ->execute(); |
|
| 107 | + } catch(UniqueConstraintViolationException $e) { |
|
| 108 | + $result = 0; |
|
| 109 | + } |
|
| 110 | + |
|
| 111 | + // Add to cache |
|
| 112 | + $this->groupCache[$gid] = $gid; |
|
| 113 | + |
|
| 114 | + return $result === 1; |
|
| 115 | + } |
|
| 116 | + |
|
| 117 | + /** |
|
| 118 | + * delete a group |
|
| 119 | + * @param string $gid gid of the group to delete |
|
| 120 | + * @return bool |
|
| 121 | + * |
|
| 122 | + * Deletes a group and removes it from the group_user-table |
|
| 123 | + */ |
|
| 124 | + public function deleteGroup(string $gid): bool { |
|
| 125 | + $this->fixDI(); |
|
| 126 | + |
|
| 127 | + // Delete the group |
|
| 128 | + $qb = $this->dbConn->getQueryBuilder(); |
|
| 129 | + $qb->delete('groups') |
|
| 130 | + ->where($qb->expr()->eq('gid', $qb->createNamedParameter($gid))) |
|
| 131 | + ->execute(); |
|
| 132 | + |
|
| 133 | + // Delete the group-user relation |
|
| 134 | + $qb = $this->dbConn->getQueryBuilder(); |
|
| 135 | + $qb->delete('group_user') |
|
| 136 | + ->where($qb->expr()->eq('gid', $qb->createNamedParameter($gid))) |
|
| 137 | + ->execute(); |
|
| 138 | + |
|
| 139 | + // Delete the group-groupadmin relation |
|
| 140 | + $qb = $this->dbConn->getQueryBuilder(); |
|
| 141 | + $qb->delete('group_admin') |
|
| 142 | + ->where($qb->expr()->eq('gid', $qb->createNamedParameter($gid))) |
|
| 143 | + ->execute(); |
|
| 144 | + |
|
| 145 | + // Delete from cache |
|
| 146 | + unset($this->groupCache[$gid]); |
|
| 147 | + |
|
| 148 | + return true; |
|
| 149 | + } |
|
| 150 | + |
|
| 151 | + /** |
|
| 152 | + * is user in group? |
|
| 153 | + * @param string $uid uid of the user |
|
| 154 | + * @param string $gid gid of the group |
|
| 155 | + * @return bool |
|
| 156 | + * |
|
| 157 | + * Checks whether the user is member of a group or not. |
|
| 158 | + */ |
|
| 159 | + public function inGroup( $uid, $gid ) { |
|
| 160 | + $this->fixDI(); |
|
| 161 | + |
|
| 162 | + // check |
|
| 163 | + $qb = $this->dbConn->getQueryBuilder(); |
|
| 164 | + $cursor = $qb->select('uid') |
|
| 165 | + ->from('group_user') |
|
| 166 | + ->where($qb->expr()->eq('gid', $qb->createNamedParameter($gid))) |
|
| 167 | + ->andWhere($qb->expr()->eq('uid', $qb->createNamedParameter($uid))) |
|
| 168 | + ->execute(); |
|
| 169 | + |
|
| 170 | + $result = $cursor->fetch(); |
|
| 171 | + $cursor->closeCursor(); |
|
| 172 | + |
|
| 173 | + return $result ? true : false; |
|
| 174 | + } |
|
| 175 | + |
|
| 176 | + /** |
|
| 177 | + * Add a user to a group |
|
| 178 | + * @param string $uid Name of the user to add to group |
|
| 179 | + * @param string $gid Name of the group in which add the user |
|
| 180 | + * @return bool |
|
| 181 | + * |
|
| 182 | + * Adds a user to a group. |
|
| 183 | + */ |
|
| 184 | + public function addToGroup(string $uid, string $gid): bool { |
|
| 185 | + $this->fixDI(); |
|
| 186 | + |
|
| 187 | + // No duplicate entries! |
|
| 188 | + if( !$this->inGroup( $uid, $gid )) { |
|
| 189 | + $qb = $this->dbConn->getQueryBuilder(); |
|
| 190 | + $qb->insert('group_user') |
|
| 191 | + ->setValue('uid', $qb->createNamedParameter($uid)) |
|
| 192 | + ->setValue('gid', $qb->createNamedParameter($gid)) |
|
| 193 | + ->execute(); |
|
| 194 | + return true; |
|
| 195 | + }else{ |
|
| 196 | + return false; |
|
| 197 | + } |
|
| 198 | + } |
|
| 199 | + |
|
| 200 | + /** |
|
| 201 | + * Removes a user from a group |
|
| 202 | + * @param string $uid Name of the user to remove from group |
|
| 203 | + * @param string $gid Name of the group from which remove the user |
|
| 204 | + * @return bool |
|
| 205 | + * |
|
| 206 | + * removes the user from a group. |
|
| 207 | + */ |
|
| 208 | + public function removeFromGroup(string $uid, string $gid): bool { |
|
| 209 | + $this->fixDI(); |
|
| 210 | + |
|
| 211 | + $qb = $this->dbConn->getQueryBuilder(); |
|
| 212 | + $qb->delete('group_user') |
|
| 213 | + ->where($qb->expr()->eq('uid', $qb->createNamedParameter($uid))) |
|
| 214 | + ->andWhere($qb->expr()->eq('gid', $qb->createNamedParameter($gid))) |
|
| 215 | + ->execute(); |
|
| 216 | + |
|
| 217 | + return true; |
|
| 218 | + } |
|
| 219 | + |
|
| 220 | + /** |
|
| 221 | + * Get all groups a user belongs to |
|
| 222 | + * @param string $uid Name of the user |
|
| 223 | + * @return array an array of group names |
|
| 224 | + * |
|
| 225 | + * This function fetches all groups a user belongs to. It does not check |
|
| 226 | + * if the user exists at all. |
|
| 227 | + */ |
|
| 228 | + public function getUserGroups( $uid ) { |
|
| 229 | + //guests has empty or null $uid |
|
| 230 | + if ($uid === null || $uid === '') { |
|
| 231 | + return []; |
|
| 232 | + } |
|
| 233 | + |
|
| 234 | + $this->fixDI(); |
|
| 235 | + |
|
| 236 | + // No magic! |
|
| 237 | + $qb = $this->dbConn->getQueryBuilder(); |
|
| 238 | + $cursor = $qb->select('gid') |
|
| 239 | + ->from('group_user') |
|
| 240 | + ->where($qb->expr()->eq('uid', $qb->createNamedParameter($uid))) |
|
| 241 | + ->execute(); |
|
| 242 | + |
|
| 243 | + $groups = []; |
|
| 244 | + while( $row = $cursor->fetch()) { |
|
| 245 | + $groups[] = $row['gid']; |
|
| 246 | + $this->groupCache[$row['gid']] = $row['gid']; |
|
| 247 | + } |
|
| 248 | + $cursor->closeCursor(); |
|
| 249 | + |
|
| 250 | + return $groups; |
|
| 251 | + } |
|
| 252 | + |
|
| 253 | + /** |
|
| 254 | + * get a list of all groups |
|
| 255 | + * @param string $search |
|
| 256 | + * @param int $limit |
|
| 257 | + * @param int $offset |
|
| 258 | + * @return array an array of group names |
|
| 259 | + * |
|
| 260 | + * Returns a list with all groups |
|
| 261 | + */ |
|
| 262 | + public function getGroups($search = '', $limit = null, $offset = null) { |
|
| 263 | + $this->fixDI(); |
|
| 264 | + |
|
| 265 | + $query = $this->dbConn->getQueryBuilder(); |
|
| 266 | + $query->select('gid') |
|
| 267 | + ->from('groups') |
|
| 268 | + ->orderBy('gid', 'ASC'); |
|
| 269 | + |
|
| 270 | + if ($search !== '') { |
|
| 271 | + $query->where($query->expr()->iLike('gid', $query->createNamedParameter( |
|
| 272 | + '%' . $this->dbConn->escapeLikeParameter($search) . '%' |
|
| 273 | + ))); |
|
| 274 | + } |
|
| 275 | + |
|
| 276 | + $query->setMaxResults($limit) |
|
| 277 | + ->setFirstResult($offset); |
|
| 278 | + $result = $query->execute(); |
|
| 279 | + |
|
| 280 | + $groups = []; |
|
| 281 | + while ($row = $result->fetch()) { |
|
| 282 | + $groups[] = $row['gid']; |
|
| 283 | + } |
|
| 284 | + $result->closeCursor(); |
|
| 285 | + |
|
| 286 | + return $groups; |
|
| 287 | + } |
|
| 288 | + |
|
| 289 | + /** |
|
| 290 | + * check if a group exists |
|
| 291 | + * @param string $gid |
|
| 292 | + * @return bool |
|
| 293 | + */ |
|
| 294 | + public function groupExists($gid) { |
|
| 295 | + $this->fixDI(); |
|
| 296 | + |
|
| 297 | + // Check cache first |
|
| 298 | + if (isset($this->groupCache[$gid])) { |
|
| 299 | + return true; |
|
| 300 | + } |
|
| 301 | + |
|
| 302 | + $qb = $this->dbConn->getQueryBuilder(); |
|
| 303 | + $cursor = $qb->select('gid') |
|
| 304 | + ->from('groups') |
|
| 305 | + ->where($qb->expr()->eq('gid', $qb->createNamedParameter($gid))) |
|
| 306 | + ->execute(); |
|
| 307 | + $result = $cursor->fetch(); |
|
| 308 | + $cursor->closeCursor(); |
|
| 309 | + |
|
| 310 | + if ($result !== false) { |
|
| 311 | + $this->groupCache[$gid] = $gid; |
|
| 312 | + return true; |
|
| 313 | + } |
|
| 314 | + return false; |
|
| 315 | + } |
|
| 316 | + |
|
| 317 | + /** |
|
| 318 | + * get a list of all users in a group |
|
| 319 | + * @param string $gid |
|
| 320 | + * @param string $search |
|
| 321 | + * @param int $limit |
|
| 322 | + * @param int $offset |
|
| 323 | + * @return array an array of user ids |
|
| 324 | + */ |
|
| 325 | + public function usersInGroup($gid, $search = '', $limit = null, $offset = null) { |
|
| 326 | + $this->fixDI(); |
|
| 327 | + |
|
| 328 | + $query = $this->dbConn->getQueryBuilder(); |
|
| 329 | + $query->select('uid') |
|
| 330 | + ->from('group_user') |
|
| 331 | + ->where($query->expr()->eq('gid', $query->createNamedParameter($gid))) |
|
| 332 | + ->orderBy('uid', 'ASC'); |
|
| 333 | + |
|
| 334 | + if ($search !== '') { |
|
| 335 | + $query->andWhere($query->expr()->like('uid', $query->createNamedParameter( |
|
| 336 | + '%' . $this->dbConn->escapeLikeParameter($search) . '%' |
|
| 337 | + ))); |
|
| 338 | + } |
|
| 339 | + |
|
| 340 | + $query->setMaxResults($limit) |
|
| 341 | + ->setFirstResult($offset); |
|
| 342 | + $result = $query->execute(); |
|
| 343 | + |
|
| 344 | + $users = []; |
|
| 345 | + while ($row = $result->fetch()) { |
|
| 346 | + $users[] = $row['uid']; |
|
| 347 | + } |
|
| 348 | + $result->closeCursor(); |
|
| 349 | + |
|
| 350 | + return $users; |
|
| 351 | + } |
|
| 352 | + |
|
| 353 | + /** |
|
| 354 | + * get the number of all users matching the search string in a group |
|
| 355 | + * @param string $gid |
|
| 356 | + * @param string $search |
|
| 357 | + * @return int |
|
| 358 | + */ |
|
| 359 | + public function countUsersInGroup(string $gid, string $search = ''): int { |
|
| 360 | + $this->fixDI(); |
|
| 361 | + |
|
| 362 | + $query = $this->dbConn->getQueryBuilder(); |
|
| 363 | + $query->select($query->func()->count('*', 'num_users')) |
|
| 364 | + ->from('group_user') |
|
| 365 | + ->where($query->expr()->eq('gid', $query->createNamedParameter($gid))); |
|
| 366 | + |
|
| 367 | + if ($search !== '') { |
|
| 368 | + $query->andWhere($query->expr()->like('uid', $query->createNamedParameter( |
|
| 369 | + '%' . $this->dbConn->escapeLikeParameter($search) . '%' |
|
| 370 | + ))); |
|
| 371 | + } |
|
| 372 | + |
|
| 373 | + $result = $query->execute(); |
|
| 374 | + $count = $result->fetchColumn(); |
|
| 375 | + $result->closeCursor(); |
|
| 376 | + |
|
| 377 | + if ($count !== false) { |
|
| 378 | + $count = (int)$count; |
|
| 379 | + } else { |
|
| 380 | + $count = 0; |
|
| 381 | + } |
|
| 382 | + |
|
| 383 | + return $count; |
|
| 384 | + } |
|
| 385 | + |
|
| 386 | + /** |
|
| 387 | + * get the number of disabled users in a group |
|
| 388 | + * |
|
| 389 | + * @param string $search |
|
| 390 | + * @return int|bool |
|
| 391 | + */ |
|
| 392 | + public function countDisabledInGroup(string $gid): int { |
|
| 393 | + $this->fixDI(); |
|
| 394 | 394 | |
| 395 | - $query = $this->dbConn->getQueryBuilder(); |
|
| 396 | - $query->select($query->createFunction('COUNT(DISTINCT ' . $query->getColumnName('uid') . ')')) |
|
| 397 | - ->from('preferences', 'p') |
|
| 398 | - ->innerJoin('p', 'group_user', 'g', $query->expr()->eq('p.userid', 'g.uid')) |
|
| 399 | - ->where($query->expr()->eq('appid', $query->createNamedParameter('core'))) |
|
| 400 | - ->andWhere($query->expr()->eq('configkey', $query->createNamedParameter('enabled'))) |
|
| 401 | - ->andWhere($query->expr()->eq('configvalue', $query->createNamedParameter('false'), IQueryBuilder::PARAM_STR)) |
|
| 402 | - ->andWhere($query->expr()->eq('gid', $query->createNamedParameter($gid), IQueryBuilder::PARAM_STR)); |
|
| 395 | + $query = $this->dbConn->getQueryBuilder(); |
|
| 396 | + $query->select($query->createFunction('COUNT(DISTINCT ' . $query->getColumnName('uid') . ')')) |
|
| 397 | + ->from('preferences', 'p') |
|
| 398 | + ->innerJoin('p', 'group_user', 'g', $query->expr()->eq('p.userid', 'g.uid')) |
|
| 399 | + ->where($query->expr()->eq('appid', $query->createNamedParameter('core'))) |
|
| 400 | + ->andWhere($query->expr()->eq('configkey', $query->createNamedParameter('enabled'))) |
|
| 401 | + ->andWhere($query->expr()->eq('configvalue', $query->createNamedParameter('false'), IQueryBuilder::PARAM_STR)) |
|
| 402 | + ->andWhere($query->expr()->eq('gid', $query->createNamedParameter($gid), IQueryBuilder::PARAM_STR)); |
|
| 403 | 403 | |
| 404 | - $result = $query->execute(); |
|
| 405 | - $count = $result->fetchColumn(); |
|
| 406 | - $result->closeCursor(); |
|
| 404 | + $result = $query->execute(); |
|
| 405 | + $count = $result->fetchColumn(); |
|
| 406 | + $result->closeCursor(); |
|
| 407 | 407 | |
| 408 | - if ($count !== false) { |
|
| 409 | - $count = (int)$count; |
|
| 410 | - } else { |
|
| 411 | - $count = 0; |
|
| 412 | - } |
|
| 413 | - |
|
| 414 | - return $count; |
|
| 415 | - } |
|
| 408 | + if ($count !== false) { |
|
| 409 | + $count = (int)$count; |
|
| 410 | + } else { |
|
| 411 | + $count = 0; |
|
| 412 | + } |
|
| 413 | + |
|
| 414 | + return $count; |
|
| 415 | + } |
|
| 416 | 416 | |
| 417 | 417 | } |
@@ -104,7 +104,7 @@ discard block |
||
| 104 | 104 | $result = $builder->insert('groups') |
| 105 | 105 | ->setValue('gid', $builder->createNamedParameter($gid)) |
| 106 | 106 | ->execute(); |
| 107 | - } catch(UniqueConstraintViolationException $e) { |
|
| 107 | + } catch (UniqueConstraintViolationException $e) { |
|
| 108 | 108 | $result = 0; |
| 109 | 109 | } |
| 110 | 110 | |
@@ -156,7 +156,7 @@ discard block |
||
| 156 | 156 | * |
| 157 | 157 | * Checks whether the user is member of a group or not. |
| 158 | 158 | */ |
| 159 | - public function inGroup( $uid, $gid ) { |
|
| 159 | + public function inGroup($uid, $gid) { |
|
| 160 | 160 | $this->fixDI(); |
| 161 | 161 | |
| 162 | 162 | // check |
@@ -185,14 +185,14 @@ discard block |
||
| 185 | 185 | $this->fixDI(); |
| 186 | 186 | |
| 187 | 187 | // No duplicate entries! |
| 188 | - if( !$this->inGroup( $uid, $gid )) { |
|
| 188 | + if (!$this->inGroup($uid, $gid)) { |
|
| 189 | 189 | $qb = $this->dbConn->getQueryBuilder(); |
| 190 | 190 | $qb->insert('group_user') |
| 191 | 191 | ->setValue('uid', $qb->createNamedParameter($uid)) |
| 192 | 192 | ->setValue('gid', $qb->createNamedParameter($gid)) |
| 193 | 193 | ->execute(); |
| 194 | 194 | return true; |
| 195 | - }else{ |
|
| 195 | + } else { |
|
| 196 | 196 | return false; |
| 197 | 197 | } |
| 198 | 198 | } |
@@ -225,7 +225,7 @@ discard block |
||
| 225 | 225 | * This function fetches all groups a user belongs to. It does not check |
| 226 | 226 | * if the user exists at all. |
| 227 | 227 | */ |
| 228 | - public function getUserGroups( $uid ) { |
|
| 228 | + public function getUserGroups($uid) { |
|
| 229 | 229 | //guests has empty or null $uid |
| 230 | 230 | if ($uid === null || $uid === '') { |
| 231 | 231 | return []; |
@@ -241,7 +241,7 @@ discard block |
||
| 241 | 241 | ->execute(); |
| 242 | 242 | |
| 243 | 243 | $groups = []; |
| 244 | - while( $row = $cursor->fetch()) { |
|
| 244 | + while ($row = $cursor->fetch()) { |
|
| 245 | 245 | $groups[] = $row['gid']; |
| 246 | 246 | $this->groupCache[$row['gid']] = $row['gid']; |
| 247 | 247 | } |
@@ -269,7 +269,7 @@ discard block |
||
| 269 | 269 | |
| 270 | 270 | if ($search !== '') { |
| 271 | 271 | $query->where($query->expr()->iLike('gid', $query->createNamedParameter( |
| 272 | - '%' . $this->dbConn->escapeLikeParameter($search) . '%' |
|
| 272 | + '%'.$this->dbConn->escapeLikeParameter($search).'%' |
|
| 273 | 273 | ))); |
| 274 | 274 | } |
| 275 | 275 | |
@@ -333,7 +333,7 @@ discard block |
||
| 333 | 333 | |
| 334 | 334 | if ($search !== '') { |
| 335 | 335 | $query->andWhere($query->expr()->like('uid', $query->createNamedParameter( |
| 336 | - '%' . $this->dbConn->escapeLikeParameter($search) . '%' |
|
| 336 | + '%'.$this->dbConn->escapeLikeParameter($search).'%' |
|
| 337 | 337 | ))); |
| 338 | 338 | } |
| 339 | 339 | |
@@ -366,7 +366,7 @@ discard block |
||
| 366 | 366 | |
| 367 | 367 | if ($search !== '') { |
| 368 | 368 | $query->andWhere($query->expr()->like('uid', $query->createNamedParameter( |
| 369 | - '%' . $this->dbConn->escapeLikeParameter($search) . '%' |
|
| 369 | + '%'.$this->dbConn->escapeLikeParameter($search).'%' |
|
| 370 | 370 | ))); |
| 371 | 371 | } |
| 372 | 372 | |
@@ -375,7 +375,7 @@ discard block |
||
| 375 | 375 | $result->closeCursor(); |
| 376 | 376 | |
| 377 | 377 | if ($count !== false) { |
| 378 | - $count = (int)$count; |
|
| 378 | + $count = (int) $count; |
|
| 379 | 379 | } else { |
| 380 | 380 | $count = 0; |
| 381 | 381 | } |
@@ -393,7 +393,7 @@ discard block |
||
| 393 | 393 | $this->fixDI(); |
| 394 | 394 | |
| 395 | 395 | $query = $this->dbConn->getQueryBuilder(); |
| 396 | - $query->select($query->createFunction('COUNT(DISTINCT ' . $query->getColumnName('uid') . ')')) |
|
| 396 | + $query->select($query->createFunction('COUNT(DISTINCT '.$query->getColumnName('uid').')')) |
|
| 397 | 397 | ->from('preferences', 'p') |
| 398 | 398 | ->innerJoin('p', 'group_user', 'g', $query->expr()->eq('p.userid', 'g.uid')) |
| 399 | 399 | ->where($query->expr()->eq('appid', $query->createNamedParameter('core'))) |
@@ -406,7 +406,7 @@ discard block |
||
| 406 | 406 | $result->closeCursor(); |
| 407 | 407 | |
| 408 | 408 | if ($count !== false) { |
| 409 | - $count = (int)$count; |
|
| 409 | + $count = (int) $count; |
|
| 410 | 410 | } else { |
| 411 | 411 | $count = 0; |
| 412 | 412 | } |