ldapconfigwidget.cpp 21.7 KB
Newer Older
1
/*
Allen Winter's avatar
Allen Winter committed
2
  This file is part of libkldap.
3
  SPDX-FileCopyrightText: 2004-2006 Szombathelyi György <gyurco@freemail.hu>
Allen Winter's avatar
Allen Winter committed
4

5
  SPDX-License-Identifier: LGPL-2.0-or-later
6
7
*/

Allen Winter's avatar
Allen Winter committed
8
9
#include "ldapconfigwidget.h"
#include "ldapsearch.h"
10

Laurent Montel's avatar
Laurent Montel committed
11
#include "ldap_debug.h"
12
#include <KAuthorized>
13
#include <KLocalizedString>
Laurent Montel's avatar
Laurent Montel committed
14
#include <KPasswordLineEdit>
Laurent Montel's avatar
Laurent Montel committed
15
16
17
#include <QComboBox>
#include <QProgressDialog>
#include <kmessagebox.h>
18

19
#include <QCheckBox>
20
#include <QFormLayout>
21
#include <QGroupBox>
Laurent Montel's avatar
Laurent Montel committed
22
#include <QObject>
23
24
25
#include <QPushButton>
#include <QRadioButton>
#include <QSpinBox>
26

27
28
using namespace KLDAP;

29
class Q_DECL_HIDDEN LdapConfigWidget::Private
30
{
Laurent Montel's avatar
Laurent Montel committed
31
32
public:
    Private(LdapConfigWidget *parent)
Laurent Montel's avatar
Laurent Montel committed
33
        : mParent(parent)
Tobias Koenig's avatar
Tobias Koenig committed
34
    {
35
36
        mainLayout = new QFormLayout(mParent);
        mainLayout->setContentsMargins(10, 0, 10, 0);
Tobias Koenig's avatar
Tobias Koenig committed
37
    }
38

Tobias Koenig's avatar
Tobias Koenig committed
39
40
    void setLDAPPort();
    void setLDAPSPort();
Laurent Montel's avatar
Laurent Montel committed
41
42
43
    void setAnonymous(bool on);
    void setSimple(bool on);
    void setSASL(bool on);
Tobias Koenig's avatar
Tobias Koenig committed
44
45
    void queryDNClicked();
    void queryMechClicked();
Laurent Montel's avatar
Laurent Montel committed
46
47
    void loadData(LdapSearch *search, const LdapObject &object);
    void loadResult(LdapSearch *search);
Tobias Koenig's avatar
Tobias Koenig committed
48
49
50
    void sendQuery();
    void initWidget();

Laurent Montel's avatar
Laurent Montel committed
51
    LdapConfigWidget *const mParent;
Tobias Koenig's avatar
Tobias Koenig committed
52
53
54
    QStringList mQResult;
    QString mAttr;

Laurent Montel's avatar
Laurent Montel committed
55
56
57
    QLineEdit *mUser = nullptr;
    KPasswordLineEdit *mPassword = nullptr;
    QLineEdit *mHost = nullptr;
Laurent Montel's avatar
Laurent Montel committed
58
    QSpinBox *mPort = nullptr;
Laurent Montel's avatar
Laurent Montel committed
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
    QSpinBox *mVersion = nullptr;
    QSpinBox *mSizeLimit = nullptr;
    QSpinBox *mTimeLimit = nullptr;
    QSpinBox *mPageSize = nullptr;
    QLineEdit *mDn = nullptr;
    QLineEdit *mBindDn = nullptr;
    QLineEdit *mRealm = nullptr;
    QLineEdit *mFilter = nullptr;
    QRadioButton *mAnonymous = nullptr;
    QRadioButton *mSimple = nullptr;
    QRadioButton *mSASL = nullptr;
    QCheckBox *mSubTree = nullptr;
    QPushButton *mEditButton = nullptr;
    QPushButton *mQueryMech = nullptr;
    QRadioButton *mSecNo = nullptr;
    QRadioButton *mSecTLS = nullptr;
    QRadioButton *mSecSSL = nullptr;
Laurent Montel's avatar
Laurent Montel committed
76
    QComboBox *mMech = nullptr;
Tobias Koenig's avatar
Tobias Koenig committed
77

Laurent Montel's avatar
Laurent Montel committed
78
    QProgressDialog *mProg = nullptr;
Tobias Koenig's avatar
Tobias Koenig committed
79

80
    QFormLayout *mainLayout = nullptr;
Laurent Montel's avatar
Laurent Montel committed
81
82
    WinFlags mFeatures = W_ALL;
    bool mCancelled = false;
Tobias Koenig's avatar
Tobias Koenig committed
83
84
85
};

