Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions configs/snipe/org.deepin.dde.lightdm-deepin-greeter.json
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,16 @@
"description": "是否长按小眼睛显示密码,true-鼠按长按小眼睛才显示密码,false-鼠标点击一下就一直显示密码",
"permissions": "readwrite",
"visibility": "private"
},
"pamTranslationTSPrefix":{
"value": "",
"serial": 0,
"flags": ["global"],
"name": "pamTranslationTSPath",
"name[zh_CN]": "提供pam翻译内容的ts文件路径前缀",
"description": "提供pam翻译内容的ts文件路径前缀",
"permissions": "readwrite",
"visibility": "private"
}
}
}
10 changes: 10 additions & 0 deletions configs/snipe/org.deepin.dde.lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,16 @@
"description": "是否打开关机、重启黑屏界面;false:不开,true:开启。默认值为true;",
"permissions": "readwrite",
"visibility": "private"
},
"pamTranslationTSPrefix":{
"value": "",
"serial": 0,
"flags": ["global"],
"name": "pamTranslationTSPath",
"name[zh_CN]": "提供pam翻译内容的ts文件路径前缀",
"description": "提供pam翻译内容的ts文件路径前缀",
"permissions": "readwrite",
"visibility": "private"
}
}
}
5 changes: 3 additions & 2 deletions src/global_util/greeterpasswordchange.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -238,14 +238,15 @@ void GreeterPasswordChange::prompt(const QString &text, bool secret)
m_promptTimer.start();
}

void GreeterPasswordChange::message(bool error)
void GreeterPasswordChange::message(bool error, const QString &translatedError)
{
if (!m_active || !m_submitted || !error)
return;
if (m_replacementSent) {
// PAM output can contain passwords. Never forward it to the UI/log.
m_errorReceived = true;
emit formChanged(currentRequired(), m_busy, tr("Password change failed. Please try again."));
emit formChanged(currentRequired(), m_busy, translatedError.isEmpty()
? tr("Password change failed. Please try again.") : translatedError);
} else if (m_expected == Input::New) {
// An error after a current-password response can be an expiry notice.
// Advancing to New confirms that authentication passed; asking Current
Expand Down
2 changes: 1 addition & 1 deletion src/global_util/greeterpasswordchange.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class GreeterPasswordChange : public QObject
void start(const QString &current = {}, const QString &locale = {}, bool passwordless = false);
// Collect a complete PAM batch and respond on the next event turn.
void prompt(const QString &text, bool secret);
void message(bool error);
void message(bool error, const QString &translatedError = {});
void submit(const QString &current, const QString &password,
const QString &confirmation, const QString &hint);
void resetIdleTimer();
Expand Down
8 changes: 7 additions & 1 deletion src/global_util/lockpasswordchange.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include "lockpasswordchange.h"
#include "dbusconstant.h"
#include "pamtranslator.h"
#include "userinfo.h"

#include <QDBusConnection>
Expand Down Expand Up @@ -383,13 +384,18 @@ void LockPasswordChange::startProcess()
}
m_timer.stop();
appendOutput(process->readAll());
const auto error = sanitizedOutput();
const auto output = sanitizedOutput();
stopProcess();
if (status == QProcess::NormalExit && exitCode == 0) {
finish(true);
} else {
// A failed current-password check also needs a chance to correct the old password.
m_oldPassword.clear();
#ifdef ENABLE_DSS_SNIPE
const auto error = PamTranslator::instance()->trans(output, true);
#else
const auto &error = output;
#endif
fail(error.isEmpty() ? tr("Password change failed. Please try again.") : error);
}
});
Expand Down
158 changes: 158 additions & 0 deletions src/global_util/pamtranslator.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
// SPDX-FileCopyrightText: 2026 UnionTech Software Technology Co., Ltd.
// SPDX-License-Identifier: GPL-3.0-or-later

#include "pamtranslator.h"
#include "public_func.h"
#include "dconfig_helper.h"

#include <QLocale>
#include <QFile>
#include <QXmlStreamReader>
#include <QDebug>

const QString serverTitle = "Server message:";
const QString serverTitle2 = "BAD PASSWORD:";

PamTranslator *PamTranslator::instance()
{
static PamTranslator pt;
return &pt;
}

PamTranslator::PamTranslator(QObject *parent)
: QObject(parent)
, m_local(QLocale::system().name())
{
m_ADomainErrorDetailMap = getLocalXmlTranslations();
}

QString PamTranslator::trans(const QString &errorTxt, bool force)
{
if (m_ADomainErrorDetailMap.isEmpty()) {
return errorTxt;
}

QString content = getTranslation(errorTxt);

// 如果force为true,没有对应方案时用complexity constraints的翻译作为结果
if (force) {
if (!m_ADomainErrorDetailMap.values().contains(content)) {
content = getTranslation("complexity constraints");
Comment thread
sourcery-ai[bot] marked this conversation as resolved.
Resolved
qInfo() << Q_FUNC_INFO << " Content lost in detailMap : " << content;
}
}

return content;
}

