From 0a130f0034a77b5bb764a41f4832bcf224c9381f Mon Sep 17 00:00:00 2001
From: liuxinhao <liuxinhao@kylinsec.com.cn>
Date: Wed, 31 May 2023 14:29:18 +0800
Subject: [PATCH 2/2] fix(polkit-agent): Remove the restriction on polkit-agent
 authentication errors
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

- 去除polkit-agent认证错误限制，错误时需要点击左下重新认证按钮重新认证.适应部分认证流程不存在prompt消息

Closes #I7961L
---
 src/polkit-agent/dialog.cpp              | 87 +++++++++++++++---------
 src/polkit-agent/dialog.h                | 11 ++-
 src/polkit-agent/dialog.ui               | 27 +++++++-
 translations/kiran-polkit-agent.zh_CN.ts | 25 +++++--
 4 files changed, 110 insertions(+), 40 deletions(-)

diff --git a/src/polkit-agent/dialog.cpp b/src/polkit-agent/dialog.cpp
index 16ca2aa..10fdbc5 100644
--- a/src/polkit-agent/dialog.cpp
+++ b/src/polkit-agent/dialog.cpp
@@ -58,7 +58,7 @@ Dialog::Dialog(QWidget* parent)
     : KiranTitlebarWindow(parent),
       ui(new Ui::Dialog)
 {
-ui->setupUi(getWindowContentWidget());
+    ui->setupUi(getWindowContentWidget());
     resize(408, 290);
     initUI();
 }
@@ -119,24 +119,44 @@ void Dialog::initUI()
     layout()->setSizeConstraint(QLayout::SetFixedSize);
     ui->label_tips->setWordWrap(true);
     Kiran::StylePropertyHelper::setButtonType(ui->btn_ok, Kiran::BUTTON_Default);
+    Kiran::StylePropertyHelper::setButtonType(ui->btn_reauth, Kiran::BUTTON_Default);
 
+    switchButtonLayout(BUTTON_LAYOUT_NORMAL);
+    
     m_switcher = new AuthTypeSwitcher(EXPAND_DIRECTION_BOTTOM, 4, this);
     m_switcher->setAdjustColorToTheme(true);
     m_switcher->setFixedSize(QSize(42, 36));
     m_switcher->setVisible(false);
     ui->layout_edit->addWidget(m_switcher);
-    connect(m_switcher, &AuthTypeSwitcher::authTypeChanged, this, &Dialog::onCurrentAuthTypeChanged);
+    connect(m_switcher, &AuthTypeSwitcher::authTypeChanged, this, &Dialog::onSwitcherAuthTypeChanged);
 
     connect(ui->btn_cancel, &QPushButton::clicked, this, &Dialog::onCancelClicked);
     connect(ui->btn_ok, &QPushButton::clicked, this, &Dialog::onOkClicked);
+    connect(ui->btn_reauth,&QPushButton::clicked,this,&Dialog::onReauthClicked);
     connect(ui->edit->lineEdit(), &QLineEdit::returnPressed, this, &Dialog::onOkClicked);
     connect(ui->combobox_user, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &Dialog::onCurrentUserChanged);
 }
 