void LdapConfigWidget::Private::initWidget()
86
{
Laurent Montel's avatar
Laurent Montel committed
87
88
    if (mFeatures & W_USER) {
        mUser = new QLineEdit(mParent);
89
        mUser->setObjectName(QStringLiteral("kcfg_ldapuser"));
Laurent Montel's avatar
Laurent Montel committed
90

91
        mainLayout->addRow(i18n("User:"), mUser);
Laurent Montel's avatar
Laurent Montel committed
92
93
94
95
    }

    if (mFeatures & W_BINDDN) {
        mBindDn = new QLineEdit(mParent);
96
        mBindDn->setObjectName(QStringLiteral("kcfg_ldapbinddn"));
Laurent Montel's avatar
Laurent Montel committed
97

98
        mainLayout->addRow(i18n("Bind DN:"), mBindDn);
Laurent Montel's avatar
Laurent Montel committed
99
100
101
102
    }

    if (mFeatures & W_REALM) {
        mRealm = new QLineEdit(mParent);
103
        mRealm->setObjectName(QStringLiteral("kcfg_ldaprealm"));
Laurent Montel's avatar
Laurent Montel committed
104

105
        mainLayout->addRow(i18n("Realm:"), mRealm);
Laurent Montel's avatar
Laurent Montel committed
106
107
108
    }

    if (mFeatures & W_PASS) {
Laurent Montel's avatar
Laurent Montel committed
109
        mPassword = new KPasswordLineEdit(mParent);
110
        mPassword->setObjectName(QStringLiteral("kcfg_ldappassword"));
111
        mPassword->setRevealPasswordAvailable(KAuthorized::authorize(QStringLiteral("lineedit_reveal_password")));
Laurent Montel's avatar
Laurent Montel committed
112

113
        mainLayout->addRow(i18n("Password:"), mPassword);
Laurent Montel's avatar
Laurent Montel committed
114
115
116
117
    }

    if (mFeatures & W_HOST) {
        mHost = new QLineEdit(mParent);
118
        mHost->setObjectName(QStringLiteral("kcfg_ldaphost"));
119
        mParent->connect(mHost, &QLineEdit::textChanged, mParent, &LdapConfigWidget::hostNameChanged);
120
        mainLayout->addRow(i18n("Host:"), mHost);
Laurent Montel's avatar
Laurent Montel committed
121
122
123
124
125
    }

    if (mFeatures & W_PORT) {
        mPort = new QSpinBox(mParent);
        mPort->setRange(0, 65535);
126
        mPort->setObjectName(QStringLiteral("kcfg_ldapport"));
Laurent Montel's avatar
Laurent Montel committed
127
128
        mPort->setValue(389);

129
        mainLayout->addRow(i18n("Port:"), mPort);
Laurent Montel's avatar
Laurent Montel committed
130
131
132
133
134
    }

    if (mFeatures & W_VER) {
        mVersion = new QSpinBox(mParent);
        mVersion->setRange(2, 3);
135
        mVersion->setObjectName(QStringLiteral("kcfg_ldapver"));
Laurent Montel's avatar
Laurent Montel committed
136
        mVersion->setValue(3);
137
        mainLayout->addRow(i18n("LDAP version:"), mVersion);
Laurent Montel's avatar
Laurent Montel committed
138
139
140
141
142
    }

    if (mFeatures & W_SIZELIMIT) {
        mSizeLimit = new QSpinBox(mParent);
        mSizeLimit->setRange(0, 9999999);
143
        mSizeLimit->setObjectName(QStringLiteral("kcfg_ldapsizelimit"));
Laurent Montel's avatar
Laurent Montel committed
144
145
        mSizeLimit->setValue(0);
        mSizeLimit->setSpecialValueText(i18nc("default ldap size limit", "Default"));
146
        mainLayout->addRow(i18n("Size limit:"), mSizeLimit);
Laurent Montel's avatar
Laurent Montel committed
147
148
149
150
151
    }

    if (mFeatures & W_TIMELIMIT) {
        mTimeLimit = new QSpinBox(mParent);
        mTimeLimit->setRange(0, 9999999);
152
        mTimeLimit->setObjectName(QStringLiteral("kcfg_ldaptimelimit"));
Laurent Montel's avatar
Laurent Montel committed
153
154
155
        mTimeLimit->setValue(0);
        mTimeLimit->setSuffix(i18n(" sec"));
        mTimeLimit->setSpecialValueText(i18nc("default ldap time limit", "Default"));
156
        mainLayout->addRow(i18n("Time limit:"), mTimeLimit);
Laurent Montel's avatar
Laurent Montel committed
157
158
159
160
161
    }

    if (mFeatures & W_PAGESIZE) {
        mPageSize = new QSpinBox(mParent);
        mPageSize->setRange(0, 9999999);
162
        mPageSize->setObjectName(QStringLiteral("kcfg_ldappagesize"));
Laurent Montel's avatar
Laurent Montel committed
163
164
        mPageSize->setValue(0);
        mPageSize->setSpecialValueText(i18n("No paging"));
165
        mainLayout->addRow(i18n("Page size:"), mPageSize);
Laurent Montel's avatar
Laurent Montel committed
166
167
168
    }

    if (mFeatures & W_DN) {
169
        auto horizontalLayout = new QHBoxLayout;
Laurent Montel's avatar
Laurent Montel committed
170
        mDn = new QLineEdit(mParent);
171
        mDn->setObjectName(QStringLiteral("kcfg_ldapdn"));
172
        horizontalLayout->addWidget(mDn);
Laurent Montel's avatar
Laurent Montel committed
173

Laurent Montel's avatar
Laurent Montel committed
174
        // without host query doesn't make sense
Laurent Montel's avatar
Laurent Montel committed
175
        if (mHost) {
Laurent Montel's avatar
Laurent Montel committed
176
            auto dnquery = new QPushButton(i18n("Query Server"), mParent);
177
            dnquery->setEnabled(false);
Laurent Montel's avatar
Laurent Montel committed
178
179
180
            connect(dnquery, &QPushButton::clicked, mParent, [this]() {
                queryDNClicked();
            });
181
182
183
            connect(mDn, &QLineEdit::textChanged, mParent, [dnquery](const QString &text) {
                dnquery->setEnabled(!text.trimmed().isEmpty());
            });
184
            horizontalLayout->addWidget(dnquery);
Laurent Montel's avatar
Laurent Montel committed
185
        }
186
        mainLayout->addRow(i18nc("Distinguished Name", "DN:"), horizontalLayout);
Laurent Montel's avatar
Laurent Montel committed
187
188
189
190
    }

    if (mFeatures & W_FILTER) {
        mFilter = new QLineEdit(mParent);
191
        mFilter->setObjectName(QStringLiteral("kcfg_ldapfilter"));
Laurent Montel's avatar
Laurent Montel committed
192

193
        mainLayout->addRow(i18n("Filter:"), mFilter);
Laurent Montel's avatar
Laurent Montel committed
194
195
196
    }

    if (mFeatures & W_SECBOX) {
197
198
199
200
201
        auto btgroup = new QWidget(mParent);
        btgroup->setContentsMargins({0, 0, 0, 0});

        auto hbox = new QHBoxLayout(btgroup);

Laurent Montel's avatar
Laurent Montel committed
202
        mSecNo = new QRadioButton(i18nc("@option:radio set no security", "No"), btgroup);
203
        mSecNo->setObjectName(QStringLiteral("kcfg_ldapnosec"));
Laurent Montel's avatar
Laurent Montel committed
204
205
        hbox->addWidget(mSecNo);
        mSecTLS = new QRadioButton(i18nc("@option:radio use TLS security", "TLS"), btgroup);
206
        mSecTLS->setObjectName(QStringLiteral("kcfg_ldaptls"));
Laurent Montel's avatar
Laurent Montel committed
207
208
        hbox->addWidget(mSecTLS);
        mSecSSL = new QRadioButton(i18nc("@option:radio use SSL security", "SSL"), btgroup);
209
        mSecSSL->setObjectName(QStringLiteral("kcfg_ldapssl"));
Laurent Montel's avatar
Laurent Montel committed
210
211
        hbox->addWidget(mSecSSL);

Laurent Montel's avatar
Laurent Montel committed
212
213
214
215
216
217
218
219
220
        connect(mSecNo, &QRadioButton::clicked, mParent, [this]() {
            setLDAPPort();
        });
        connect(mSecTLS, &QRadioButton::clicked, mParent, [this]() {
            setLDAPPort();
        });
        connect(mSecSSL, &QRadioButton::clicked, mParent, [this]() {
            setLDAPSPort();
        });
Laurent Montel's avatar
Laurent Montel committed
221
222

        mSecNo->setChecked(true);
223
        mainLayout->addRow(i18n("Security:"), btgroup);
Laurent Montel's avatar
Laurent Montel committed
224
225
226
    }

    if (mFeatures & W_AUTHBOX) {
227
228
229
230
231
        // invisible QWidget for auto-exclusive radiobutton
        auto authbox = new QWidget(mParent);
        authbox->setContentsMargins({0, 0, 0, 0});

        auto hbox = new QHBoxLayout(authbox);
Laurent Montel's avatar
Laurent Montel committed
232

Laurent Montel's avatar
Laurent Montel committed
233
        mAnonymous = new QRadioButton(i18nc("@option:radio anonymous authentication", "Anonymous"), authbox);
234
        mAnonymous->setObjectName(QStringLiteral("kcfg_ldapanon"));
Laurent Montel's avatar
Laurent Montel committed
235
        hbox->addWidget(mAnonymous);
Laurent Montel's avatar
Laurent Montel committed
236
        mSimple = new QRadioButton(i18nc("@option:radio simple authentication", "Simple"), authbox);
237
        mSimple->setObjectName(QStringLiteral("kcfg_ldapsimple"));
Laurent Montel's avatar
Laurent Montel committed
238
        hbox->addWidget(mSimple);
Laurent Montel's avatar
Laurent Montel committed
239
        mSASL = new QRadioButton(i18nc("@option:radio SASL authentication", "SASL"), authbox);
240
        mSASL->setObjectName(QStringLiteral("kcfg_ldapsasl"));
Laurent Montel's avatar
Laurent Montel committed
241
        hbox->addWidget(mSASL);
Luigi Toscano's avatar
Luigi Toscano committed
242
        mainLayout->addRow(i18n("Authentication:"), authbox);
Laurent Montel's avatar
Laurent Montel committed
243
244

        hbox = new QHBoxLayout;
245
        mMech = new QComboBox(mParent);
246
247
248
249
        mMech->setObjectName(QStringLiteral("kcfg_ldapsaslmech"));
        mMech->addItem(QStringLiteral("DIGEST-MD5"));
        mMech->addItem(QStringLiteral("GSSAPI"));
        mMech->addItem(QStringLiteral("PLAIN"));
Laurent Montel's avatar
Laurent Montel committed
250
251
        hbox->addWidget(mMech);

Laurent Montel's avatar
Laurent Montel committed
252
        // without host query doesn't make sense
Laurent Montel's avatar
Laurent Montel committed
253
254
255
        if (mHost) {
            mQueryMech = new QPushButton(i18n("Query Server"), authbox);
            hbox->addWidget(mQueryMech);
Laurent Montel's avatar
Laurent Montel committed
256
257
258
            connect(mQueryMech, &QPushButton::clicked, mParent, [this]() {
                queryMechClicked();
            });
Laurent Montel's avatar
Laurent Montel committed
259
        }
260
        mainLayout->addRow(i18n("SASL mechanism:"), hbox);
Laurent Montel's avatar
Laurent Montel committed
261

Laurent Montel's avatar
Laurent Montel committed
262
263
264
265
266
267
268
269
270
        connect(mAnonymous, &QRadioButton::toggled, mParent, [this](bool b) {
            setAnonymous(b);
        });
        connect(mSimple, &QRadioButton::toggled, mParent, [this](bool b) {
            setSimple(b);
        });
        connect(mSASL, &QRadioButton::toggled, mParent, [this](bool b) {
            setSASL(b);
        });
Laurent Montel's avatar
Laurent Montel committed
271
272
273

        mAnonymous->setChecked(true);
    }
274
275
}

