|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * @copyright Copyright (c) 2023, ownCloud GmbH. |
| 4 | + * @license AGPL-3.0 |
| 5 | + * |
| 6 | + * This code is free software: you can redistribute it and/or modify |
| 7 | + * it under the terms of the GNU Affero General Public License, version 3, |
| 8 | + * as published by the Free Software Foundation. |
| 9 | + * |
| 10 | + * This program is distributed in the hope that it will be useful, |
| 11 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | + * GNU Affero General Public License for more details. |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU Affero General Public License, version 3, |
| 16 | + * along with this program. If not, see <http://www.gnu.org/licenses/> |
| 17 | + * |
| 18 | + */ |
| 19 | + |
| 20 | +namespace OCA\User_LDAP; |
| 21 | + |
| 22 | +use OC\ServerNotAvailableException; |
| 23 | +use OCA\User_LDAP\Exceptions\BindFailedException; |
| 24 | +use OCA\User_LDAP\User_Proxy; |
| 25 | +use OCP\UserInterface; |
| 26 | +use OCP\Sync\User\IUserSyncBackend; |
| 27 | +use OCP\Sync\User\SyncingUser; |
| 28 | +use OCP\Sync\User\SyncBackendUserFailedException; |
| 29 | +use OCP\Sync\User\SyncBackendBrokenException; |
| 30 | + |
| 31 | +class UserSyncLDAPBackend implements IUserSyncBackend { |
| 32 | + /** @var User_Proxy */ |
| 33 | + private $userProxy; |
| 34 | + |
| 35 | + private $connectionTested = false; |
| 36 | + private $pointer = 0; |
| 37 | + private $cachedUserData = ['min' => 0, 'max' => 0, 'last' => false]; |
| 38 | + |
| 39 | + public function __construct(User_Proxy $userProxy) { |
| 40 | + $this->userProxy = $userProxy; |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * @inheritDoc |
| 45 | + */ |
| 46 | + public function resetPointer() { |
| 47 | + $this->connectionTested = false; |
| 48 | + $this->pointer = 0; |
| 49 | + $this->cachedUserData = ['min' => 0, 'max' => 0, 'last' => false]; |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * @inheritDoc |
| 54 | + */ |
| 55 | + public function getNextUser(): ?SyncingUser { |
| 56 | + $chunk = 500; // TODO: this should depend on the actual configuration |
| 57 | + $minPointer = $this->cachedUserData['min']; |
| 58 | + if (!isset($this->cachedUserData['users'][$this->pointer - $minPointer])) { |
| 59 | + if ($this->cachedUserData['last']) { |
| 60 | + // we've reached the end |
| 61 | + return null; |
| 62 | + } |
| 63 | + |
| 64 | + try { |
| 65 | + if (!$this->connectionTested) { |
| 66 | + $test = $this->userProxy->testConnection(); |
| 67 | + $this->connectionTested = true; |
| 68 | + } |
| 69 | + $ldap_entries = $this->userProxy->getRawUsersEntriesWithPrefix('', $chunk, $this->pointer); |
| 70 | + } catch (ServerNotAvailableException | BindFailedException $ex) { |
| 71 | + throw new SyncBackendBrokenException('Failed to get user entries', 1, $ex); |
| 72 | + } |
| 73 | + |
| 74 | + $minPointer = $this->pointer; |
| 75 | + $this->cachedUserData = [ |
| 76 | + 'min' => $this->pointer, |
| 77 | + 'max' => $this->pointer + \count($ldap_entries), |
| 78 | + 'last' => empty($ldap_entries), |
| 79 | + 'users' => $ldap_entries, |
| 80 | + ]; |
| 81 | + } |
| 82 | + |
| 83 | + $syncingUser = null; |
| 84 | + if (isset($this->cachedUserData['users'][$this->pointer - $minPointer])) { |
| 85 | + $ldapEntryData = $this->cachedUserData['users'][$this->pointer - $minPointer]; |
| 86 | + $this->pointer++; |
| 87 | + try { |
| 88 | + $userEntry = $this->userProxy->getUserEntryFromRawWithPrefix($ldapEntryData['prefix'], $ldapEntryData['entry']); |
| 89 | + } catch (\OutOfBoundsException $ex) { |
| 90 | + throw new SyncBackendUserFailedException("Failed to get user with dn {$ldapEntryData['entry']['dn'][0]}", 1, $ex); |
| 91 | + } |
| 92 | + |
| 93 | + try { |
| 94 | + $uid = $userEntry->getOwnCloudUID(); |
| 95 | + $displayname = $userEntry->getDisplayName(); |
| 96 | + $quota = $userEntry->getQuota(); |
| 97 | + $email = $userEntry->getEMailAddress(); |
| 98 | + $home = $userEntry->getHome(); |
| 99 | + $searchTerms = $userEntry->getSearchTerms(); |
| 100 | + } catch (\Exception $e) { |
| 101 | + throw new SyncBackendUserFailedException("Can't sync user with dn {$userEntry->getDN()}", 1, $ex); |
| 102 | + } |
| 103 | + |
| 104 | + $syncingUser = new SyncingUser($uid); |
| 105 | + $syncingUser->setDisplayName($displayname); |
| 106 | + if ($email !== null) { |
| 107 | + $syncingUser->setEmail($email); |
| 108 | + } |
| 109 | + if ($home !== null) { |
| 110 | + $syncingUser->setHome($home); |
| 111 | + } |
| 112 | + if ($searchTerms !== null) { |
| 113 | + $syncingUser->setSearchTerms($searchTerms); |
| 114 | + } |
| 115 | + if ($quota !== false) { |
| 116 | + $syncingUser->setQuota($quota); |
| 117 | + } |
| 118 | + } else { |
| 119 | + $this->pointer++; |
| 120 | + } |
| 121 | + return $syncingUser; |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * @inheritDoc |
| 126 | + */ |
| 127 | + public function getSyncingUser(string $id): ?SyncingUser { |
| 128 | + $syncingUser = null; |
| 129 | + |
| 130 | + try { |
| 131 | + $userEntry = $this->userProxy->getUserEntry($id); |
| 132 | + } catch (ServerNotAvailableException | BindFailedException $ex) { |
| 133 | + throw new SyncBackendBrokenException('Failed to get the user entry', 1, $ex); |
| 134 | + } |
| 135 | + |
| 136 | + if ($userEntry !== null) { |
| 137 | + try { |
| 138 | + $uid = $userEntry->getOwnCloudUID(); |
| 139 | + $displayname = $userEntry->getDisplayName(); |
| 140 | + $quota = $userEntry->getQuota(); |
| 141 | + $email = $userEntry->getEMailAddress(); |
| 142 | + $home = $userEntry->getHome(); |
| 143 | + $searchTerms = $userEntry->getSearchTerms(); |
| 144 | + } catch (\Exception $e) { |
| 145 | + throw new SyncBackendUserFailedException("Can't sync user with dn {$userEntry->getDN()}", 1, $ex); |
| 146 | + } |
| 147 | + |
| 148 | + $syncingUser = new SyncingUser($uid); |
| 149 | + $syncingUser->setDisplayName($displayname); |
| 150 | + if ($email !== null) { |
| 151 | + $syncingUser->setEmail($email); |
| 152 | + } |
| 153 | + if ($home !== null) { |
| 154 | + $syncingUser->setHome($home); |
| 155 | + } |
| 156 | + if ($searchTerms !== null) { |
| 157 | + $syncingUser->setSearchTerms($searchTerms); |
| 158 | + } |
| 159 | + if ($quota !== false) { |
| 160 | + $syncingUser->setQuota($quota); |
| 161 | + } |
| 162 | + } |
| 163 | + return $syncingUser; |
| 164 | + } |
| 165 | + |
| 166 | + /** |
| 167 | + * @inheritDoc |
| 168 | + */ |
| 169 | + public function userCount(): ?int { |
| 170 | + $nUsers = $this->userProxy->countUsers(); |
| 171 | + if ($nUsers !== false) { |
| 172 | + return $nUsers; |
| 173 | + } |
| 174 | + return null; |
| 175 | + } |
| 176 | + |
| 177 | + /** |
| 178 | + * @inheritDoc |
| 179 | + */ |
| 180 | + public function getUserInterface(): UserInterface { |
| 181 | + return $this->userProxy; |
| 182 | + } |
| 183 | +} |
0 commit comments