+void Dialog::switchButtonLayout(ButtonLayout layout)
+{
+    switch (layout)
+    {
+    case BUTTON_LAYOUT_NORMAL:
+        ui->btn_reauth->setVisible(false);
+        ui->btn_ok->setVisible(true);
+        break;
+    case BUTTON_LAYOUT_REAUTH:
+        ui->btn_reauth->setVisible(true);
+        ui->btn_ok->setVisible(false);
+        break;
+    default:
+        break;
+    }
+}
+
 void Dialog::startAuth(const QString& userName)
 {
     m_havePrompt = false;
-    m_triesCount++;
 
     if (m_authController->inAuthentication())
     {
@@ -171,29 +191,19 @@ void Dialog::onOkClicked()
     ui->btn_ok->setEnabled(false);
 }
 
-void Dialog::onCurrentUserChanged(int idx)
+void Dialog::onReauthClicked()
 {
     startAuth(ui->combobox_user->currentText());
 }
 
-void Dialog::onCurrentAuthTypeChanged(KADAuthType authType)
+void Dialog::onCurrentUserChanged(int idx)
 {
-    QMap<KADAuthType, QString> authTypeDesc = {
-        {KAD_AUTH_TYPE_FINGERPRINT, tr("fingerprint auth")},
-        {KAD_AUTH_TYPE_FACE, tr("face auth")},
-        {KAD_AUTH_TYPE_FINGERVEIN, tr("fingervein auth")}};
+    startAuth(ui->combobox_user->currentText());
+}
 
-    ui->label_tips->setText("");
+void Dialog::onSwitcherAuthTypeChanged(KADAuthType authType)
+{
     m_authController->switchAuthType(authType);
-    ui->edit->setEnabled(false);
-    ui->btn_ok->setEnabled(false);
-    ui->edit->lineEdit()->clear();
-    ui->edit->lineEdit()->setPlaceholderText("");
-
-    if (authTypeDesc.contains(authType))
-    {
-        ui->edit->lineEdit()->setPlaceholderText(authTypeDesc[authType]);
-    }
 }
 
 void Dialog::onNotifyAuthMode(KADAuthMode mode)
@@ -208,7 +218,29 @@ void Dialog::onSupportedAuthTypeChanged(QList<KADAuthType> authTypes)
 
 void Dialog::onNotifyAuthTypeChanged(KADAuthType authType)
 {
-    m_switcher->setCurrentAuthType(authType);
+    if( authType != m_switcher->getCurrentAuthType() )
+    {
+        QSignalBlocker blocker(m_switcher);
+        m_switcher->setCurrentAuthType(authType);
+    }
+
+    QMap<KADAuthType, QString> authTypeDesc = {
+        {KAD_AUTH_TYPE_FINGERPRINT, tr("fingerprint auth")},
+        {KAD_AUTH_TYPE_FACE, tr("face auth")},
+        {KAD_AUTH_TYPE_FINGERVEIN, tr("fingervein auth")},
+        {KAD_AUTH_TYPE_IRIS,tr("iris auth")}};
+
+    switchButtonLayout(BUTTON_LAYOUT_NORMAL);
+    ui->label_tips->setText("");
+    ui->edit->setEnabled(false);
+    ui->btn_ok->setEnabled(false);
+    ui->edit->lineEdit()->clear();
+    ui->edit->lineEdit()->setPlaceholderText("");
+
+    if (authTypeDesc.contains(authType))
+    {
+        ui->edit->lineEdit()->setPlaceholderText(authTypeDesc[authType]);
+    }
 }
 
 void Dialog::onAuthComplete(bool success)
@@ -219,21 +251,13 @@ void Dialog::onAuthComplete(bool success)
         this->close();
         return;
     }
-
-    if (m_havePrompt && (m_triesCount < MAX_ERROR_COUNT))
-    {
-        onAuthShowMessage("Authentication error, please authenticate again.", MessageTypeInfo);
-        startAuth(ui->combobox_user->currentText());
-    }
     else
     {
-        if (m_triesCount == MAX_ERROR_COUNT)
-        {
-            onAuthShowMessage("Authentication error", MessageTypeError);
-        }
-        ui->btn_ok->setEnabled(false);
+        onAuthShowMessage("Authentication error", MessageTypeError);
+
         ui->edit->lineEdit()->clear();
         ui->edit->setEnabled(false);
+        switchButtonLayout(BUTTON_LAYOUT_REAUTH);
     }
 }
 
@@ -256,7 +280,6 @@ void Dialog::onAuthShowMessage(const QString& text, MessageType msgType)
 
     ui->label_tips->setText(tips);
 }
-
 }  // namespace PolkitAgent
 }  // namespace SessionGuard
 }  // namespace Kiran
\ No newline at end of file
diff --git a/src/polkit-agent/dialog.h b/src/polkit-agent/dialog.h
index fe56251..7fbb85d 100644
--- a/src/polkit-agent/dialog.h
+++ b/src/polkit-agent/dialog.h
@@ -64,6 +64,11 @@ public:
 class Dialog : public KiranTitlebarWindow
 {
     Q_OBJECT
+    enum ButtonLayout
+    {
+        BUTTON_LAYOUT_NORMAL,
+        BUTTON_LAYOUT_REAUTH
+    };
 public:
     explicit Dialog(QWidget* parent = nullptr);
     ~Dialog();
@@ -77,14 +82,16 @@ signals:
 
 private:
     void initUI();
+    void switchButtonLayout(ButtonLayout layout);
     bool setAuthInfo(const AuthInfo& authInfo);
     void startAuth(const QString& userName);
 
 private slots:
     void onCancelClicked();
     void onOkClicked();
+    void onReauthClicked();
     void onCurrentUserChanged(int idx);
-    void onCurrentAuthTypeChanged(KADAuthType authType);
+    void onSwitcherAuthTypeChanged(KADAuthType authType);
 
     void onNotifyAuthMode(KADAuthMode mode);
     void onSupportedAuthTypeChanged(QList<KADAuthType> authTypes);
@@ -102,7 +109,7 @@ private:
     AuthController* m_authController;
     AuthTypeSwitcher* m_switcher;
     bool m_havePrompt = false;
-    int m_triesCount = 0;
+    ButtonLayout m_buttonLayout = BUTTON_LAYOUT_NORMAL;
 };
 }  // namespace PolkitAgent
 }  // namespace SessionGuard