Tobias Koenig's avatar
Tobias Koenig committed
276
void LdapConfigWidget::Private::sendQuery()
277
{
Laurent Montel's avatar
Laurent Montel committed
278
279
280
281
282
283
284
285
286
287
288
    LdapServer _server(mParent->server());

    mQResult.clear();
    mCancelled = true;

    if (mAttr == QLatin1String("supportedsaslmechanisms")) {
        _server.setAuth(LdapServer::Anonymous);
    }

    LdapUrl _url(_server.url());

Laurent Montel's avatar
Laurent Montel committed
289
    _url.setDn(LdapDN(QLatin1String("")));
Laurent Montel's avatar
Laurent Montel committed
290
291
292
    _url.setAttributes(QStringList(mAttr));
    _url.setScope(LdapUrl::Base);

Laurent Montel's avatar
Laurent Montel committed
293
    qCDebug(LDAP_LOG) << "sendQuery url:" << _url.toDisplayString();
Laurent Montel's avatar
Laurent Montel committed
294
295

    LdapSearch search;
Laurent Montel's avatar
Laurent Montel committed
296
297
298
299
300
301
    connect(&search, &LdapSearch::data, mParent, [this](KLDAP::LdapSearch *s, const KLDAP::LdapObject &obj) {
        loadData(s, obj);
    });
    connect(&search, &LdapSearch::result, mParent, [this](KLDAP::LdapSearch *s) {
        loadResult(s);
    });
Laurent Montel's avatar
Laurent Montel committed
302
303

    if (!search.search(_url)) {
304
        KMessageBox::error(mParent, search.errorString(), i18n("Check server"));
Laurent Montel's avatar
Laurent Montel committed
305
306
307
        return;
    }

Laurent Montel's avatar
Laurent Montel committed
308
    if (mProg == nullptr) {
Laurent Montel's avatar
Laurent Montel committed
309
        mProg = new QProgressDialog(mParent);
310
        mProg->setWindowTitle(i18nc("@title:window", "LDAP Query"));
Laurent Montel's avatar
Laurent Montel committed
311
312
313
314
315
316
317
318
        mProg->setModal(true);
    }
    mProg->setLabelText(_url.toDisplayString());
    mProg->setMaximum(1);
    mProg->setMinimum(0);
    mProg->setValue(0);
    mProg->exec();
    if (mCancelled) {
Laurent Montel's avatar
Laurent Montel committed
319
        qCDebug(LDAP_LOG) << "query canceled!";
Laurent Montel's avatar
Laurent Montel committed
320
321
322
323
324
325
326
327
328
329
        search.abandon();
    } else {
        if (search.error()) {
            if (search.errorString().isEmpty()) {
                KMessageBox::error(mParent, i18nc("%1 is a url to ldap server", "Unknown error connecting %1", _url.toDisplayString()));
            } else {
                KMessageBox::error(mParent, search.errorString());
            }
        }
    }
330
331
}

