add check password
This commit is contained in:
18
Main.qml
18
Main.qml
@@ -30,6 +30,19 @@ ApplicationWindow {
|
||||
if (ProcessManager.status === ProcessManager.Status.Updating)
|
||||
root.consoleOpen = true
|
||||
}
|
||||
|
||||
function onPasswordAccepted() {
|
||||
passwordDialog.checking = false;
|
||||
if (!passwordDialog.visible) return;
|
||||
passwordDialog.accept();
|
||||
ProcessManager.update();
|
||||
}
|
||||
|
||||
function onPasswordRejected(reason) {
|
||||
passwordDialog.checking = false;
|
||||
if (!passwordDialog.visible) return;
|
||||
passwordDialog.errorText = reason;
|
||||
}
|
||||
}
|
||||
|
||||
ColumnLayout {
|
||||
@@ -119,9 +132,6 @@ ApplicationWindow {
|
||||
|
||||
Dialogs.PasswordDialog {
|
||||
id: passwordDialog
|
||||
onPasswordEntered: (password) => {
|
||||
ProcessManager.setPassword(password);
|
||||
ProcessManager.update();
|
||||
}
|
||||
onPasswordSubmitted: (password) => ProcessManager.checkPassword(password)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,11 +23,15 @@ ProcessManager::ProcessManager(QObject* parent) : QObject{parent}, m_console_man
|
||||
connect(&m_update_manager, &UpdateManager::add_log_line, &m_console_manager, &ConsoleManager::add_log_line);
|
||||
connect(&m_update_manager, &UpdateManager::append_update_log, &m_console_manager, &ConsoleManager::append_update_log);
|
||||
connect(&m_check_update_manager, &CheckUpdateManager::add_log_line, &m_console_manager, &ConsoleManager::add_log_line);
|
||||
|
||||
// проверка пароля: sudo -k -A -v
|
||||
connect(&m_password_check, &QProcess::finished, this, &ProcessManager::on_password_check_finished);
|
||||
connect(&m_password_check, &QProcess::errorOccurred, this, &ProcessManager::on_password_check_error);
|
||||
}
|
||||
|
||||
void ProcessManager::checkUpdates()
|
||||
{
|
||||
if (m_status != Status::Null) return;
|
||||
if (m_status != Status::Null || m_password_check_running) return;
|
||||
|
||||
set_packages({});
|
||||
set_status(Status::Checking);
|
||||
@@ -88,14 +92,97 @@ void ProcessManager::setPackageManager(int v)
|
||||
}
|
||||
}
|
||||
|
||||
void ProcessManager::setPassword(QString password)
|
||||
void ProcessManager::checkPassword(const QString& password)
|
||||
{
|
||||
m_password = password;
|
||||
if (m_status != Status::Null)
|
||||
{
|
||||
emit passwordRejected(QStringLiteral("Идёт другая операция"));
|
||||
return;
|
||||
}
|
||||
|
||||
if (m_password_check_running)
|
||||
{
|
||||
emit passwordRejected(QStringLiteral("Проверка пароля уже идёт"));
|
||||
return;
|
||||
}
|
||||
|
||||
if (password.isEmpty())
|
||||
{
|
||||
emit passwordRejected(QStringLiteral("Пароль пустой"));
|
||||
return;
|
||||
}
|
||||
|
||||
const QString sudo = QStandardPaths::findExecutable(QStringLiteral("sudo"));
|
||||
if (sudo.isEmpty())
|
||||
{
|
||||
emit passwordRejected(QStringLiteral("sudo не найден"));
|
||||
return;
|
||||
}
|
||||
|
||||
const QString askpass = create_ask_pass_helper(password);
|
||||
if (askpass.isEmpty())
|
||||
{
|
||||
emit passwordRejected(QStringLiteral("Не удалось подготовить проверку пароля"));
|
||||
return;
|
||||
}
|
||||
|
||||
// -v только проверяет пароль, команду не запускает;
|
||||
// -k не даёт sudo поверить кэшу, иначе неверный пароль прошёл бы.
|
||||
QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
|
||||
env.insert(QStringLiteral("SUDO_ASKPASS"), askpass);
|
||||
env.insert(QStringLiteral("SUDO_ASKPASS_REQUIRE"), QStringLiteral("force"));
|
||||
env.insert(QStringLiteral("LC_ALL"), QStringLiteral("C"));
|
||||
|
||||
m_password_check.setProcessEnvironment(env);
|
||||
m_password_candidate = password;
|
||||
m_password_check_running = true;
|
||||
m_password_check.start(sudo, {QStringLiteral("-k"), QStringLiteral("-A"), QStringLiteral("-v")});
|
||||
}
|
||||
|
||||
void ProcessManager::on_password_check_finished(int exit_code, QProcess::ExitStatus status)
|
||||
{
|
||||
if (!m_password_check_running) return;
|
||||
m_password_check_running = false;
|
||||
|
||||
const QString stderr_text = QString::fromLocal8Bit(m_password_check.readAllStandardError()).trimmed();
|
||||
m_askpass_dir.reset();
|
||||
|
||||
if (status == QProcess::NormalExit && exit_code == 0)
|
||||
{
|
||||
// пароль проверен — запоминаем до конца работы программы
|
||||
m_password = m_password_candidate;
|
||||
m_password_candidate.clear();
|
||||
emit passwordAccepted();
|
||||
return;
|
||||
}
|
||||
|
||||
m_password_candidate.clear();
|
||||
qWarning().noquote() << "password check failed:" << stderr_text;
|
||||
|
||||
if (stderr_text.contains(QStringLiteral("incorrect password"), Qt::CaseInsensitive)
|
||||
|| stderr_text.contains(QStringLiteral("Sorry, try again"), Qt::CaseInsensitive)
|
||||
|| stderr_text.contains(QStringLiteral("authentication failure"), Qt::CaseInsensitive))
|
||||
emit passwordRejected(QStringLiteral("Неверный пароль, попробуйте ещё раз"));
|
||||
else
|
||||
emit passwordRejected(QStringLiteral("Не удалось проверить пароль"));
|
||||
}
|
||||
|
||||
void ProcessManager::on_password_check_error(QProcess::ProcessError error)
|
||||
{
|
||||
if (error != QProcess::FailedToStart) return;
|
||||
if (!m_password_check_running) return;
|
||||
|
||||
m_password_check_running = false;
|
||||
m_password_candidate.clear();
|
||||
m_askpass_dir.reset();
|
||||
|
||||
qWarning() << "cannot start sudo for password check:" << m_password_check.errorString();
|
||||
emit passwordRejected(QStringLiteral("Не удалось запустить sudo"));
|
||||
}
|
||||
|
||||
void ProcessManager::update()
|
||||
{
|
||||
if (m_status != Status::Null) return;
|
||||
if (m_status != Status::Null || m_password_check_running) return;
|
||||
|
||||
const auto res = check_askpas();
|
||||
if (!res)
|
||||
@@ -123,13 +210,13 @@ std::expected<QString, QString> ProcessManager::check_askpas()
|
||||
return std::unexpected(QStringLiteral("cannot update: ") + m_db_lck +
|
||||
QStringLiteral("exists — another pacman is running or a stale lock is left. \nClose the package manager, or remove the lock if no pacman is running."));
|
||||
|
||||
askpass = create_ask_pass_helper();
|
||||
askpass = create_ask_pass_helper(m_password);
|
||||
if (askpass.isEmpty()) return std::unexpected(QStringLiteral("cannot create askpass helper for yay"));
|
||||
|
||||
return askpass;
|
||||
}
|
||||
|
||||
QString ProcessManager::create_ask_pass_helper()
|
||||
QString ProcessManager::create_ask_pass_helper(const QString& password)
|
||||
{
|
||||
m_askpass_dir = std::make_unique<QTemporaryDir>();
|
||||
if (!m_askpass_dir->isValid())
|
||||
@@ -149,7 +236,7 @@ QString ProcessManager::create_ask_pass_helper()
|
||||
return {};
|
||||
}
|
||||
|
||||
QString escaped = m_password;
|
||||
QString escaped = password;
|
||||
escaped.replace(QLatin1Char('\''), QStringLiteral("'\\''"));
|
||||
|
||||
script.write("#!/bin/sh\nprintf '%s\\n' '" + escaped.toUtf8() + "'\n");
|
||||
|
||||
@@ -38,15 +38,14 @@ class ProcessManager : public QObject
|
||||
|
||||
public:
|
||||
Q_INVOKABLE void checkUpdates();
|
||||
Q_INVOKABLE void checkPassword(const QString& password);
|
||||
Q_INVOKABLE void setPackageManager(int v);
|
||||
Q_INVOKABLE void setPassword(QString password);
|
||||
Q_INVOKABLE bool passwordEmpty() const;
|
||||
Q_INVOKABLE void update();
|
||||
|
||||
public:
|
||||
int packageCount() const;
|
||||
QStringList updateLog() const;
|
||||
QString password() const;
|
||||
QVariantList packages() const;
|
||||
Status status() const;
|
||||
|
||||
@@ -59,15 +58,20 @@ class ProcessManager : public QObject
|
||||
PackageManager m_package_manager = PackageManager::Yay;
|
||||
Status m_status = Status::Null;
|
||||
QString m_password;
|
||||
QString m_password_candidate;
|
||||
QVariantList m_packages;
|
||||
int m_package_count = 0;
|
||||
std::unique_ptr<QTemporaryDir> m_askpass_dir;
|
||||
QProcess m_password_check;
|
||||
bool m_password_check_running = false;
|
||||
|
||||
private:
|
||||
void set_packages(const QVariantList& v);
|
||||
void set_status(Status s);
|
||||
QString create_ask_pass_helper();
|
||||
QString create_ask_pass_helper(const QString& password);
|
||||
std::expected<QString, QString> check_askpas();
|
||||
void on_password_check_finished(int exit_code, QProcess::ExitStatus status);
|
||||
void on_password_check_error(QProcess::ProcessError error);
|
||||
void finished();
|
||||
void set_updating_flag(bool updated, bool updating);
|
||||
|
||||
@@ -76,4 +80,6 @@ class ProcessManager : public QObject
|
||||
void package_count_changed();
|
||||
void status_changed();
|
||||
void updateLogChanged();
|
||||
void passwordAccepted();
|
||||
void passwordRejected(const QString& reason);
|
||||
};
|
||||
|
||||
@@ -14,7 +14,16 @@ Dialog {
|
||||
|
||||
property string action: "update"
|
||||
property string packageName: ""
|
||||
signal passwordEntered(string password)
|
||||
property bool checking: false
|
||||
property string errorText: ""
|
||||
signal passwordSubmitted(string password)
|
||||
|
||||
function submit() {
|
||||
if (root.checking || passwordField.text.length === 0) return;
|
||||
root.checking = true;
|
||||
root.errorText = "";
|
||||
root.passwordSubmitted(passwordField.text);
|
||||
}
|
||||
|
||||
background: Rectangle {
|
||||
color: Theme.color1
|
||||
@@ -61,7 +70,16 @@ Dialog {
|
||||
border.color: passwordField.activeFocus ? Theme.color6 : Theme.color4
|
||||
}
|
||||
|
||||
onAccepted: if (okButton.enabled) root.accept()
|
||||
onAccepted: root.submit()
|
||||
}
|
||||
|
||||
Label {
|
||||
text: root.errorText
|
||||
visible: text.length > 0
|
||||
color: "#e78284"
|
||||
font.pixelSize: 12
|
||||
wrapMode: Text.WordWrap
|
||||
Layout.fillWidth: true
|
||||
}
|
||||
}
|
||||
|
||||
@@ -105,13 +123,13 @@ Dialog {
|
||||
Button {
|
||||
id: okButton
|
||||
text: root.action === "install" ? "✓ Установить" : root.action === "remove" ? "✓ Удалить" : "✓ Обновить"
|
||||
enabled: passwordField.text.length > 0
|
||||
enabled: passwordField.text.length > 0 && !root.checking
|
||||
leftPadding: 14
|
||||
rightPadding: 14
|
||||
topPadding: 8
|
||||
bottomPadding: 8
|
||||
|
||||
onClicked: root.accept()
|
||||
onClicked: root.submit()
|
||||
|
||||
background: Rectangle {
|
||||
radius: 4
|
||||
@@ -130,7 +148,14 @@ Dialog {
|
||||
}
|
||||
}
|
||||
|
||||
onOpened: passwordField.forceActiveFocus()
|
||||
onAccepted: root.passwordEntered(passwordField.text)
|
||||
onClosed: passwordField.text = ""
|
||||
onOpened: {
|
||||
root.checking = false
|
||||
root.errorText = ""
|
||||
passwordField.forceActiveFocus()
|
||||
}
|
||||
onClosed: {
|
||||
passwordField.text = ""
|
||||
root.checking = false
|
||||
root.errorText = ""
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user