void PamTranslator::setLocal(const QString &localName)
{
if (m_local == localName) {
return;
}

qCDebug(DDE_SHELL) << "switch to local : " << m_local;

m_local = localName;
m_ADomainErrorDetailMap.clear();
m_ADomainErrorDetailMap = getLocalXmlTranslations();
}

QMap<QString, QString> PamTranslator::getLocalXmlTranslations()
{
QMap<QString, QString> translationsMap;

auto tsPath = DConfigHelper::instance()->getConfig("pamTranslationTSPrefix", "").toString();
if (tsPath.isEmpty()) {
return translationsMap;
}

QFile file(tsPath + m_local + ".ts");
qDebug() << "getLocalXmlTranslations xmlFilePath : " << file.fileName();
if (!file.exists()) {
qCWarning(DDE_SHELL) << "File not exist : " << file.fileName();
return translationsMap;
}

if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
qCWarning(DDE_SHELL) << "Open file failed : " << file.fileName();
return translationsMap;
}

QXmlStreamReader xml(&file);
while (!xml.atEnd() && !xml.hasError()) {
xml.readNext();

if (xml.isStartElement() && xml.name() == "message") {
QString source;
QString translation;

while (xml.readNextStartElement()) {
Comment thread
sourcery-ai[bot] marked this conversation as resolved.
Resolved
if (xml.name() == "source") {
source = xml.readElementText();
} else if (xml.name() == "translation") {
translation = xml.readElementText();
Comment thread
sourcery-ai[bot] marked this conversation as resolved.
Resolved
}
}

if (!source.isEmpty() && !translation.isEmpty()) {
translationsMap[source] = translation;
}
}
}
file.close();

return translationsMap;
}

QString PamTranslator::getTranslation(const QString &content)
{
QString data = content.toLower();

// 优先严格匹配
auto source = content.trimmed().toLower();
source = source.replace(":", "");
if (m_ADomainErrorDetailMap.contains(content)) {
return m_ADomainErrorDetailMap.value(content);
}

auto keys = m_ADomainErrorDetailMap.keys();

QStringList lines = data.split("\n", Qt::SkipEmptyParts);
QString tmpResutl = "";
QString result = "";

for (auto line : lines) {
auto currentLine = line;
currentLine = currentLine.replace(serverTitle, "");
currentLine = currentLine.replace(serverTitle2, "");
currentLine = currentLine.trimmed();

if (keys.contains(currentLine)) {
return m_ADomainErrorDetailMap.value(currentLine);
} else {
// 取最长内容翻译
int matchDelta = INT_MAX;
for (auto key : keys) {
if (currentLine.contains(key.toLower())) {
tmpResutl = m_ADomainErrorDetailMap.value(key);
if (!tmpResutl.isEmpty()) {
auto delta = qAbs(currentLine.length() - key.length());
if (delta < matchDelta) {
result = tmpResutl;
matchDelta = delta;
}
}
}
}
}
}

// 尝试取最后一个匹配项,避免翻译冲突
if (!result.isEmpty()) {
return result;
}

qWarning() << "No translation found for PAM message";
return content;
}
31 changes: 31 additions & 0 deletions src/global_util/pamtranslator.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// SPDX-FileCopyrightText: 2026 UnionTech Software Technology Co., Ltd.
// SPDX-License-Identifier: GPL-3.0-or-later

#ifndef PAMTRANSLATOR_H
#define PAMTRANSLATOR_H

#include <QObject>
#include <QMap>

class PamTranslator : public QObject
{
Q_OBJECT
public:
static PamTranslator *instance();
QString trans(const QString &text, bool force = false);
void setLocal(const QString &);

private:
explicit PamTranslator(QObject *parent = nullptr);
PamTranslator(const PamTranslator &) = delete;
PamTranslator &operator=(const PamTranslator &) = delete;

QMap<QString, QString> getLocalXmlTranslations();
QString getTranslation(const QString &content);

private:
QMap<QString, QString> m_ADomainErrorDetailMap;
QString m_local;
};

#endif // PAMTRANSLATOR_H
6 changes: 6 additions & 0 deletions src/lightdm-deepin-greeter/greeterworker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "dconfig_helper.h"
#include "login_plugin_util.h"
#include "mfasequencecontrol.h"
#include "pamtranslator.h"
#include "signal_bridge.h"