Tobias Koenig's avatar
Tobias Koenig committed
332
void LdapConfigWidget::Private::queryMechClicked()
333
{
334
    mAttr = QStringLiteral("supportedsaslmechanisms");
Laurent Montel's avatar
Laurent Montel committed
335
336
337
338
339
340
    sendQuery();
    if (!mQResult.isEmpty()) {
        mQResult.sort();
        mMech->clear();
        mMech->addItems(mQResult);
    }
341
342
}

Tobias Koenig's avatar
Tobias Koenig committed
343
void LdapConfigWidget::Private::queryDNClicked()
344
{
345
    mAttr = QStringLiteral("namingcontexts");
Laurent Montel's avatar
Laurent Montel committed
346
347
    sendQuery();
    if (!mQResult.isEmpty()) {
348
        mDn->setText(mQResult.constFirst());
Laurent Montel's avatar
Laurent Montel committed
349
350
351
352
353
    }
}

void LdapConfigWidget::Private::loadData(LdapSearch *, const LdapObject &object)
{
Laurent Montel's avatar
Laurent Montel committed
354
    qCDebug(LDAP_LOG) << "object:" << object.toString();
Laurent Montel's avatar
Laurent Montel committed
355
356
    mProg->setValue(mProg->value() + 1);
    LdapAttrMap::ConstIterator end(object.attributes().constEnd());
Laurent Montel's avatar
Laurent Montel committed
357
    for (LdapAttrMap::ConstIterator it = object.attributes().constBegin(); it != end; ++it) {
Laurent Montel's avatar
Laurent Montel committed
358
        LdapAttrValue::ConstIterator end2((*it).constEnd());
Laurent Montel's avatar
Laurent Montel committed
359
        for (LdapAttrValue::ConstIterator it2 = (*it).constBegin(); it2 != end2; ++it2) {
Laurent Montel's avatar
Laurent Montel committed
360
361
362
363
364
365
366
            mQResult.push_back(QString::fromUtf8(*it2));
        }
    }
}

