Skip to content

feat(users): display and sort registered HWID counts - #951

Merged
T3ST3ST3R0N merged 5 commits into
PasarGuard:devfrom
dr-hoseyn:codex/hwid-monitoring
Oct 10, 2026
Merged

T3ST3ST3R0N merged 5 commits into
PasarGuard:devfrom
dr-hoseyn:codex/hwid-monitoring

Conversation

@dr-hoseyn

@dr-hoseyn dr-hoseyn commented Sep 27, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Show each user's registered HWID count in the Users table, including zero for users without registered devices. Desktop gets a sortable HWIDs column; mobile shows the count below the username. Both layouts can sort by HWID count from the sort menu.

Closes #516.

  • Add hwid_count to user-list responses and support sort=hwid_count / sort=-hwid_count, with a user-ID tie-breaker for stable pagination.
  • Calculate counts in the existing list query using the indexed HWID user ID, without loading device records or making per-user requests. Keep counts out of unrelated user/subscription responses and background queries.
  • Refresh cached user lists after removing/resetting HWIDs, and preserve counts when editing a user.
  • Include English, Persian, Russian, and Chinese labels.

Type of change

  • New feature

Checklist

  • Tested locally, including actual browser interactions.
  • Updated translations and API response types.
  • Checked schema impact: the ORM query expression adds no database column and requires no migration.
  • No secrets or unrelated changes.
  • Test scripts, documentation files, build output, and screenshot files are intentionally excluded from this PR's diff. Screenshots are hosted separately.

Testing

  • Production dashboard build: passed.
  • Local feature checks: zero/nonzero counts, duplicate registrations, ascending/descending sorting, tied counts, pagination, empty results, deletion/reset updates, and a constant query count as page size grows; passed. SQL expressions also compile for SQLite, PostgreSQL, and MySQL (runtime database checks used SQLite).
  • Browser checks: desktop header sorting, mobile layout, Persian RTL/dark mode, no horizontal overflow, and resetting HWIDs immediately refreshes and reorders the users table; passed.
  • Cache checks: user edits retain counts and new entries respect HWID sorting; passed.
  • Existing tests/api/test_hwid.py and tests/api/test_user.py: 91 passed, 1 failed. The same test_bulk_apply_template_to_users failure reproduces on unchanged dev at 26047f6b.
  • TypeScript checking is already failing on that base: 533 diagnostics before and after, with no added diagnostics. Python formatting and lint checks for the changed logic passed.

Screenshots

Captured from the running production build with a local database containing synthetic users/devices.

Desktop — highest HWID count first

Desktop HWID counts

Sort menu

HWID sort menu

Mobile

Mobile HWID counts

Persian RTL — dark mode

Persian HWID monitoring

Notes for reviewers

Counts represent registered hardware IDs, not currently online devices. Existing user visibility/ownership filters are applied before pagination. The new count is loaded only for the full user-list endpoint.

Summary by CodeRabbit

  • New Features
    • User lists now show registered hardware ID (HWID) counts, including a compact count on mobile.
    • Sort users by HWID count in ascending or descending order.
    • Updated HWID count labels are available in English, Persian, Russian, and Chinese.
  • Bug Fixes
    • User lists refresh after hardware IDs are changed.
    • Updating a user in the cached list now preserves existing fields not included in the update.

@coderabbitai

coderabbitai Bot commented Sep 27, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration
  • Configuration used: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: dc1aba48-85b0-4bb8-8e0e-7f55ca8143d2

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Advanced

Run ID: 46248dcc-d99c-459e-b91c-1887aefda680


📥 Commits

Reviewing files that changed from the base of the PR and between 26047f6 and f0dfa58.


📒 Files selected for processing (13)
  • app/db/crud/user.py
  • app/db/models.py
  • app/models/user.py
  • app/operation/user.py
  • dashboard/public/statics/locales/en.json
  • dashboard/public/statics/locales/fa.json
  • dashboard/public/statics/locales/ru.json
  • dashboard/public/statics/locales/zh.json
  • dashboard/src/features/users/components/columns.tsx
  • dashboard/src/features/users/components/filters.tsx
  • dashboard/src/features/users/dialogs/user-hwids-modal.tsx
  • dashboard/src/service/api/index.ts
  • dashboard/src/utils/usersCache.ts

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 3 remain after this review.



Walkthrough

The user list now returns registered HWID counts. The dashboard displays those counts, supports sorting by count, and refreshes the users query after HWID changes.

Changes

User HWID Counts

Layer / File(s) Summary
Count query and user-list contract
app/db/crud/user.py, app/db/models.py, app/models/user.py, app/operation/user.py
The user-list query can load correlated HWID counts and sort by count. The operation enables count loading, and the response model includes the count.
Dashboard count display and sorting
dashboard/src/service/api/index.ts, dashboard/src/features/users/components/columns.tsx, dashboard/src/features/users/components/filters.tsx, dashboard/src/utils/usersCache.ts, dashboard/src/features/users/dialogs/user-hwids-modal.tsx, dashboard/public/statics/locales/*
The dashboard displays HWID counts, offers ascending and descending sort options, and invalidates the users query after HWID changes. The user cache handles count sorting and merges updated user fields. Locale files add HWID count labels.

Priority: ➖ Normal

Estimated code review effort: 3 (Moderate) | ~20 minutes

Change: Feature

Sequence Diagram(s)

sequenceDiagram
  participant DashboardUsersTable
  participant UserOperation
  participant get_users
  participant UserHWID
  DashboardUsersTable->>UserOperation: Request user list
  UserOperation->>get_users: Load users with HWID counts
  get_users->>UserHWID: Count rows for each user
  UserHWID-->>get_users: Return correlated counts
  get_users-->>UserOperation: Return users with counts
  UserOperation-->>DashboardUsersTable: Provide user-list response
Loading

Suggested reviewers: immohammad20000

Merge Risk | ⚪ Minimal · up to f0dfa

Merge Risk: ⚪ Minimal · up to f0dfa

The identified count-loading and refresh concerns do not block the Users list. The change is mergeable after normal checks.

Security Architecture Review

Security architecture risk: 🟡 Moderate · up to f0dfa

Administrators allowed to list users can now see each listed user’s registered-device count even when they lack permission to view hardware IDs. The response does not reveal the IDs themselves, and existing user-list scoping limits which accounts are visible.

Retained concerns

  • Medium · security · inferred: The users.read list contract newly reveals per-user HWID registration counts without requiring the separate hwids.read permission.
Security review details

Security Blast Radius

  • inferred — A role with users.read but not hwids.read can obtain counts for users within its list scope, potentially across all users that role may list. The new response exposes counts, not HWID values or device records.

Security Findings and Attack Paths

  • inferred — An authenticated administrator granted users.read alone can request the user list to learn device-registration counts that the HWID-record endpoint would withhold under its separate read permission. Whether production roles use that combination is unknown.

Trust Boundaries and Controls

  • observed — The existing users.read route check and administrator-scope filter remain on the inspected list path. Sort values are validated against explicit options rather than used as arbitrary SQL identifiers.

Resilience and Maintainability Implications

  • inferred — HWID delete and reset use scoped server operations that commit their deletions before returning. The client’s success-only invalidation supports normal reconciliation, but recovery from a response failure after commit or a failed refetch was not established; no new authorization bypass follows from that uncertainty.

Hardening Proposals

  • proposed — Decide explicitly whether HWID registration counts belong under users.read or hwids.read; if the latter, enforce that permission on the count projection rather than relying on dashboard visibility.

🚥 Pre-merge checks | ✅ 4 | ❓ 1

❌ Failed checks (1 inconclusive)

Check name Status Explanation Resolution
Docstring Coverage ❓ Inconclusive Docstring coverage is 42.86% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 7 functions across 8 files. (5 skipped: 4… Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely summarizes the main change: displaying and sorting registered HWID counts in the Users feature.
Linked Issues check ✅ Passed The PR meets the coding requirements in [#516]. The backend loads UserHWID counts for the user-list endpoint and exposes hwid_count. The Users UI displays the count on desktop and mobile. The UI a…
Out of Scope Changes check ✅ Passed The changes stay within [#516]. Backend query support, API types, table rendering, sort controls, cache handling, and localization all support displaying or sorting registered HWID counts in the Users…

Full details: Docstring Coverage

Explanation

Docstring coverage is 42.86% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 7 functions across 8 files. (5 skipped: 4 unsupported, 1 too large.)


✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create a new PR


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

A rabbit counts each device in the queue,
Fingerprints mark the totals, neat and true.
Sort them low or sort them high,
Fresh counts appear when changes fly.
The users list hops into view.

Comment @coderabbitai help to get the list of available commands.

@dr-hoseyn

Copy link
Copy Markdown
Contributor Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Sep 27, 2026 •

Copy link
Copy Markdown
Contributor
✅ Action performed

Review finished.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@dr-hoseyn
dr-hoseyn force-pushed the codex/hwid-monitoring branch from f0dfa58 to cfbf25d Compare October 8, 2026 22:29
dr-hoseyn and others added 4 commits October 9, 2026 02:06
The HWID list needs hwids.read, but the users list needs only users.read, so the new count column exposed device counts to roles that cannot see devices. Without hwids.read the count is not loaded and a hwid_count sort is ignored, the same way node filters are ignored without nodes.stats. hwid_count is now null when it was not loaded instead of a misleading 0, so a caller that skips the count cannot fail validation.
Counts per user, ascending and descending sort with an unchanged total, and an admin without hwids.read who gets no counts and no device sort while an admin with it gets both. The shared set_user_hwids helper clears a user's devices first, since SQLite keeps a deleted user's devices and can reuse its id.
….read

Hide the device count column, its mobile line and the sort option for admins without hwids.read, matching the backend. Regenerate the API client with the pinned orval: hwid_count is now nullable, and UserListItem uses the same inline field types as the rest of the client.
@T3ST3ST3R0N
T3ST3ST3R0N merged commit cc7c710 into PasarGuard:dev Oct 10, 2026
10 checks passed
T3ST3ST3R0N added a commit that referenced this pull request Oct 10, 2026
SQLite keeps a deleted user's devices (foreign keys off) and can give its id to the next test's user, so the two device count tests from #951 left devices that a later test inherited: running them before tests/api/test_hwid.py made test_hwid_respects_admin_role_policy see 3 devices instead of 1. Clear the devices before deleting the users.
@T3ST3ST3R0N T3ST3ST3R0N mentioned this pull request Oct 10, 2026
Free-Guy-IR added a commit to Free-Guy-IR/panel that referenced this pull request Nov 2, 2026
…ain bba95f274

Checked with git ls-remote on 2026-10-11 02:55 UTC. Panel dev moved from
d94e157 to 83fe90e with 22 commits; main is still v5.4.1.

- PasarGuard#879 (2d0ca81, loopback Xray client inbounds) is ported by design: the
  fork took the open pull request on 2026-09-25 as 2e7fc45 with migration
  bfd88026565b, the same listener rewrite.
- PasarGuard#934 (a5b28f3) moves from the port queue to a dated waiver until
  2026-11-09: the fork refuses to delete a core that nodes still use
  (W3-013, fix/node-health) instead of restarting those nodes.
- PasarGuard#993 (Hysteria2 FinalMask without a udp mask) is queued until 2026-10-18:
  the fork has the same FinalMask | dict field and the same non-null-safe
  udpHop reads, so a udpHop-only host can fail the whole subscription.
- PasarGuard#984, PasarGuard#985, PasarGuard#986, PasarGuard#989, PasarGuard#944, PasarGuard#864, PasarGuard#948, PasarGuard#951, PasarGuard#778, PasarGuard#774, PasarGuard#787, PasarGuard#994,
  PasarGuard#865, PasarGuard#875, PasarGuard#718, PasarGuard#967, PasarGuard#772, PasarGuard#662, PasarGuard#995 and PasarGuard#913 are queued as
  security: no until 2026-10-25, each with its reason; PasarGuard#913's node-removal
  half is already in the fork through sync_remove_users.
- The branch commits that upstream squash-merged (the three dashboard
  branches, telegram/tests, fix/core-editor-api-section-followup,
  fix/hwid-count-test-cleanup, fix/hysteria-finalmask-quic-only) follow
  their squash, by patch-id or by an equal tree.

Scripts main moved to bba95f274 (PasarGuard#35, PasarGuard#36, PasarGuard#37 after PasarGuard#34). The three
squashes are queued security: no until 2026-10-25, and their pre-squash
branch commits are waived as superseded (equal trees).

Against live upstream the panel gate now fails only on the 17 port-queue
commits classified security: yes, and the forks gate only on node
f72ac0ef (security: yes). No upstream commit is undecided.
Free-Guy-IR added a commit to Free-Guy-IR/panel that referenced this pull request Nov 4, 2026
* feat(users): display and sort registered HWID counts

* fix(users): show device counts only to admins with hwids.read

The HWID list needs hwids.read, but the users list needs only users.read, so the new count column exposed device counts to roles that cannot see devices. Without hwids.read the count is not loaded and a hwid_count sort is ignored, the same way node filters are ignored without nodes.stats. hwid_count is now null when it was not loaded instead of a misleading 0, so a caller that skips the count cannot fail validation.

* test(users): cover device counts in the users list

Counts per user, ascending and descending sort with an unchanged total, and an admin without hwids.read who gets no counts and no device sort while an admin with it gets both. The shared set_user_hwids helper clears a user's devices first, since SQLite keeps a deleted user's devices and can reuse its id.

* fix(dashboard): show the device count column and sort only with hwids.read

Hide the device count column, its mobile line and the sort option for admins without hwids.read, matching the backend. Regenerate the API client with the pinned orval: hwid_count is now nullable, and UserListItem uses the same inline field types as the rest of the client.

---------

Co-authored-by: T3ST3ST3R0N <[email protected]>
(cherry picked from commit cc7c710)

Fork port: backend and tests only. The dashboard half is owed by lane B-DASH. Conflicts resolved against fork main 2da54c4: get_users keeps the fork populate_existing option, list_users keeps the fork group restriction before the hwids.read gate, and get_admin_users_for_node_sync from upstream context is not added. tests/api/test_permission_leaks.py is not in the fork; its device count test is ported into tests/api/test_user_list_hwid_count_scope.py.

(cherry picked from commit cdab821ab9382d2a461c656924d0bab2157855bc)
Free-Guy-IR added a commit to Free-Guy-IR/panel that referenced this pull request Nov 4, 2026
SQLite keeps a deleted user's devices (foreign keys off) and can give its id to the next test's user, so the two device count tests from PasarGuard#951 left devices that a later test inherited: running them before tests/api/test_hwid.py made test_hwid_respects_admin_role_policy see 3 devices instead of 1. Clear the devices before deleting the users.
(cherry picked from commit 3ef603b)

Fork port: tests/api/test_permission_leaks.py is not in the fork; the same clean-up is in tests/api/test_user_list_hwid_count_scope.py.

(cherry picked from commit 8aa3b56483275da41b5d4e749b8be06d4f829c94)
Free-Guy-IR added a commit to Free-Guy-IR/panel that referenced this pull request Nov 4, 2026
… users

Ports the device count test of upstream tests/api/test_permission_leaks.py (PasarGuard#951, with the PasarGuard#994 clean-up), which the fork does not have, and adds an own-scope admin with hwids.read that sorts by hwid_count both ways over a search that also matches three other admins' users with more devices: the total stays 1 and only its own user and count are returned.

(cherry picked from commit c2f34e699400c06bdb179769acaf2bfd596e16c6)
Free-Guy-IR added a commit to Free-Guy-IR/panel that referenced this pull request Nov 4, 2026
The fork scopes hwids.read: GET /api/user/{id}/hwids answers 404 for another admin's user when hwids.read is OWN. Upstream PasarGuard#951 gates the users list count and sort on hwids.read without its scope, so a role with users.read ALL and hwids.read OWN got every user's device count and could sort all users by it. The count and the hwid_count sort now load only when hwids.read is ALL or the listing is already limited to the admin's own users.

(cherry picked from commit 548ecaa9ccbedbbaa5cf5fe1f87b79568b1ab001)
Free-Guy-IR added a commit to Free-Guy-IR/panel that referenced this pull request Nov 4, 2026
…ef603b as carried, with the leak tests replaced

Both rows stay in the port queue until the dashboard half of PasarGuard#951 lands on
port/b-dash-951 (then 2f76096). Upstream's test_permission_leaks.py
hunks are replaced by tests/api/test_user_list_hwid_count_scope.py, which
checks the same hwids.read gate plus the own-scope cases and the
grant/revoke race, rather than dropped.

(cherry picked from commit 1c50b4d96d1d0a555ef36c5f986679a727a90bbe)
Free-Guy-IR pushed a commit to Free-Guy-IR/panel that referenced this pull request Nov 10, 2026
* feat(cleanup): delete inactive users by last activity

* fix(cleanup): remove deleted active and on_hold users from the nodes

The inactive target matches users of any status, but the cleanup delete
only removed database rows. Active users deleted this way kept working on
the nodes until the next full resync, and their released WireGuard IPs
could be handed out while the old peers were still configured. The
existing on_hold target had the same gap.

The cleanup delete now returns the deleted users that nodes still serve
(active and on_hold), and the operation dispatches their removal like the
bulk delete does. Dry runs dispatch nothing.

Tests: inactive selection by online_at with the created_at fallback, the
required cutoff, and node removal for active and on_hold users only.

---------

Co-authored-by: T3ST3ST3R0N <[email protected]>
(cherry picked from commit 83fe90e)

Fork resolution, backend half only:
- app/db/crud/user.py: the inactive branch of
  _cleanup_target_user_conditions is upstream's, with the fork's
  to_utc_for_filter on both bounds like every other branch, and the
  fork's allowed_group_ids filter is kept.
- app/operation/user.py and app/db/crud/user.py keep the fork's chunked
  delete: remove_expired_users still returns only the usernames and
  still calls on_chunk_deleted=sync_remove_users for every deleted
  chunk, instead of upstream's (usernames, node_users) return and the
  single sync_remove_users afterwards. Upstream's node-sync filter
  (only active and on_hold deleted users reach the nodes) is therefore
  not ported: the fork removes every deleted chunk, which is a no-op for
  a user already off the nodes. tests/api/test_user_cleanup.py asserts
  the fork's set instead of upstream's three memberships.
- The dashboard half (the four locale files,
  dashboard/src/pages/_dashboard.settings.cleanup.tsx and the generated
  dashboard/src/service/api/index.ts) is left to the dashboard lane, as
  the PasarGuard#951 row already does.
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.

2 participants