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

301 lines
8.2 KiB
Plaintext
Raw Normal View History

import { notEmpty, and } from "@ember/object/computed";
2019-10-29 13:31:44 -04:00
import { inject as service } from "@ember/service";
import Controller from "@ember/controller";
2018-06-15 11:03:24 -04: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 discourseComputed } from "discourse-common/utils/decorators";
import { fmt } from "discourse/lib/computed";
import { htmlSafe } from "@ember/template";
2014-08-12 19:04:36 -04:00
export default Controller.extend(CanCheckEmails, {
adminTools: service(),
originalPrimaryGroupId: null,
customGroupIdsBuffer: null,
availableGroups: null,
userTitleValue: null,
2018-06-15 11:03:24 -04:00
showBadges: setting("enable_badges"),
hasLockedTrustLevel: notEmpty("model.manual_locked_trust_level"),
2014-03-05 07:52:20 -05:00
2018-06-15 11:03:24 -04:00
primaryGroupDirty: propertyNotEqual(
"originalPrimaryGroupId",
"model.primary_group_id"
),
canDisableSecondFactor: and(
2018-06-15 11:03:24 -04:00
"model.second_factor_enabled",
"model.can_disable_second_factor"
),
@discourseComputed("model.customGroups")
customGroupIds(customGroups) {
return customGroups.mapBy("id");
},
@discourseComputed("customGroupIdsBuffer", "customGroupIds")
customGroupsDirty(buffer, original) {
if (buffer === null) return false;
return buffer.length === original.length
? buffer.any(id => !original.includes(id))
: true;
},
@discourseComputed("model.automaticGroups")
automaticGroups(automaticGroups) {
2018-06-15 11:03:24 -04:00
return automaticGroups
.map(group => {
const name = htmlSafe(group.name);
return `<a href="/g/${name}">${name}</a>`;
2018-06-15 11:03:24 -04:00
})
.join(", ");
},
@discourseComputed("model.associated_accounts")
associatedAccountsLoaded(associatedAccounts) {
return typeof associatedAccounts !== "undefined";
},
@discourseComputed("model.associated_accounts")
associatedAccounts(associatedAccounts) {
return associatedAccounts
.map(provider => `${provider.name} (${provider.description})`)
.join(", ");
},
@discourseComputed("model.user_fields.[]")
userFields(userFields) {
return this.site.collectUserFields(userFields);
},
preferencesPath: fmt("model.username_lower", userPath("%@/preferences")),
@discourseComputed(
"model.can_delete_all_posts",
"model.staff",
"model.post_count"
)
deleteAllPostsExplanation(canDeleteAllPosts, staff, postCount) {
if (canDeleteAllPosts) {
return null;
}
if (staff) {
2018-06-15 11:03:24 -04:00
return I18n.t("admin.user.delete_posts_forbidden_because_staff");
}
if (postCount > this.siteSettings.delete_all_posts_max) {
2018-06-15 11:03:24 -04:00
return I18n.t("admin.user.cant_delete_all_too_many_posts", {
count: this.siteSettings.delete_all_posts_max
});
} else {
2018-06-15 11:03:24 -04:00
return I18n.t("admin.user.cant_delete_all_posts", {
count: this.siteSettings.delete_user_max_post_age
});
}
},
@discourseComputed("model.canBeDeleted", "model.staff")
deleteExplanation(canBeDeleted, staff) {
if (canBeDeleted) {
return null;
}
if (staff) {
2018-06-15 11:03:24 -04:00
return I18n.t("admin.user.delete_forbidden_because_staff");
} else {
2018-06-15 11:03:24 -04:00
return I18n.t("admin.user.delete_forbidden", {
count: this.siteSettings.delete_user_max_post_age
});
}
},
groupAdded(added) {
this.model
.groupAdded(added)
.catch(() => bootbox.alert(I18n.t("generic_error")));
},
groupRemoved(groupId) {
this.model
.groupRemoved(groupId)
.then(() => {
if (groupId === this.originalPrimaryGroupId) {
this.set("originalPrimaryGroupId", null);
}
})
.catch(() => bootbox.alert(I18n.t("generic_error")));
},
2013-09-16 14:08:55 -04:00
actions: {
2018-06-15 11:03:24 -04:00
impersonate() {
return this.model.impersonate();
2018-06-15 11:03:24 -04:00
},
logOut() {
return this.model.logOut();
2018-06-15 11:03:24 -04:00
},
resetBounceScore() {
return this.model.resetBounceScore();
2018-06-15 11:03:24 -04:00
},
approve() {
return this.model.approve(this.currentUser);
2018-06-15 11:03:24 -04:00
},
deactivate() {
return this.model.deactivate();
2018-06-15 11:03:24 -04:00
},
sendActivationEmail() {
return this.model.sendActivationEmail();
2018-06-15 11:03:24 -04:00
},
activate() {
return this.model.activate();
2018-06-15 11:03:24 -04:00
},
revokeAdmin() {
return this.model.revokeAdmin();
2018-06-15 11:03:24 -04:00
},
grantAdmin() {
return this.model.grantAdmin();
2018-06-15 11:03:24 -04:00
},
revokeModeration() {
return this.model.revokeModeration();
2018-06-15 11:03:24 -04:00
},
grantModeration() {
return this.model.grantModeration();
2018-06-15 11:03:24 -04:00
},
saveTrustLevel() {
return this.model.saveTrustLevel();
2018-06-15 11:03:24 -04:00
},
restoreTrustLevel() {
return this.model.restoreTrustLevel();
2018-06-15 11:03:24 -04:00
},
lockTrustLevel(locked) {
return this.model.lockTrustLevel(locked);
2018-06-15 11:03:24 -04:00
},
unsilence() {
return this.model.unsilence();
2018-06-15 11:03:24 -04:00
},
silence() {
return this.model.silence();
2018-06-15 11:03:24 -04:00
},
deleteAllPosts() {
return this.model.deleteAllPosts();
2018-06-15 11:03:24 -04:00
},
anonymize() {
return this.model.anonymize();
2018-06-15 11:03:24 -04:00
},
disableSecondFactor() {
return this.model.disableSecondFactor();
2018-06-15 11:03:24 -04:00
},
clearPenaltyHistory() {
const user = this.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 11:03:24 -04:00
.catch(popupAjaxError);
},
destroy() {
2018-06-15 11:03:24 -04:00
const postCount = this.get("model.post_count");
if (postCount <= 5) {
return this.model.destroy({ deletePosts: true });
} else {
return this.model.destroy();
}
},
viewActionLogs() {
this.adminTools.showActionLogs(this, {
2018-06-15 11:03:24 -04:00
target_user: this.get("model.username")
});
},
2017-09-12 17:07:42 -04:00
showSuspendModal() {
this.adminTools.showSuspendModal(this.model);
2017-09-12 17:07:42 -04:00
},
unsuspend() {
this.model.unsuspend().catch(popupAjaxError);
},
showSilenceModal() {
this.adminTools.showSilenceModal(this.model);
},
2017-09-12 17:07:42 -04:00
saveUsername(newUsername) {
2018-06-15 11:03:24 -04: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 11:03:24 -04:00
.catch(e => {
this.set("model.username", oldUsername);
popupAjaxError(e);
})
.finally(() => this.toggleProperty("editingUsername"));
},
saveName(newName) {
2018-06-15 11:03:24 -04:00
const oldName = this.get("model.name");
this.set("model.name", newName);
2018-06-15 11:03:24 -04:00
const path = userPath(`${this.get("model.username").toLowerCase()}.json`);
return ajax(path, { data: { name: newName }, type: "PUT" })
2018-06-15 11:03:24 -04: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 11:03:24 -04:00
.catch(e => {
this.set("model.title", oldTitle);
2018-06-15 11:03:24 -04:00
popupAjaxError(e);
})
.finally(() => this.toggleProperty("editingTitle"));
2013-10-22 15:53:08 -04:00
},
saveCustomGroups() {
const currentIds = this.customGroupIds;
const bufferedIds = this.customGroupIdsBuffer;
const availableGroups = this.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 10:11:39 -04: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 11:03:24 -04:00
type: "PUT",
data: { primary_group_id: primaryGroupId }
2018-06-15 11:03:24 -04:00
})
.then(() => this.set("originalPrimaryGroupId", primaryGroupId))
.catch(() => bootbox.alert(I18n.t("generic_error")));
},
resetPrimaryGroup() {
this.set("model.primary_group_id", this.originalPrimaryGroupId);
2013-09-16 14:08:55 -04:00
}
}
});