KLdap

ldapclientsearchconfigreadconfigjob.cpp
1/*
2 * SPDX-FileCopyrightText: 2020-2025 Laurent Montel <montel@kde.org>
3 *
4 * SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6
7#include "ldapclientsearchconfigreadconfigjob.h"
8
9#include "ldap_core_debug.h"
10
11#include "kldapcore/ldapdn.h"
12#include <KConfig>
13#include <KLocalizedString>
14#include <qt6keychain/keychain.h>
15using namespace QKeychain;
16using namespace Qt::Literals::StringLiterals;
17
18using namespace KLDAPCore;
19LdapClientSearchConfigReadConfigJob::LdapClientSearchConfigReadConfigJob(QObject *parent)
20 : QObject(parent)
21{
22}
23
24LdapClientSearchConfigReadConfigJob::~LdapClientSearchConfigReadConfigJob() = default;
25
26bool LdapClientSearchConfigReadConfigJob::canStart() const
27{
28 return mServerIndex != -1 && mConfig.isValid();
29}
30
31void LdapClientSearchConfigReadConfigJob::readLdapClientConfigFinished()
32{
33 Q_EMIT configLoaded(mServer);
35}
36
37void LdapClientSearchConfigReadConfigJob::start()
38{
39 if (!canStart()) {
40 // Failed !
41 readLdapClientConfigFinished();
42 return;
43 }
44 readConfig();
45}
46
47bool LdapClientSearchConfigReadConfigJob::active() const
48{
49 return mActive;
50}
51
52void LdapClientSearchConfigReadConfigJob::setActive(bool newActive)
53{
54 mActive = newActive;
55}
56
57int LdapClientSearchConfigReadConfigJob::serverIndex() const
58{
59 return mServerIndex;
60}
61
62void LdapClientSearchConfigReadConfigJob::setServerIndex(int newServerIndex)
63{
64 mServerIndex = newServerIndex;
65}
66
67KConfigGroup LdapClientSearchConfigReadConfigJob::config() const
68{
69 return mConfig;
70}
71
72void LdapClientSearchConfigReadConfigJob::setConfig(const KConfigGroup &newConfig)
73{
74 mConfig = newConfig;
75}
76
77void LdapClientSearchConfigReadConfigJob::readConfig()
78{
79 QString prefix;
80 if (mActive) {
81 prefix = QStringLiteral("Selected");
82 }
83
84 const QString host = mConfig.readEntry(prefix + QStringLiteral("Host%1").arg(mServerIndex), QString()).trimmed();
85 if (!host.isEmpty()) {
86 mServer.setHost(host);
87 }
88
89 const int port = mConfig.readEntry(prefix + QStringLiteral("Port%1").arg(mServerIndex), 389);
90 mServer.setPort(port);
91
92 const QString base = mConfig.readEntry(prefix + QStringLiteral("Base%1").arg(mServerIndex), QString()).trimmed();
93 if (!base.isEmpty()) {
94 mServer.setBaseDn(KLDAPCore::LdapDN(base));
95 }
96
97 const QString user = mConfig.readEntry(prefix + QStringLiteral("User%1").arg(mServerIndex), QString()).trimmed();
98 if (!user.isEmpty()) {
99 mServer.setUser(user);
100 }
101
102 const QString bindDN = mConfig.readEntry(prefix + QStringLiteral("Bind%1").arg(mServerIndex), QString()).trimmed();
103 if (!bindDN.isEmpty()) {
104 mServer.setBindDn(bindDN);
105 }
106 mServer.setTimeLimit(mConfig.readEntry(prefix + QStringLiteral("TimeLimit%1").arg(mServerIndex), 0));
107 mServer.setSizeLimit(mConfig.readEntry(prefix + QStringLiteral("SizeLimit%1").arg(mServerIndex), 0));
108 mServer.setPageSize(mConfig.readEntry(prefix + QStringLiteral("PageSize%1").arg(mServerIndex), 0));
109 mServer.setVersion(mConfig.readEntry(prefix + QStringLiteral("Version%1").arg(mServerIndex), 3));
110
111 QString tmp = mConfig.readEntry(prefix + QStringLiteral("Security%1").arg(mServerIndex), QStringLiteral("None"));
112 mServer.setSecurity(KLDAPCore::LdapServer::None);
113 if (tmp == "SSL"_L1) {
114 mServer.setSecurity(KLDAPCore::LdapServer::SSL);
115 } else if (tmp == "TLS"_L1) {
116 mServer.setSecurity(KLDAPCore::LdapServer::TLS);
117 }
118
119 tmp = mConfig.readEntry(prefix + QStringLiteral("Auth%1").arg(mServerIndex), QStringLiteral("Anonymous"));
120 mServer.setAuth(KLDAPCore::LdapServer::Anonymous);
121 if (tmp == "Simple"_L1) {
122 mServer.setAuth(KLDAPCore::LdapServer::Simple);
123 } else if (tmp == "SASL"_L1) {
124 mServer.setAuth(KLDAPCore::LdapServer::SASL);
125 }
126
127 mServer.setMech(mConfig.readEntry(prefix + QStringLiteral("Mech%1").arg(mServerIndex), QString()));
128 mServer.setFilter(mConfig.readEntry(prefix + QStringLiteral("UserFilter%1").arg(mServerIndex), QString()));
129 mServer.setCompletionWeight(mConfig.readEntry(prefix + QStringLiteral("CompletionWeight%1").arg(mServerIndex), -1));
130 mServer.setActivities(mConfig.readEntry(prefix + QStringLiteral("Activities%1").arg(mServerIndex), QStringList()));
131 mServer.setEnablePlasmaActivities(mConfig.readEntry(prefix + QStringLiteral("EnabledActivities%1").arg(mServerIndex), false));
132
133 const QString pwdBindBNEntry = prefix + QStringLiteral("PwdBind%1").arg(mServerIndex);
134
135 auto readJob = new ReadPasswordJob(QStringLiteral("ldapclient"), this);
136 connect(readJob, &Job::finished, this, [this, pwdBindBNEntry](QKeychain::Job *baseJob) {
137 auto job = qobject_cast<ReadPasswordJob *>(baseJob);
138 Q_ASSERT(job);
139 if (!job->error()) {
140 mServer.setPassword(job->textData());
141 } else {
142 qCWarning(LDAP_CORE_LOG) << "We have an error during reading password " << job->errorString() << " password key " << pwdBindBNEntry;
143 }
144 readLdapClientConfigFinished();
145 });
146 readJob->setKey(pwdBindBNEntry);
147 readJob->start();
148}
149
150#include "moc_ldapclientsearchconfigreadconfigjob.cpp"
QString readEntry(const char *key, const char *aDefault=nullptr) const
Q_EMITQ_EMIT
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
void deleteLater()
T qobject_cast(QObject *object)
QString arg(Args &&... args) const const
bool isEmpty() const const
QString trimmed() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2025 The KDE developers.
Generated on Fri Jan 24 2025 11:52:20 by doxygen 1.13.2 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.