void LdapConfigWidget::Private::loadResult(LdapSearch *search)
{
Laurent Montel's avatar
Laurent Montel committed
367
    Q_UNUSED(search)
Laurent Montel's avatar
Laurent Montel committed
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
    mCancelled = false;
    mProg->close();
}

void LdapConfigWidget::Private::setAnonymous(bool on)
{
    if (!on) {
        return;
    }
    if (mUser) {
        mUser->setEnabled(false);
    }
    if (mPassword) {
        mPassword->setEnabled(false);
    }
    if (mBindDn) {
        mBindDn->setEnabled(false);
    }
    if (mRealm) {
        mRealm->setEnabled(false);
    }
    if (mMech) {
        mMech->setEnabled(false);
    }
    if (mQueryMech) {
        mQueryMech->setEnabled(false);
    }
}

void LdapConfigWidget::Private::setSimple(bool on)
{
    if (!on) {
        return;
    }
    if (mUser) {
        mUser->setEnabled(false);
    }
    if (mPassword) {
        mPassword->setEnabled(true);
    }
    if (mBindDn) {
        mBindDn->setEnabled(true);
    }
    if (mRealm) {
        mRealm->setEnabled(false);
    }
    if (mMech) {
        mMech->setEnabled(false);
    }
    if (mQueryMech) {
        mQueryMech->setEnabled(false);
    }
}

void LdapConfigWidget::Private::setSASL(bool on)
{
    if (!on) {
        return;
    }
    if (mUser) {
        mUser->setEnabled(true);
    }
    if (mPassword) {
        mPassword->setEnabled(true);
    }
    if (mBindDn) {
        mBindDn->setEnabled(true);
    }
    if (mRealm) {
        mRealm->setEnabled(true);
    }
    if (mMech) {
        mMech->setEnabled(true);
    }
    if (mQueryMech) {
        mQueryMech->setEnabled(true);
    }
445
446
}

Tobias Koenig's avatar
Tobias Koenig committed
447
void LdapConfigWidget::Private::setLDAPPort()
448
{
Laurent Montel's avatar
Laurent Montel committed
449
450
451
    if (mPort) {
        mPort->setValue(389);
    }
452
453
}

Tobias Koenig's avatar
Tobias Koenig committed
454
void LdapConfigWidget::Private::setLDAPSPort()
455
{
Laurent Montel's avatar
Laurent Montel committed
456
457
458
    if (mPort) {
        mPort->setValue(636);
    }
459
460
}

Laurent Montel's avatar
Laurent Montel committed
461
LdapConfigWidget::LdapConfigWidget(QWidget *parent, Qt::WindowFlags fl)
Laurent Montel's avatar
Laurent Montel committed
462
463
    : QWidget(parent, fl)
    , d(new Private(this))
Tobias Koenig's avatar
Tobias Koenig committed
464
465
466
{
}

