Skip to content

Commit 4785e68

Browse files
committed
Search also from Thales LDAP
IB-8403 Signed-off-by: Raul Metsma <raul@metsma.ee>
1 parent f94e20f commit 4785e68

File tree

4 files changed

+36
-41
lines changed

4 files changed

+36
-41
lines changed

client/LdapSearch.cpp

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ using namespace std::chrono;
5050
template<typename T>
5151
static constexpr auto TO_QSTR(const T *str)
5252
{
53-
if constexpr (std::is_same<T,char>::value)
53+
if constexpr (std::is_same_v<T,char>)
5454
return QLatin1String(str);
5555
else
5656
return QStringView(str);
@@ -60,15 +60,15 @@ class LdapSearch::Private
6060
{
6161
public:
6262
LDAP *ldap {};
63-
QByteArray host;
63+
QUrl url;
6464
QTimer *timer {};
6565
};
6666

67-
LdapSearch::LdapSearch(QByteArray host, QObject *parent)
67+
LdapSearch::LdapSearch(const QString &url, QObject *parent)
6868
: QObject( parent )
6969
, d(new Private)
7070
{
71-
d->host = std::move(host);
71+
d->url = QUrl(url);
7272
d->timer = new QTimer(this);
7373
d->timer->setSingleShot(true);
7474
connect(d->timer, &QTimer::timeout, this, [this]{
@@ -94,18 +94,18 @@ bool LdapSearch::init()
9494
}
9595

9696
#ifdef Q_OS_WIN
97-
QUrl url(d->host);
98-
int ssl = url.scheme() == QStringLiteral("ldaps") ? 1 : 0;
99-
QString host = url.host();
100-
ULONG port = ULONG(url.port(ssl ? LDAP_SSL_PORT : LDAP_PORT));
97+
int ssl = d->url.scheme() == QStringLiteral("ldaps") ? 1 : 0;
98+
QString host = d->url.host();
99+
ULONG port = ULONG(d->url.port(ssl ? LDAP_SSL_PORT : LDAP_PORT));
101100
if(d->ldap = ldap_sslinit(TO_STR(host), port, ssl); !d->ldap)
102101
{
103102
setLastError(tr("Failed to init ldap"), int(LdapGetLastError()));
104103
return false;
105104
}
106105
ULONG err = 0;
107106
#else
108-
int err = ldap_initialize(&d->ldap, d->host.constData());
107+
QByteArray host = d->url.toString(QUrl::RemovePath|QUrl::RemoveQuery|QUrl::RemoveFragment).toUtf8();
108+
int err = ldap_initialize(&d->ldap, host.constData());
109109
if(err)
110110
{
111111
setLastError(tr("Failed to init ldap"), err);
@@ -148,11 +148,6 @@ bool LdapSearch::init()
148148
return !err;
149149
}
150150

151-
bool LdapSearch::isSSL() const
152-
{
153-
return QUrl(d->host).scheme() == QStringLiteral("ldaps");
154-
}
155-
156151
void LdapSearch::search(const QString &search, const QVariantMap &userData)
157152
{
158153
if(!init())
@@ -166,7 +161,8 @@ void LdapSearch::search(const QString &search, const QVariantMap &userData)
166161
std::array<STR_T, 2> attrs { STR("userCertificate;binary"), nullptr };
167162

168163
ULONG msg_id = 0;
169-
int err = ldap_search_ext(d->ldap, STR("c=EE"), LDAP_SCOPE_SUBTREE,
164+
QString path = d->url.path();
165+
int err = ldap_search_ext(d->ldap, TO_STR(path.isEmpty() ? "c=EE" : path.remove(0, 1)), LDAP_SCOPE_SUBTREE,
170166
TO_STR(search), attrs.data(), 0, nullptr, nullptr, LDAP_NO_LIMIT, LDAP_NO_LIMIT, &msg_id);
171167
if(err)
172168
return setLastError( tr("Failed to init ldap search"), err );
@@ -175,8 +171,7 @@ void LdapSearch::search(const QString &search, const QVariantMap &userData)
175171
connect(timer, &QTimer::timeout, this, [this, msg_id, timer, userData] {
176172
LDAPMessage *result = nullptr;
177173
LDAP_TIMEVAL t { 5, 0 };
178-
int err = ldap_result(d->ldap, msg_id, LDAP_MSG_ALL, &t, &result);
179-
switch(err)
174+
switch(int err = ldap_result(d->ldap, msg_id, LDAP_MSG_ALL, &t, &result))
180175
{
181176
case LDAP_SUCCESS: //Timeout
182177
return;

client/LdapSearch.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,9 @@ class LdapSearch final: public QObject
2727
Q_OBJECT
2828

2929
public:
30-
LdapSearch(QByteArray host, QObject *parent = nullptr);
30+
LdapSearch(const QString &url, QObject *parent = nullptr);
3131
~LdapSearch() final;
3232

33-
bool isSSL() const;
3433
void search(const QString &search, const QVariantMap &userData);
3534

3635
Q_SIGNALS:

client/dialogs/AddRecipients.cpp

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323

2424
#include "Application.h"
2525
#include "CheckConnection.h"
26-
#include "common_enums.h"
2726
#include "FileDialog.h"
2827
#include "IKValidator.h"
2928
#include "LdapSearch.h"
@@ -32,19 +31,21 @@
3231
#include "TokenData.h"
3332
#include "dialogs/WarningDialog.h"
3433
#include "effects/Overlay.h"
34+
#include "widgets/AddressItem.h"
35+
#include "widgets/ItemList.h"
3536

3637
#include <QtCore/QDateTime>
37-
#include <QtCore/QJsonArray>
38-
#include <QtCore/QJsonObject>
38+
#include <QtCore/QJsonValue>
3939
#include <QtNetwork/QSslConfiguration>
4040
#include <QtNetwork/QSslError>
4141
#include <QtWidgets/QMessageBox>
4242

4343
AddRecipients::AddRecipients(ItemList* itemList, QWidget *parent)
4444
: QDialog(parent)
4545
, ui(new Ui::AddRecipients)
46-
, ldap_person(new LdapSearch(defaultUrl(QLatin1String("LDAP-PERSON-URL"), QStringLiteral("ldaps://esteid.ldap.sk.ee")).toUtf8(), this))
47-
, ldap_corp(new LdapSearch(defaultUrl(QLatin1String("LDAP-CORP-URL"), QStringLiteral("ldaps://k3.ldap.sk.ee")).toUtf8(), this))
46+
, ldap_person(new LdapSearch(defaultUrl(QLatin1String("LDAP-PERSON-URL"), QStringLiteral("ldaps://esteid.ldap.sk.ee")), this))
47+
, ldap_person2(new LdapSearch("ldaps://ldap-test.eidpki.ee/dc=eidpki,dc=ee", this))
48+
, ldap_corp(new LdapSearch(defaultUrl(QLatin1String("LDAP-CORP-URL"), QStringLiteral("ldaps://k3.ldap.sk.ee")), this))
4849
{
4950
ui->setupUi(this);
5051
#if defined (Q_OS_WIN)
@@ -63,8 +64,10 @@ AddRecipients::AddRecipients(ItemList* itemList, QWidget *parent)
6364
search(term);
6465
});
6566
connect(ldap_person, &LdapSearch::searchResult, this, &AddRecipients::showResult);
67+
connect(ldap_person2, &LdapSearch::searchResult, this, &AddRecipients::showResult);
6668
connect(ldap_corp, &LdapSearch::searchResult, this, &AddRecipients::showResult);
6769
connect(ldap_person, &LdapSearch::error, this, &AddRecipients::showError);
70+
connect(ldap_person2, &LdapSearch::error, this, &AddRecipients::showError);
6871
connect(ldap_corp, &LdapSearch::error, this, &AddRecipients::showError);
6972
connect(this, &AddRecipients::finished, this, &AddRecipients::close);
7073

@@ -139,7 +142,7 @@ void AddRecipients::addRecipientFromHistory()
139142

140143
ui->leftPane->clear();
141144
for(const HistoryCertData &certData: selectedCertData) {
142-
QString term = (certData.type == QStringLiteral("1") || certData.type == QStringLiteral("3")) ? certData.CN : certData.CN.split(',').value(2);
145+
QString term = (certData.type == QLatin1String("1") || certData.type == QLatin1String("3")) ? certData.CN : certData.CN.split(',').value(2);
143146
search(term, true, certData.type);
144147
}
145148
});
@@ -153,13 +156,9 @@ void AddRecipients::addRecipient(const QSslCertificate& cert, bool select)
153156
{
154157
leftItem = new AddressItem(cert, AddressItem::Add, ui->leftPane);
155158
ui->leftPane->addWidget(leftItem);
156-
bool contains = rightList.contains(cert);
157-
leftItem->setDisabled(contains);
159+
leftItem->setDisabled(rightList.contains(cert));
158160
connect(leftItem, &AddressItem::add, this, [this](Item *item) { addRecipientToRightPane(item); });
159-
if(auto *add = ui->leftPane->findChild<QWidget*>(QStringLiteral("add")))
160-
add->setVisible(true);
161161
}
162-
163162
if(select)
164163
addRecipientToRightPane(leftItem);
165164
}
@@ -202,7 +201,6 @@ void AddRecipients::addRecipientToRightPane(Item *item, bool update)
202201
return;
203202
}
204203
}
205-
updated = update;
206204

207205
rightList.append(key);
208206

@@ -212,7 +210,6 @@ void AddRecipients::addRecipientToRightPane(Item *item, bool update)
212210
if(auto *leftItem = itemListValue(ui->leftPane, rightItem->getKey().cert))
213211
leftItem->setDisabled(false);
214212
rightList.removeAll(rightItem->getKey());
215-
updated = true;
216213
ui->confirm->setDisabled(rightList.isEmpty());
217214
});
218215
ui->rightPane->addWidget(rightItem);
@@ -229,7 +226,7 @@ QString AddRecipients::defaultUrl(QLatin1String key, const QString &defaultValue
229226

230227
bool AddRecipients::isUpdated() const
231228
{
232-
return updated;
229+
return ui->confirm->isEnabled();
233230
}
234231

235232
AddressItem* AddRecipients::itemListValue(ItemList *list, const CKey &cert)
@@ -273,6 +270,7 @@ void AddRecipients::search(const QString &term, bool select, const QString &type
273270
.replace(QStringLiteral("("), QStringLiteral("\\("))
274271
.replace(QStringLiteral(")"), QStringLiteral("\\)"));
275272
#endif
273+
dualSeach = false;
276274
bool isDigit = false;
277275
void(cleanTerm.toULongLong(&isDigit));
278276
if(!isDigit || (cleanTerm.size() != 11 && cleanTerm.size() != 8))
@@ -282,7 +280,9 @@ void AddRecipients::search(const QString &term, bool select, const QString &type
282280
else if(IKValidator::isValid(cleanTerm))
283281
{
284282
userData[QStringLiteral("personSearch")] = true;
285-
ldap_person->search(QStringLiteral("(serialNumber=%1%2)" ).arg(ldap_person->isSSL() ? QStringLiteral("PNOEE-") : QString(), cleanTerm), userData);
283+
dualSeach = true;
284+
ldap_person->search(QStringLiteral("(serialNumber=PNOEE-%1)").arg(cleanTerm), userData);
285+
ldap_person2->search(QStringLiteral("(serialNumber=PNOEE-%1)").arg(cleanTerm), userData);
286286
}
287287
else
288288
{
@@ -314,11 +314,12 @@ void AddRecipients::showResult(const QList<QSslCertificate> &result, int resultC
314314
}
315315
if(resultCount >= 50)
316316
showError(tr("The name you were looking for gave too many results, please refine your search."));
317-
else if(ui->leftPane->items.isEmpty())
317+
else if(!dualSeach && ui->leftPane->items.isEmpty())
318318
{
319319
showError(tr("Person or company does not own a valid certificate.<br />"
320320
"It is necessary to have a valid certificate for encryption.<br />"
321321
"<a href='https://www.id.ee/en/article/encryption-and-decryption-of-documents/'>Read more about it</a>."));
322322
}
323+
dualSeach = false;
323324
QApplication::restoreOverrideCursor();
324325
}

client/dialogs/AddRecipients.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,16 @@
2020
#pragma once
2121

2222
#include "CertificateHistory.h"
23-
#include "widgets/AddressItem.h"
24-
#include "widgets/ItemList.h"
2523

2624
#include <QDialog>
27-
#include <QHash>
2825

2926
namespace Ui {
3027
class AddRecipients;
3128
}
3229

30+
class AddressItem;
31+
class Item;
32+
class ItemList;
3333
class LdapSearch;
3434
class QSslCertificate;
3535

@@ -50,17 +50,17 @@ class AddRecipients final : public QDialog
5050
void addRecipient(const QSslCertificate& cert, bool select = true);
5151
void addRecipientToRightPane(Item *item, bool update = true);
5252

53-
AddressItem* itemListValue(ItemList *list, const CKey &cert);
5453
void search(const QString &term, bool select = false, const QString &type = {});
5554
void showError(const QString &msg, const QString &details = {});
5655
void showResult(const QList<QSslCertificate> &result, int resultCount, const QVariantMap &userData);
5756

57+
static AddressItem* itemListValue(ItemList *list, const CKey &cert);
5858
static QString defaultUrl(QLatin1String key, const QString &defaultValue);
5959

6060
Ui::AddRecipients *ui;
6161
QList<CKey> rightList;
62-
LdapSearch *ldap_person, *ldap_corp;
63-
bool updated = false;
62+
LdapSearch *ldap_person, *ldap_person2, *ldap_corp;
63+
bool dualSeach = false;
6464

6565
HistoryList historyCertData;
6666
};

0 commit comments

Comments
 (0)