Skip to content
Merged
Show file tree
Hide file tree
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
29 changes: 20 additions & 9 deletions lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,11 @@
use OCP\AppFramework\Bootstrap\IRegistrationContext;
use OCP\AppFramework\OCS\OCSBadRequestException;
use OCP\Collaboration\Reference\RenderReferenceEvent;
use OCP\Config\IUserConfig;
use OCP\Files\Events\Node\BeforeNodeDeletedEvent;
use OCP\Files\Events\Node\BeforeNodeRenamedEvent;
use OCP\Group\Events\BeforeGroupDeletedEvent;
use OCP\IAppConfig;
use OCP\IConfig;
use OCP\IGroupManager;
use OCP\IL10N;
Expand All @@ -44,6 +46,7 @@
use OCP\IURLGenerator;
use OCP\IUserManager;
use OCP\IUserSession;
use OCP\Server;
use OCP\User\Events\BeforeUserDeletedEvent;
use OCP\User\Events\UserChangedEvent;
use OCP\User\Events\UserLoggedInEvent;
Expand Down Expand Up @@ -86,6 +89,14 @@ class Application extends App implements IBootstrap {
* @var mixed
*/
private $config;
/**
* @var mixed
*/
private $appConfig;
/**
* @var mixed
*/
private $userConfig;

/**
* Constructor
Expand All @@ -95,8 +106,9 @@ class Application extends App implements IBootstrap {
public function __construct(array $urlParams = []) {
parent::__construct(self::APP_ID, $urlParams);

$container = $this->getContainer();
$this->config = $container->get(IConfig::class);
$this->config = Server::get(IConfig::class);
$this->appConfig = Server::get(IAppConfig::class);
$this->userConfig = Server::get(IUserConfig::class);
}

/**
Expand Down Expand Up @@ -156,18 +168,17 @@ public function registerNavigation(IUserSession $userSession): void {
$user = $userSession->getUser();
if ($user !== null) {
$userId = $user->getUID();
$container = $this->getContainer();

if ($this->config->getUserValue(
if ($this->userConfig->getValueString(
$userId,
self::APP_ID,
'navigation_enabled',
$this->config->getAppValue(self::APP_ID, 'default_enable_navigation', '0')) === '1') {
$openprojectUrl = $this->config->getAppValue(self::APP_ID, 'openproject_instance_url', '');
$this->appConfig->getValueString(self::APP_ID, 'default_enable_navigation', '0')) === '1') {
$openprojectUrl = $this->appConfig->getValueString(self::APP_ID, 'openproject_instance_url', '');
if ($openprojectUrl !== '') {
$container->get(INavigationManager::class)->add(function () use ($container, $openprojectUrl) {
$urlGenerator = $container->get(IURLGenerator::class);
$l10n = $container->get(IL10N::class);
Server::get(INavigationManager::class)->add(function () use ($openprojectUrl) {
$urlGenerator = Server::get(IURLGenerator::class);
$l10n = Server::get(IL10N::class);
return [
'id' => self::APP_ID,

Expand Down
114 changes: 57 additions & 57 deletions lib/Controller/ConfigController.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\DataResponse;
use OCP\AppFramework\Http\RedirectResponse;
use OCP\Config\IUserConfig;
use OCP\DB\Exception as DBException;
use OCP\IConfig;
use OCP\IAppConfig;
use OCP\IL10N;
use OCP\IRequest;
use OCP\IURLGenerator;
Expand All @@ -36,9 +37,13 @@
class ConfigController extends Controller {

/**
* @var IConfig
* @var IAppConfig
*/
private $config;
private $appConfig;
/**
* @var IUserConfig
*/
private $userConfig;
/**
* @var IURLGenerator
*/
Expand Down Expand Up @@ -75,7 +80,8 @@ class ConfigController extends Controller {
public function __construct(
string $appName,
IRequest $request,
IConfig $config,
IAppConfig $appConfig,
IUserConfig $userConfig,
IURLGenerator $urlGenerator,
IUserManager $userManager,
IL10N $l,
Expand All @@ -87,7 +93,8 @@ public function __construct(
?string $userId
) {
parent::__construct($appName, $request);
$this->config = $config;
$this->appConfig = $appConfig;
$this->userConfig = $userConfig;
$this->urlGenerator = $urlGenerator;
$this->userManager = $userManager;
$this->l = $l;
Expand All @@ -106,12 +113,12 @@ public function clearUserInfo(?string $userId = null) {
if ($userId === null) {
$userId = $this->userId;
}
$this->config->deleteUserValue($userId, Application::APP_ID, 'token');
$this->config->deleteUserValue($userId, Application::APP_ID, 'login');
$this->config->deleteUserValue($userId, Application::APP_ID, 'user_id');
$this->config->deleteUserValue($userId, Application::APP_ID, 'user_name');
$this->config->deleteUserValue($userId, Application::APP_ID, 'refresh_token');
$this->config->deleteUserValue($userId, Application::APP_ID, 'token_expires_at');
$this->userConfig->deleteUserConfig($userId, Application::APP_ID, 'token');
$this->userConfig->deleteUserConfig($userId, Application::APP_ID, 'login');
$this->userConfig->deleteUserConfig($userId, Application::APP_ID, 'user_id');
$this->userConfig->deleteUserConfig($userId, Application::APP_ID, 'user_name');
$this->userConfig->deleteUserConfig($userId, Application::APP_ID, 'refresh_token');
$this->userConfig->deleteUserConfig($userId, Application::APP_ID, 'token_expires_at');
}

/**
Expand All @@ -123,7 +130,7 @@ private function logAccountConnected(): void {
'User %s has connected the OpenProject account %s in application ' . Application::APP_ID,
[
'userId' => $this->userId,
'openprojectUserId' => (string)$this->config->getUserValue($this->userId, Application::APP_ID, 'user_id'),
'openprojectUserId' => $this->userConfig->getValueString($this->userId, Application::APP_ID, 'user_id'),
],
);
}
Expand All @@ -133,17 +140,17 @@ private function logAccountConnected(): void {
*/
public function resetOauth2Configs(): void {
// for oauth2 reset we reset openproject client credential as well as nextcloud client credential
$this->config->setAppValue(Application::APP_ID, 'openproject_client_id', "");
$this->config->setAppValue(Application::APP_ID, 'openproject_client_secret', "");
$this->appConfig->setValueString(Application::APP_ID, 'openproject_client_id', "");
$this->appConfig->setValueString(Application::APP_ID, 'openproject_client_secret', "");
$this->deleteOauthClient();
}

/**
* @return void
*/
public function resetOIDCConfigs(): void {
$this->config->setAppValue(Application::APP_ID, 'oidc_provider', "");
$this->config->setAppValue(Application::APP_ID, 'targeted_audience_client_id', "");
$this->appConfig->setValueString(Application::APP_ID, 'oidc_provider', "");
$this->appConfig->setValueString(Application::APP_ID, 'targeted_audience_client_id', "");
}


Expand All @@ -156,7 +163,7 @@ public function resetOIDCConfigs(): void {
*/
public function setConfig(array $values): DataResponse {
foreach ($values as $key => $value) {
$this->config->setUserValue($this->userId, Application::APP_ID, $key, trim($value));
$this->userConfig->setValueString($this->userId, Application::APP_ID, $key, trim($value));
}
$result = [];

Expand Down Expand Up @@ -211,10 +218,10 @@ private function setIntegrationConfig(array $values): array {
$appPassword = $this->openprojectAPIService->generateAppPasswordTokenForUser($isAppPasswordBeingReplaced);
}
$oldClientId = $oldClientSecret = '';
$oldOpenProjectOauthUrl = $this->config->getAppValue(
$oldOpenProjectOauthUrl = $this->appConfig->getValueString(
Application::APP_ID, 'openproject_instance_url', ''
);
$oldAuthMethod = $this->config->getAppValue(
$oldAuthMethod = $this->appConfig->getValueString(
Application::APP_ID, 'authorization_method', ''
);
// when 'openproject_instance_url' && 'authorization_method' does not have a value at the same time,
Expand All @@ -238,10 +245,10 @@ private function setIntegrationConfig(array $values): array {
if (
(key_exists('openproject_client_id', $values) && key_exists('openproject_client_secret', $values))
) {
$oldClientId = $this->config->getAppValue(
$oldClientId = $this->appConfig->getValueString(
Application::APP_ID, 'openproject_client_id', ''
);
$oldClientSecret = $this->config->getAppValue(
$oldClientSecret = $this->appConfig->getValueString(
Application::APP_ID, 'openproject_client_secret', ''
);
}
Expand All @@ -262,10 +269,10 @@ private function setIntegrationConfig(array $values): array {
key_exists('oidc_provider', $values) &&
key_exists('targeted_audience_client_id', $values)
) {
$oldOidcProvider = $this->config->getAppValue(
$oldOidcProvider = $this->appConfig->getValueString(
Application::APP_ID, 'oidc_provider', ''
);
$oldTargetedAudienceClient = $this->config->getAppValue(
$oldTargetedAudienceClient = $this->appConfig->getValueString(
Application::APP_ID, 'targeted_audience_client_id', ''
);
$runningOIDCReset = (
Expand All @@ -279,7 +286,7 @@ private function setIntegrationConfig(array $values): array {
if ($key === 'setup_project_folder' || $key === 'setup_app_password') {
continue;
}
$this->config->setAppValue(Application::APP_ID, $key, trim((string)$value));
$this->appConfig->setValueString(Application::APP_ID, $key, trim((string)$value));
}

// if the OpenProject OAuth URL has changed
Expand All @@ -294,7 +301,7 @@ private function setIntegrationConfig(array $values): array {
$this->deleteOauthClient();
} else {
// otherwise just change the redirect URI for the existing OAuth client
$oauthClientInternalId = $this->config->getAppValue(
$oauthClientInternalId = $this->appConfig->getValueString(
Application::APP_ID, 'nc_oauth_client_id', ''
);
$this->oauthService->setClientRedirectUri(
Expand Down Expand Up @@ -338,7 +345,7 @@ private function setIntegrationConfig(array $values): array {
// whenever doing full reset we want the project folder switch state to be "on" in the UI
// so setting `fresh_project_folder_setup` as true
if ($runningFullReset) {
$this->config->setAppValue(Application::APP_ID, 'fresh_project_folder_setup', "1");
$this->appConfig->setValueString(Application::APP_ID, 'fresh_project_folder_setup', "1");
$this->openprojectAPIService->logToAuditLog(
Operation::IntegrationReset,
'OpenProject Integration admin configuration has been reset in application ' . Application::APP_ID,
Expand All @@ -347,7 +354,7 @@ private function setIntegrationConfig(array $values): array {
} elseif (key_exists('setup_app_password', $values) && key_exists('setup_project_folder', $values)) {
// for other cases when api has key 'setup_app_password' and 'setup_project_folder' we set it to false
// assuming user has either fully set the integration or partially without project folder/app password
$this->config->setAppValue(Application::APP_ID, 'fresh_project_folder_setup', "0");
$this->appConfig->setValueString(Application::APP_ID, 'fresh_project_folder_setup', "0");
}

$savedSettings = array_values(array_diff(array_keys($values), ['setup_project_folder', 'setup_app_password']));
Expand All @@ -366,7 +373,7 @@ private function setIntegrationConfig(array $values): array {
}

return [
"status" => OpenProjectAPIService::isAdminConfigOk($this->config),
"status" => OpenProjectAPIService::isAdminConfigOk($this->appConfig),
"oPUserAppPassword" => $appPassword,
];
}
Expand Down Expand Up @@ -422,13 +429,13 @@ public function setAdminConfig(array $values): DataResponse {
* @throws PreConditionNotMetException
*/
public function oauthRedirect(string $code = '', string $state = ''): RedirectResponse {
$configState = $this->config->getUserValue($this->userId, Application::APP_ID, 'oauth_state');
$clientID = $this->config->getAppValue(Application::APP_ID, 'openproject_client_id');
$clientSecret = $this->config->getAppValue(Application::APP_ID, 'openproject_client_secret');
$codeVerifier = $this->config->getUserValue(
$this->userId, Application::APP_ID, 'code_verifier', false
$configState = $this->userConfig->getValueString($this->userId, Application::APP_ID, 'oauth_state');
$clientID = $this->appConfig->getValueString(Application::APP_ID, 'openproject_client_id');
$clientSecret = $this->appConfig->getValueString(Application::APP_ID, 'openproject_client_secret');
$codeVerifier = $this->userConfig->getValueString(
$this->userId, Application::APP_ID, 'code_verifier', ''
);
$oauthJourneyStartingPage = $this->config->getUserValue(
$oauthJourneyStartingPage = $this->userConfig->getValueString(
$this->userId, Application::APP_ID, 'oauth_journey_starting_page'
);

Expand Down Expand Up @@ -460,21 +467,14 @@ public function oauthRedirect(string $code = '', string $state = ''): RedirectRe
}

// anyway, reset state
$this->config->deleteUserValue($this->userId, Application::APP_ID, 'oauth_state');
$this->userConfig->deleteUserConfig($this->userId, Application::APP_ID, 'oauth_state');

$validCodeVerifier = false;
if (is_string($codeVerifier)) {
$validCodeVerifier = (preg_match('/^[A-Za-z0-9\-._~]{43,128}$/', $codeVerifier) === 1);
}

$validClientSecret = false;
if (is_string($clientSecret)) {
$validClientSecret = (preg_match('/^.{10,}$/', $clientSecret) === 1);
}
$validCodeVerifier = (preg_match('/^[A-Za-z0-9\-._~]{43,128}$/', $codeVerifier) === 1);
$validClientSecret = (preg_match('/^.{10,}$/', $clientSecret) === 1);

$errorMessage = '';
if ($clientID && $validClientSecret && $validCodeVerifier && $configState !== '' && $configState === $state) {
$openprojectUrl = $this->config->getAppValue(Application::APP_ID, 'openproject_instance_url');
$openprojectUrl = $this->appConfig->getValueString(Application::APP_ID, 'openproject_instance_url');
$result = $this->openprojectAPIService->requestOAuthAccessToken(
$this->userId,
$openprojectUrl,
Expand All @@ -491,7 +491,7 @@ public function oauthRedirect(string $code = '', string $state = ''): RedirectRe
// set user info
$userInfo = $this->openprojectAPIService->initUserInfo($this->userId, $result['access_token']);
if (isset($userInfo['user_name'])) {
$this->config->setUserValue(
$this->userConfig->setValueString(
$this->userId, Application::APP_ID, 'oauth_connection_result', 'success'
);
$this->logAccountConnected();
Expand All @@ -515,10 +515,10 @@ public function oauthRedirect(string $code = '', string $state = ''): RedirectRe
}
$errorMessage = $this->l->t('Error during OAuth exchanges');
}
$this->config->setUserValue(
$this->userConfig->setValueString(
$this->userId, Application::APP_ID, 'oauth_connection_result', 'error'
);
$this->config->setUserValue(
$this->userConfig->setValueString(
$this->userId, Application::APP_ID, 'oauth_connection_error_message', $errorMessage
);
return new RedirectResponse($newUrl);
Expand All @@ -536,7 +536,7 @@ public function autoOauthCreation(): DataResponse {
* @return bool whether an OAuth client was configured
*/
private function deleteOauthClient(): bool {
$oauthClientInternalId = $this->config->getAppValue(
$oauthClientInternalId = $this->appConfig->getValueString(
Application::APP_ID, 'nc_oauth_client_id', ''
);
if ($oauthClientInternalId !== '') {
Expand All @@ -545,7 +545,7 @@ private function deleteOauthClient(): bool {
$this->clientService->deleteClient($id);
} catch (ClientNotFoundException $e) {
}
$this->config->deleteAppValue(Application::APP_ID, 'nc_oauth_client_id');
$this->appConfig->deleteKey(Application::APP_ID, 'nc_oauth_client_id');
return true;
}
return false;
Expand Down Expand Up @@ -575,7 +575,7 @@ public function checkAdminConfigOk(): DataResponse {
// 1. config without project folder set up (which is compulsory for integration)
// 2. config with project folder set up (which is optional for admin)
return new DataResponse([
'config_status_without_project_folder' => OpenProjectAPIService::isAdminConfigOk($this->config),
'config_status_without_project_folder' => OpenProjectAPIService::isAdminConfigOk($this->appConfig),
'project_folder_setup_status' => $appPasswordSetStatus
]);
}
Expand Down Expand Up @@ -612,7 +612,7 @@ public function setUpIntegration(?array $values): DataResponse {

if ($values['authorization_method'] === Application::AUTH_METHOD_OAUTH) {
$response = \array_merge($response, $this->recreateOauthClientInformation());
$response['status'] = OpenProjectAPIService::isAdminConfigOk($this->config);
$response['status'] = OpenProjectAPIService::isAdminConfigOk($this->appConfig);
}

if ($setup['oPUserAppPassword']) {
Expand Down Expand Up @@ -660,18 +660,18 @@ public function updateIntegration(?array $values): DataResponse {
$this->settingsService->validateAdminSettingsForm($values);
$setup = $this->setIntegrationConfig($values);
$response = ['status' => $setup['status']];
$authMethod = $this->config->getAppValue(Application::APP_ID, 'authorization_method', '');
$authMethod = $this->appConfig->getValueString(Application::APP_ID, 'authorization_method', '');

if ($authMethod === Application::AUTH_METHOD_OIDC) {
return new DataResponse($response);
}

$oauthDbId = (int)$this->config->getAppValue(Application::APP_ID, 'nc_oauth_client_id', '');
$oauthDbId = (int)$this->appConfig->getValueString(Application::APP_ID, 'nc_oauth_client_id', '');
if ($oauthDbId) {
$response = \array_merge($response, $this->oauthService->getClientInfo($oauthDbId));
} else {
$response = \array_merge($response, $this->recreateOauthClientInformation());
$response['status'] = OpenProjectAPIService::isAdminConfigOk($this->config);
$response['status'] = OpenProjectAPIService::isAdminConfigOk($this->appConfig);
}

if ($setup['oPUserAppPassword']) {
Expand Down Expand Up @@ -723,9 +723,9 @@ public function resetIntegration(): DataResponse {
*/
private function recreateOauthClientInformation(): array {
$replaced = $this->deleteOauthClient();
$opUrl = $this->config->getAppValue(Application::APP_ID, 'openproject_instance_url', '');
$opUrl = $this->appConfig->getValueString(Application::APP_ID, 'openproject_instance_url', '');
$clientInfo = $this->oauthService->createNcOauthClient('OpenProject client', rtrim($opUrl, '/') .'/oauth_clients/%s/callback');
$this->config->setAppValue(Application::APP_ID, 'nc_oauth_client_id', $clientInfo['id']);
$this->appConfig->setValueString(Application::APP_ID, 'nc_oauth_client_id', $clientInfo['id']);
$this->openprojectAPIService->logToAuditLog(
Operation::OauthClientCreated,
$replaced
Expand Down
Loading
Loading