Laurent Montel's avatar
Laurent Montel committed
467
468
469
LdapConfigWidget::LdapConfigWidget(LdapConfigWidget::WinFlags flags, QWidget *parent, Qt::WindowFlags fl)
    : QWidget(parent, fl)
    , d(new Private(this))
Tobias Koenig's avatar
Tobias Koenig committed
470
{
Laurent Montel's avatar
Laurent Montel committed
471
    d->mFeatures = flags;
Tobias Koenig's avatar
Tobias Koenig committed
472

Laurent Montel's avatar
Laurent Montel committed
473
    d->initWidget();
Tobias Koenig's avatar
Tobias Koenig committed
474
475
476
477
}

LdapConfigWidget::~LdapConfigWidget()
{
Laurent Montel's avatar
Laurent Montel committed
478
    delete d;
Tobias Koenig's avatar
Tobias Koenig committed
479
480
}

481
482
LdapUrl LdapConfigWidget::url() const
{
Laurent Montel's avatar
Laurent Montel committed
483
    return server().url();
Szombathelyi György's avatar
Szombathelyi György committed
484
}
485

Laurent Montel's avatar
Laurent Montel committed
486
void LdapConfigWidget::setUrl(const LdapUrl &url)
Szombathelyi György's avatar
Szombathelyi György committed
487
{
Laurent Montel's avatar
Laurent Montel committed
488
489
490
    LdapServer _server;
    _server.setUrl(url);
    setServer(_server);
Szombathelyi György's avatar
Szombathelyi György committed
491
492
493
494
}

LdapServer LdapConfigWidget::server() const
{
Laurent Montel's avatar
Laurent Montel committed
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
    LdapServer _server;
    if (d->mSecSSL && d->mSecSSL->isChecked()) {
        _server.setSecurity(LdapServer::SSL);
    } else if (d->mSecTLS && d->mSecTLS->isChecked()) {
        _server.setSecurity(LdapServer::TLS);
    } else {
        _server.setSecurity(LdapServer::None);
    }

    if (d->mUser) {
        _server.setUser(d->mUser->text());
    }
    if (d->mBindDn) {
        _server.setBindDn(d->mBindDn->text());
    }
    if (d->mPassword) {
Laurent Montel's avatar
Laurent Montel committed
511
        _server.setPassword(d->mPassword->password());
Laurent Montel's avatar
Laurent Montel committed
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
    }
    if (d->mRealm) {
        _server.setRealm(d->mRealm->text());
    }
    if (d->mHost) {
        _server.setHost(d->mHost->text());
    }
    if (d->mPort) {
        _server.setPort(d->mPort->value());
    }
    if (d->mDn) {
        _server.setBaseDn(LdapDN(d->mDn->text()));
    }
    if (d->mFilter) {
        _server.setFilter(d->mFilter->text());
    }
    if (d->mVersion) {
        _server.setVersion(d->mVersion->value());
    }
    if (d->mSizeLimit && d->mSizeLimit->value() != 0) {
        _server.setSizeLimit(d->mSizeLimit->value());
    }
    if (d->mTimeLimit && d->mTimeLimit->value() != 0) {
        _server.setTimeLimit(d->mTimeLimit->value());
    }
    if (d->mPageSize && d->mPageSize->value() != 0) {
        _server.setPageSize(d->mPageSize->value());
    }
    if (d->mAnonymous && d->mAnonymous->isChecked()) {
        _server.setAuth(LdapServer::Anonymous);
    } else if (d->mSimple && d->mSimple->isChecked()) {
        _server.setAuth(LdapServer::Simple);
    } else if (d->mSASL && d->mSASL->isChecked()) {
        _server.setAuth(LdapServer::SASL);
        _server.setMech(d->mMech->currentText());
    }
    return _server;
}

void LdapConfigWidget::setServer(const LdapServer &server)
{
    switch (server.security()) {
    case LdapServer::SSL:
        if (d->mSecSSL) {
            d->mSecSSL->setChecked(true);
        }
        break;
    case LdapServer::TLS:
        if (d->mSecTLS) {
            d->mSecTLS->setChecked(true);
        }
        break;
    case LdapServer::None:
        if (d->mSecNo) {
            d->mSecNo->setChecked(true);
        }
        break;
    }

    switch (server.auth()) {
    case LdapServer::Anonymous:
        if (d->mAnonymous) {
            d->mAnonymous->setChecked(true);
        }
        break;
    case LdapServer::Simple:
        if (d->mSimple) {
            d->mSimple->setChecked(true);
        }
        break;
    case LdapServer::SASL:
        if (d->mSASL) {
            d->mSASL->setChecked(true);
        }
        break;
    }

    setUser(server.user());
    setBindDn(server.bindDn());
    setPassword(server.password());
    setRealm(server.realm());
    setHost(server.host());
    setPort(server.port());
    setFilter(server.filter());
    setDn(server.baseDn());
    setVersion(server.version());
    setSizeLimit(server.sizeLimit());
    setTimeLimit(server.timeLimit());
    setPageSize(server.pageSize());
    setMech(server.mech());
}