diff --git a/src/polkit-agent/dialog.ui b/src/polkit-agent/dialog.ui
index 74120eb..b1d4994 100644
--- a/src/polkit-agent/dialog.ui
+++ b/src/polkit-agent/dialog.ui
@@ -6,7 +6,7 @@
    <rect>
     <x>0</x>
     <y>0</y>
-    <width>407</width>
+    <width>410</width>
     <height>234</height>
    </rect>
   </property>
@@ -214,6 +214,31 @@
           </property>
          </spacer>
         </item>
+        <item>
+         <widget class="QPushButton" name="btn_reauth">
+          <property name="sizePolicy">
+           <sizepolicy hsizetype="Fixed" vsizetype="Fixed">
+            <horstretch>0</horstretch>
+            <verstretch>0</verstretch>
+           </sizepolicy>
+          </property>
+          <property name="minimumSize">
+           <size>
+            <width>110</width>
+            <height>40</height>
+           </size>
+          </property>
+          <property name="maximumSize">
+           <size>
+            <width>110</width>
+            <height>40</height>
+           </size>
+          </property>
+          <property name="text">
+           <string>Recertification</string>
+          </property>
+         </widget>
+        </item>
         <item>
          <widget class="QPushButton" name="btn_ok">
           <property name="sizePolicy">
diff --git a/translations/kiran-polkit-agent.zh_CN.ts b/translations/kiran-polkit-agent.zh_CN.ts
index 97ce7f8..ae75cc2 100644
--- a/translations/kiran-polkit-agent.zh_CN.ts
+++ b/translations/kiran-polkit-agent.zh_CN.ts
@@ -41,11 +41,16 @@
     </message>
     <message>
         <location filename="../src/polkit-agent/dialog.ui" line="238"/>
+        <source>Recertification</source>
+        <translation>重新认证</translation>
+    </message>
+    <message>
+        <location filename="../src/polkit-agent/dialog.ui" line="263"/>
         <source>Ok</source>
         <translation>确认</translation>
     </message>
     <message>
-        <location filename="../src/polkit-agent/dialog.ui" line="279"/>
+        <location filename="../src/polkit-agent/dialog.ui" line="304"/>
         <source>Cancel</source>
         <translation>取消</translation>
     </message>
@@ -69,7 +74,7 @@
 <context>
     <name>Kiran::SessionGuard::AuthController</name>
     <message>
-        <location filename="../lib/auth-proxy/auth-controller.cpp" line="275"/>
+        <location filename="../lib/auth-proxy/auth-controller.cpp" line="290"/>
         <source>Failed to authenticate</source>
         <translation>认证失败</translation>
     </message>
@@ -96,6 +101,16 @@
         <source>finger vein auth</source>
         <translation>指静脉认证</translation>
     </message>
+    <message>
+        <location filename="../lib/common-widgets/auth-type-switcher.cpp" line="59"/>
+        <source>iris auth</source>
+        <translation>虹膜认证</translation>
+    </message>
+    <message>
+        <location filename="../lib/common-widgets/auth-type-switcher.cpp" line="60"/>
+        <source>ukey auth</source>
+        <translation>UKey认证</translation>
+    </message>
 </context>
 <context>
     <name>Kiran::SessionGuard::PolkitAgent::Dialog</name>
@@ -105,17 +120,17 @@
         <translation>认证</translation>
     </message>
     <message>
-        <location filename="../src/polkit-agent/dialog.cpp" line="182"/>
+        <location filename="../src/polkit-agent/dialog.cpp" line="187"/>
         <source>fingerprint auth</source>
         <translation>指纹认证</translation>
     </message>
     <message>
-        <location filename="../src/polkit-agent/dialog.cpp" line="183"/>
+        <location filename="../src/polkit-agent/dialog.cpp" line="188"/>
         <source>face auth</source>
         <translation>人脸认证</translation>
     </message>
     <message>
-        <location filename="../src/polkit-agent/dialog.cpp" line="184"/>
+        <location filename="../src/polkit-agent/dialog.cpp" line="189"/>
         <source>fingervein auth</source>
         <translation>指静脉认证</translation>
     </message>
-- 
2.33.0

