KLdap

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

KDE's Doxygen guidelines are available online.