KLdap

ldapclientsearchconfigwriteconfigjob.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 "ldapclientsearchconfigwriteconfigjob.h"
8#include "ldapclient_debug.h"
9
10#include "kldapcore/ldapdn.h"
11#include <qt6keychain/keychain.h>
12using namespace QKeychain;
13
14using namespace KLDAPWidgets;
15LdapClientSearchConfigWriteConfigJob::LdapClientSearchConfigWriteConfigJob(QObject *parent)
16 : QObject(parent)
17{
18}
19
20LdapClientSearchConfigWriteConfigJob::~LdapClientSearchConfigWriteConfigJob() = default;
21
22bool LdapClientSearchConfigWriteConfigJob::canStart() const
23{
24 return mServerIndex != -1 && mConfig.isValid();
25}
26
27void LdapClientSearchConfigWriteConfigJob::writeLdapClientConfigFinished()
28{
29 Q_EMIT configSaved();
31}
32
33void LdapClientSearchConfigWriteConfigJob::start()
34{
35 if (!canStart()) {
36 // Failed !
37 writeLdapClientConfigFinished();
38 return;
39 }
40 writeConfig();
41}
42
43bool LdapClientSearchConfigWriteConfigJob::active() const
44{
45 return mActive;
46}
47
48void LdapClientSearchConfigWriteConfigJob::setActive(bool newActive)
49{
50 mActive = newActive;
51}
52
53int LdapClientSearchConfigWriteConfigJob::serverIndex() const
54{
55 return mServerIndex;
56}
57
58void LdapClientSearchConfigWriteConfigJob::setServerIndex(int newServerIndex)
59{
60 mServerIndex = newServerIndex;
61}
62
63KConfigGroup LdapClientSearchConfigWriteConfigJob::config() const
64{
65 return mConfig;
66}
67
68void LdapClientSearchConfigWriteConfigJob::setConfig(const KConfigGroup &newConfig)
69{
70 mConfig = newConfig;
71}
72
73void LdapClientSearchConfigWriteConfigJob::writeConfig()
74{
75 QString prefix;
76 if (mActive) {
77 prefix = QStringLiteral("Selected");
78 }
79
80 mConfig.writeEntry(prefix + QStringLiteral("Host%1").arg(mServerIndex), mServer.host());
81 mConfig.writeEntry(prefix + QStringLiteral("Port%1").arg(mServerIndex), mServer.port());
82 mConfig.writeEntry(prefix + QStringLiteral("Base%1").arg(mServerIndex), mServer.baseDn().toString());
83 mConfig.writeEntry(prefix + QStringLiteral("User%1").arg(mServerIndex), mServer.user());
84 mConfig.writeEntry(prefix + QStringLiteral("Bind%1").arg(mServerIndex), mServer.bindDn());
85
86 const QString passwordEntry = prefix + QStringLiteral("PwdBind%1").arg(mServerIndex);
87 const QString password = mServer.password();
88 if (!password.isEmpty()) {
89 auto writeJob = new WritePasswordJob(QStringLiteral("ldapclient"), this);
90 connect(writeJob, &Job::finished, this, [](QKeychain::Job *baseJob) {
91 if (baseJob->error()) {
92 qCWarning(LDAPCLIENT_LOG) << "Error writing password using QKeychain:" << baseJob->errorString();
93 }
94 });
95 writeJob->setKey(passwordEntry);
96 writeJob->setTextData(password);
97 writeJob->start();
98 }
99
100 mConfig.writeEntry(prefix + QStringLiteral("TimeLimit%1").arg(mServerIndex), mServer.timeLimit());
101 mConfig.writeEntry(prefix + QStringLiteral("SizeLimit%1").arg(mServerIndex), mServer.sizeLimit());
102 mConfig.writeEntry(prefix + QStringLiteral("PageSize%1").arg(mServerIndex), mServer.pageSize());
103 mConfig.writeEntry(prefix + QStringLiteral("Version%1").arg(mServerIndex), mServer.version());
104 QString tmp;
105 switch (mServer.security()) {
106 case KLDAPCore::LdapServer::TLS:
107 tmp = QStringLiteral("TLS");
108 break;
109 case KLDAPCore::LdapServer::SSL:
110 tmp = QStringLiteral("SSL");
111 break;
112 default:
113 tmp = QStringLiteral("None");
114 }
115 mConfig.writeEntry(prefix + QStringLiteral("Security%1").arg(mServerIndex), tmp);
116 switch (mServer.auth()) {
117 case KLDAPCore::LdapServer::Simple:
118 tmp = QStringLiteral("Simple");
119 break;
120 case KLDAPCore::LdapServer::SASL:
121 tmp = QStringLiteral("SASL");
122 break;
123 default:
124 tmp = QStringLiteral("Anonymous");
125 }
126 mConfig.writeEntry(prefix + QStringLiteral("Auth%1").arg(mServerIndex), tmp);
127 mConfig.writeEntry(prefix + QStringLiteral("Mech%1").arg(mServerIndex), mServer.mech());
128 mConfig.writeEntry(prefix + QStringLiteral("UserFilter%1").arg(mServerIndex), mServer.filter().trimmed());
129 if (mServer.completionWeight() > -1) {
130 mConfig.writeEntry(prefix + QStringLiteral("CompletionWeight%1").arg(mServerIndex), mServer.completionWeight());
131 }
132}
133
134KLDAPCore::LdapServer LdapClientSearchConfigWriteConfigJob::server() const
135{
136 return mServer;
137}
138
139void LdapClientSearchConfigWriteConfigJob::setServer(const KLDAPCore::LdapServer &server)
140{
141 mServer = server;
142}
143
144#include "moc_ldapclientsearchconfigwriteconfigjob.cpp"
void writeEntry(const char *key, const char *value, WriteConfigFlags pFlags=Normal)
bool isValid() const
A class that contains LDAP server connection settings.
Definition ldapserver.h:27
Security security() const
Returns the security mode of the LDAP connection.
QString filter() const
Returns the filter string of the LDAP connection.
LdapDN baseDn() const
Returns the baseDn of the LDAP connection.
int timeLimit() const
Returns the time limit of the LDAP connection.
QString password() const
Returns the password of the LDAP connection.
QString bindDn() const
Returns the bindDn of the LDAP connection.
int version() const
Returns the protocol version of the LDAP connection.
int port() const
Returns the port of the LDAP connection.
int sizeLimit() const
Returns the size limit of the LDAP connection.
QString host() const
Returns the host of the LDAP connection.
int pageSize() const
Returns the page size of the LDAP connection.
QString user() const
Returns the user of the LDAP connection.
Auth auth() const
Returns the authentication method of the LDAP connection.
QString mech() const
Returns the mech 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.