From 7884a9781aa24f8c28183eedcbccd297f73768c2 Mon Sep 17 00:00:00 2001 From: chenyuanbo Date: Wed, 25 Nov 2026 17:47:15 +0800 Subject: [PATCH] fix: localize expired AD password change errors MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. Load the configured PAM translation catalog for Snipe greeter and lock screen password changes. 2. Preserve translated errors when PAM requests another entry. 3. Translate mismatched password inputs and render catalog line breaks. Log: Localize errors when changing expired AD passwords. Influence: 1. Verify the Chinese message for mismatched password inputs. 2. Verify translated policy errors and line breaks on the lock screen. 3. Verify translated errors persist when the greeter requests re-entry. fix: 修复 AD 域用户过期改密提示的汉化缺口 1. 登录和锁屏改密时读取 Snipe 配置指定的 PAM 翻译资源。 2. 同一 PAM 会话要求重输新密码时保留错误译文。 3. 补齐两次密码输入不一致的译文并正确显示换行。 Influence: 1. 验证两次密码输入不一致时显示中文提示。 2. 验证锁屏改密的策略错误译文和换行。 3. 验证登录界面要求重输新密码时继续显示错误译文。 Log: 修复 AD 域密码过期后修改密码时的错误提示汉化。 PMS: BUG-384335 --- ...org.deepin.dde.lightdm-deepin-greeter.json | 10 ++ configs/snipe/org.deepin.dde.lock.json | 10 ++ src/global_util/greeterpasswordchange.cpp | 5 +- src/global_util/greeterpasswordchange.h | 2 +- src/global_util/lockpasswordchange.cpp | 8 +- src/global_util/pamtranslator.cpp | 158 ++++++++++++++++++ src/global_util/pamtranslator.h | 31 ++++ src/lightdm-deepin-greeter/greeterworker.cpp | 6 + .../lockpasswordchangewidget.cpp | 4 +- tests/dde-lock/ut_greeterpasswordchange.cpp | 26 +++ tests/dde-lock/ut_lockpasswordchange.cpp | 6 +- translations/dde-session-shell_zh_CN.ts | 4 + 12 files changed, 262 insertions(+), 8 deletions(-) create mode 100644 src/global_util/pamtranslator.cpp create mode 100644 src/global_util/pamtranslator.h diff --git a/configs/snipe/org.deepin.dde.lightdm-deepin-greeter.json b/configs/snipe/org.deepin.dde.lightdm-deepin-greeter.json index 47cf6702..819a53bf 100644 --- a/configs/snipe/org.deepin.dde.lightdm-deepin-greeter.json +++ b/configs/snipe/org.deepin.dde.lightdm-deepin-greeter.json @@ -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" } } } \ No newline at end of file diff --git a/configs/snipe/org.deepin.dde.lock.json b/configs/snipe/org.deepin.dde.lock.json index 03e1ba4f..92e93f1e 100755 --- a/configs/snipe/org.deepin.dde.lock.json +++ b/configs/snipe/org.deepin.dde.lock.json @@ -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" } } } diff --git a/src/global_util/greeterpasswordchange.cpp b/src/global_util/greeterpasswordchange.cpp index 8034dafc..098fec62 100644 --- a/src/global_util/greeterpasswordchange.cpp +++ b/src/global_util/greeterpasswordchange.cpp @@ -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 diff --git a/src/global_util/greeterpasswordchange.h b/src/global_util/greeterpasswordchange.h index 8ae6240f..2d2c5150 100644 --- a/src/global_util/greeterpasswordchange.h +++ b/src/global_util/greeterpasswordchange.h @@ -24,7 +24,7 @@ class GreeterPasswordChange : public QObject void start(const QString ¤t = {}, 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 ¤t, const QString &password, const QString &confirmation, const QString &hint); void resetIdleTimer(); diff --git a/src/global_util/lockpasswordchange.cpp b/src/global_util/lockpasswordchange.cpp index 1e5d5547..b4e97362 100644 --- a/src/global_util/lockpasswordchange.cpp +++ b/src/global_util/lockpasswordchange.cpp @@ -3,6 +3,7 @@ #include "lockpasswordchange.h" #include "dbusconstant.h" +#include "pamtranslator.h" #include "userinfo.h" #include @@ -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); } }); diff --git a/src/global_util/pamtranslator.cpp b/src/global_util/pamtranslator.cpp new file mode 100644 index 00000000..c576b44b --- /dev/null +++ b/src/global_util/pamtranslator.cpp @@ -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 +#include +#include +#include + +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"); + 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 PamTranslator::getLocalXmlTranslations() +{ + QMap 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()) { + if (xml.name() == "source") { + source = xml.readElementText(); + } else if (xml.name() == "translation") { + translation = xml.readElementText(); + } + } + + 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; +} diff --git a/src/global_util/pamtranslator.h b/src/global_util/pamtranslator.h new file mode 100644 index 00000000..90727724 --- /dev/null +++ b/src/global_util/pamtranslator.h @@ -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 +#include + +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 getLocalXmlTranslations(); + QString getTranslation(const QString &content); + +private: + QMap m_ADomainErrorDetailMap; + QString m_local; +}; + +#endif // PAMTRANSLATOR_H diff --git a/src/lightdm-deepin-greeter/greeterworker.cpp b/src/lightdm-deepin-greeter/greeterworker.cpp index ec31cefe..9f05810c 100644 --- a/src/lightdm-deepin-greeter/greeterworker.cpp +++ b/src/lightdm-deepin-greeter/greeterworker.cpp @@ -11,6 +11,7 @@ #include "dconfig_helper.h" #include "login_plugin_util.h" #include "mfasequencecontrol.h" +#include "pamtranslator.h" #include "signal_bridge.h" #include @@ -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); +#else m_passwordChange->message(type == QLightDM::Greeter::MessageTypeError); +#endif return; } if (type == QLightDM::Greeter::MessageTypeError) { diff --git a/src/session-widgets/lockpasswordchangewidget.cpp b/src/session-widgets/lockpasswordchangewidget.cpp index ea114e7f..0a64c31b 100644 --- a/src/session-widgets/lockpasswordchangewidget.cpp +++ b/src/session-widgets/lockpasswordchangewidget.cpp @@ -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; diff --git a/tests/dde-lock/ut_greeterpasswordchange.cpp b/tests/dde-lock/ut_greeterpasswordchange.cpp index ef22cd75..d0bf8198 100644 --- a/tests/dde-lock/ut_greeterpasswordchange.cpp +++ b/tests/dde-lock/ut_greeterpasswordchange.cpp @@ -17,6 +17,32 @@ #include #include +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.")); +} + TEST(UT_GreeterPasswordChange, RespondsOnlyToSecretPromptsAfterSave) { GreeterPasswordChange change; diff --git a/tests/dde-lock/ut_lockpasswordchange.cpp b/tests/dde-lock/ut_lockpasswordchange.cpp index f66d81ae..80007227 100644 --- a/tests/dde-lock/ut_lockpasswordchange.cpp +++ b/tests/dde-lock/ut_lockpasswordchange.cpp @@ -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("policy error")); - EXPECT_EQ(page->m_error->toPlainText(), QStringLiteral("policy error")); + // Convert catalog line breaks while displaying markup as plain text. + emit model->lockPasswordChangeForm(true, false, false, QStringLiteral("policy error\\nTry again")); + EXPECT_EQ(page->m_error->toPlainText(), QStringLiteral("policy error\nTry again")); QSignalSpy finished(model, &SessionBaseModel::authFinished); QTest::keyClick(page->m_password->lineEdit(), Qt::Key_Escape); EXPECT_EQ(finished.count(), 0); diff --git a/translations/dde-session-shell_zh_CN.ts b/translations/dde-session-shell_zh_CN.ts index 47af01b5..505fad40 100644 --- a/translations/dde-session-shell_zh_CN.ts +++ b/translations/dde-session-shell_zh_CN.ts @@ -624,6 +624,10 @@ Password cannot be empty 密码不能为空 + + The two password inputs are inconsistent. + 两次密码输入不一致 + Password must have at least %1 characters 密码长度至少为%1个字符