#include <DSysInfo>
Expand Down Expand Up @@ -956,7 +957,12 @@ void GreeterWorker::showMessage(const QString &text, const QLightDM::Greeter::Me
if (m_passwordChange->active()) {
if (!ensurePasswordChangeAllowed())
return;
#ifdef ENABLE_DSS_SNIPE
const auto message = PamTranslator::instance()->trans(text);
m_passwordChange->message(type == QLightDM::Greeter::MessageTypeError, message == text ? QString() : message);
Comment thread
sourcery-ai[bot] marked this conversation as resolved.
Resolved
#else
m_passwordChange->message(type == QLightDM::Greeter::MessageTypeError);
#endif
return;
}
if (type == QLightDM::Greeter::MessageTypeError) {
Expand Down
4 changes: 3 additions & 1 deletion src/session-widgets/lockpasswordchangewidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,9 @@ void LockPasswordChangeWidget::setState(bool currentRequired, bool checkingOnly,
m_submit->setText(checkingOnly ? tr("Unlock") : tr("Save"));
m_info->setText(busy ? (checkingOnly ? tr("Checking password expiration...") : tr("Changing password..."))
: (checkingOnly ? tr("Please enter the old password.") : tr("Password expired. Please change it.")));
m_error->setPlainText(error);
QString displayError = error;
displayError.replace(QStringLiteral("\\n"), QStringLiteral("\n"));
m_error->setPlainText(displayError);
m_error->setVisible(!error.isEmpty());
if (!error.isEmpty() && !busy) {
auto *input = currentRequired ? m_current : m_password;
Expand Down
26 changes: 26 additions & 0 deletions tests/dde-lock/ut_greeterpasswordchange.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,32 @@
#include <gtest/gtest.h>
#include <libintl.h>

TEST(UT_GreeterPasswordChange, ShowsTranslatedPamErrorUntilRetry)
{
const auto detail = QStringLiteral("新密码过于简单或规律性太强,请更换密码。");

GreeterPasswordChange change;
QSignalSpy form(&change, &GreeterPasswordChange::formChanged);
QSignalSpy responses(&change, &GreeterPasswordChange::respond);
change.start("old");
change.submit({}, "newSecret!", "newSecret!", {});
change.prompt("New password:", true);
ASSERT_TRUE(QTest::qWaitFor([&] { return responses.count() == 1; }));

const auto initialFormCount = form.count();
change.message(false, QStringLiteral("PAM informational message"));
EXPECT_EQ(form.count(), initialFormCount);
change.message(true, detail);
ASSERT_EQ(form.count(), initialFormCount + 1);
EXPECT_EQ(form.last()[2].toString(), detail);

const auto formCount = form.count();
change.prompt("New password:", true);
ASSERT_TRUE(QTest::qWaitFor([&] { return form.count() > formCount; }));
EXPECT_FALSE(form.last()[1].toBool());
EXPECT_EQ(form.last()[2].toString(), GreeterPasswordChange::tr("Password change failed. Please try again."));
}
Comment thread
sourcery-ai[bot] marked this conversation as resolved.
Resolved

TEST(UT_GreeterPasswordChange, RespondsOnlyToSecretPromptsAfterSave)
{
GreeterPasswordChange change;
Expand Down
6 changes: 3 additions & 3 deletions tests/dde-lock/ut_lockpasswordchange.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2036,9 +2036,9 @@ TEST_F(UT_LockPasswordGate, ExpiredBiometricFormShowsAllFieldsAndClearsOnCancel)
EXPECT_EQ(page->m_password->text(), QStringLiteral("new"));
EXPECT_EQ(page->m_confirmation->text(), QStringLiteral("different"));
EXPECT_TRUE(page->m_confirmation->lineEdit()->hasFocus());
// A raw string containing HTML is rendered as plain text.
emit model->lockPasswordChangeForm(true, false, false, QStringLiteral("<b>policy error</b>"));
EXPECT_EQ(page->m_error->toPlainText(), QStringLiteral("<b>policy error</b>"));
// Convert catalog line breaks while displaying markup as plain text.
emit model->lockPasswordChangeForm(true, false, false, QStringLiteral("<b>policy error</b>\\nTry again"));
EXPECT_EQ(page->m_error->toPlainText(), QStringLiteral("<b>policy error</b>\nTry again"));
QSignalSpy finished(model, &SessionBaseModel::authFinished);
QTest::keyClick(page->m_password->lineEdit(), Qt::Key_Escape);
EXPECT_EQ(finished.count(), 0);
Expand Down
4 changes: 4 additions & 0 deletions translations/dde-session-shell_zh_CN.ts
Original file line number Diff line number Diff line change
Expand Up @@ -624,6 +624,10 @@
<source>Password cannot be empty</source>
<translation>密码不能为空</translation>
</message>
<message>
<source>The two password inputs are inconsistent.</source>
<translation>两次密码输入不一致</translation>
Comment thread
sourcery-ai[bot] marked this conversation as resolved.
Resolved
</message>
<message>
<source>Password must have at least %1 characters</source>
<translation>密码长度至少为%1个字符</translation>
Expand Down
Loading