Skip to content

Conversation

@caixr23
Copy link
Contributor

@caixr23 caixr23 commented Oct 31, 2025

Obtain password only during user activity

pms: BUG-338367

Summary by Sourcery

Defer hotspot password retrieval until the user session is active by integrating systemd-logind DBus signals to monitor display and session ‘Active’ properties, and update the configuration only when the session is active. Also adjust the default PSK flags in the Hotspot QML to zero.

New Features:

  • Add m_userActive flag and DBus calls in NetHotspotController to track user and session activity via systemd-logind.
  • Introduce onLoginSessionPropertiesChanged and updateDisplay slots to update active state and trigger configuration updates.

Bug Fixes:

  • Prevent password acquisition when the user session is inactive by checking m_userActive before updating hotspot configuration.

Enhancements:

  • Change default PSK flags in PageHotspot.qml from 1 to 0 for WPA/RSN and SAE security modes.

@sourcery-ai
Copy link

sourcery-ai bot commented Oct 31, 2025

Reviewer's Guide

Integrate DBus-based login session monitoring in NetHotspotController to only retrieve the hotspot password when the user is active, and update QML security settings to use PSK flag 0 for WPA and SAE networks.

Sequence diagram for DBus-based user activity monitoring in NetHotspotController

sequenceDiagram
participant NetHotspotController
participant DBus
participant LoginSession
NetHotspotController->>DBus: createMethodCall("Get", "Display")
DBus-->>NetHotspotController: updateDisplay(QDBusVariant)
NetHotspotController->>LoginSession: createMethodCall("Get", "Remote")
LoginSession-->>NetHotspotController: QDBusPendingReply<QDBusVariant>
NetHotspotController->>DBus: connect("PropertiesChanged", onLoginSessionPropertiesChanged)
DBus-->>NetHotspotController: onLoginSessionPropertiesChanged(properties)
NetHotspotController->>NetHotspotController: updateConfig() (only if user active)
Loading

Updated class diagram for NetHotspotController

classDiagram
class NetHotspotController {
  -HotspotController *m_hotspotController
  -bool m_isEnabled
  -bool m_enabledable
  -bool m_deviceEnabled
  -bool m_userActive
  -QVariantMap m_config
  -QStringList m_shareDevice
  -QStringList m_optionalDevice
  +void updateEnabledable()
  +void updateData()
  +void updateConfig()
  +void onLoginSessionPropertiesChanged(const QString &, const QVariantMap &properties, const QStringList &)
  +void updateDisplay(const QDBusVariant &display)
}
NetHotspotController --> HotspotController
Loading

File-Level Changes

Change Details Files
Gate password retrieval on user activity via DBus session monitoring
  • Introduce m_userActive flag and initialize it to true
  • Include QDBusVariant and declare updateDisplay and onLoginSessionPropertiesChanged slots
  • Add DBus call in constructor to fetch Display property and set up callback
  • Implement updateDisplay slot to subscribe to session property changes
  • Implement onLoginSessionPropertiesChanged slot to update m_userActive and trigger updateConfig
  • Modify updateConfig to return early when user is not active or timer is active
net-view/operation/private/nethotspotcontroller.cpp
net-view/operation/private/nethotspotcontroller.h
Adjust PSK flag configuration for WPA and SAE networks in QML
  • Change psk-flags value from 1 to 0 in both 'psk' and 'sae' cases
dcc-network/qml/PageHotspot.qml

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey there - I've reviewed your changes - here's some feedback:

  • Consider replacing QDBusPendingReply.waitForFinished with an asynchronous callback to avoid blocking the main event loop when fetching session properties.
  • Instead of defaulting m_userActive to true, perform an initial D-Bus query of the Active property on startup (and handle errors) so you don't trigger updateConfig prematurely.
  • The QML change setting psk-flags to 0 makes password storage less restrictive—please add an inline comment or doc explaining this security decision for future maintainers.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- Consider replacing QDBusPendingReply.waitForFinished with an asynchronous callback to avoid blocking the main event loop when fetching session properties.
