@@ -25,234 +25,234 @@ |
||
| 25 | 25 | * cache |
| 26 | 26 | */ |
| 27 | 27 | class Manager { |
| 28 | - protected ?Access $access = null; |
|
| 29 | - protected IDBConnection $db; |
|
| 30 | - /** @var CappedMemoryCache<User> $usersByDN */ |
|
| 31 | - protected CappedMemoryCache $usersByDN; |
|
| 32 | - /** @var CappedMemoryCache<User> $usersByUid */ |
|
| 33 | - protected CappedMemoryCache $usersByUid; |
|
| 28 | + protected ?Access $access = null; |
|
| 29 | + protected IDBConnection $db; |
|
| 30 | + /** @var CappedMemoryCache<User> $usersByDN */ |
|
| 31 | + protected CappedMemoryCache $usersByDN; |
|
| 32 | + /** @var CappedMemoryCache<User> $usersByUid */ |
|
| 33 | + protected CappedMemoryCache $usersByUid; |
|
| 34 | 34 | |
| 35 | - public function __construct( |
|
| 36 | - protected IConfig $ocConfig, |
|
| 37 | - protected LoggerInterface $logger, |
|
| 38 | - protected IAvatarManager $avatarManager, |
|
| 39 | - protected Image $image, |
|
| 40 | - protected IUserManager $userManager, |
|
| 41 | - protected INotificationManager $notificationManager, |
|
| 42 | - private IManager $shareManager, |
|
| 43 | - ) { |
|
| 44 | - $this->usersByDN = new CappedMemoryCache(); |
|
| 45 | - $this->usersByUid = new CappedMemoryCache(); |
|
| 46 | - } |
|
| 35 | + public function __construct( |
|
| 36 | + protected IConfig $ocConfig, |
|
| 37 | + protected LoggerInterface $logger, |
|
| 38 | + protected IAvatarManager $avatarManager, |
|
| 39 | + protected Image $image, |
|
| 40 | + protected IUserManager $userManager, |
|
| 41 | + protected INotificationManager $notificationManager, |
|
| 42 | + private IManager $shareManager, |
|
| 43 | + ) { |
|
| 44 | + $this->usersByDN = new CappedMemoryCache(); |
|
| 45 | + $this->usersByUid = new CappedMemoryCache(); |
|
| 46 | + } |
|
| 47 | 47 | |
| 48 | - /** |
|
| 49 | - * Binds manager to an instance of Access. |
|
| 50 | - * It needs to be assigned first before the manager can be used. |
|
| 51 | - * @param Access |
|
| 52 | - */ |
|
| 53 | - public function setLdapAccess(Access $access) { |
|
| 54 | - $this->access = $access; |
|
| 55 | - } |
|
| 48 | + /** |
|
| 49 | + * Binds manager to an instance of Access. |
|
| 50 | + * It needs to be assigned first before the manager can be used. |
|
| 51 | + * @param Access |
|
| 52 | + */ |
|
| 53 | + public function setLdapAccess(Access $access) { |
|
| 54 | + $this->access = $access; |
|
| 55 | + } |
|
| 56 | 56 | |
| 57 | - /** |
|
| 58 | - * @brief creates an instance of User and caches (just runtime) it in the |
|
| 59 | - * property array |
|
| 60 | - * @param string $dn the DN of the user |
|
| 61 | - * @param string $uid the internal (owncloud) username |
|
| 62 | - * @return User |
|
| 63 | - */ |
|
| 64 | - private function createAndCache($dn, $uid) { |
|
| 65 | - $this->checkAccess(); |
|
| 66 | - $user = new User($uid, $dn, $this->access, $this->ocConfig, |
|
| 67 | - clone $this->image, $this->logger, |
|
| 68 | - $this->avatarManager, $this->userManager, |
|
| 69 | - $this->notificationManager); |
|
| 70 | - $this->usersByDN[$dn] = $user; |
|
| 71 | - $this->usersByUid[$uid] = $user; |
|
| 72 | - return $user; |
|
| 73 | - } |
|
| 57 | + /** |
|
| 58 | + * @brief creates an instance of User and caches (just runtime) it in the |
|
| 59 | + * property array |
|
| 60 | + * @param string $dn the DN of the user |
|
| 61 | + * @param string $uid the internal (owncloud) username |
|
| 62 | + * @return User |
|
| 63 | + */ |
|
| 64 | + private function createAndCache($dn, $uid) { |
|
| 65 | + $this->checkAccess(); |
|
| 66 | + $user = new User($uid, $dn, $this->access, $this->ocConfig, |
|
| 67 | + clone $this->image, $this->logger, |
|
| 68 | + $this->avatarManager, $this->userManager, |
|
| 69 | + $this->notificationManager); |
|
| 70 | + $this->usersByDN[$dn] = $user; |
|
| 71 | + $this->usersByUid[$uid] = $user; |
|
| 72 | + return $user; |
|
| 73 | + } |
|
| 74 | 74 | |
| 75 | - /** |
|
| 76 | - * removes a user entry from the cache |
|
| 77 | - * @param $uid |
|
| 78 | - */ |
|
| 79 | - public function invalidate($uid) { |
|
| 80 | - if (!isset($this->usersByUid[$uid])) { |
|
| 81 | - return; |
|
| 82 | - } |
|
| 83 | - $dn = $this->usersByUid[$uid]->getDN(); |
|
| 84 | - unset($this->usersByUid[$uid]); |
|
| 85 | - unset($this->usersByDN[$dn]); |
|
| 86 | - } |
|
| 75 | + /** |
|
| 76 | + * removes a user entry from the cache |
|
| 77 | + * @param $uid |
|
| 78 | + */ |
|
| 79 | + public function invalidate($uid) { |
|
| 80 | + if (!isset($this->usersByUid[$uid])) { |
|
| 81 | + return; |
|
| 82 | + } |
|
| 83 | + $dn = $this->usersByUid[$uid]->getDN(); |
|
| 84 | + unset($this->usersByUid[$uid]); |
|
| 85 | + unset($this->usersByDN[$dn]); |
|
| 86 | + } |
|
| 87 | 87 | |
| 88 | - /** |
|
| 89 | - * @brief checks whether the Access instance has been set |
|
| 90 | - * @throws \Exception if Access has not been set |
|
| 91 | - * @psalm-assert !null $this->access |
|
| 92 | - * @return null |
|
| 93 | - */ |
|
| 94 | - private function checkAccess() { |
|
| 95 | - if (is_null($this->access)) { |
|
| 96 | - throw new \Exception('LDAP Access instance must be set first'); |
|
| 97 | - } |
|
| 98 | - } |
|
| 88 | + /** |
|
| 89 | + * @brief checks whether the Access instance has been set |
|
| 90 | + * @throws \Exception if Access has not been set |
|
| 91 | + * @psalm-assert !null $this->access |
|
| 92 | + * @return null |
|
| 93 | + */ |
|
| 94 | + private function checkAccess() { |
|
| 95 | + if (is_null($this->access)) { |
|
| 96 | + throw new \Exception('LDAP Access instance must be set first'); |
|
| 97 | + } |
|
| 98 | + } |
|
| 99 | 99 | |
| 100 | - /** |
|
| 101 | - * returns a list of attributes that will be processed further, e.g. quota, |
|
| 102 | - * email, displayname, or others. |
|
| 103 | - * |
|
| 104 | - * @param bool $minimal - optional, set to true to skip attributes with big |
|
| 105 | - * payload |
|
| 106 | - * @return string[] |
|
| 107 | - */ |
|
| 108 | - public function getAttributes($minimal = false) { |
|
| 109 | - $baseAttributes = array_merge(Access::UUID_ATTRIBUTES, ['dn', 'uid', 'samaccountname', 'memberof']); |
|
| 110 | - $attributes = [ |
|
| 111 | - $this->access->getConnection()->ldapExpertUUIDUserAttr, |
|
| 112 | - $this->access->getConnection()->ldapExpertUsernameAttr, |
|
| 113 | - $this->access->getConnection()->ldapQuotaAttribute, |
|
| 114 | - $this->access->getConnection()->ldapEmailAttribute, |
|
| 115 | - $this->access->getConnection()->ldapUserDisplayName, |
|
| 116 | - $this->access->getConnection()->ldapUserDisplayName2, |
|
| 117 | - $this->access->getConnection()->ldapExtStorageHomeAttribute, |
|
| 118 | - $this->access->getConnection()->ldapAttributePhone, |
|
| 119 | - $this->access->getConnection()->ldapAttributeWebsite, |
|
| 120 | - $this->access->getConnection()->ldapAttributeAddress, |
|
| 121 | - $this->access->getConnection()->ldapAttributeTwitter, |
|
| 122 | - $this->access->getConnection()->ldapAttributeFediverse, |
|
| 123 | - $this->access->getConnection()->ldapAttributeOrganisation, |
|
| 124 | - $this->access->getConnection()->ldapAttributeRole, |
|
| 125 | - $this->access->getConnection()->ldapAttributeHeadline, |
|
| 126 | - $this->access->getConnection()->ldapAttributeBiography, |
|
| 127 | - $this->access->getConnection()->ldapAttributeBirthDate, |
|
| 128 | - $this->access->getConnection()->ldapAttributePronouns, |
|
| 129 | - ]; |
|
| 100 | + /** |
|
| 101 | + * returns a list of attributes that will be processed further, e.g. quota, |
|
| 102 | + * email, displayname, or others. |
|
| 103 | + * |
|
| 104 | + * @param bool $minimal - optional, set to true to skip attributes with big |
|
| 105 | + * payload |
|
| 106 | + * @return string[] |
|
| 107 | + */ |
|
| 108 | + public function getAttributes($minimal = false) { |
|
| 109 | + $baseAttributes = array_merge(Access::UUID_ATTRIBUTES, ['dn', 'uid', 'samaccountname', 'memberof']); |
|
| 110 | + $attributes = [ |
|
| 111 | + $this->access->getConnection()->ldapExpertUUIDUserAttr, |
|
| 112 | + $this->access->getConnection()->ldapExpertUsernameAttr, |
|
| 113 | + $this->access->getConnection()->ldapQuotaAttribute, |
|
| 114 | + $this->access->getConnection()->ldapEmailAttribute, |
|
| 115 | + $this->access->getConnection()->ldapUserDisplayName, |
|
| 116 | + $this->access->getConnection()->ldapUserDisplayName2, |
|
| 117 | + $this->access->getConnection()->ldapExtStorageHomeAttribute, |
|
| 118 | + $this->access->getConnection()->ldapAttributePhone, |
|
| 119 | + $this->access->getConnection()->ldapAttributeWebsite, |
|
| 120 | + $this->access->getConnection()->ldapAttributeAddress, |
|
| 121 | + $this->access->getConnection()->ldapAttributeTwitter, |
|
| 122 | + $this->access->getConnection()->ldapAttributeFediverse, |
|
| 123 | + $this->access->getConnection()->ldapAttributeOrganisation, |
|
| 124 | + $this->access->getConnection()->ldapAttributeRole, |
|
| 125 | + $this->access->getConnection()->ldapAttributeHeadline, |
|
| 126 | + $this->access->getConnection()->ldapAttributeBiography, |
|
| 127 | + $this->access->getConnection()->ldapAttributeBirthDate, |
|
| 128 | + $this->access->getConnection()->ldapAttributePronouns, |
|
| 129 | + ]; |
|
| 130 | 130 | |
| 131 | - $homeRule = (string)$this->access->getConnection()->homeFolderNamingRule; |
|
| 132 | - if (str_starts_with($homeRule, 'attr:')) { |
|
| 133 | - $attributes[] = substr($homeRule, strlen('attr:')); |
|
| 134 | - } |
|
| 131 | + $homeRule = (string)$this->access->getConnection()->homeFolderNamingRule; |
|
| 132 | + if (str_starts_with($homeRule, 'attr:')) { |
|
| 133 | + $attributes[] = substr($homeRule, strlen('attr:')); |
|
| 134 | + } |
|
| 135 | 135 | |
| 136 | - if (!$minimal) { |
|
| 137 | - // attributes that are not really important but may come with big |
|
| 138 | - // payload. |
|
| 139 | - $attributes = array_merge( |
|
| 140 | - $attributes, |
|
| 141 | - $this->access->getConnection()->resolveRule('avatar') |
|
| 142 | - ); |
|
| 143 | - } |
|
| 136 | + if (!$minimal) { |
|
| 137 | + // attributes that are not really important but may come with big |
|
| 138 | + // payload. |
|
| 139 | + $attributes = array_merge( |
|
| 140 | + $attributes, |
|
| 141 | + $this->access->getConnection()->resolveRule('avatar') |
|
| 142 | + ); |
|
| 143 | + } |
|
| 144 | 144 | |
| 145 | - $attributes = array_reduce($attributes, |
|
| 146 | - function ($list, $attribute) { |
|
| 147 | - $attribute = strtolower(trim((string)$attribute)); |
|
| 148 | - if (!empty($attribute) && !in_array($attribute, $list)) { |
|
| 149 | - $list[] = $attribute; |
|
| 150 | - } |
|
| 145 | + $attributes = array_reduce($attributes, |
|
| 146 | + function ($list, $attribute) { |
|
| 147 | + $attribute = strtolower(trim((string)$attribute)); |
|
| 148 | + if (!empty($attribute) && !in_array($attribute, $list)) { |
|
| 149 | + $list[] = $attribute; |
|
| 150 | + } |
|
| 151 | 151 | |
| 152 | - return $list; |
|
| 153 | - }, |
|
| 154 | - $baseAttributes // hard-coded, lower-case, non-empty attributes |
|
| 155 | - ); |
|
| 152 | + return $list; |
|
| 153 | + }, |
|
| 154 | + $baseAttributes // hard-coded, lower-case, non-empty attributes |
|
| 155 | + ); |
|
| 156 | 156 | |
| 157 | - return $attributes; |
|
| 158 | - } |
|
| 157 | + return $attributes; |
|
| 158 | + } |
|
| 159 | 159 | |
| 160 | - /** |
|
| 161 | - * Checks whether the specified user is marked as deleted |
|
| 162 | - * @param string $id the Nextcloud user name |
|
| 163 | - * @return bool |
|
| 164 | - */ |
|
| 165 | - public function isDeletedUser($id) { |
|
| 166 | - $isDeleted = $this->ocConfig->getUserValue( |
|
| 167 | - $id, 'user_ldap', 'isDeleted', 0); |
|
| 168 | - return (int)$isDeleted === 1; |
|
| 169 | - } |
|
| 160 | + /** |
|
| 161 | + * Checks whether the specified user is marked as deleted |
|
| 162 | + * @param string $id the Nextcloud user name |
|
| 163 | + * @return bool |
|
| 164 | + */ |
|
| 165 | + public function isDeletedUser($id) { |
|
| 166 | + $isDeleted = $this->ocConfig->getUserValue( |
|
| 167 | + $id, 'user_ldap', 'isDeleted', 0); |
|
| 168 | + return (int)$isDeleted === 1; |
|
| 169 | + } |
|
| 170 | 170 | |
| 171 | - /** |
|
| 172 | - * creates and returns an instance of OfflineUser for the specified user |
|
| 173 | - * @param string $id |
|
| 174 | - * @return OfflineUser |
|
| 175 | - */ |
|
| 176 | - public function getDeletedUser($id) { |
|
| 177 | - return new OfflineUser( |
|
| 178 | - $id, |
|
| 179 | - $this->ocConfig, |
|
| 180 | - $this->access->getUserMapper(), |
|
| 181 | - $this->shareManager |
|
| 182 | - ); |
|
| 183 | - } |
|
| 171 | + /** |
|
| 172 | + * creates and returns an instance of OfflineUser for the specified user |
|
| 173 | + * @param string $id |
|
| 174 | + * @return OfflineUser |
|
| 175 | + */ |
|
| 176 | + public function getDeletedUser($id) { |
|
| 177 | + return new OfflineUser( |
|
| 178 | + $id, |
|
| 179 | + $this->ocConfig, |
|
| 180 | + $this->access->getUserMapper(), |
|
| 181 | + $this->shareManager |
|
| 182 | + ); |
|
| 183 | + } |
|
| 184 | 184 | |
| 185 | - /** |
|
| 186 | - * @brief returns a User object by its Nextcloud username |
|
| 187 | - * @param string $id the DN or username of the user |
|
| 188 | - * @return User|OfflineUser|null |
|
| 189 | - */ |
|
| 190 | - protected function createInstancyByUserName($id) { |
|
| 191 | - //most likely a uid. Check whether it is a deleted user |
|
| 192 | - if ($this->isDeletedUser($id)) { |
|
| 193 | - return $this->getDeletedUser($id); |
|
| 194 | - } |
|
| 195 | - $dn = $this->access->username2dn($id); |
|
| 196 | - if ($dn !== false) { |
|
| 197 | - return $this->createAndCache($dn, $id); |
|
| 198 | - } |
|
| 199 | - return null; |
|
| 200 | - } |
|
| 185 | + /** |
|
| 186 | + * @brief returns a User object by its Nextcloud username |
|
| 187 | + * @param string $id the DN or username of the user |
|
| 188 | + * @return User|OfflineUser|null |
|
| 189 | + */ |
|
| 190 | + protected function createInstancyByUserName($id) { |
|
| 191 | + //most likely a uid. Check whether it is a deleted user |
|
| 192 | + if ($this->isDeletedUser($id)) { |
|
| 193 | + return $this->getDeletedUser($id); |
|
| 194 | + } |
|
| 195 | + $dn = $this->access->username2dn($id); |
|
| 196 | + if ($dn !== false) { |
|
| 197 | + return $this->createAndCache($dn, $id); |
|
| 198 | + } |
|
| 199 | + return null; |
|
| 200 | + } |
|
| 201 | 201 | |
| 202 | - /** |
|
| 203 | - * @brief returns a User object by its DN or Nextcloud username |
|
| 204 | - * @param string $id the DN or username of the user |
|
| 205 | - * @return User|OfflineUser|null |
|
| 206 | - * @throws \Exception when connection could not be established |
|
| 207 | - */ |
|
| 208 | - public function get($id) { |
|
| 209 | - $this->checkAccess(); |
|
| 210 | - if (isset($this->usersByDN[$id])) { |
|
| 211 | - return $this->usersByDN[$id]; |
|
| 212 | - } elseif (isset($this->usersByUid[$id])) { |
|
| 213 | - return $this->usersByUid[$id]; |
|
| 214 | - } |
|
| 202 | + /** |
|
| 203 | + * @brief returns a User object by its DN or Nextcloud username |
|
| 204 | + * @param string $id the DN or username of the user |
|
| 205 | + * @return User|OfflineUser|null |
|
| 206 | + * @throws \Exception when connection could not be established |
|
| 207 | + */ |
|
| 208 | + public function get($id) { |
|
| 209 | + $this->checkAccess(); |
|
| 210 | + if (isset($this->usersByDN[$id])) { |
|
| 211 | + return $this->usersByDN[$id]; |
|
| 212 | + } elseif (isset($this->usersByUid[$id])) { |
|
| 213 | + return $this->usersByUid[$id]; |
|
| 214 | + } |
|
| 215 | 215 | |
| 216 | - if ($this->access->stringResemblesDN($id)) { |
|
| 217 | - $uid = $this->access->dn2username($id); |
|
| 218 | - if ($uid !== false) { |
|
| 219 | - return $this->createAndCache($id, $uid); |
|
| 220 | - } |
|
| 221 | - } |
|
| 216 | + if ($this->access->stringResemblesDN($id)) { |
|
| 217 | + $uid = $this->access->dn2username($id); |
|
| 218 | + if ($uid !== false) { |
|
| 219 | + return $this->createAndCache($id, $uid); |
|
| 220 | + } |
|
| 221 | + } |
|
| 222 | 222 | |
| 223 | - return $this->createInstancyByUserName($id); |
|
| 224 | - } |
|
| 223 | + return $this->createInstancyByUserName($id); |
|
| 224 | + } |
|
| 225 | 225 | |
| 226 | - /** |
|
| 227 | - * @brief Checks whether a User object by its DN or Nextcloud username exists |
|
| 228 | - * @param string $id the DN or username of the user |
|
| 229 | - * @throws \Exception when connection could not be established |
|
| 230 | - */ |
|
| 231 | - public function exists($id): bool { |
|
| 232 | - $this->checkAccess(); |
|
| 233 | - $this->logger->debug('Checking if {id} exists', ['id' => $id]); |
|
| 234 | - if (isset($this->usersByDN[$id])) { |
|
| 235 | - return true; |
|
| 236 | - } elseif (isset($this->usersByUid[$id])) { |
|
| 237 | - return true; |
|
| 238 | - } |
|
| 226 | + /** |
|
| 227 | + * @brief Checks whether a User object by its DN or Nextcloud username exists |
|
| 228 | + * @param string $id the DN or username of the user |
|
| 229 | + * @throws \Exception when connection could not be established |
|
| 230 | + */ |
|
| 231 | + public function exists($id): bool { |
|
| 232 | + $this->checkAccess(); |
|
| 233 | + $this->logger->debug('Checking if {id} exists', ['id' => $id]); |
|
| 234 | + if (isset($this->usersByDN[$id])) { |
|
| 235 | + return true; |
|
| 236 | + } elseif (isset($this->usersByUid[$id])) { |
|
| 237 | + return true; |
|
| 238 | + } |
|
| 239 | 239 | |
| 240 | - if ($this->access->stringResemblesDN($id)) { |
|
| 241 | - $this->logger->debug('{id} looks like a dn', ['id' => $id]); |
|
| 242 | - $uid = $this->access->dn2username($id); |
|
| 243 | - if ($uid !== false) { |
|
| 244 | - return true; |
|
| 245 | - } |
|
| 246 | - } |
|
| 240 | + if ($this->access->stringResemblesDN($id)) { |
|
| 241 | + $this->logger->debug('{id} looks like a dn', ['id' => $id]); |
|
| 242 | + $uid = $this->access->dn2username($id); |
|
| 243 | + if ($uid !== false) { |
|
| 244 | + return true; |
|
| 245 | + } |
|
| 246 | + } |
|
| 247 | 247 | |
| 248 | - // Most likely a uid. Check whether it is a deleted user |
|
| 249 | - if ($this->isDeletedUser($id)) { |
|
| 250 | - return true; |
|
| 251 | - } |
|
| 252 | - $dn = $this->access->username2dn($id); |
|
| 253 | - if ($dn !== false) { |
|
| 254 | - return true; |
|
| 255 | - } |
|
| 256 | - return false; |
|
| 257 | - } |
|
| 248 | + // Most likely a uid. Check whether it is a deleted user |
|
| 249 | + if ($this->isDeletedUser($id)) { |
|
| 250 | + return true; |
|
| 251 | + } |
|
| 252 | + $dn = $this->access->username2dn($id); |
|
| 253 | + if ($dn !== false) { |
|
| 254 | + return true; |
|
| 255 | + } |
|
| 256 | + return false; |
|
| 257 | + } |
|
| 258 | 258 | } |