Skip to content

Commit

Permalink
fix: Avoid slow queries in scenarios where we do not need a search
Browse files Browse the repository at this point in the history
Signed-off-by: Julius Knorr <[email protected]>
  • Loading branch information
juliusknorr committed Jan 13, 2025
1 parent 60d84ee commit 25c551c
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 4 deletions.
11 changes: 7 additions & 4 deletions lib/Controller/LoginController.php
Original file line number Diff line number Diff line change
Expand Up @@ -467,10 +467,13 @@ public function code(string $state = '', string $code = '', string $scope = '',

$autoProvisionAllowed = (!isset($oidcSystemConfig['auto_provision']) || $oidcSystemConfig['auto_provision']);

// in case user is provisioned by user_ldap, userManager->search() triggers an ldap search which syncs the results
// so new users will be directly available even if they were not synced before this login attempt
$this->userManager->search($userId);
$this->ldapService->syncUser($userId);
if (!$this->provisioningService->hasOidcUserProvisitioned($userId) && $this->ldapService->isLDAPEnabled()) {
// in case user is provisioned by user_ldap, userManager->search() triggers an ldap search which syncs the results
// so new users will be directly available even if they were not synced before this login attempt
$this->userManager->search($userId, 1, 0);
$this->ldapService->syncUser($userId);
}

$userFromOtherBackend = $this->userManager->get($userId);
if ($userFromOtherBackend !== null && $this->ldapService->isLdapDeletedUser($userFromOtherBackend)) {
$userFromOtherBackend = null;
Expand Down
10 changes: 10 additions & 0 deletions lib/Service/LdapService.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

namespace OCA\UserOIDC\Service;

use OCP\App\IAppManager;
use OCP\AppFramework\QueryException;
use OCP\IUser;
use Psr\Log\LoggerInterface;
Expand All @@ -16,16 +17,25 @@ class LdapService {

public function __construct(
private LoggerInterface $logger,
private IAppManager $appManager,
) {
}

public function isLDAPEnabled(): bool {
return $this->appManager->isAppLoaded('user_ldap');

Check failure on line 25 in lib/Service/LdapService.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis

UndefinedInterfaceMethod

lib/Service/LdapService.php:25:29: UndefinedInterfaceMethod: Method OCP\App\IAppManager::isAppLoaded does not exist (see https://psalm.dev/181)
}

/**
* @param IUser $user
* @return bool
* @throws \Psr\Container\ContainerExceptionInterface
* @throws \Psr\Container\NotFoundExceptionInterface
*/
public function isLdapDeletedUser(IUser $user): bool {
if ($this->isLDAPEnabled()) {
return false;
}

$className = $user->getBackendClassName();
if ($className !== 'LDAP') {
return false;
Expand Down
11 changes: 11 additions & 0 deletions lib/Service/ProvisioningService.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
use OCA\UserOIDC\Db\UserMapper;
use OCA\UserOIDC\Event\AttributeMappedEvent;
use OCP\Accounts\IAccountManager;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Db\MultipleObjectsReturnedException;
use OCP\DB\Exception;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\Http\Client\IClientService;
Expand Down Expand Up @@ -40,6 +42,15 @@ public function __construct(
) {
}

public function hasOidcUserProvisitioned(string $userId): bool {
try {
$this->userMapper->getUser($userId);
return true;
} catch (DoesNotExistException|MultipleObjectsReturnedException) {
}
return false;
}

/**
* @param string $tokenUserId
* @param int $providerId
Expand Down

0 comments on commit 25c551c

Please sign in to comment.