- Instead of defaulting m_userActive to true, perform an initial D-Bus query of the Active property on startup (and handle errors) so you don't trigger updateConfig prematurely.
- The QML change setting psk-flags to 0 makes password storage less restrictive—please add an inline comment or doc explaining this security decision for future maintainers.

## Individual Comments

### Comment 1
<location> `net-view/operation/private/nethotspotcontroller.cpp:38-40` </location>
<code_context>
     connect(m_hotspotController, &HotspotController::itemAdded, this, &NetHotspotController::updateConfig);
     connect(m_hotspotController, &HotspotController::itemRemoved, this, &NetHotspotController::updateConfig);
     connect(m_hotspotController, &HotspotController::itemChanged, this, &NetHotspotController::updateConfig, Qt::QueuedConnection);
+    QDBusMessage msg = QDBusMessage::createMethodCall("org.freedesktop.login1", "/org/freedesktop/login1/user/self", "org.freedesktop.DBus.Properties", "Get");
+    msg << "org.freedesktop.login1.User" << "Display";
+    QDBusConnection::systemBus().callWithCallback(msg, this, SLOT(updateDisplay(QDBusVariant)));
 }

</code_context>

<issue_to_address>
**suggestion (bug_risk):** Consider error handling for DBus call failures.

If the DBus service is unavailable or the call fails, the code may behave unexpectedly. Please add error handling or a fallback to ensure robustness.

Suggested implementation:

```cpp
    QDBusMessage msg = QDBusMessage::createMethodCall("org.freedesktop.login1", "/org/freedesktop/login1/user/self", "org.freedesktop.login1.User", "Display");
    msg << "org.freedesktop.login1.User" << "Display";
    QDBusConnection::systemBus().callWithCallback(
        msg,
        this,
        SLOT(updateDisplay(QDBusVariant)),
        SLOT(handleDBusError(QDBusError))
    );

```

```cpp
}

// Error handling slot for DBus call failures
void NetHotspotController::handleDBusError(const QDBusError &error)
{
    qWarning() << "DBus call failed:" << error.name() << error.message();
    // Optionally, set a fallback value or notify the user/UI
}

```

You must declare the new slot `handleDBusError(const QDBusError &error)` in the header file (`nethotspotcontroller.h`) under the `public slots:` or `private slots:` section, depending on your class design.

Example:
```cpp
private slots:
    void handleDBusError(const QDBusError &error);
```
</issue_to_address>

