Skip to content

Commit

Permalink
Fix review comments from Claudio II.
Browse files Browse the repository at this point in the history
Signed-off-by: alex-z <blackslayer4@gmail.com>
  • Loading branch information
allexzander committed Dec 29, 2022
1 parent 4230a15 commit 551021a
Show file tree
Hide file tree
Showing 6 changed files with 318 additions and 264 deletions.
2 changes: 2 additions & 0 deletions src/gui/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ set(client_SRCS
accountmanager.cpp
accountsettings.h
accountsettings.cpp
accountsetupfromcommandlinejob.h
accountsetupfromcommandlinejob.cpp
accountsetupcommandlinemanager.h
accountsetupcommandlinemanager.cpp
application.h
Expand Down
227 changes: 7 additions & 220 deletions src/gui/accountsetupcommandlinemanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,225 +13,12 @@
*/

#include "accountsetupcommandlinemanager.h"

#include "accountmanager.h"
#include "creds/webflowcredentials.h"
#include "filesystem.h"
#include "folder.h"
#include "folderman.h"
#include "networkjobs.h"

#include <QDir>
#include <QGuiApplication>
#include <QJsonObject>
#include <QLoggingCategory>
#include <QObject>
#include <QUrl>
#include "accountsetupfromcommandlinejob.h"

namespace OCC
{
Q_LOGGING_CATEGORY(lcAccountSetupCommandLineManager, "nextcloud.gui.accountsetupcommandlinemanager", QtInfoMsg)

AccountSetupFromCommandLineJob::AccountSetupFromCommandLineJob(QString appPassword,
QString userId,
QUrl serverUrl,
QString localDirPath,
bool nonVfsMode,
QString remoteDirPath,
QObject *parent)
: QObject(parent)
, _appPassword(appPassword)
, _userId(userId)
, _serverUrl(serverUrl)
, _localDirPath(localDirPath)
, _nonVfsMode(nonVfsMode)
, _remoteDirPath(remoteDirPath)
{
}

void AccountSetupFromCommandLineJob::handleAccountSetupFromCommandLine()
{
if (AccountManager::instance()->accountFromUserId(QStringLiteral("%1@%2").arg(_userId).arg(_serverUrl.host()))) {
printAccountSetupFromCommandLineStatusAndExit(QStringLiteral("Account %1 already exists!").arg(QDir::toNativeSeparators(_userId)), true);
return;
}

if (!_localDirPath.isEmpty()) {
QDir dir(_localDirPath);
if (dir.exists() && !dir.isEmpty()) {
printAccountSetupFromCommandLineStatusAndExit(
QStringLiteral("Local folder %1 already exists and is non-empty!").arg(QDir::toNativeSeparators(_localDirPath)),
true);
return;
}

qCInfo(lcAccountSetupCommandLineManager) << "Creating folder" << _localDirPath;
if (!dir.exists() && !dir.mkpath(".")) {
printAccountSetupFromCommandLineStatusAndExit(
QStringLiteral("Folder creation failed. Could not create local folder %1").arg(QDir::toNativeSeparators(_localDirPath)),
true);
return;
}

FileSystem::setFolderMinimumPermissions(_localDirPath);
Utility::setupFavLink(_localDirPath);
}

const auto credentials = new WebFlowCredentials(_userId, _appPassword);
_account = AccountManager::createAccount();
_account->setCredentials(credentials);
_account->setUrl(_serverUrl);

fetchUserName();
}

void AccountSetupFromCommandLineJob::checkLastModifiedWithPropfind()
{
const auto job = new PropfindJob(_account, "/", this);
job->setIgnoreCredentialFailure(true);
// There is custom redirect handling in the error handler,
// so don't automatically follow redirects.
job->setFollowRedirects(false);
job->setProperties(QList<QByteArray>() << QByteArrayLiteral("getlastmodified"));
connect(job, &PropfindJob::result, this, &AccountSetupFromCommandLineJob::accountSetupFromCommandLinePropfindHandleSuccess);
connect(job, &PropfindJob::finishedWithError, this, &AccountSetupFromCommandLineJob::accountSetupFromCommandLinePropfindHandleFailure);
job->start();
}

void AccountSetupFromCommandLineJob::accountSetupFromCommandLinePropfindHandleSuccess()
{
const auto accountManager = AccountManager::instance();
const auto accountState = accountManager->addAccount(_account);
accountManager->save();

if (!_localDirPath.isEmpty()) {
FolderDefinition definition;
definition.localPath = _localDirPath;
definition.targetPath = FolderDefinition::prepareTargetPath(!_remoteDirPath.isEmpty() ? _remoteDirPath : QStringLiteral("/"));
definition.virtualFilesMode = _nonVfsMode ? Vfs::Off : bestAvailableVfsMode();

const auto folderMan = FolderMan::instance();

definition.ignoreHiddenFiles = folderMan->ignoreHiddenFiles();
definition.alias = folderMan->map().size() > 0 ? QString::number(folderMan->map().size()) : QString::number(0);

if (folderMan->navigationPaneHelper().showInExplorerNavigationPane()) {
definition.navigationPaneClsid = QUuid::createUuid();
}

folderMan->setSyncEnabled(false);

if (const auto folder = folderMan->addFolder(accountState, definition)) {
if (definition.virtualFilesMode != Vfs::Off) {
folder->setRootPinState(PinState::OnlineOnly);
}
folder->journalDb()->setSelectiveSyncList(SyncJournalDb::SelectiveSyncWhiteList, QStringList() << QLatin1String("/"));
qCInfo(lcAccountSetupCommandLineManager) << QStringLiteral("Folder %1 setup from command line success.").arg(definition.localPath);
printAccountSetupFromCommandLineStatusAndExit(QStringLiteral("Account %1 setup from command line success.").arg(_account->displayName()), false);
} else {
accountManager->deleteAccount(accountState);
printAccountSetupFromCommandLineStatusAndExit(
QStringLiteral("Account %1 setup from command line failed, due to folder creation failure.").arg(_account->displayName()),
false);
}
} else {
qCInfo(lcAccountSetupCommandLineManager) << QStringLiteral("Set up a new account without a folder.");
printAccountSetupFromCommandLineStatusAndExit(QStringLiteral("Account %1 setup from command line success.").arg(_account->displayName()), false);
}
}

void AccountSetupFromCommandLineJob::accountSetupFromCommandLinePropfindHandleFailure()
{
const auto job = qobject_cast<PropfindJob *>(sender());
if (!job) {
printAccountSetupFromCommandLineStatusAndExit(QStringLiteral("Cannot check for authed redirects. This slot should be invoked from PropfindJob!"), true);
return;
}
const auto reply = job->reply();

QString errorMsg;

// If there were redirects on the *authed* requests, also store
// the updated server URL, similar to redirects on status.php.
QUrl redirectUrl = reply->attribute(QNetworkRequest::RedirectionTargetAttribute).toUrl();
if (!redirectUrl.isEmpty()) {
qCInfo(lcAccountSetupCommandLineManager) << "Authed request was redirected to" << redirectUrl.toString();

// strip the expected path
auto path = redirectUrl.path();
static QString expectedPath = "/" + _account->davPath();
if (path.endsWith(expectedPath)) {
path.chop(expectedPath.size());
redirectUrl.setPath(path);

qCInfo(lcAccountSetupCommandLineManager) << "Setting account url to" << redirectUrl.toString();
_account->setUrl(redirectUrl);
checkLastModifiedWithPropfind();
}
errorMsg = tr("The authenticated request to the server was redirected to "
"\"%1\". The URL is bad, the server is misconfigured.")
.arg(Utility::escape(redirectUrl.toString()));

// A 404 is actually a success: we were authorized to know that the folder does
// not exist. It will be created later...
} else if (reply->error() == QNetworkReply::ContentNotFoundError) {
accountSetupFromCommandLinePropfindHandleSuccess();
} else if (reply->error() != QNetworkReply::NoError) {
if (!_account->credentials()->stillValid(reply)) {
errorMsg = tr("Access forbidden by server. To verify that you have proper access, "
"<a href=\"%1\">click here</a> to access the service with your browser.")
.arg(Utility::escape(_account->url().toString()));
} else {
errorMsg = job->errorStringParsingBody();
}
// Something else went wrong, maybe the response was 200 but with invalid data.
} else {
errorMsg = tr("There was an invalid response to an authenticated WebDAV request");
}
printAccountSetupFromCommandLineStatusAndExit(
QStringLiteral("Account %1 setup from command line failed with error: %2.").arg(_account->displayName()).arg(errorMsg),
true);
}

void AccountSetupFromCommandLineJob::printAccountSetupFromCommandLineStatusAndExit(const QString &status, bool isFailure)
{
if (isFailure) {
qCWarning(lcAccountSetupCommandLineManager) << status;
} else {
qCInfo(lcAccountSetupCommandLineManager) << status;
}
QTimer::singleShot(0, this, [this, isFailure]() {
this->deleteLater();
if (!isFailure) {
qApp->quit();
} else {
qApp->exit(1);
}
});
}

void AccountSetupFromCommandLineJob::fetchUserName()
{
const auto fetchUserNameJob = new JsonApiJob(_account, QStringLiteral("/ocs/v1.php/cloud/user"));
connect(fetchUserNameJob, &JsonApiJob::jsonReceived, this, [this](const QJsonDocument &json, int statusCode) {
sender()->deleteLater();

if (statusCode != 100) {
printAccountSetupFromCommandLineStatusAndExit("Could not fetch username.", true);
return;
}

const auto objData = json.object().value("ocs").toObject().value("data").toObject();
const auto userId = objData.value("id").toString("");
const auto displayName = objData.value("display-name").toString("");
_account->setDavUser(userId);
_account->setDavDisplayName(displayName);

checkLastModifiedWithPropfind();
});
fetchUserNameJob->start();
}

bool AccountSetupCommandLineManager::parseCommandlineOption(const QString &option, QStringListIterator &optionsIterator, QString &errorMessage)
{
if (option == QStringLiteral("--apppassword")) {
Expand Down Expand Up @@ -269,12 +56,12 @@ bool AccountSetupCommandLineManager::parseCommandlineOption(const QString &optio
} else {
errorMessage = QStringLiteral("userid not specified");
}
} else if (option == QLatin1String("--nonvfsmode")) {
} else if (option == QLatin1String("--isvfsenabled")) {
if (optionsIterator.hasNext() && !optionsIterator.peekNext().startsWith(QLatin1String("--"))) {
_nonVfsMode = optionsIterator.next().toInt() != 0;
_isVfsEnabled = optionsIterator.next().toInt() != 0;
return true;
} else {
errorMessage = QStringLiteral("nonVfsMode not specified");
errorMessage = QStringLiteral("isvfsenabled not specified");
}
}
return false;
Expand All @@ -289,7 +76,7 @@ void AccountSetupCommandLineManager::setupAccountFromCommandLine(QObject *parent
{
if (isCommandLineParsed()) {
qCInfo(lcAccountSetupCommandLineManager) << QStringLiteral("Command line has been parsed and account setup parameters have been found. Attempting setup a new account %1...").arg(_userId);
const auto accountSetupJob = new AccountSetupFromCommandLineJob(_appPassword, _userId, _serverUrl, _localDirPath, _nonVfsMode, _remoteDirPath, parent);
const auto accountSetupJob = new AccountSetupFromCommandLineJob(_appPassword, _userId, _serverUrl, _localDirPath, _isVfsEnabled, _remoteDirPath, parent);
accountSetupJob->handleAccountSetupFromCommandLine();
} else {
qCInfo(lcAccountSetupCommandLineManager) << QStringLiteral("No account setup parameters have been found, or they are invalid. Proceed with normal startup...");
Expand All @@ -299,13 +86,13 @@ void AccountSetupCommandLineManager::setupAccountFromCommandLine(QObject *parent
_serverUrl.clear();
_remoteDirPath.clear();
_localDirPath.clear();
_nonVfsMode = false;
_isVfsEnabled = true;
}

QString AccountSetupCommandLineManager::_appPassword;
QString AccountSetupCommandLineManager::_userId;
QUrl AccountSetupCommandLineManager::_serverUrl;
QString AccountSetupCommandLineManager::_remoteDirPath;
QString AccountSetupCommandLineManager::_localDirPath;
bool AccountSetupCommandLineManager::_nonVfsMode = false;
bool AccountSetupCommandLineManager::_isVfsEnabled = true;
}
47 changes: 4 additions & 43 deletions src/gui/accountsetupcommandlinemanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,51 +14,12 @@

#pragma once

#include "account.h"
#include "accountstate.h"

#include <QObject>
#include <QString>
#include <QStringList>
#include <QUrl>

namespace OCC {

class AccountSetupFromCommandLineJob : public QObject
{
Q_OBJECT
public:
AccountSetupFromCommandLineJob(QString appPassword,
QString userId,
QUrl serverUrl,
QString localDirPath = {},
bool nonVfsMode = false,
QString remoteDirPath = QStringLiteral("/"),
QObject *parent = nullptr);

public slots:
void handleAccountSetupFromCommandLine();

private slots:
void checkLastModifiedWithPropfind();

void accountSetupFromCommandLinePropfindHandleSuccess();

void accountSetupFromCommandLinePropfindHandleFailure();

void printAccountSetupFromCommandLineStatusAndExit(const QString &status, bool isFailure);

void fetchUserName();

private:
QString _appPassword;
QString _userId;
QUrl _serverUrl;
QString _localDirPath;
bool _nonVfsMode = false;
QString _remoteDirPath;

AccountPtr _account;
};


class AccountSetupCommandLineManager
{
public:
Expand All @@ -76,7 +37,7 @@ class AccountSetupCommandLineManager
static QUrl _serverUrl;
static QString _remoteDirPath;
static QString _localDirPath;
static bool _nonVfsMode;
static bool _isVfsEnabled;
};

}
Loading

0 comments on commit 551021a

Please sign in to comment.