File: LdapBrowseModel.cpp

package info (click to toggle)
veyon 4.7.5%2Brepack1-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 11,912 kB
  • sloc: cpp: 47,553; ansic: 7,236; makefile: 230; python: 219; sh: 47
file content (407 lines) | stat: -rw-r--r-- 7,930 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
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
// Copyright (c) 2019-2023 Tobias Junghans <tobydox@veyon.io>
// This file is part of Veyon - https://veyon.io
// SPDX-License-Identifier: LGPL-2.0-or-later

#include <QRegularExpression>

#include "LdapClient.h"
#include "LdapConfiguration.h"
#include "LdapBrowseModel.h"

#if defined(QT_TESTLIB_LIB) && QT_VERSION >= QT_VERSION_CHECK(5, 11, 0)
#include <QAbstractItemModelTester>
#endif

// clazy:excludeall=rule-of-three

class LdapBrowseModelNode {
public:
	enum Type {
		Root,
		DN,
		Attribute
	};

	explicit LdapBrowseModelNode( Type type, const QString& name, LdapBrowseModelNode *parent ) :
		m_parent( parent ),
		m_name( name ),
		m_type( type ),
		m_populated( false )
	{
		if( type == Attribute )
		{
			m_populated = true;
		}
	}

	~LdapBrowseModelNode()
	{
		qDeleteAll(m_childItems);
	}

#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
	Q_DISABLE_COPY_MOVE(LdapBrowseModelNode)
#else
	Q_DISABLE_COPY(LdapBrowseModelNode)
	Q_DISABLE_MOVE(LdapBrowseModelNode)
#endif

	Type type() const
	{
		return m_type;
	}

	LdapBrowseModelNode* parent() const
	{
		return m_parent;
	}

	int row() const
	{
		if( m_parent )
		{
			return m_parent->children().indexOf( const_cast<LdapBrowseModelNode *>(this) );
		}

		return 0;
	}

	void setPopulated( bool populated )
	{
		m_populated = populated;
	}

	bool isPopulated() const
	{
		return m_populated;
	}

	void appendChild( LdapBrowseModelNode* item )
	{
		m_childItems.append( item );
	}

	const QList<LdapBrowseModelNode *>& children() const
	{
		return m_childItems;
	}

	const QString& name() const
	{
		return m_name;
	}

private:
	LdapBrowseModelNode* m_parent;
	QList<LdapBrowseModelNode *> m_childItems;
	QString m_name;
	Type m_type;
	bool m_populated;

};



LdapBrowseModel::LdapBrowseModel( Mode mode, const LdapConfiguration& configuration, QObject* parent ) :
	QAbstractItemModel( parent ),
	m_mode( mode ),
	m_client( new LdapClient( configuration, QUrl(), this ) ),
	m_root( new Node( Node::Root, {}, nullptr ) ),
	m_objectIcon( QStringLiteral(":/core/document-open.png") ),
	m_ouIcon( QStringLiteral( ":/ldap/folder-stash.png") ),
	m_attributeIcon( QStringLiteral(":/ldap/attribute.png") )
{
	populateRoot();

#if defined(QT_TESTLIB_LIB) && QT_VERSION >= QT_VERSION_CHECK(5, 11, 0)
	new QAbstractItemModelTester( this, QAbstractItemModelTester::FailureReportingMode::Warning, this );
#endif
}



LdapBrowseModel::~LdapBrowseModel()
{
	delete m_root;
	delete m_client;
}



QModelIndex LdapBrowseModel::index( int row, int col, const QModelIndex& parent ) const
{
	auto childNode = toNode( parent )->children().value( row );
	if( childNode )
	{
		return createIndex( row, col, childNode );
	}

	return {};
}



QModelIndex LdapBrowseModel::parent( const QModelIndex& child ) const
{
	if( child.isValid() == false )
	{
		return {};
	}

	auto childItem = static_cast<Node *>( child.internalPointer() );
	auto parentItem = childItem->parent();

	if( parentItem == m_root )
	{
		return {};
	}

	return createIndex( parentItem->row(), 0, parentItem );
}



QVariant LdapBrowseModel::data( const QModelIndex& index, int role ) const
{
	if( index.isValid() == false )
	{
		return {};
	}

	const auto node = toNode( index );

	switch( role )
	{
	case Qt::DisplayRole:
		return LdapClient::toRDNs( node->name() ).value( 0 );

	case Qt::DecorationRole:
		switch( node->type() )
		{
		case Node::Root: break;
		case Node::DN: return node->name().startsWith( QLatin1String("ou="), Qt::CaseInsensitive ) ? m_ouIcon : m_objectIcon;
		case Node::Attribute: return m_attributeIcon;
		}
		break;

	case ItemNameRole:
		return node->name();

	default:
		break;
	}

	return {};
}



