| 
 | 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();  | 
 | 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 | +        $statement->bindValue(':group', $gid, \PDO::PARAM_STR);  | 
 | 115 | +        if (isset($limit) && $limit > 0) {  | 
 | 116 | +            $statement->bindValue(':limit', intval($limit), \PDO::PARAM_INT);  | 
 | 117 | +        }  | 
 | 118 | +        if (isset($offset) && $offset > 0) {  | 
 | 119 | +            $statement->bindValue(':offset', intval($offset), \PDO::PARAM_INT);  | 
 | 120 | +        }  | 
 | 121 | +        $statement->execute();  | 
 | 122 | +        $groups = $statement->fetchAll(\PDO::FETCH_COLUMN, 0);  | 
 | 123 | +        return $groups;  | 
 | 124 | +    }  | 
 | 125 | + | 
 | 126 | +    public function getGroupDetails(string $gid): array  | 
 | 127 | +    {  | 
 | 128 | +        $queryFromConfig = $this->config->getQueryGroupDetails();  | 
 | 129 | +        if (empty($queryFromConfig)) {  | 
 | 130 | +            return [];  | 
 | 131 | +        }  | 
 | 132 | +        $statement = $this->db->getDbHandle()->prepare($queryFromConfig);  | 
 | 133 | +        $statement->execute(['group' => $gid]);  | 
 | 134 | +        $groupDetails = $statement->fetchColumn();  | 
 | 135 | +        if (!$groupDetails) {  | 
 | 136 | +            return [];  | 
 | 137 | +        }  | 
 | 138 | +        return ['displayName' => $groupDetails];  | 
 | 139 | +    }  | 
 | 140 | +}  | 
0 commit comments