2020-09-18 11:55:47 +00:00
|
|
|
<?php
|
2022-04-15 11:34:01 +00:00
|
|
|
|
2020-09-18 11:55:47 +00:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
/**
|
2024-09-08 13:46:22 +00:00
|
|
|
* SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-or-later
|
2020-09-18 11:55:47 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
namespace OCA\Social\Db;
|
|
|
|
|
|
|
|
use DateTime;
|
|
|
|
use Exception;
|
2020-09-21 10:53:54 +00:00
|
|
|
use OCA\Social\Exceptions\ClientNotFoundException;
|
2020-09-18 11:55:47 +00:00
|
|
|
use OCA\Social\Model\Client\SocialClient;
|
|
|
|
use OCA\Social\Service\ClientService;
|
2023-05-25 20:45:28 +00:00
|
|
|
use OCA\Social\Tools\Traits\TArrayTools;
|
2020-09-18 11:55:47 +00:00
|
|
|
use OCP\DB\QueryBuilder\IQueryBuilder;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class ClientAppRequest
|
|
|
|
*
|
|
|
|
* @package OCA\Social\Db
|
|
|
|
*/
|
|
|
|
class ClientRequest extends ClientRequestBuilder {
|
|
|
|
use TArrayTools;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Insert a new OAuth client in the database.
|
2022-04-15 16:13:33 +00:00
|
|
|
* @throws \OCP\DB\Exception
|
2020-09-18 11:55:47 +00:00
|
|
|
*/
|
2022-04-15 16:13:33 +00:00
|
|
|
public function saveApp(SocialClient $client): void {
|
2020-09-18 11:55:47 +00:00
|
|
|
$qb = $this->getClientInsertSql();
|
|
|
|
$qb->setValue('app_name', $qb->createNamedParameter($client->getAppName()))
|
2024-10-28 12:53:06 +00:00
|
|
|
->setValue('app_website', $qb->createNamedParameter($client->getAppWebsite()))
|
|
|
|
->setValue(
|
|
|
|
'app_redirect_uris', $qb->createNamedParameter(json_encode($client->getAppRedirectUris()))
|
|
|
|
)
|
|
|
|
->setValue('app_client_id', $qb->createNamedParameter($client->getAppClientId()))
|
|
|
|
->setValue('app_client_secret', $qb->createNamedParameter($client->getAppClientSecret()))
|
|
|
|
->setValue('app_scopes', $qb->createNamedParameter(json_encode($client->getAppScopes())));
|
2020-09-18 11:55:47 +00:00
|
|
|
|
|
|
|
try {
|
2020-09-21 10:53:54 +00:00
|
|
|
$dt = new DateTime('now');
|
|
|
|
$qb->setValue('last_update', $qb->createNamedParameter($dt, IQueryBuilder::PARAM_DATE));
|
|
|
|
$qb->setValue('creation', $qb->createNamedParameter($dt, IQueryBuilder::PARAM_DATE));
|
2020-09-18 11:55:47 +00:00
|
|
|
} catch (Exception $e) {
|
|
|
|
}
|
|
|
|
|
2022-04-15 16:13:33 +00:00
|
|
|
$qb->executeStatement();
|
2020-09-18 11:55:47 +00:00
|
|
|
|
|
|
|
$client->setId($qb->getLastInsertId());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param SocialClient $client
|
|
|
|
*/
|
2022-04-15 16:13:33 +00:00
|
|
|
public function authClient(SocialClient $client): void {
|
2020-09-18 11:55:47 +00:00
|
|
|
$qb = $this->getClientUpdateSql();
|
|
|
|
$qb->set('auth_code', $qb->createNamedParameter($client->getAuthCode()));
|
|
|
|
$qb->set('auth_scopes', $qb->createNamedParameter(json_encode($client->getAuthScopes())));
|
|
|
|
$qb->set('auth_account', $qb->createNamedParameter($client->getAuthAccount()));
|
|
|
|
$qb->set('auth_user_id', $qb->createNamedParameter($client->getAuthUserId()));
|
|
|
|
|
|
|
|
$qb->limitToId($client->getId());
|
|
|
|
|
2022-04-15 16:13:33 +00:00
|
|
|
$qb->executeStatement();
|
2020-09-18 11:55:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param SocialClient $client
|
|
|
|
*/
|
2022-04-15 16:13:33 +00:00
|
|
|
public function updateToken(SocialClient $client): void {
|
2020-09-18 11:55:47 +00:00
|
|
|
$qb = $this->getClientUpdateSql();
|
|
|
|
$qb->set('token', $qb->createNamedParameter($client->getToken()));
|
|
|
|
$qb->set('auth_code', $qb->createNamedParameter(''));
|
|
|
|
|
|
|
|
$qb->limitToId($client->getId());
|
|
|
|
|
|
|
|
$qb->execute();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param SocialClient $client
|
|
|
|
*/
|
2022-04-15 16:13:33 +00:00
|
|
|
public function updateTime(SocialClient $client): void {
|
2020-09-18 11:55:47 +00:00
|
|
|
$now = new DateTime('now');
|
|
|
|
$client->setLastUpdate($now->getTimestamp());
|
|
|
|
|
|
|
|
$qb = $this->getClientUpdateSql();
|
|
|
|
$qb->set('last_update', $qb->createNamedParameter($now, IQueryBuilder::PARAM_DATE));
|
|
|
|
|
|
|
|
$qb->limitToId($client->getId());
|
|
|
|
|
|
|
|
$qb->execute();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $clientId
|
|
|
|
*
|
|
|
|
* @return SocialClient
|
2020-09-21 10:53:54 +00:00
|
|
|
* @throws ClientNotFoundException
|
2020-09-18 11:55:47 +00:00
|
|
|
*/
|
|
|
|
public function getFromClientId(string $clientId): SocialClient {
|
|
|
|
$qb = $this->getClientSelectSql();
|
|
|
|
$qb->limitToAppClientId($clientId);
|
|
|
|
|
|
|
|
return $this->getClientFromRequest($qb);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $token
|
|
|
|
*
|
|
|
|
* @return SocialClient
|
2020-09-21 10:53:54 +00:00
|
|
|
* @throws ClientNotFoundException
|
2020-09-18 11:55:47 +00:00
|
|
|
*/
|
|
|
|
public function getFromToken(string $token): SocialClient {
|
|
|
|
$qb = $this->getClientSelectSql();
|
|
|
|
$qb->limitToToken($token);
|
|
|
|
|
|
|
|
return $this->getClientFromRequest($qb);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
|
|
|
public function deprecateToken() {
|
|
|
|
$qb = $this->getClientDeleteSql();
|
|
|
|
|
|
|
|
$date = new DateTime();
|
|
|
|
$date->setTimestamp(time() - ClientService::TIME_TOKEN_TTL);
|
|
|
|
$qb->limitToDBFieldDateTime('last_update', $date, true);
|
|
|
|
|
|
|
|
$qb->execute();
|
|
|
|
}
|
|
|
|
}
|