8000 FIX: Correct assignment of admin user groups property (#35315) · discourse/discourse@2ebfd82 · GitHub
[go: up one dir, main page]

Skip to content

Commit 2ebfd82

Browse files
authored
FIX: Correct assignment of admin user groups property (#35315)
This commit ensures that the `groups` property on admin users is reliably updated when loading user details. - Fixes an issue with tracked properties not being set due to non-enumerability - Guarantees that admin user details reflect the accurate group memberships
1 parent 68c0ca0 commit 2ebfd82

File tree

4 files changed

+40
-13
lines changed

4 files changed

+40
-13
lines changed

app/assets/javascripts/admin/addon/models/admin-user.js

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { filter, gt, lt, not, or } from "@ember/object/computed";
2-
import { Promise } from "rsvp";
32
import { ajax } from "discourse/lib/ajax";
43
import { popupAjaxError } from "discourse/lib/ajax-error";
54
import { propertyNotEqual } from "discourse/lib/computed";
@@ -12,11 +11,18 @@ import User from "discourse/models/user";
1211
import { i18n } from "discourse-i18n";
1312

1413
export default class AdminUser extends User {
15-
static find(user_id) {
16-
return ajax(`/admin/users/${user_id}.json`).then((result) => {
17-
result.loadedDetails = true;
18-
return AdminUser.create(result);
19-
});
14+
/**
15+
* Retrieves user details for the specified user ID and formats the result based on the provided options.
16+
*
17+
* @param {string|number} user_id - The ID of the user to be retrieved.
18+
* @param {Object} [opts] - Options to customize the result format.
19+
* @param {boolean} [opts.raw=false] - If true, returns the raw response instead of formatting it using AdminUser.create.
20+
* @return {Promise<Object|AdminUser>} A promise that resolves with either the raw response object or an instance of AdminUser.
21+
*/
22+
static async find(user_id, opts = { raw: false }) {
23+
const result = await ajax(`/admin/users/${user_id}.json`);
24+
result.loadedDetails = true;
25+
return opts?.raw ? result : AdminUser.create(result);
2026
}
2127

2228
static findAll(query, userFilter) {
@@ -338,15 +344,17 @@ export default class AdminUser extends User {
338344
});
339345
}
340346

341-
loadDetails() {
347+
async loadDetails() {
342348
if (this.loadedDetails) {
343-
return Promise.resolve(this);
349+
return this;
344350
}
345351

346-
return AdminUser.find(this.id).then((result) => {
347-
const userProperties = Object.assign(result, { loadedDetails: true });
348-
this.setProperties(userProperties);
349-
});
352+
// we need to ask find to provide a raw object instead of AdminUser model because we're using
353+
// setProperties to update the values. Otherwise we would miss tracked properties which are not enumerable.
354+
const userProperties = await AdminUser.find(this.id, { raw: true });
355+
this.setProperties(userProperties);
356+
357+
return this;
350358
}
351359

352360
@discourseComputed("tl3_requirements")

app/assets/javascripts/admin/addon/templates/admin-user/index.gjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -680,7 +680,7 @@ export default RouteTemplate(
680680
{{#if @controller.currentUser.admin}}
681681
<section class="details">
682682
<h1>{{i18n "admin.groups.title"}}</h1>
683-
<div class="display-row">
683+
<div class="display-row admin-user__automatic-groups">
684684
<div class="field">{{i18n "admin.groups.automatic"}}</div>
685685
<div class="value">{{htmlSafe @controller.automaticGroups}}</div>
686686
</div>

spec/system/admin_user_spec.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
describe "Admin User Page", type: :system do
44
fab!(:current_user, :admin)
55

6+
let(:admin_users_page) { PageObjects::Pages::AdminUsers.new }
67
let(:admin_user_page) { PageObjects::Pages::AdminUser.new }
78
let(:suspend_user_modal) { PageObjects::Modals::PenalizeUser.new("suspend") }
89
let(:silence_user_modal) { PageObjects::Modals::PenalizeUser.new("silence") }
@@ -153,4 +154,18 @@
153154
end
154155
end
155156
end
157+
158+
context "when navigating to a user's page from the list" do
159+
fab!(:user) { Fabricate(:user, refresh_auto_groups: true) }
160+
161+
it "displays the groups correctly" do
162+
admin_users_page.visit
163+
164+
# navigate to the user page
165+
admin_users_page.user_row(user.id).username.click
166+
167+
# ensure the automatic groups are displayed
168+
page.find(".admin-user__aut 528D omatic-groups").has_text?("trust_level")
169+
end
170+
end
156171
end

spec/system/page_objects/pages/admin_users.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ def has_bulk_select_checkbox?
2121
def has_no_bulk_select_checkbox?
2222
element.has_no_css?(".directory-table__cell-bulk-select")
2323
end
24+
25+
def username
26+
element.find("a[href^='/admin/users']")
27+
end
2428
end
2529

2630
def visit

0 commit comments

Comments
 (0)
0