8#include "ldapconfigwidget.h"
11#include "ldapsearch.h"
13#include "ldap_widgets_debug.h"
15#include <KLineEditEventHandler>
16#include <KLocalizedString>
18#include <KPasswordLineEdit>
20#include <QProgressDialog>
26#include <QRadioButton>
29using namespace KLDAPWidgets;
34 LdapConfigWidgetPrivate(LdapConfigWidget *parent)
37 mainLayout =
new QFormLayout(mParent);
38 mainLayout->setContentsMargins(10, 0, 10, 0);
43 void setAnonymous(
bool on);
44 void setSimple(
bool on);
45 void setSASL(
bool on);
46 void queryDNClicked();
47 void queryMechClicked();
48 void loadData(KLDAPCore::LdapSearch *search,
const KLDAPCore::LdapObject &
object);
49 void loadResult(KLDAPCore::LdapSearch *search);
53 LdapConfigWidget *
const mParent;
57 QLineEdit *mUser =
nullptr;
58 KPasswordLineEdit *mPassword =
nullptr;
59 QLineEdit *mHost =
nullptr;
60 QSpinBox *mPort =
nullptr;
61 QSpinBox *mVersion =
nullptr;
62 QSpinBox *mSizeLimit =
nullptr;
63 QSpinBox *mTimeLimit =
nullptr;
64 QSpinBox *mPageSize =
nullptr;
65 QLineEdit *mDn =
nullptr;
66 QLineEdit *mBindDn =
nullptr;
67 QLineEdit *mRealm =
nullptr;
68 QLineEdit *mFilter =
nullptr;
69 QRadioButton *mAnonymous =
nullptr;
70 QRadioButton *mSimple =
nullptr;
71 QRadioButton *mSASL =
nullptr;
72 QCheckBox *mSubTree =
nullptr;
73 QPushButton *mEditButton =
nullptr;
74 QPushButton *mQueryMech =
nullptr;
75 QRadioButton *mSecNo =
nullptr;
76 QRadioButton *mSecTLS =
nullptr;
77 QRadioButton *mSecSSL =
nullptr;
78 QComboBox *mMech =
nullptr;
80 QProgressDialog *mProg =
nullptr;
82 QFormLayout *mainLayout =
nullptr;
83 WinFlags mFeatures = W_ALL;
84 bool mCancelled =
false;
87void LdapConfigWidget::LdapConfigWidgetPrivate::initWidget()
89 if (mFeatures & W_USER) {
90 mUser =
new QLineEdit(mParent);
92 mUser->setObjectName(
"kcfg_ldapuser"_L1);
94 mainLayout->addRow(
i18n(
"User:"), mUser);
97 if (mFeatures & W_BINDDN) {
98 mBindDn =
new QLineEdit(mParent);
100 mBindDn->setObjectName(
"kcfg_ldapbinddn"_L1);
102 mainLayout->addRow(
i18n(
"Bind DN:"), mBindDn);
105 if (mFeatures & W_REALM) {
106 mRealm =
new QLineEdit(mParent);
108 mRealm->setObjectName(
"kcfg_ldaprealm"_L1);
110 mainLayout->addRow(
i18n(
"Realm:"), mRealm);
113 if (mFeatures & W_PASS) {
114 mPassword =
new KPasswordLineEdit(mParent);
116 mPassword->setObjectName(
"kcfg_ldappassword"_L1);
117 mPassword->setRevealPasswordMode(
KAuthorized::authorize(QStringLiteral(
"lineedit_reveal_password")) ? KPassword::RevealMode::OnlyNew
118 : KPassword::RevealMode::Never);
120 mainLayout->addRow(
i18n(
"Password:"), mPassword);
123 if (mFeatures & W_HOST) {
124 mHost =
new QLineEdit(mParent);
126 mHost->setObjectName(
"kcfg_ldaphost"_L1);
128 mainLayout->addRow(
i18n(
"Host:"), mHost);
131 if (mFeatures & W_PORT) {
132 mPort =
new QSpinBox(mParent);
133 mPort->setRange(0, 65535);
134 mPort->setObjectName(
"kcfg_ldapport"_L1);
135 mPort->setValue(389);
137 mainLayout->addRow(
i18n(
"Port:"), mPort);
140 if (mFeatures & W_VER) {
141 mVersion =
new QSpinBox(mParent);
142 mVersion->setRange(2, 3);
143 mVersion->setObjectName(
"kcfg_ldapver"_L1);
144 mVersion->setValue(3);
145 mainLayout->addRow(
i18n(
"LDAP version:"), mVersion);
148 if (mFeatures & W_SIZELIMIT) {
149 mSizeLimit =
new QSpinBox(mParent);
150 mSizeLimit->setRange(0, 9999999);
151 mSizeLimit->setObjectName(
"kcfg_ldapsizelimit"_L1);
152 mSizeLimit->setValue(0);
153 mSizeLimit->setSpecialValueText(
i18nc(
"default ldap size limit",
"Default"));
154 mainLayout->addRow(
i18n(
"Size limit:"), mSizeLimit);
157 if (mFeatures & W_TIMELIMIT) {
158 mTimeLimit =
new QSpinBox(mParent);
159 mTimeLimit->setRange(0, 9999999);
160 mTimeLimit->setObjectName(
"kcfg_ldaptimelimit"_L1);
161 mTimeLimit->setValue(0);
162 mTimeLimit->setSuffix(
i18n(
" sec"));
163 mTimeLimit->setSpecialValueText(
i18nc(
"default ldap time limit",
"Default"));
164 mainLayout->addRow(
i18n(
"Time limit:"), mTimeLimit);
167 if (mFeatures & W_PAGESIZE) {
168 mPageSize =
new QSpinBox(mParent);
169 mPageSize->setRange(0, 9999999);
170 mPageSize->setObjectName(
"kcfg_ldappagesize"_L1);
171 mPageSize->setValue(0);
172 mPageSize->setSpecialValueText(
i18n(
"No paging"));
173 mainLayout->addRow(
i18n(
"Page size:"), mPageSize);
176 if (mFeatures & W_DN) {
177 auto horizontalLayout =
new QHBoxLayout;
178 mDn =
new QLineEdit(mParent);
180 mDn->setObjectName(
"kcfg_ldapdn"_L1);
181 horizontalLayout->addWidget(mDn);
185 auto dnquery =
new QPushButton(
i18nc(
"@action:button",
"Query Server"), mParent);
186 dnquery->setEnabled(
false);
193 horizontalLayout->addWidget(dnquery);
195 mainLayout->addRow(
i18nc(
"Distinguished Name",
"DN:"), horizontalLayout);
198 if (mFeatures & W_FILTER) {
199 mFilter =
new QLineEdit(mParent);
201 mFilter->setObjectName(
"kcfg_ldapfilter"_L1);
203 mainLayout->addRow(
i18n(
"Filter:"), mFilter);
206 if (mFeatures & W_SECBOX) {
207 auto btgroup =
new QWidget(mParent);
208 btgroup->setContentsMargins({0, 0, 0, 0});
210 auto hbox =
new QHBoxLayout(btgroup);
212 mSecNo =
new QRadioButton(
i18nc(
"@option:radio set no security",
"No"), btgroup);
213 mSecNo->setObjectName(
"kcfg_ldapnosec"_L1);
214 hbox->addWidget(mSecNo);
215 mSecTLS =
new QRadioButton(
i18nc(
"@option:radio use TLS security",
"TLS"), btgroup);
216 mSecTLS->setObjectName(
"kcfg_ldaptls"_L1);
217 hbox->addWidget(mSecTLS);
218 mSecSSL =
new QRadioButton(
i18nc(
"@option:radio use SSL security",
"SSL"), btgroup);
219 mSecSSL->setObjectName(
"kcfg_ldapssl"_L1);
220 hbox->addWidget(mSecSSL);
232 mSecNo->setChecked(
true);
233 mainLayout->addRow(
i18n(
"Security:"), btgroup);
236 if (mFeatures & W_AUTHBOX) {
238 auto authbox =
new QWidget(mParent);
239 authbox->setContentsMargins({0, 0, 0, 0});
241 auto hbox =
new QHBoxLayout(authbox);
243 mAnonymous =
new QRadioButton(
i18nc(
"@option:radio anonymous authentication",
"Anonymous"), authbox);
244 mAnonymous->setObjectName(
"kcfg_ldapanon"_L1);
245 hbox->addWidget(mAnonymous);
246 mSimple =
new QRadioButton(
i18nc(
"@option:radio simple authentication",
"Simple"), authbox);
247 mSimple->setObjectName(
"kcfg_ldapsimple"_L1);
248 hbox->addWidget(mSimple);
249 mSASL =
new QRadioButton(
i18nc(
"@option:radio SASL authentication",
"SASL"), authbox);
250 mSASL->setObjectName(
"kcfg_ldapsasl"_L1);
251 hbox->addWidget(mSASL);
252 mainLayout->addRow(
i18n(
"Authentication:"), authbox);
254 hbox =
new QHBoxLayout;
255 mMech =
new QComboBox(mParent);
256 mMech->setObjectName(
"kcfg_ldapsaslmech"_L1);
257 mMech->addItem(QStringLiteral(
"DIGEST-MD5"));
258 mMech->addItem(QStringLiteral(
"GSSAPI"));
259 mMech->addItem(QStringLiteral(
"PLAIN"));
260 hbox->addWidget(mMech);
264 mQueryMech =
new QPushButton(
i18nc(
"@action:button",
"Query Server"), authbox);
265 hbox->addWidget(mQueryMech);
270 mainLayout->addRow(
i18n(
"SASL mechanism:"), hbox);
282 mAnonymous->setChecked(
true);
286void LdapConfigWidget::LdapConfigWidgetPrivate::sendQuery()
288 KLDAPCore::LdapServer _server(mParent->server());
293 if (mAttr ==
"supportedsaslmechanisms"_L1) {
294 _server.setAuth(KLDAPCore::LdapServer::Anonymous);
297 KLDAPCore::LdapUrl _url(_server.url());
299 _url.setDn(KLDAPCore::LdapDN(
""_L1));
300 _url.setAttributes(QStringList(mAttr));
301 _url.setScope(KLDAPCore::LdapUrl::Base);
303 qCDebug(LDAP_LOG) <<
"sendQuery url:" << _url.toDisplayString();
305 KLDAPCore::LdapSearch search;
313 if (!search.
search(_url)) {
319 mProg =
new QProgressDialog(mParent);
320 mProg->setWindowTitle(
i18nc(
"@title:window",
"LDAP Query"));
321 mProg->setModal(
true);
323 mProg->setLabelText(_url.toDisplayString());
324 mProg->setMaximum(1);
325 mProg->setMinimum(0);
329 qCDebug(LDAP_LOG) <<
"query canceled!";
332 if (search.
error()) {
334 KMessageBox::error(mParent,
i18nc(
"%1 is a url to ldap server",
"Unknown error connecting %1", _url.toDisplayString()));
342void LdapConfigWidget::LdapConfigWidgetPrivate::queryMechClicked()
344 mAttr = QStringLiteral(
"supportedsaslmechanisms");
346 if (!mQResult.isEmpty()) {
349 mMech->addItems(mQResult);
353void LdapConfigWidget::LdapConfigWidgetPrivate::queryDNClicked()
355 mAttr = QStringLiteral(
"namingcontexts");
357 if (!mQResult.isEmpty()) {
358 mDn->setText(mQResult.constFirst());
362void LdapConfigWidget::LdapConfigWidgetPrivate::loadData(KLDAPCore::LdapSearch *,
const KLDAPCore::LdapObject &
object)
364 qCDebug(LDAP_LOG) <<
"object:" <<
object.toString();
365 mProg->setValue(mProg->value() + 1);
375void LdapConfigWidget::LdapConfigWidgetPrivate::loadResult(KLDAPCore::LdapSearch *search)
382void LdapConfigWidget::LdapConfigWidgetPrivate::setAnonymous(
bool on)
388 mUser->setEnabled(
false);
391 mPassword->setEnabled(
false);
394 mBindDn->setEnabled(
false);
397 mRealm->setEnabled(
false);
400 mMech->setEnabled(
false);
403 mQueryMech->setEnabled(
false);
407void LdapConfigWidget::LdapConfigWidgetPrivate::setSimple(
bool on)
413 mUser->setEnabled(
false);
416 mPassword->setEnabled(
true);
419 mBindDn->setEnabled(
true);
422 mRealm->setEnabled(
false);
425 mMech->setEnabled(
false);
428 mQueryMech->setEnabled(
false);
432void LdapConfigWidget::LdapConfigWidgetPrivate::setSASL(
bool on)
438 mUser->setEnabled(
true);
441 mPassword->setEnabled(
true);
444 mBindDn->setEnabled(
true);
447 mRealm->setEnabled(
true);
450 mMech->setEnabled(
true);
453 mQueryMech->setEnabled(
true);
457void LdapConfigWidget::LdapConfigWidgetPrivate::setLDAPPort()
460 mPort->setValue(389);
464void LdapConfigWidget::LdapConfigWidgetPrivate::setLDAPSPort()
467 mPort->setValue(636);
473 , d(new LdapConfigWidgetPrivate(this))
479 , d(new LdapConfigWidgetPrivate(this))
481 d->mFeatures = flags;
503 if (d->mSecSSL && d->mSecSSL->isChecked()) {
505 }
else if (d->mSecTLS && d->mSecTLS->isChecked()) {
512 _server.
setUser(d->mUser->text());
521 _server.
setRealm(d->mRealm->text());
524 _server.
setHost(d->mHost->text());
527 _server.
setPort(d->mPort->value());
530 _server.
setBaseDn(KLDAPCore::LdapDN(d->mDn->text()));
538 if (d->mSizeLimit && d->mSizeLimit->value() != 0) {
541 if (d->mTimeLimit && d->mTimeLimit->value() != 0) {
544 if (d->mPageSize && d->mPageSize->value() != 0) {
547 if (d->mAnonymous && d->mAnonymous->isChecked()) {
548 _server.
setAuth(KLDAPCore::LdapServer::Anonymous);
549 }
else if (d->mSimple && d->mSimple->isChecked()) {
550 _server.
setAuth(KLDAPCore::LdapServer::Simple);
551 }
else if (d->mSASL && d->mSASL->isChecked()) {
552 _server.
setAuth(KLDAPCore::LdapServer::SASL);
553 _server.
setMech(d->mMech->currentText());
560 switch (
server.security()) {
561 case KLDAPCore::LdapServer::SSL:
563 d->mSecSSL->setChecked(
true);
566 case KLDAPCore::LdapServer::TLS:
568 d->mSecTLS->setChecked(
true);
571 case KLDAPCore::LdapServer::None:
573 d->mSecNo->setChecked(
true);
579 case KLDAPCore::LdapServer::Anonymous:
581 d->mAnonymous->setChecked(
true);
584 case KLDAPCore::LdapServer::Simple:
586 d->mSimple->setChecked(
true);
589 case KLDAPCore::LdapServer::SASL:
591 d->mSASL->setChecked(
true);
614 d->mUser->setText(user);
620 return d->mUser ? d->mUser->text() :
QString();
626 d->mPassword->setPassword(password);
632 return d->mPassword ? d->mPassword->password() :
QString();
638 d->mBindDn->setText(binddn);
644 return d->mBindDn ? d->mBindDn->text() :
QString();
650 d->mRealm->setText(realm);
656 return d->mRealm ? d->mRealm->text() :
QString();
662 d->mHost->setText(host);
668 return d->mHost ? d->mHost->text() :
QString();
674 d->mPort->setValue(port);
678int LdapConfigWidget::port()
const
680 return d->mPort ? d->mPort->value() : 389;
686 d->mVersion->setValue(version);
690int LdapConfigWidget::version()
const
692 return d->mVersion ? d->mVersion->value() : 3;
698 d->mDn->setText(dn.toString());
702KLDAPCore::LdapDN LdapConfigWidget::dn()
const
704 return d->mDn ? KLDAPCore::LdapDN(d->mDn->text()) : KLDAPCore::LdapDN();
710 d->mFilter->setText(filter);
716 return d->mFilter ? d->mFilter->text() :
QString();
721 if (d->mMech ==
nullptr) {
724 if (!mech.isEmpty()) {
726 while (i < d->mMech->count()) {
727 if (d->mMech->itemText(i) == mech) {
732 if (i == d->mMech->count()) {
733 d->mMech->addItem(mech);
735 d->mMech->setCurrentIndex(i);
741 return d->mMech ? d->mMech->currentText() :
QString();
748 d->mSecNo->setChecked(
true);
751 d->mSecSSL->setChecked(
true);
754 d->mSecTLS->setChecked(
true);
759LdapConfigWidget::Security LdapConfigWidget::security()
const
761 if (d->mSecTLS->isChecked()) {
764 if (d->mSecSSL->isChecked()) {
774 d->mAnonymous->setChecked(
true);
777 d->mSimple->setChecked(
true);
780 d->mSASL->setChecked(
true);
785LdapConfigWidget::Auth LdapConfigWidget::auth()
const
787 if (d->mSimple->isChecked()) {
790 if (d->mSASL->isChecked()) {
799 d->mSizeLimit->setValue(sizelimit);
803int LdapConfigWidget::sizeLimit()
const
805 return d->mSizeLimit ? d->mSizeLimit->value() : 0;
811 d->mTimeLimit->setValue(timelimit);
815int LdapConfigWidget::timeLimit()
const
817 return d->mTimeLimit ? d->mTimeLimit->value() : 0;
823 d->mPageSize->setValue(pagesize);
827int LdapConfigWidget::pageSize()
const
829 return d->mPageSize ? d->mPageSize->value() : 0;
832LdapConfigWidget::WinFlags LdapConfigWidget::features()
const
837void LdapConfigWidget::setFeatures(LdapConfigWidget::WinFlags features)
839 d->mFeatures = features;
844 const int numberOfChild(ch.
count());
845 for (
int i = 0; i < numberOfChild; ++i) {
847 if (widget && widget->
parent() ==
this) {
856#include "moc_ldapconfigwidget.cpp"
static Q_INVOKABLE bool authorize(const QString &action)
QString errorString() const
Returns the error description of the search operation.
void result(KLDAPCore::LdapSearch *search)
Emitted when the searching finished.
int error() const
Returns the error code of the search operation (0 if no error).
void abandon()
Tries to abandon the search.
void data(KLDAPCore::LdapSearch *search, const KLDAPCore::LdapObject &obj)
Emitted for each result object.
bool search(const LdapServer &server, const QStringList &attributes=QStringList(), int count=0)
Starts a search operation on the LDAP server.
A class that contains LDAP server connection settings.
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.
LdapUrl url() const
Returns the server parameters as an RFC2255 compliant LDAP Url.
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 setRealm(const QString &realm)
Sets the realm of the LDAP connection.
void setUrl(const LdapUrl &url)
Sets the server parameters from an RFC2255 compliant LDAP url.
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.
A special url class for LDAP.
QString i18nc(const char *context, const char *text, const TYPE &arg...)
QString i18n(const char *text, const TYPE &arg...)
void catchReturnKey(QObject *lineEdit)
void error(QWidget *parent, const QString &text, const QString &title, const KGuiItem &buttonOk, Options options=Notify)
const QList< QKeySequence > & end()
void textChanged(const QString &text)
qsizetype count() const const
const QObjectList & children() const const
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
QObject * parent() const const
T qobject_cast(QObject *object)
QString fromUtf8(QByteArrayView str)
bool isEmpty() const const
QString trimmed() const const