Skip to content
Draft
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
'OCA\\Provisioning_API\\Controller\\UsersController' => $baseDir . '/../lib/Controller/UsersController.php',
'OCA\\Provisioning_API\\Controller\\VerificationController' => $baseDir . '/../lib/Controller/VerificationController.php',
'OCA\\Provisioning_API\\FederatedShareProviderFactory' => $baseDir . '/../lib/FederatedShareProviderFactory.php',
'OCA\\Provisioning_API\\Listener\\UserDataCacheListener' => $baseDir . '/../lib/Listener/UserDataCacheListener.php',
'OCA\\Provisioning_API\\Listener\\UserDeletedListener' => $baseDir . '/../lib/Listener/UserDeletedListener.php',
'OCA\\Provisioning_API\\Middleware\\Exceptions\\NotSubAdminException' => $baseDir . '/../lib/Middleware/Exceptions/NotSubAdminException.php',
'OCA\\Provisioning_API\\Middleware\\ProvisioningApiMiddleware' => $baseDir . '/../lib/Middleware/ProvisioningApiMiddleware.php',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class ComposerStaticInitProvisioning_API
'OCA\\Provisioning_API\\Controller\\UsersController' => __DIR__ . '/..' . '/../lib/Controller/UsersController.php',
'OCA\\Provisioning_API\\Controller\\VerificationController' => __DIR__ . '/..' . '/../lib/Controller/VerificationController.php',
'OCA\\Provisioning_API\\FederatedShareProviderFactory' => __DIR__ . '/..' . '/../lib/FederatedShareProviderFactory.php',
'OCA\\Provisioning_API\\Listener\\UserDataCacheListener' => __DIR__ . '/..' . '/../lib/Listener/UserDataCacheListener.php',
'OCA\\Provisioning_API\\Listener\\UserDeletedListener' => __DIR__ . '/..' . '/../lib/Listener/UserDeletedListener.php',
'OCA\\Provisioning_API\\Middleware\\Exceptions\\NotSubAdminException' => __DIR__ . '/..' . '/../lib/Middleware/Exceptions/NotSubAdminException.php',
'OCA\\Provisioning_API\\Middleware\\ProvisioningApiMiddleware' => __DIR__ . '/..' . '/../lib/Middleware/ProvisioningApiMiddleware.php',
Expand Down
6 changes: 6 additions & 0 deletions apps/provisioning_api/lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

use OC\Group\Manager as GroupManager;
use OCA\Provisioning_API\Capabilities;
use OCA\Provisioning_API\Listener\UserDataCacheListener;
use OCA\Provisioning_API\Listener\UserDeletedListener;
use OCA\Provisioning_API\Middleware\ProvisioningApiMiddleware;
use OCA\Settings\Mailer\NewUserMailHelper;
Expand All @@ -27,6 +28,8 @@
use OCP\Mail\IMailer;
use OCP\Security\ICrypto;
use OCP\Security\ISecureRandom;
use OCP\User\Events\PasswordUpdatedEvent;
use OCP\User\Events\UserChangedEvent;
use OCP\User\Events\UserDeletedEvent;
use OCP\Util;
use Psr\Container\ContainerInterface;
Expand All @@ -37,7 +40,10 @@ public function __construct(array $urlParams = []) {
}

public function register(IRegistrationContext $context): void {
$context->registerEventListener(UserChangedEvent::class, UserDataCacheListener::class);
$context->registerEventListener(UserDeletedEvent::class, UserDataCacheListener::class);
$context->registerEventListener(UserDeletedEvent::class, UserDeletedListener::class);
$context->registerEventListener(PasswordUpdatedEvent::class, UserDataCacheListener::class);

$context->registerService(NewUserMailHelper::class, function (ContainerInterface $c) {
return new NewUserMailHelper(
Expand Down
20 changes: 18 additions & 2 deletions apps/provisioning_api/lib/Controller/AUserDataOCSController.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
use OCP\Files\IRootFolder;
use OCP\Files\NotFoundException;
use OCP\Group\ISubAdmin;
use OCP\ICache;
use OCP\ICacheFactory;
use OCP\IConfig;
use OCP\IRequest;
use OCP\IUser;
Expand Down Expand Up @@ -50,6 +52,10 @@ abstract class AUserDataOCSController extends OCSController {
public const USER_FIELD_MANAGER = 'manager';
public const USER_FIELD_NOTIFICATION_EMAIL = 'notify_email';

private const CACHE_TTL = 300; // 5 minutes

private ICache $cache;

public function __construct(
string $appName,
IRequest $request,
Expand All @@ -61,8 +67,10 @@ public function __construct(
protected ISubAdmin $subAdminManager,
protected IFactory $l10nFactory,
protected IRootFolder $rootFolder,
ICacheFactory $cacheFactory,
) {
parent::__construct($appName, $request);
$this->cache = $cacheFactory->createDistributed('provisioning_api');
}

/**
Expand All @@ -79,8 +87,6 @@ protected function getUserData(string $userId, bool $includeScopes = false): ?ar
$currentLoggedInUser = $this->userSession->getUser();
assert($currentLoggedInUser !== null, 'No user logged in');

$data = [];

// Check if the target user exists
$targetUserObject = $this->userManager->get($userId);
if ($targetUserObject === null) {
Expand All @@ -89,6 +95,15 @@ protected function getUserData(string $userId, bool $includeScopes = false): ?ar

$isAdmin = $this->groupManager->isAdmin($currentLoggedInUser->getUID());
$isDelegatedAdmin = $this->groupManager->isDelegatedAdmin($currentLoggedInUser->getUID());

$cacheKey = 'user_data_' . $userId . '_' . ($isAdmin || $isDelegatedAdmin ? 'admin' : 'noadmin') . ($includeScopes ? '_scoped' : '');
/** @var Provisioning_APIUserDetails|null $cached */
$cached = $this->cache->get($cacheKey);
if ($cached !== null) {
return $cached;
}

$data = [];
if ($isAdmin
|| $isDelegatedAdmin
|| $this->groupManager->getSubAdmin()->isUserAccessible($currentLoggedInUser, $targetUserObject)) {
Expand Down Expand Up @@ -197,6 +212,7 @@ protected function getUserData(string $userId, bool $includeScopes = false): ?ar
'setPassword' => $backend instanceof ISetPasswordBackend || $backend->implementsActions(Backend::SET_PASSWORD),
];

$this->cache->set($cacheKey, $data, self::CACHE_TTL);
return $data;
}

Expand Down
3 changes: 3 additions & 0 deletions apps/provisioning_api/lib/Controller/GroupsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use OCP\AppFramework\OCSController;
use OCP\Files\IRootFolder;
use OCP\Group\ISubAdmin;
use OCP\ICacheFactory;
use OCP\IConfig;
use OCP\IGroup;
use OCP\IGroupManager;
Expand Down Expand Up @@ -51,6 +52,7 @@ public function __construct(
IFactory $l10nFactory,
IRootFolder $rootFolder,
private LoggerInterface $logger,
ICacheFactory $cacheFactory,
) {
parent::__construct($appName,
$request,
Expand All @@ -62,6 +64,7 @@ public function __construct(
$subAdminManager,
$l10nFactory,
$rootFolder,
$cacheFactory,
);
}

Expand Down
Loading
Loading