Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: do no hit LDAP if no exposed attribute is configured #823

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)

## [Unreleased] - xxxx-xx-xx

### Fixed

- [#823](https://github.com/owncloud/user_ldap/pull/823) - Don't hit the LDAP server if no exposed attribute is configured


## [0.19.0] - 2024-01-18
Expand Down
7 changes: 7 additions & 0 deletions lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,13 @@ public function registerEventListener() {

try {
$attrs = $uProxy->getExposedAttributes($targetUser->getUID());
if ($attrs === null || $attrs === false) {
// either there are no exposed attributes or the user isn't found in LDAP
// if there are no exposed attributes configured, we won't contact the LDAP server,
// so we don't know if the user really exists.
$event->setAttributes('user_ldap_state', 'Unknown');
return;
}

$event->setAttributes('user_ldap_state', 'OK');
foreach ($attrs as $key => $value) {
Expand Down
1 change: 1 addition & 0 deletions lib/User/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ public function getConnection() {
* The `getAttributes` method will contain these exposed attributes, and all
* of them will be requested. This list is public in order to know what
* attributes can be exposed to the outside.
* @return string[]
*/
public function getExposedAttributes() {
$ldapConfig = $this->getConnection();
Expand Down
11 changes: 8 additions & 3 deletions lib/User_LDAP.php
Original file line number Diff line number Diff line change
Expand Up @@ -413,16 +413,21 @@ public function getAvatar($uid) {
* only the first value will be returned.
*
* @param string $uid
* @return array<string,string>|false key -> value map containing the attributes
* and their values. False if the user isn't found
* @return array<string,string>|false|null key -> value map containing the
* attributes and their values. False if the user isn't found. Null if no
* attribute is configured
*/
public function getExposedAttributes($uid) {
$exposedAttrs = $this->userManager->getExposedAttributes();
if (\count($exposedAttrs) === 0) {
return null;
}

$userEntry = $this->userManager->getCachedEntry($uid);
if ($userEntry === null) {
return false;
}

$exposedAttrs = $this->userManager->getExposedAttributes();
$attrs = [];
foreach ($exposedAttrs as $attr) {
$attrs[$attr] = $userEntry->getAttribute($attr);
Expand Down
8 changes: 8 additions & 0 deletions tests/unit/User_LDAPTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -392,10 +392,18 @@ public function testGetExposedAttributes() {

public function testGetExposedAttributesMissingEntry() {
$this->manager->method('getCachedEntry')->willReturn(null);
$this->manager->method('getExposedAttributes')->willReturn(['attr1']);

$this->assertFalse($this->backend->getExposedAttributes('usertest'));
}

public function testGetExposedAttributesNoAttrConfigured() {
$this->manager->method('getCachedEntry')->willReturn(null);
$this->manager->method('getExposedAttributes')->willReturn([]);

$this->assertNull($this->backend->getExposedAttributes('usertest'));
}

public function testClearConnectionCache() {
$connection = $this->createMock(Connection::class);
$connection->expects($this->once())->method('clearCache');
Expand Down