File indexing completed on 2025-01-26 04:52:15

0001 /*
0002  * SPDX-FileCopyrightText: 2020-2024 Laurent Montel <montel@kde.org>
0003  *
0004  * SPDX-License-Identifier: LGPL-2.0-or-later
0005  */
0006 
0007 #include "ldapclientsearchconfigwriteconfigjob.h"
0008 #include "ldapclient_debug.h"
0009 
0010 #include "kldapcore/ldapdn.h"
0011 #include <qt6keychain/keychain.h>
0012 using namespace QKeychain;
0013 
0014 using namespace KLDAPWidgets;
0015 LdapClientSearchConfigWriteConfigJob::LdapClientSearchConfigWriteConfigJob(QObject *parent)
0016     : QObject(parent)
0017 {
0018 }
0019 
0020 LdapClientSearchConfigWriteConfigJob::~LdapClientSearchConfigWriteConfigJob() = default;
0021 
0022 bool LdapClientSearchConfigWriteConfigJob::canStart() const
0023 {
0024     return mServerIndex != -1 && mConfig.isValid();
0025 }
0026 
0027 void LdapClientSearchConfigWriteConfigJob::writeLdapClientConfigFinished()
0028 {
0029     Q_EMIT configSaved();
0030     deleteLater();
0031 }
0032 
0033 void LdapClientSearchConfigWriteConfigJob::start()
0034 {
0035     if (!canStart()) {
0036         // Failed !
0037         writeLdapClientConfigFinished();
0038         return;
0039     }
0040     writeConfig();
0041 }
0042 
0043 bool LdapClientSearchConfigWriteConfigJob::active() const
0044 {
0045     return mActive;
0046 }
0047 
0048 void LdapClientSearchConfigWriteConfigJob::setActive(bool newActive)
0049 {
0050     mActive = newActive;
0051 }
0052 
0053 int LdapClientSearchConfigWriteConfigJob::serverIndex() const
0054 {
0055     return mServerIndex;
0056 }
0057 
0058 void LdapClientSearchConfigWriteConfigJob::setServerIndex(int newServerIndex)
0059 {
0060     mServerIndex = newServerIndex;
0061 }
0062 
0063 KConfigGroup LdapClientSearchConfigWriteConfigJob::config() const
0064 {
0065     return mConfig;
0066 }
0067 
0068 void LdapClientSearchConfigWriteConfigJob::setConfig(const KConfigGroup &newConfig)
0069 {
0070     mConfig = newConfig;
0071 }
0072 
0073 void LdapClientSearchConfigWriteConfigJob::writeConfig()
0074 {
0075     QString prefix;
0076     if (mActive) {
0077         prefix = QStringLiteral("Selected");
0078     }
0079 
0080     mConfig.writeEntry(prefix + QStringLiteral("Host%1").arg(mServerIndex), mServer.host());
0081     mConfig.writeEntry(prefix + QStringLiteral("Port%1").arg(mServerIndex), mServer.port());
0082     mConfig.writeEntry(prefix + QStringLiteral("Base%1").arg(mServerIndex), mServer.baseDn().toString());
0083     mConfig.writeEntry(prefix + QStringLiteral("User%1").arg(mServerIndex), mServer.user());
0084     mConfig.writeEntry(prefix + QStringLiteral("Bind%1").arg(mServerIndex), mServer.bindDn());
0085 
0086     const QString passwordEntry = prefix + QStringLiteral("PwdBind%1").arg(mServerIndex);
0087     const QString password = mServer.password();
0088     if (!password.isEmpty()) {
0089         auto writeJob = new WritePasswordJob(QStringLiteral("ldapclient"), this);
0090         connect(writeJob, &Job::finished, this, [](QKeychain::Job *baseJob) {
0091             if (baseJob->error()) {
0092                 qCWarning(LDAPCLIENT_LOG) << "Error writing password using QKeychain:" << baseJob->errorString();
0093             }
0094         });
0095         writeJob->setKey(passwordEntry);
0096         writeJob->setTextData(password);
0097         writeJob->start();
0098     }
0099 
0100     mConfig.writeEntry(prefix + QStringLiteral("TimeLimit%1").arg(mServerIndex), mServer.timeLimit());
0101     mConfig.writeEntry(prefix + QStringLiteral("SizeLimit%1").arg(mServerIndex), mServer.sizeLimit());
0102     mConfig.writeEntry(prefix + QStringLiteral("PageSize%1").arg(mServerIndex), mServer.pageSize());
0103     mConfig.writeEntry(prefix + QStringLiteral("Version%1").arg(mServerIndex), mServer.version());
0104     QString tmp;
0105     switch (mServer.security()) {
0106     case KLDAPCore::LdapServer::TLS:
0107         tmp = QStringLiteral("TLS");
0108         break;
0109     case KLDAPCore::LdapServer::SSL:
0110         tmp = QStringLiteral("SSL");
0111         break;
0112     default:
0113         tmp = QStringLiteral("None");
0114     }
0115     mConfig.writeEntry(prefix + QStringLiteral("Security%1").arg(mServerIndex), tmp);
0116     switch (mServer.auth()) {
0117     case KLDAPCore::LdapServer::Simple:
0118         tmp = QStringLiteral("Simple");
0119         break;
0120     case KLDAPCore::LdapServer::SASL:
0121         tmp = QStringLiteral("SASL");
0122         break;
0123     default:
0124         tmp = QStringLiteral("Anonymous");
0125     }
0126     mConfig.writeEntry(prefix + QStringLiteral("Auth%1").arg(mServerIndex), tmp);
0127     mConfig.writeEntry(prefix + QStringLiteral("Mech%1").arg(mServerIndex), mServer.mech());
0128     mConfig.writeEntry(prefix + QStringLiteral("UserFilter%1").arg(mServerIndex), mServer.filter().trimmed());
0129     if (mServer.completionWeight() > -1) {
0130         mConfig.writeEntry(prefix + QStringLiteral("CompletionWeight%1").arg(mServerIndex), mServer.completionWeight());
0131     }
0132 }
0133 
0134 KLDAPCore::LdapServer LdapClientSearchConfigWriteConfigJob::server() const
0135 {
0136     return mServer;
0137 }
0138 
0139 void LdapClientSearchConfigWriteConfigJob::setServer(const KLDAPCore::LdapServer &server)
0140 {
0141     mServer = server;
0142 }
0143 
0144 #include "moc_ldapclientsearchconfigwriteconfigjob.cpp"