Skip to content
Merged
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
99 changes: 45 additions & 54 deletions lib/Db/ProviderMapper.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

declare(strict_types=1);

/**
* SPDX-FileCopyrightText: 2020 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
Expand All @@ -9,11 +10,9 @@
namespace OCA\UserOIDC\Db;

use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Db\Entity;
use OCP\AppFramework\Db\MultipleObjectsReturnedException;
use OCP\AppFramework\Db\QBMapper;
use OCP\DB\Exception;

use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\IDBConnection;

Expand All @@ -26,47 +25,40 @@ public function __construct(IDBConnection $db) {
}

/**
* @param int $id
* @return Provider
* @throws \OCP\AppFramework\Db\DoesNotExistException
* @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException
* @throws DoesNotExistException
* @throws MultipleObjectsReturnedException
*/
public function getProvider(int $id): Provider {
$qb = $this->db->getQueryBuilder();

$qb->select('*')
->from($this->getTableName())
->where(
$qb->expr()->eq('id', $qb->createNamedParameter($id, IQueryBuilder::PARAM_INT))
);
->where($qb->expr()->eq('id', $qb->createNamedParameter($id, IQueryBuilder::PARAM_INT)));

return $this->findEntity($qb);
}

/**
* Find provider by provider identifier, the admin-given name for
* the provider configuration.
* @param string $identifier
* @return Provider
* @throws \OCP\AppFramework\Db\DoesNotExistException
* @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException
*
* @throws DoesNotExistException
* @throws MultipleObjectsReturnedException
*/
public function findProviderByIdentifier(string $identifier): Provider {
$qb = $this->db->getQueryBuilder();

$qb->select('*')
->from($this->getTableName())
->where(
$qb->expr()->eq('identifier', $qb->createNamedParameter($identifier, IQueryBuilder::PARAM_STR))
);
->where($qb->expr()->eq('identifier', $qb->createNamedParameter($identifier, IQueryBuilder::PARAM_STR)));

return $this->findEntity($qb);
}

/**
* @return Provider[]
*/
public function getProviders() {
public function getProviders(): array {
$qb = $this->db->getQueryBuilder();

$qb->select('*')
Expand All @@ -78,58 +70,57 @@ public function getProviders() {
/**
* Create or update provider settings
*
* @param string $identifier
* @param string|null $clientid
* @param string|null $clientsecret
* @param string|null $discoveryuri
* @param string $scope
* @param string|null $endsessionendpointuri
* @param string|null $postLogoutUri
* @return Provider|Entity
* @throws DoesNotExistException
* @throws Exception
* @throws MultipleObjectsReturnedException
*/
public function createOrUpdateProvider(string $identifier, ?string $clientid = null,
?string $clientsecret = null, ?string $discoveryuri = null, string $scope = 'openid email profile',
?string $endsessionendpointuri = null, ?string $postLogoutUri = null) {
public function createOrUpdateProvider(
string $identifier,
?string $clientId = null,
?string $clientSecret = null,
?string $discoveryUri = null,
string $scope = 'openid email profile',
?string $endSessionEndpointUri = null,
?string $postLogoutUri = null,
): Provider {
try {
$provider = $this->findProviderByIdentifier($identifier);
} catch (DoesNotExistException $eNotExist) {
$provider = null;
}

if ($provider === null) {
$provider = new Provider();
if (($clientid === null) || ($clientsecret === null) || ($discoveryuri === null)) {
throw new DoesNotExistException('Provider must be created. All provider parameters required.');
// Update existing provider
if ($clientId !== null) {
$provider->setClientId($clientId);
}
$provider->setIdentifier($identifier);
$provider->setClientId($clientid);
$provider->setClientSecret($clientsecret);
$provider->setDiscoveryEndpoint($discoveryuri);
$provider->setEndSessionEndpoint($endsessionendpointuri);
$provider->setPostLogoutUri($postLogoutUri);
$provider->setScope($scope);
return $this->insert($provider);
} else {
if ($clientid !== null) {
$provider->setClientId($clientid);
}
if ($clientsecret !== null) {
$provider->setClientSecret($clientsecret);
if ($clientSecret !== null) {
$provider->setClientSecret($clientSecret);
}
if ($discoveryuri !== null) {
$provider->setDiscoveryEndpoint($discoveryuri);
if ($discoveryUri !== null) {
$provider->setDiscoveryEndpoint($discoveryUri);
}
if ($endsessionendpointuri !== null) {
$provider->setEndSessionEndpoint($endsessionendpointuri ?: null);
if ($endSessionEndpointUri !== null) {
$provider->setEndSessionEndpoint($endSessionEndpointUri);
}
if ($postLogoutUri !== null) {
$provider->setPostLogoutUri($postLogoutUri ?: null);
$provider->setPostLogoutUri($postLogoutUri);
}
$provider->setScope($scope);

return $this->update($provider);
} catch (DoesNotExistException $e) {
// Create new provider
if ($clientId === null || $clientSecret === null || $discoveryUri === null) {
throw new DoesNotExistException('Provider must be created. All provider parameters required.');
}

$provider = new Provider();
$provider->setIdentifier($identifier);
$provider->setClientId($clientId);
$provider->setClientSecret($clientSecret);
$provider->setDiscoveryEndpoint($discoveryUri);
$provider->setEndSessionEndpoint($endSessionEndpointUri);
$provider->setPostLogoutUri($postLogoutUri);
$provider->setScope($scope);

return $this->insert($provider);
}
}
}
Loading