discourse/app/assets/javascripts/admin/controllers/admin-user-index.js.es6

343 lines
9.3 KiB
Plaintext
Raw Normal View History

2018-06-15 23:03:24 +08:00
import { ajax } from "discourse/lib/ajax";
import CanCheckEmails from "discourse/mixins/can-check-emails";
import { propertyNotEqual, setting } from "discourse/lib/computed";
import { userPath } from "discourse/lib/url";
import { popupAjaxError } from "discourse/lib/ajax-error";
import { default as computed } from "ember-addons/ember-computed-decorators";
import { fmt } from "discourse/lib/computed";
2014-08-13 07:04:36 +08:00
export default Ember.Controller.extend(CanCheckEmails, {
2017-09-13 05:07:42 +08:00
adminTools: Ember.inject.service(),
originalPrimaryGroupId: null,
customGroupIdsBuffer: null,
availableGroups: null,
userTitleValue: null,
2018-06-15 23:03:24 +08:00
showApproval: setting("must_approve_users"),
showBadges: setting("enable_badges"),
2018-06-20 09:07:41 +08:00
hasLockedTrustLevel: Ember.computed.notEmpty(
"model.manual_locked_trust_level"
),
2014-03-05 20:52:20 +08:00
2018-06-15 23:03:24 +08:00
primaryGroupDirty: propertyNotEqual(
"originalPrimaryGroupId",
"model.primary_group_id"
),
canDisableSecondFactor: Ember.computed.and(
2018-06-15 23:03:24 +08:00
"model.second_factor_enabled",
"model.can_disable_second_factor"
),
@computed("model.customGroups")
customGroupIds(customGroups) {
return customGroups.mapBy("id");
},
@computed("customGroupIdsBuffer", "customGroupIds")
customGroupsDirty(buffer, original) {
if (buffer === null) return false;
return buffer.length === original.length
? buffer.any(id => !original.includes(id))
: true;
},
@computed("model.automaticGroups")
automaticGroups(automaticGroups) {
2018-06-15 23:03:24 +08:00
return automaticGroups
.map(group => {
const name = Ember.String.htmlSafe(group.name);
return `<a href="/groups/${name}">${name}</a>`;
})
.join(", ");
},
@computed("model.associated_accounts")
associatedAccountsLoaded(associatedAccounts) {
return typeof associatedAccounts !== "undefined";
},
@computed("model.associated_accounts")
associatedAccounts(associatedAccounts) {
return associatedAccounts
.map(provider => `${provider.name} (${provider.description})`)
.join(", ");
},
@computed("model.user_fields.[]")
userFields(userFields) {
const siteUserFields = this.site.get("user_fields");
if (!Ember.isEmpty(siteUserFields)) {
return siteUserFields.map(uf => {
const value = userFields ? userFields[uf.get("id").toString()] : null;
return { name: uf.get("name"), value };
});
}
return [];
},
preferencesPath: fmt("model.username_lower", userPath("%@/preferences")),
2018-06-15 23:03:24 +08:00
@computed("model.can_delete_all_posts", "model.staff", "model.post_count")
deleteAllPostsExplanation(canDeleteAllPosts, staff, postCount) {
if (canDeleteAllPosts) {
return null;
}
if (staff) {
2018-06-15 23:03:24 +08:00
return I18n.t("admin.user.delete_posts_forbidden_because_staff");
}
if (postCount > this.siteSettings.delete_all_posts_max) {
2018-06-15 23:03:24 +08:00
return I18n.t("admin.user.cant_delete_all_too_many_posts", {
count: this.siteSettings.delete_all_posts_max
});
} else {
2018-06-15 23:03:24 +08:00
return I18n.t("admin.user.cant_delete_all_posts", {
count: this.siteSettings.delete_user_max_post_age
});
}
},
2018-06-15 23:03:24 +08:00
@computed("model.canBeDeleted", "model.staff")
deleteExplanation(canBeDeleted, staff) {
if (canBeDeleted) {
return null;
}
if (staff) {
2018-06-15 23:03:24 +08:00
return I18n.t("admin.user.delete_forbidden_because_staff");
} else {
2018-06-15 23:03:24 +08:00
return I18n.t("admin.user.delete_forbidden", {
count: this.siteSettings.delete_user_max_post_age
});
}
},
groupAdded(added) {
this.get("model")
.groupAdded(added)
.catch(() => bootbox.alert(I18n.t("generic_error")));
},
groupRemoved(groupId) {
this.get("model")
.groupRemoved(groupId)
.then(() => {
if (groupId === this.get("originalPrimaryGroupId")) {
this.set("originalPrimaryGroupId", null);
}
})
.catch(() => bootbox.alert(I18n.t("generic_error")));
},
2013-09-17 02:08:55 +08:00
actions: {
2018-06-15 23:03:24 +08:00
impersonate() {
return this.get("model").impersonate();
},
logOut() {
return this.get("model").logOut();
},
resetBounceScore() {
return this.get("model").resetBounceScore();
},
refreshBrowsers() {
return this.get("model").refreshBrowsers();
},
approve() {
return this.get("model").approve();
},
deactivate() {
return this.get("model").deactivate();
},
sendActivationEmail() {
return this.get("model").sendActivationEmail();
},
activate() {
return this.get("model").activate();
},
revokeAdmin() {
return this.get("model").revokeAdmin();
},
grantAdmin() {
return this.get("model").grantAdmin();
},
revokeModeration() {
return this.get("model").revokeModeration();
},
grantModeration() {
return this.get("model").grantModeration();
},
saveTrustLevel() {
return this.get("model").saveTrustLevel();
},
restoreTrustLevel() {
return this.get("model").restoreTrustLevel();
},
lockTrustLevel(locked) {
return this.get("model").lockTrustLevel(locked);
},
unsilence() {
return this.get("model").unsilence();
},
silence() {
return this.get("model").silence();
},
deleteAllPosts() {
return this.get("model").deleteAllPosts();
},
anonymize() {
return this.get("model").anonymize();
},
disableSecondFactor() {
return this.get("model").disableSecondFactor();
},
clearPenaltyHistory() {
const user = this.get("model");
const path = `/admin/users/${user.get("id")}/penalty_history`;
return ajax(path, { type: "DELETE" })
.then(() => user.set("tl3_requirements.penalty_counts.total", 0))
2018-06-15 23:03:24 +08:00
.catch(popupAjaxError);
},
destroy() {
2018-06-15 23:03:24 +08:00
const postCount = this.get("model.post_count");
if (postCount <= 5) {
2018-06-15 23:03:24 +08:00
return this.get("model").destroy({ deletePosts: true });
} else {
2018-06-15 23:03:24 +08:00
return this.get("model").destroy();
}
},
viewActionLogs() {
2018-06-15 23:03:24 +08:00
this.get("adminTools").showActionLogs(this, {
target_user: this.get("model.username")
});
},
showFlagsReceived() {
2018-06-15 23:03:24 +08:00
this.get("adminTools").showFlagsReceived(this.get("model"));
},
2017-09-13 05:07:42 +08:00
showSuspendModal() {
2018-06-15 23:03:24 +08:00
this.get("adminTools").showSuspendModal(this.get("model"));
2017-09-13 05:07:42 +08:00
},
unsuspend() {
2018-06-15 23:03:24 +08:00
this.get("model")
.unsuspend()
.catch(popupAjaxError);
},
showSilenceModal() {
2018-06-15 23:03:24 +08:00
this.get("adminTools").showSilenceModal(this.get("model"));
},
2017-09-13 05:07:42 +08:00
saveUsername(newUsername) {
2018-06-15 23:03:24 +08:00
const oldUsername = this.get("model.username");
this.set("model.username", newUsername);
const path = `/users/${oldUsername.toLowerCase()}/preferences/username`;
return ajax(path, { data: { new_username: newUsername }, type: "PUT" })
2018-06-15 23:03:24 +08:00
.catch(e => {
this.set("model.username", oldUsername);
popupAjaxError(e);
})
.finally(() => this.toggleProperty("editingUsername"));
},
saveName(newName) {
2018-06-15 23:03:24 +08:00
const oldName = this.get("model.name");
this.set("model.name", newName);
2018-06-15 23:03:24 +08:00
const path = userPath(`${this.get("model.username").toLowerCase()}.json`);
return ajax(path, { data: { name: newName }, type: "PUT" })
2018-06-15 23:03:24 +08:00
.catch(e => {
this.set("model.name", oldName);
popupAjaxError(e);
})
.finally(() => this.toggleProperty("editingName"));
},
saveTitle(newTitle) {
const oldTitle = this.get("model.title");
this.set("model.title", newTitle);
const path = userPath(`${this.get("model.username").toLowerCase()}.json`);
return ajax(path, { data: { title: newTitle }, type: "PUT" })
2018-06-15 23:03:24 +08:00
.catch(e => {
this.set("model.title", oldTitle);
2018-06-15 23:03:24 +08:00
popupAjaxError(e);
})
.finally(() => this.toggleProperty("editingTitle"));
2013-10-23 03:53:08 +08:00
},
generateApiKey() {
2018-06-15 23:03:24 +08:00
this.get("model").generateApiKey();
2013-10-23 03:53:08 +08:00
},
saveCustomGroups() {
const currentIds = this.get("customGroupIds");
const bufferedIds = this.get("customGroupIdsBuffer");
const availableGroups = this.get("availableGroups");
bufferedIds
.filter(id => !currentIds.includes(id))
.forEach(id => this.groupAdded(availableGroups.findBy("id", id)));
currentIds
.filter(id => !bufferedIds.includes(id))
.forEach(id => this.groupRemoved(id));
},
2014-07-15 22:11:39 +08:00
resetCustomGroups() {
this.set("customGroupIdsBuffer", null);
},
savePrimaryGroup() {
const primaryGroupId = this.get("model.primary_group_id");
const path = `/admin/users/${this.get("model.id")}/primary_group`;
return ajax(path, {
2018-06-15 23:03:24 +08:00
type: "PUT",
data: { primary_group_id: primaryGroupId }
2018-06-15 23:03:24 +08:00
})
.then(() => this.set("originalPrimaryGroupId", primaryGroupId))
.catch(() => bootbox.alert(I18n.t("generic_error")));
},
resetPrimaryGroup() {
2018-06-15 23:03:24 +08:00
this.set("model.primary_group_id", this.get("originalPrimaryGroupId"));
},
regenerateApiKey() {
bootbox.confirm(
I18n.t("admin.api.confirm_regen"),
I18n.t("no_value"),
I18n.t("yes_value"),
result => {
2018-06-15 23:03:24 +08:00
if (result) {
this.get("model").generateApiKey();
2018-06-15 23:03:24 +08:00
}
2013-10-23 03:53:08 +08:00
}
);
2013-10-23 03:53:08 +08:00
},
revokeApiKey() {
bootbox.confirm(
I18n.t("admin.api.confirm_revoke"),
I18n.t("no_value"),
I18n.t("yes_value"),
result => {
2018-06-15 23:03:24 +08:00
if (result) {
this.get("model").revokeApiKey();
2018-06-15 23:03:24 +08:00
}
2013-10-23 03:53:08 +08:00
}
);
2013-09-17 02:08:55 +08:00
}
}
});