void LdapConfigWidget::setUser(const QString &user)
{
    if (d->mUser) {
        d->mUser->setText(user);
    }
609
610
611
612
}

QString LdapConfigWidget::user() const
{
Laurent Montel's avatar
Laurent Montel committed
613
    return d->mUser ? d->mUser->text() : QString();
614
615
}

Laurent Montel's avatar
Laurent Montel committed
616
void LdapConfigWidget::setPassword(const QString &password)
617
{
Laurent Montel's avatar
Laurent Montel committed
618
    if (d->mPassword) {
Laurent Montel's avatar
Laurent Montel committed
619
        d->mPassword->setPassword(password);
Laurent Montel's avatar
Laurent Montel committed
620
    }
621
622
623
624
}

QString LdapConfigWidget::password() const
{
Laurent Montel's avatar
Laurent Montel committed
625
    return d->mPassword ? d->mPassword->password() : QString();
626
627
}

Laurent Montel's avatar
Laurent Montel committed
628
void LdapConfigWidget::setBindDn(const QString &binddn)
629
{
Laurent Montel's avatar
Laurent Montel committed
630
631
632
    if (d->mBindDn) {
        d->mBindDn->setText(binddn);
    }
633
634
635
636
}

QString LdapConfigWidget::bindDn() const
{
Laurent Montel's avatar
Laurent Montel committed
637
    return d->mBindDn ? d->mBindDn->text() : QString();
638
639
}

Laurent Montel's avatar
Laurent Montel committed
640
void LdapConfigWidget::setRealm(const QString &realm)
641
{
Laurent Montel's avatar
Laurent Montel committed
642
643
644
    if (d->mRealm) {
        d->mRealm->setText(realm);
    }
645
646
647
648
}

QString LdapConfigWidget::realm() const
{
Laurent Montel's avatar
Laurent Montel committed
649
    return d->mRealm ? d->mRealm->text() : QString();
650
651
}

Laurent Montel's avatar
Laurent Montel committed
652
void LdapConfigWidget::setHost(const QString &host)
653
{
Laurent Montel's avatar
Laurent Montel committed
654
655
656
    if (d->mHost) {
        d->mHost->setText(host);
    }
657
658
659
660
}

QString LdapConfigWidget::host() const
{
Laurent Montel's avatar
Laurent Montel committed
661
    return d->mHost ? d->mHost->text() : QString();
662
663
}

Laurent Montel's avatar
Laurent Montel committed
664
void LdapConfigWidget::setPort(int port)
665
{
Laurent Montel's avatar
Laurent Montel committed
666
667
668
    if (d->mPort) {
        d->mPort->setValue(port);
    }
669
670
671
672
}

int LdapConfigWidget::port() const
{
Laurent Montel's avatar
Laurent Montel committed
673
    return d->mPort ? d->mPort->value() : 389;
674
675
}

Laurent Montel's avatar
Laurent Montel committed
676
void LdapConfigWidget::setVersion(int version)
677
{
Laurent Montel's avatar
Laurent Montel committed
678
679
680
    if (d->mVersion) {
        d->mVersion->setValue(version);
    }
681
682
683
684
}

int LdapConfigWidget::version() const
{
Laurent Montel's avatar
Laurent Montel committed
685
    return d->mVersion ? d->mVersion->value() : 3;
686
687
}

Laurent Montel's avatar
Laurent Montel committed
688
void LdapConfigWidget::setDn(const LdapDN &dn)
689
{
Laurent Montel's avatar
Laurent Montel committed
690
691
692
    if (d->mDn) {
        d->mDn->setText(dn.toString());
    }
693
694
}

695
LdapDN LdapConfigWidget::dn() const
696
{
Laurent Montel's avatar
Laurent Montel committed
697
    return d->mDn ? LdapDN(d->mDn->text()) : LdapDN();
698
699
}

Laurent Montel's avatar
Laurent Montel committed
700
void LdapConfigWidget::setFilter(const QString &filter)
701
{
Laurent Montel's avatar
Laurent Montel committed
702
703
704
    if (d->mFilter) {
        d->mFilter->setText(filter);
    }
705
706
707
708
}

QString LdapConfigWidget::filter() const
{
Laurent Montel's avatar
Laurent Montel committed
709
    return d->mFilter ? d->mFilter->text() : QString();
710
711
}

Laurent Montel's avatar
Laurent Montel committed
712
void LdapConfigWidget::setMech(const QString &mech)
713
{
Laurent Montel's avatar
Laurent Montel committed
714
    if (d->mMech == nullptr) {
Laurent Montel's avatar
Laurent Montel committed
715
        return;
716
    }
Laurent Montel's avatar
Laurent Montel committed
717
718
719
720
721
722
723
724
725
726
727
728
    if (!mech.isEmpty()) {
        int i = 0;
        while (i < d->mMech->count()) {
            if (d->mMech->itemText(i) == mech) {
                break;
            }
            i++;
        }
        if (i == d->mMech->count()) {
            d->mMech->addItem(mech);
        }
        d->mMech->setCurrentIndex(i);
Allen Winter's avatar
Allen Winter committed
729
    }
730
731
732
733
}

