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 "ldap_core_debug.h"
9
10#include "kldapcore/ldapdn.h"
11#include <qt6keychain/keychain.h>
12using namespace QKeychain;
13
14using namespace KLDAPCore;
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(LDAP_CORE_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 mConfig.writeEntry(prefix + QStringLiteral("Activities%1").arg(mServerIndex), mServer.activities());
105 mConfig.writeEntry(prefix + QStringLiteral("EnabledActivities%1").arg(mServerIndex), mServer.enablePlasmaActivities());
106
107 QString tmp;
108 switch (mServer.security()) {
109 case KLDAPCore::LdapServer::TLS:
110 tmp = QStringLiteral("TLS");
111 break;
112 case KLDAPCore::LdapServer::SSL:
113 tmp = QStringLiteral("SSL");
114 break;
115 default:
116 tmp = QStringLiteral("None");
117 }
118 mConfig.writeEntry(prefix + QStringLiteral("Security%1").arg(mServerIndex), tmp);
119 switch (mServer.auth()) {
120 case KLDAPCore::LdapServer::Simple:
121 tmp = QStringLiteral("Simple");
122 break;
123 case KLDAPCore::LdapServer::SASL:
124 tmp = QStringLiteral("SASL");
125 break;
126 default:
127 tmp = QStringLiteral("Anonymous");
128 }
129 mConfig.writeEntry(prefix + QStringLiteral("Auth%1").arg(mServerIndex), tmp);
130 mConfig.writeEntry(prefix + QStringLiteral("Mech%1").arg(mServerIndex), mServer.mech());
131 mConfig.writeEntry(prefix + QStringLiteral("UserFilter%1").arg(mServerIndex), mServer.filter().trimmed());
132 if (mServer.completionWeight() > -1) {
133 mConfig.writeEntry(prefix + QStringLiteral("CompletionWeight%1").arg(mServerIndex), mServer.completionWeight());
134 }
135}
136
137KLDAPCore::LdapServer LdapClientSearchConfigWriteConfigJob::server() const
138{
139 return mServer;
140}
141
142void LdapClientSearchConfigWriteConfigJob::setServer(const KLDAPCore::LdapServer &server)
143{
144 mServer = server;
145}
146
147#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()
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 Sat Dec 21 2024 17:03:36 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.