Skip to content

Commit d2d9de7

Browse files
authored
Merge pull request #5296 from nextcloud/feature/authentication-via-apppassword-argument
Allow setting up an account with apppasword and folder via command-line arguments. For deployment.
2 parents 3b2b60a + cecd24b commit d2d9de7

6 files changed

Lines changed: 499 additions & 2 deletions

src/gui/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,10 @@ set(client_SRCS
6565
accountmanager.cpp
6666
accountsettings.h
6767
accountsettings.cpp
68+
accountsetupfromcommandlinejob.h
69+
accountsetupfromcommandlinejob.cpp
70+
accountsetupcommandlinemanager.h
71+
accountsetupcommandlinemanager.cpp
6872
application.h
6973
application.cpp
7074
invalidfilenamedialog.h
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
/*
2+
* Copyright (C) by Oleksandr Zolotov <alex@nextcloud.com>
3+
*
4+
* This program is free software; you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation; either version 2 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful, but
10+
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11+
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* for more details.
13+
*/
14+
15+
#include "accountsetupcommandlinemanager.h"
16+
#include "accountsetupfromcommandlinejob.h"
17+
18+
namespace OCC
19+
{
20+
Q_LOGGING_CATEGORY(lcAccountSetupCommandLineManager, "nextcloud.gui.accountsetupcommandlinemanager", QtInfoMsg)
21+
22+
AccountSetupCommandLineManager *AccountSetupCommandLineManager::_instance = nullptr;
23+
24+
AccountSetupCommandLineManager::AccountSetupCommandLineManager(QObject *parent)
25+
: QObject{parent}
26+
{
27+
}
28+
29+
AccountSetupCommandLineManager *AccountSetupCommandLineManager::instance()
30+
{
31+
if (!_instance) {
32+
_instance = new AccountSetupCommandLineManager();
33+
}
34+
return _instance;
35+
}
36+
37+
void AccountSetupCommandLineManager::destroy()
38+
{
39+
if (_instance) {
40+
_instance->deleteLater();
41+
_instance = nullptr;
42+
}
43+
}
44+
45+
bool AccountSetupCommandLineManager::parseCommandlineOption(const QString &option, QStringListIterator &optionsIterator, QString &errorMessage)
46+
{
47+
if (option == QStringLiteral("--apppassword")) {
48+
if (optionsIterator.hasNext() && !optionsIterator.peekNext().startsWith(QLatin1String("--"))) {
49+
_appPassword = optionsIterator.next();
50+
return true;
51+
} else {
52+
errorMessage = QStringLiteral("apppassword not specified");
53+
}
54+
} else if (option == QStringLiteral("--localdirpath")) {
55+
if (optionsIterator.hasNext() && !optionsIterator.peekNext().startsWith(QLatin1String("--"))) {
56+
_localDirPath = optionsIterator.next();
57+
return true;
58+
} else {
59+
errorMessage = QStringLiteral("basedir not specified");
60+
}
61+
} else if (option == QStringLiteral("--remotedirpath")) {
62+
if (optionsIterator.hasNext() && !optionsIterator.peekNext().startsWith(QLatin1String("--"))) {
63+
_remoteDirPath = optionsIterator.next();
64+
return true;
65+
} else {
66+
errorMessage = QStringLiteral("remotedir not specified");
67+
}
68+
} else if (option == QStringLiteral("--serverurl")) {
69+
if (optionsIterator.hasNext() && !optionsIterator.peekNext().startsWith(QLatin1String("--"))) {
70+
_serverUrl = optionsIterator.next();
71+
return true;
72+
} else {
73+
errorMessage = QStringLiteral("serverurl not specified");
74+
}
75+
} else if (option == QStringLiteral("--userid")) {
76+
if (optionsIterator.hasNext() && !optionsIterator.peekNext().startsWith(QLatin1String("--"))) {
77+
_userId = optionsIterator.next();
78+
return true;
79+
} else {
80+
errorMessage = QStringLiteral("userid not specified");
81+
}
82+
} else if (option == QLatin1String("--isvfsenabled")) {
83+
if (optionsIterator.hasNext() && !optionsIterator.peekNext().startsWith(QLatin1String("--"))) {
84+
_isVfsEnabled = optionsIterator.next().toInt() != 0;
85+
return true;
86+
} else {
87+
errorMessage = QStringLiteral("isvfsenabled not specified");
88+
}
89+
}
90+
return false;
91+
}
92+
93+
bool AccountSetupCommandLineManager::isCommandLineParsed() const
94+
{
95+
return !_appPassword.isEmpty() && !_userId.isEmpty() && _serverUrl.isValid();
96+
}
97+
98+
void AccountSetupCommandLineManager::setupAccountFromCommandLine()
99+
{
100+
if (isCommandLineParsed()) {
101+
qCInfo(lcAccountSetupCommandLineManager) << QStringLiteral("Command line has been parsed and account setup parameters have been found. Attempting setup a new account %1...").arg(_userId);
102+
const auto accountSetupJob = new AccountSetupFromCommandLineJob(_appPassword, _userId, _serverUrl, _localDirPath, _isVfsEnabled, _remoteDirPath, parent());
103+
accountSetupJob->handleAccountSetupFromCommandLine();
104+
} else {
105+
qCInfo(lcAccountSetupCommandLineManager) << QStringLiteral("No account setup parameters have been found, or they are invalid. Proceed with normal startup...");
106+
}
107+
_appPassword.clear();
108+
_userId.clear();
109+
_serverUrl.clear();
110+
_remoteDirPath.clear();
111+
_localDirPath.clear();
112+
_isVfsEnabled = true;
113+
}
114+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Copyright (C) by Oleksandr Zolotov <alex@nextcloud.com>
3+
*
4+
* This program is free software; you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation; either version 2 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful, but
10+
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11+
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* for more details.
13+
*/
14+
15+
#pragma once
16+
17+
#include <QObject>
18+
#include <QString>
19+
#include <QStringList>
20+
#include <QUrl>
21+
22+
namespace OCC {
23+
class AccountSetupCommandLineManager : public QObject
24+
{
25+
Q_OBJECT
26+
27+
public:
28+
[[nodiscard]] static AccountSetupCommandLineManager *instance();
29+
static void destroy();
30+
31+
[[nodiscard]] bool parseCommandlineOption(const QString &option, QStringListIterator &optionsIterator, QString &errorMessage);
32+
33+
[[nodiscard]] bool isCommandLineParsed() const;
34+
35+
public slots:
36+
void setupAccountFromCommandLine();
37+
38+
private:
39+
explicit AccountSetupCommandLineManager(QObject *parent = nullptr);
40+
41+
static AccountSetupCommandLineManager *_instance;
42+
43+
QString _appPassword;
44+
QString _userId;
45+
QUrl _serverUrl;
46+
QString _remoteDirPath;
47+
QString _localDirPath;
48+
bool _isVfsEnabled;
49+
};
50+
51+
}

0 commit comments

Comments
 (0)