### Comment 2
<location> `net-view/operation/private/nethotspotcontroller.cpp:218-222` </location>
<code_context>
+    }
+    QDBusMessage msg = QDBusMessage::createMethodCall("org.freedesktop.login1", path.path(), "org.freedesktop.DBus.Properties", "Get");
+    msg << "org.freedesktop.login1.Session" << "Remote";
+    QDBusPendingReply<QDBusVariant> remoteReply = QDBusConnection::systemBus().asyncCall(msg);
+    remoteReply.waitForFinished();
+    if (!remoteReply.value().variant().toBool()) {
+        QDBusConnection::systemBus().connect("org.freedesktop.login1", path.path(), "org.freedesktop.DBus.Properties", "PropertiesChanged", this, SLOT(onLoginSessionPropertiesChanged(QString, QVariantMap, QStringList)));
</code_context>

<issue_to_address>
**suggestion (performance):** Blocking wait on async DBus call may impact responsiveness.

Consider replacing waitForFinished() with a non-blocking mechanism, such as connecting the reply to a slot or lambda, to prevent blocking the main thread.

```suggestion
    QDBusPendingReply<QDBusVariant> *remoteReply = new QDBusPendingReply<QDBusVariant>(QDBusConnection::systemBus().asyncCall(msg));
    QObject *self = this;
    QObject::connect(remoteReply, &QDBusPendingReply<QDBusVariant>::finished, [remoteReply, path, self]() {
        if (!remoteReply->value().variant().toBool()) {
            QDBusConnection::systemBus().connect("org.freedesktop.login1", path.path(), "org.freedesktop.DBus.Properties", "PropertiesChanged", self, SLOT(onLoginSessionPropertiesChanged(QString, QVariantMap, QStringList)));
        }
        remoteReply->deleteLater();
    });
```
</issue_to_address>

### Comment 3
<location> `net-view/operation/private/nethotspotcontroller.cpp:198-201` </location>
<code_context>
+void NetHotspotController::onLoginSessionPropertiesChanged(const QString &, const QVariantMap &properties, const QStringList &)
+{
+    if (properties.contains("Active")) {
+        m_userActive = properties.value("Active").value<bool>();
+        updateConfig();
+    }
</code_context>

<issue_to_address>
**suggestion (bug_risk):** Type conversion from QVariant to bool may be fragile.

Using value<bool>() may not handle type mismatches or missing values safely. Prefer QVariant::toBool() or check the type before conversion to avoid unexpected behavior.

```suggestion
    if (properties.contains("Active")) {
        const QVariant activeValue = properties.value("Active");
        m_userActive = activeValue.toBool();
        updateConfig();
    }
```
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Obtain password only during user activity

pms: BUG-338367
@deepin-ci-robot
Copy link

deepin pr auto review

我来对这个代码变更进行详细审查:

  1. 代码语法和逻辑审查:
  • 语法上没有明显错误,符合QML和C++的规范
  • 逻辑上增加了用户活动状态检查,用于控制热点配置的更新
  1. 代码质量改进建议:
  • 在nethotspotcontroller.cpp中,DBUS相关的代码可以抽取成单独的函数,提高代码可读性
  • updateDisplay函数中的异步调用链较长,建议增加错误处理和日志
  • 建议为m_userActive添加文档说明其具体含义
  1. 代码性能优化建议:
  • updateConfig函数中的条件判断 !m_userActive || m_updatedTimer->isActive() 可能导致频繁返回,建议考虑是否需要添加防抖机制
  • DBUS调用是异步的,但多个连续的DBUS调用可能影响性能,建议考虑合并或优化调用顺序
  1. 代码安全改进建议:
  • PageHotspot.qml中将psk-flags从1改为0需要特别关注,这涉及密码存储策略,应该确认这个修改的安全性
  • DBUS调用缺少错误处理,建议增加对调用失败的处理逻辑
  • 建议对用户活动状态的变更增加权限验证
  1. 具体改进建议:
// 在nethotspotcontroller.h中添加注释
/**
 * @brief m_userActive 用户当前是否处于活动状态
 * 用于控制是否需要认证才能访问热点配置
 */
bool m_userActive;

// 在nethotspotcontroller.cpp中优化DBUS调用
void NetHotspotController::initializeDBusConnection()
{
    // 初始化DBUS连接
    QDBusMessage msg = QDBusMessage::createMethodCall("org.freedesktop.login1", 
                                                     "/org/freedesktop/login1/user/self", 
                                                     "org.freedesktop.DBus.Properties", 
                                                     "Get");
    msg << "org.freedesktop.login1.User" << "Display";
    
    QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(
        QDBusConnection::systemBus().asyncCall(msg), this);
    
    connect(watcher, &QDBusPendingCallWatcher::finished, 
            this, &NetHotspotController::handleDisplayResponse);
}

// 添加错误处理
void NetHotspotController::handleDisplayResponse(QDBusPendingCallWatcher *watcher)
{
    QDBusPendingReply<QDBusVariant> reply = *watcher;
    if (reply.isError()) {
        qWarning() << "Failed to get display property:" << reply.error().message();
        watcher->deleteLater();
        return;
    }
    
    // 处理正常响应
    updateDisplay(reply.value());
    watcher->deleteLater();
}
  1. 其他建议:
  • 考虑添加单元测试来验证用户活动状态变更时的行为
  • 建议在代码中添加更多注释说明业务逻辑,特别是关于安全相关的修改
  • 考虑添加配置验证机制,确保热点配置的合法性

总的来说,这个变更主要是增加了用户活动状态的检查机制,但在代码结构、错误处理和安全性方面还有改进空间。建议在合并前进行更全面的测试。

@caixr23 caixr23 requested a review from mhduiy November 3, 2025 05:24
@deepin-ci-robot
Copy link

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: caixr23, mhduiy

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@caixr23 caixr23 merged commit d2b6eb3 into linuxdeepin:master Nov 3, 2025
16 of 18 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants