From 9f37e0c44ba82368d5cd7f77e4eaf29d02040e87 Mon Sep 17 00:00:00 2001 From: Mehdi Salem Date: Wed, 9 Oct 2024 18:15:12 +0200 Subject: [PATCH] Axivion: delete Api token if server is removed Fixes: QTCREATORBUG-31669 Change-Id: I646f604c5cf17d77fd7c8880164c27e7a6e61f10 Reviewed-by: Jarek Kobus Reviewed-by: hjk --- src/plugins/axivion/axivionplugin.cpp | 12 ------- src/plugins/axivion/axivionsettings.cpp | 43 +++++++++++++++++++++++++ src/plugins/axivion/axivionsettings.h | 7 ++++ 3 files changed, 50 insertions(+), 12 deletions(-) diff --git a/src/plugins/axivion/axivionplugin.cpp b/src/plugins/axivion/axivionplugin.cpp index 6ec2eca29f0..f72379c7528 100644 --- a/src/plugins/axivion/axivionplugin.cpp +++ b/src/plugins/axivion/axivionplugin.cpp @@ -45,7 +45,6 @@ #include constexpr char s_axivionTextMarkId[] = "AxivionTextMark"; -constexpr char s_axivionKeychainService[] = "keychain.axivion.qtcreator"; using namespace Core; using namespace ProjectExplorer; @@ -114,17 +113,6 @@ static QString apiTokenDescription() return "Automatically created by " + ua + " on " + user + "@" + QSysInfo::machineHostName(); } -static QString escapeKey(const QString &string) -{ - QString escaped = string; - return escaped.replace('\\', "\\\\").replace('@', "\\@"); -} - -static QString credentialKey(const AxivionServer &server) -{ - return escapeKey(server.username) + '@' + escapeKey(server.dashboard); -} - template struct GetDtoStorage { diff --git a/src/plugins/axivion/axivionsettings.cpp b/src/plugins/axivion/axivionsettings.cpp index 6c07811b0a4..ad121d1ae94 100644 --- a/src/plugins/axivion/axivionsettings.cpp +++ b/src/plugins/axivion/axivionsettings.cpp @@ -4,9 +4,12 @@ #include "axivionsettings.h" #include "axiviontr.h" +#include "coreplugin/messagemanager.h" #include +#include #include +#include #include #include @@ -28,6 +31,7 @@ using namespace Core; using namespace Utils; +using namespace Tasking; namespace Axivion::Internal { @@ -252,9 +256,37 @@ bool AxivionSettings::updateDashboardServers(const QList &other, if (selected == oldDefault && m_allServers == other) return false; + + // collect dashserver items that have been removed, + // so we can delete the api tokens from the credentials store + const QStringList previousKeys = Utils::transform(m_allServers, &credentialKey); + const QStringList updatedKeys = Utils::transform(other, &credentialKey); + const QStringList keysToRemove = Utils::filtered(previousKeys, [updatedKeys](const QString &key) { + return !updatedKeys.contains(key); + }); + m_defaultServerId.setValue(selected.toString(), BeQuiet); m_allServers = other; emit changed(); // should we be more detailed? (id) + + const LoopList iterator(keysToRemove); + + const auto onDeleteKeySetup = [iterator](CredentialQuery &query) { + MessageManager::writeSilently(Tr::tr("Axivion: Deleting Api token for %1 as respective " + "dashboard server was removed.").arg(*iterator)); + query.setOperation(CredentialOperation::Delete); + query.setService(s_axivionKeychainService); + query.setKey(*iterator); + }; + + const Group recipe { + For (iterator) >> Do { + CredentialQueryTask(onDeleteKeySetup) + } + }; + + m_taskTreeRunner.start(recipe); + return true; } @@ -263,6 +295,17 @@ const QList AxivionSettings::validPathMappings() const return pathMappingSettings().validPathMappings(); } +static QString escapeKey(const QString &string) +{ + QString escaped = string; + return escaped.replace('\\', "\\\\").replace('@', "\\@"); +} + +QString credentialKey(const AxivionServer &server) +{ + return escapeKey(server.username) + '@' + escapeKey(server.dashboard); +} + // AxivionSettingsPage // may allow some invalid, but does some minimal check for legality diff --git a/src/plugins/axivion/axivionsettings.h b/src/plugins/axivion/axivionsettings.h index 4bedfae7658..6d25e15ca64 100644 --- a/src/plugins/axivion/axivionsettings.h +++ b/src/plugins/axivion/axivionsettings.h @@ -8,12 +8,16 @@ #include +#include + QT_BEGIN_NAMESPACE class QJsonObject; QT_END_NAMESPACE namespace Axivion::Internal { +constexpr char s_axivionKeychainService[] = "keychain.axivion.qtcreator"; + class AxivionServer { public: @@ -64,8 +68,11 @@ public: private: Utils::StringAspect m_defaultServerId{this}; QList m_allServers; + Tasking::TaskTreeRunner m_taskTreeRunner; }; AxivionSettings &settings(); +QString credentialKey(const AxivionServer &server); + } // Axivion::Internal