QString LdapConfigWidget::mech() const
{
Laurent Montel's avatar
Laurent Montel committed
734
    return d->mMech ? d->mMech->currentText() : QString();
735
736
}

Laurent Montel's avatar
Laurent Montel committed
737
void LdapConfigWidget::setSecurity(Security security)
738
{
Laurent Montel's avatar
Laurent Montel committed
739
740
741
742
743
744
745
746
747
748
749
    switch (security) {
    case None:
        d->mSecNo->setChecked(true);
        break;
    case SSL:
        d->mSecSSL->setChecked(true);
        break;
    case TLS:
        d->mSecTLS->setChecked(true);
        break;
    }
750
751
752
753
}

LdapConfigWidget::Security LdapConfigWidget::security() const
{
Laurent Montel's avatar
Laurent Montel committed
754
755
756
757
758
759
760
    if (d->mSecTLS->isChecked()) {
        return TLS;
    }
    if (d->mSecSSL->isChecked()) {
        return SSL;
    }
    return None;
761
762
}

Laurent Montel's avatar
Laurent Montel committed
763
void LdapConfigWidget::setAuth(Auth auth)
764
{
Laurent Montel's avatar
Laurent Montel committed
765
766
767
768
769
770
771
772
773
774
775
    switch (auth) {
    case Anonymous:
        d->mAnonymous->setChecked(true);
        break;
    case Simple:
        d->mSimple->setChecked(true);
        break;
    case SASL:
        d->mSASL->setChecked(true);
        break;
    }
776
777
778
779
}

LdapConfigWidget::Auth LdapConfigWidget::auth() const
{
Laurent Montel's avatar
Laurent Montel committed
780
781
782
783
784
785
786
    if (d->mSimple->isChecked()) {
        return Simple;
    }
    if (d->mSASL->isChecked()) {
        return SASL;
    }
    return Anonymous;
787
788
}

Laurent Montel's avatar
Laurent Montel committed
789
void LdapConfigWidget::setSizeLimit(int sizelimit)
790
{
Laurent Montel's avatar
Laurent Montel committed
791
792
793
    if (d->mSizeLimit) {
        d->mSizeLimit->setValue(sizelimit);
    }
794
795
796
797
}

int LdapConfigWidget::sizeLimit() const
{
Laurent Montel's avatar
Laurent Montel committed
798
    return d->mSizeLimit ? d->mSizeLimit->value() : 0;
799
800
}

Laurent Montel's avatar
Laurent Montel committed
801
void LdapConfigWidget::setTimeLimit(int timelimit)
802
{
Laurent Montel's avatar
Laurent Montel committed
803
804
805
    if (d->mTimeLimit) {
        d->mTimeLimit->setValue(timelimit);
    }
806
807
808
809
}

int LdapConfigWidget::timeLimit() const
{
Laurent Montel's avatar
Laurent Montel committed
810
    return d->mTimeLimit ? d->mTimeLimit->value() : 0;
811
812
}

Laurent Montel's avatar
Laurent Montel committed
813
void LdapConfigWidget::setPageSize(int pagesize)
814
{
Laurent Montel's avatar
Laurent Montel committed
815
816
817
    if (d->mPageSize) {
        d->mPageSize->setValue(pagesize);
    }
818
819
820
821
}

int LdapConfigWidget::pageSize() const
{
Laurent Montel's avatar
Laurent Montel committed
822
    return d->mPageSize ? d->mPageSize->value() : 0;
823
824
825
826
}

LdapConfigWidget::WinFlags LdapConfigWidget::features() const
{
Laurent Montel's avatar
Laurent Montel committed
827
    return d->mFeatures;
828
829
}

Laurent Montel's avatar
Laurent Montel committed
830
void LdapConfigWidget::setFeatures(LdapConfigWidget::WinFlags features)
831
{
Laurent Montel's avatar
Laurent Montel committed
832
    d->mFeatures = features;
833

Laurent Montel's avatar
Laurent Montel committed
834
835
836
837
838
    // First delete all the child widgets.
    // FIXME: I hope it's correct
    QList<QObject *> ch = children();
    const int numberOfChild(ch.count());
    for (int i = 0; i < numberOfChild; ++i) {
Laurent Montel's avatar
Laurent Montel committed
839
        QWidget *widget = qobject_cast<QWidget *>(ch[i]);
Laurent Montel's avatar
Laurent Montel committed
840
        if (widget && widget->parent() == this) {
Laurent Montel's avatar
Laurent Montel committed
841
            delete (widget);
Laurent Montel's avatar
Laurent Montel committed
842
        }
Allen Winter's avatar
Allen Winter committed
843
    }
844

Laurent Montel's avatar
Laurent Montel committed
845
846
    // Re-create child widgets according to the new flags
    d->initWidget();
847
848
}

849
#include "moc_ldapconfigwidget.cpp"