|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * @copyright Copyright (c) 2024 Vitor Mattos <vitor@php.rio> |
| 4 | + * |
| 5 | + * @license GNU AGPL version 3 or any later version |
| 6 | + * |
| 7 | + * This program is free software: you can redistribute it and/or modify |
| 8 | + * it under the terms of the GNU Affero General Public License as |
| 9 | + * published by the Free Software Foundation, either version 3 of the |
| 10 | + * License, or (at your option) any later version. |
| 11 | + * |
| 12 | + * This program is distributed in the hope that it will be useful, |
| 13 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | + * GNU Affero General Public License for more details. |
| 16 | + * |
| 17 | + * You should have received a copy of the GNU Affero General Public License |
| 18 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 | + * |
| 20 | + */ |
| 21 | + |
| 22 | +namespace OCA\UserBackendSqlRaw\Backend; |
| 23 | + |
| 24 | +use OCA\UserBackendSqlRaw\Config; |
| 25 | +use OCA\UserBackendSqlRaw\Db; |
| 26 | +use OCP\Group\Backend\ABackend; |
| 27 | +use OCP\Group\Backend\IGroupDetailsBackend; |
| 28 | +use Psr\Log\LoggerInterface; |
| 29 | + |
| 30 | +class GroupBackend extends ABackend implements IGroupDetailsBackend |
| 31 | +{ |
| 32 | + public function __construct( |
| 33 | + private LoggerInterface $logger, |
| 34 | + private Config $config, |
| 35 | + private Db $db, |
| 36 | + ) { |
| 37 | + } |
| 38 | + |
| 39 | + public function inGroup($uid, $gid): bool |
| 40 | + { |
| 41 | + $queryFromConfig = $this->config->getQueryInGroup(); |
| 42 | + if (empty($queryFromConfig)) { |
| 43 | + return false; |
| 44 | + } |
| 45 | + $statement = $this->db->getDbHandle()->prepare($queryFromConfig); |
| 46 | + $statement->execute(['username' => $uid, 'group' => $gid]); |
| 47 | + $inGroup = $statement->fetchColumn(); |
| 48 | + return (bool) $inGroup; |
| 49 | + } |
| 50 | + |
| 51 | + public function getUserGroups($uid) |
| 52 | + { |
| 53 | + $queryFromConfig = $this->config->getQueryUserGroups(); |
| 54 | + if (empty($queryFromConfig)) { |
| 55 | + return []; |
| 56 | + } |
| 57 | + $statement = $this->db->getDbHandle()->prepare($queryFromConfig); |
| 58 | + $statement->execute(['username' => $uid]); |
| 59 | + $groups = $statement->fetchAll(\PDO::FETCH_COLUMN, 0); |
| 60 | + return $groups; |
| 61 | + } |
| 62 | + |
| 63 | + public function getGroups($search = '', $limit = -1, $offset = 0) |
| 64 | + { |
| 65 | + $queryFromConfig = $this->config->getQueryGroups(); |
| 66 | + if (empty($queryFromConfig)) { |
| 67 | + return []; |
| 68 | + } |
| 69 | + isset($limit) && $limit > 0 ? $limitSegment = ' LIMIT :limit' : $limitSegment = ''; |
| 70 | + isset($offset) && $offset > 0? $offsetSegment = ' OFFSET :offset' : $offsetSegment = ''; |
| 71 | + $finalQuery = $queryFromConfig . $limitSegment . $offsetSegment; |
| 72 | + $statement = $this->db->getDbHandle()->prepare($finalQuery); |
| 73 | + // Because MariaDB can not handle string parameters for LIMIT/OFFSET we have to bind the |
| 74 | + // values "manually" instead of passing an array to execute(). This is another instance of |
| 75 | + // MariaDB making the code "uglier". |
| 76 | + $statement->bindValue(':search', '%' . $search . '%', \PDO::PARAM_STR); |
| 77 | + if (isset($limit) && $limit > 0) { |
| 78 | + $statement->bindValue(':limit', intval($limit), \PDO::PARAM_INT); |
| 79 | + } |
| 80 | + if (isset($offset) && $offset > 0) { |
| 81 | + $statement->bindValue(':offset', intval($offset), \PDO::PARAM_INT); |
| 82 | + } |
| 83 | + $statement->execute(['search' => $search]); |
| 84 | + $groups = $statement->fetchAll(\PDO::FETCH_COLUMN, 0); |
| 85 | + return $groups; |
| 86 | + } |
| 87 | + |
| 88 | + public function groupExists($gid) |
| 89 | + { |
| 90 | + $queryFromConfig = $this->config->getQueryGroupExists(); |
| 91 | + if (empty($queryFromConfig)) { |
| 92 | + return false; |
| 93 | + } |
| 94 | + $statement = $this->db->getDbHandle()->prepare($queryFromConfig); |
| 95 | + $statement->execute(['group' => $gid]); |
| 96 | + $groupExists = $statement->fetchColumn(); |
| 97 | + return (bool) $groupExists; |
| 98 | + } |
| 99 | + |
| 100 | + public function usersInGroup($gid, $search = '', $limit = -1, $offset = 0) |
| 101 | + { |
| 102 | + $queryFromConfig = $this->config->getQueryUsersInGroup(); |
| 103 | + if (empty($queryFromConfig)) { |
| 104 | + return []; |
| 105 | + } |
| 106 | + isset($limit) && $limit > 0 ? $limitSegment = ' LIMIT :limit' : $limitSegment = ''; |
| 107 | + isset($offset) && $offset > 0? $offsetSegment = ' OFFSET :offset' : $offsetSegment = ''; |
| 108 | + $finalQuery = $queryFromConfig . $limitSegment . $offsetSegment; |
| 109 | + $statement = $this->db->getDbHandle()->prepare($finalQuery); |
| 110 | + // Because MariaDB can not handle string parameters for LIMIT/OFFSET we have to bind the |
| 111 | + // values "manually" instead of passing an array to execute(). This is another instance of |
| 112 | + // MariaDB making the code "uglier". |
| 113 | + $statement->bindValue(':search', '%' . $search . '%', \PDO::PARAM_STR); |
| 114 | + if (isset($limit) && $limit > 0) { |
| 115 | + $statement->bindValue(':limit', intval($limit), \PDO::PARAM_INT); |
| 116 | + } |
| 117 | + if (isset($offset) && $offset > 0) { |
| 118 | + $statement->bindValue(':offset', intval($offset), \PDO::PARAM_INT); |
| 119 | + } |
| 120 | + $statement->execute(['group' => $gid, 'search' => $search]); |
| 121 | + $groups = $statement->fetchAll(\PDO::FETCH_COLUMN, 0); |
| 122 | + return $groups; |
| 123 | + } |
| 124 | + |
| 125 | + public function getGroupDetails(string $gid): array |
| 126 | + { |
| 127 | + $queryFromConfig = $this->config->getQueryGroupDetails(); |
| 128 | + if (empty($queryFromConfig)) { |
| 129 | + return []; |
| 130 | + } |
| 131 | + $statement = $this->db->getDbHandle()->prepare($queryFromConfig); |
| 132 | + $statement->execute(['group' => $gid]); |
| 133 | + $groupDetails = $statement->fetchColumn(); |
| 134 | + if (!$groupDetails) { |
| 135 | + return []; |
| 136 | + } |
| 137 | + return ['displayName' => $groupDetails]; |
| 138 | + } |
| 139 | +} |
0 commit comments