Qt::ItemFlags LdapBrowseModel::flags( const QModelIndex& index ) const
{
	if( index.isValid() == false )
	{
		return QAbstractItemModel::flags(index);
	}

	return QAbstractItemModel::flags(index) | Qt::ItemIsSelectable;
}



int LdapBrowseModel::columnCount( const QModelIndex& parent ) const
{
	Q_UNUSED(parent);
	return 1;
}



int LdapBrowseModel::rowCount( const QModelIndex& parent ) const
{
	if( parent.column() >  0 )
	{
		return 0;
	}

	return toNode( parent )->children().count();
}



bool LdapBrowseModel::hasChildren( const QModelIndex& parent ) const
{
	auto parentNode = toNode( parent );

	if( parent.isValid() && parentNode && parentNode->isPopulated() )
	{
		return parentNode->children().isEmpty() == false;
	}

	return true;
}



bool LdapBrowseModel::canFetchMore( const QModelIndex& parent ) const
{
	return toNode( parent )->isPopulated() == false;
}



void LdapBrowseModel::fetchMore( const QModelIndex& parent )
{
	populateNode( parent );
}



QModelIndex LdapBrowseModel::dnToIndex( const QString& dn )
{
	auto rdns = LdapClient::toRDNs( dn );

	auto node = m_root;
	QModelIndex index;
	QStringList fullDn;

	while( rdns.isEmpty() == false )
	{
		populateNode( index );

		fullDn.prepend( rdns.takeLast() );
		const auto currentDn = fullDn.join( QLatin1Char(',') ).toLower();

		int row = 0;
		const int count = node->children().count();

		for( auto child : node->children() )
		{
			if( child->name().toLower() == currentDn )
			{
				node = child;
				index = createIndex( row, 0, node );
				break;
			}
			++row;
		}

		if( row >= count )
		{
			return index;
		}
	}

	return index;
}



QString LdapBrowseModel::baseDn() const
{
	return m_client->baseDn();
}



void LdapBrowseModel::populateRoot() const
{
	QStringList namingContexts;
	const auto baseDn = m_client->configuration().baseDn();

	if( baseDn.isEmpty() || m_mode == BrowseBaseDN )
	{
		namingContexts.append( m_client->queryNamingContexts() );
		namingContexts.append( m_client->queryNamingContexts( QStringLiteral("namingContexts") ) );
		namingContexts.append( m_client->queryNamingContexts( QStringLiteral("defaultNamingContext") ) );
		namingContexts.removeDuplicates();

		// remove sub naming contexts
		for( const auto& context : namingContexts )
		{
			if( context.isEmpty() == false )
			{
				namingContexts.replaceInStrings( QRegularExpression( QStringLiteral(".*,%1").arg( context ) ), {} );
			}
		}
		namingContexts.removeAll( {} );
		namingContexts.sort();
	}
	else
	{
		namingContexts.append( baseDn );
	}

	for( const auto& namingContext : namingContexts )
	{
		auto parent = m_root;
		QStringList fullDn;
		const auto dns = namingContext.split( QLatin1Char(',') );
#if QT_VERSION < 0x050600
#warning Building compat code for unsupported version of Qt
		using QStringListReverseIterator = std::reverse_iterator<QStringList::const_iterator>;
		for( auto it = QStringListReverseIterator(dns.cend()),
			 end = QStringListReverseIterator(dns.cbegin()); it != end; ++it )
#else
		for( auto it = dns.crbegin(), end = dns.crend(); it != end; ++it )
#endif
		{
			fullDn.prepend( *it );
			auto node = new Node( Node::DN, fullDn.join( QLatin1Char(',') ), parent );
			parent->appendChild( node );
			parent = node;
		}
	}

	m_root->setPopulated( true );
}



void LdapBrowseModel::populateNode( const QModelIndex& parent )
{
	auto node = toNode( parent );

	if( node->isPopulated() == false )
	{
		auto dns = m_client->queryDistinguishedNames( node->name(), {}, LdapClient::Scope::One );
		dns.sort();

		QStringList attributes;

		if( m_mode == BrowseAttributes )
		{
			attributes = m_client->queryObjectAttributes( node->name() );
			attributes.sort();
		}

		const auto itemCount = ( dns + attributes ).size();

		if( itemCount > 0 )
		{
			beginInsertRows( parent, 0, itemCount - 1 );

			for( const auto& dn : qAsConst(dns) )
			{
				node->appendChild( new Node( Node::DN, dn, node ) );
			}

			for( const auto& attribute : qAsConst(attributes) )
			{
				node->appendChild( new Node( Node::Attribute, attribute, node ) );
			}

			endInsertRows();

			Q_EMIT layoutChanged();
		}

		node->setPopulated( true );
	}
}



LdapBrowseModel::Node*LdapBrowseModel::toNode( const QModelIndex& index ) const
{
	return index.isValid() ? static_cast<Node *>( index.internalPointer() ) : m_root;
}