Implement attribute mapping events

Signed-off-by: Julius Härtl <jus@bitgrid.net>
This commit is contained in:
Julius Härtl 2021-06-30 13:26:01 +02:00
Родитель 6c0d5cee4e
Коммит deabeae781
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4C614C6ED2CDE6DF
2 изменённых файлов: 88 добавлений и 17 удалений

Просмотреть файл

@ -25,6 +25,7 @@ declare(strict_types=1);
namespace OCA\UserOIDC\Controller;
use OCA\UserOIDC\Event\AttributeMappedEvent;
use OCA\UserOIDC\Service\ProviderService;
use OCA\UserOIDC\Vendor\Firebase\JWT\JWK;
use OCA\UserOIDC\Vendor\Firebase\JWT\JWT;
@ -36,8 +37,8 @@ use OCP\AppFramework\Http;
use OCP\AppFramework\Http\JSONResponse;
use OCP\AppFramework\Http\RedirectResponse;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\Http\Client\IClientService;
use OCP\IConfig;
use OCP\ILogger;
use OCP\IRequest;
use OCP\ISession;
@ -77,17 +78,14 @@ class LoginController extends Controller {
/** @var ProviderMapper */
private $providerMapper;
/**
* @var ILogger
*/
/** @var IEventDispatcher */
private $eventDispatcher;
/** @var ILogger */
private $logger;
/**
* @var IConfig
*/
private $config;
/**
* @var ProviderService
*/
/** @var ProviderService */
private $providerService;
public function __construct(
@ -102,7 +100,7 @@ class LoginController extends Controller {
IUserSession $userSession,
IUserManager $userManager,
ITimeFactory $timeFactory,
IConfig $config,
IEventDispatcher $eventDispatcher,
ILogger $logger
) {
parent::__construct(Application::APP_ID, $request);
@ -115,9 +113,9 @@ class LoginController extends Controller {
$this->userSession = $userSession;
$this->userManager = $userManager;
$this->timeFactory = $timeFactory;
$this->config = $config;
$this->providerMapper = $providerMapper;
$this->providerService = $providerService;
$this->eventDispatcher = $eventDispatcher;
$this->logger = $logger;
}
@ -239,7 +237,9 @@ class LoginController extends Controller {
if (!isset($payload->{$uidAttribute})) {
return new JSONResponse($payload);
}
$backendUser = $this->userMapper->getOrCreate($providerId, $payload->{$uidAttribute});
$event = new AttributeMappedEvent(ProviderService::SETTING_MAPPING_UID, $payload->{$uidAttribute});
$this->eventDispatcher->dispatchTyped($event);
$backendUser = $this->userMapper->getOrCreate($providerId, $event->getValue());
$this->logger->debug('User obtained: ' . $backendUser->getUserId());
@ -247,6 +247,9 @@ class LoginController extends Controller {
$displaynameAttribute = $this->providerService->getSetting($providerId, ProviderService::SETTING_MAPPING_DISPLAYNAME, 'name');
if (isset($payload->{$displaynameAttribute})) {
$newDisplayName = mb_substr($payload->{$displaynameAttribute}, 0, 255);
$event = new AttributeMappedEvent(ProviderService::SETTING_MAPPING_DISPLAYNAME, $newDisplayName);
$this->eventDispatcher->dispatchTyped($event);
$newDisplayName = $event->getValue();
if ($newDisplayName != $backendUser->getDisplayName()) {
$backendUser->setDisplayName($payload->{$displaynameAttribute});
@ -264,13 +267,17 @@ class LoginController extends Controller {
// Update e-mail
$emailAttribute = $this->providerService->getSetting($providerId, ProviderService::SETTING_MAPPING_EMAIL, 'email');
if (isset($payload->{$emailAttribute})) {
$event = new AttributeMappedEvent(ProviderService::SETTING_MAPPING_EMAIL, $payload->{$emailAttribute});
$this->eventDispatcher->dispatchTyped($event);
$this->logger->debug('Updating e-mail');
$user->setEMailAddress($payload->{$emailAttribute});
$user->setEMailAddress($event->getValue());
}
$quotaAttribute = $this->providerService->getSetting($providerId, ProviderService::SETTING_MAPPING_EMAIL, 'quota');
$quotaAttribute = $this->providerService->getSetting($providerId, ProviderService::SETTING_MAPPING_QUOTA, 'quota');
if (isset($payload->{$quotaAttribute})) {
$user->setQuota($payload->{$quotaAttribute});
$event = new AttributeMappedEvent(ProviderService::SETTING_MAPPING_QUOTA, $payload->{$quotaAttribute});
$this->eventDispatcher->dispatchTyped($event);
$user->setQuota($event->getValue());
}
$this->logger->debug('Logging user in');

Просмотреть файл

@ -0,0 +1,64 @@
<?php
/*
* @copyright Copyright (c) 2021 Julius Härtl <jus@bitgrid.net>
*
* @author Julius Härtl <jus@bitgrid.net>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
declare(strict_types=1);
namespace OCA\UserOIDC\Event;
use OCP\EventDispatcher\Event;
/**
* Event to provide custom mapping logic based on the OIDC token data
* In order to avoid further processing the event propagation should be stopped
* in the listener after processing as the value might get overwritten afterwards
* by other listeners through $event->stopPropagation();
*/
class AttributeMappedEvent extends Event {
/** @var string */
private $attribute;
/** @var string */
private $value;
public function __construct(string $attribute, string $value) {
parent::__construct();
$this->attribute = $attribute;
$this->value = $value;
}
/**
* @return string One of the ProviderService::SETTING_MAPPING_* constants for the attribute mapping that is currently processed
*/
public function getAttribute(): string {
return $this->attribute;
}
public function getValue(): string {
return $this->value;
}
public function setValue(string $value): void {
$this->value = $value;
}
}