File indexing completed on 2024-11-24 04:44:30

0001 /*
0002     This file is part of oxaccess.
0003 
0004     SPDX-FileCopyrightText: 2009 Tobias Koenig <tokoe@kde.org>
0005 
0006     SPDX-License-Identifier: LGPL-2.0-or-later
0007 */
0008 
0009 #include "usersrequestjob.h"
0010 
0011 #include "davmanager.h"
0012 #include "davutils.h"
0013 #include "oxutils.h"
0014 
0015 #include <KIO/DavJob>
0016 
0017 #include <QDomDocument>
0018 
0019 using namespace OXA;
0020 
0021 UsersRequestJob::UsersRequestJob(QObject *parent)
0022     : KJob(parent)
0023 {
0024 }
0025 
0026 void UsersRequestJob::start()
0027 {
0028     QDomDocument document;
0029     QDomElement multistatus = DAVUtils::addDavElement(document, document, QStringLiteral("multistatus"));
0030     QDomElement prop = DAVUtils::addDavElement(document, multistatus, QStringLiteral("prop"));
0031     DAVUtils::addOxElement(document, prop, QStringLiteral("user"), QStringLiteral("*"));
0032 
0033     const QString path = QStringLiteral("/servlet/webdav.groupuser");
0034 
0035     KIO::DavJob *job = DavManager::self()->createFindJob(path, document);
0036     connect(job, &KIO::DavJob::result, this, &UsersRequestJob::davJobFinished);
0037 
0038     job->start();
0039 }
0040 
0041 User::List UsersRequestJob::users() const
0042 {
0043     return mUsers;
0044 }
0045 
0046 void UsersRequestJob::davJobFinished(KJob *job)
0047 {
0048     if (job->error()) {
0049         setError(job->error());
0050         setErrorText(job->errorText());
0051         emitResult();
0052         return;
0053     }
0054 
0055     auto davJob = qobject_cast<KIO::DavJob *>(job);
0056 
0057     const QByteArray ba = davJob->responseData();
0058     QDomDocument document;
0059     document.setContent(ba);
0060 
0061     QDomElement multistatus = document.documentElement();
0062     QDomElement response = multistatus.firstChildElement(QStringLiteral("response"));
0063     QDomElement propstat = response.firstChildElement(QStringLiteral("propstat"));
0064     QDomElement prop = propstat.firstChildElement(QStringLiteral("prop"));
0065     QDomElement users = prop.firstChildElement(QStringLiteral("users"));
0066 
0067     QDomElement userElement = users.firstChildElement(QStringLiteral("user"));
0068     while (!userElement.isNull()) {
0069         User user;
0070 
0071         QDomElement element = userElement.firstChildElement();
0072         while (!element.isNull()) {
0073             if (element.tagName() == QLatin1StringView("uid")) {
0074                 user.setUid(OXUtils::readNumber(element.text()));
0075             } else if (element.tagName() == QLatin1StringView("email1")) {
0076                 user.setEmail(OXUtils::readString(element.text()));
0077             } else if (element.tagName() == QLatin1StringView("displayname")) {
0078                 user.setName(OXUtils::readString(element.text()));
0079             }
0080 
0081             element = element.nextSiblingElement();
0082         }
0083 
0084         mUsers.append(user);
0085 
0086         userElement = userElement.nextSiblingElement(QStringLiteral("user"));
0087     }
0088 
0089     emitResult();
0090 }
0091 
0092 #include "moc_usersrequestjob.cpp"