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

350 lines
9.1 KiB
Plaintext
Raw Normal View History

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 computed from "ember-addons/ember-computed-decorators";
2014-08-12 19:04:36 -04:00
export default Ember.Controller.extend(CanCheckEmails, {
2017-09-12 17:07:42 -04:00
adminTools: Ember.inject.service(),
editingUsername: false,
editingName: false,
editingTitle: false,
originalPrimaryGroupId: null,
availableGroups: null,
userTitleValue: null,
2018-06-15 11:03:24 -04:00
showApproval: setting("must_approve_users"),
showBadges: setting("enable_badges"),
2018-06-19 21:07:41 -04:00
hasLockedTrustLevel: Ember.computed.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: Ember.computed.and(
2018-06-15 11:03:24 -04:00
"model.second_factor_enabled",
"model.can_disable_second_factor"
),
@computed("model.automaticGroups")
automaticGroups(automaticGroups) {
2018-06-15 11:03:24 -04: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(", ");
},
userFields: function() {
2018-06-15 11:03:24 -04:00
const siteUserFields = this.site.get("user_fields"),
userFields = this.get("model.user_fields");
if (!Ember.isEmpty(siteUserFields)) {
return siteUserFields.map(function(uf) {
2018-06-15 11:03:24 -04:00
let value = userFields ? userFields[uf.get("id").toString()] : null;
return { name: uf.get("name"), value: value };
});
}
return [];
2018-06-15 11:03:24 -04:00
}.property("model.user_fields.[]"),
2018-06-15 11:03:24 -04:00
@computed("model.username_lower")
preferencesPath(username) {
return userPath(`${username}/preferences`);
},
2018-06-15 11:03:24 -04: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 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
});
}
},
2018-06-15 11:03:24 -04:00
@computed("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
});
}
},
2013-09-16 14:08:55 -04:00
actions: {
2018-06-15 11:03:24 -04: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() {
2018-06-15 11:03:24 -04:00
let user = this.get("model");
return ajax(`/admin/users/${user.get("id")}/penalty_history`, {
type: "DELETE"
})
.then(() => {
user.set("tl3_requirements.penalty_counts.total", 0);
})
.catch(popupAjaxError);
},
destroy() {
2018-06-15 11:03:24 -04:00
const postCount = this.get("model.post_count");
if (postCount <= 5) {
2018-06-15 11:03:24 -04:00
return this.get("model").destroy({ deletePosts: true });
} else {
2018-06-15 11:03:24 -04:00
return this.get("model").destroy();
}
},
viewActionLogs() {
2018-06-15 11:03:24 -04:00
this.get("adminTools").showActionLogs(this, {
target_user: this.get("model.username")
});
},
showFlagsReceived() {
2018-06-15 11:03:24 -04:00
this.get("adminTools").showFlagsReceived(this.get("model"));
},
2017-09-12 17:07:42 -04:00
showSuspendModal() {
2018-06-15 11:03:24 -04:00
this.get("adminTools").showSuspendModal(this.get("model"));
2017-09-12 17:07:42 -04:00
},
unsuspend() {
2018-06-15 11:03:24 -04:00
this.get("model")
.unsuspend()
.catch(popupAjaxError);
},
showSilenceModal() {
2018-06-15 11:03:24 -04:00
this.get("adminTools").showSilenceModal(this.get("model"));
},
2017-09-12 17:07:42 -04:00
toggleUsernameEdit() {
2018-06-15 11:03:24 -04:00
this.set("userUsernameValue", this.get("model.username"));
this.toggleProperty("editingUsername");
},
saveUsername() {
2018-06-15 11:03:24 -04:00
const oldUsername = this.get("model.username");
this.set("model.username", this.get("userUsernameValue"));
return ajax(`/users/${oldUsername.toLowerCase()}/preferences/username`, {
2018-06-15 11:03:24 -04:00
data: { new_username: this.get("userUsernameValue") },
type: "PUT"
})
.catch(e => {
this.set("model.username", oldUsername);
popupAjaxError(e);
})
.finally(() => this.toggleProperty("editingUsername"));
},
toggleNameEdit() {
2018-06-15 11:03:24 -04:00
this.set("userNameValue", this.get("model.name"));
this.toggleProperty("editingName");
},
saveName() {
2018-06-15 11:03:24 -04:00
const oldName = this.get("model.name");
this.set("model.name", this.get("userNameValue"));
return ajax(
userPath(`${this.get("model.username").toLowerCase()}.json`),
{
data: { name: this.get("userNameValue") },
type: "PUT"
}
)
.catch(e => {
this.set("model.name", oldName);
popupAjaxError(e);
})
.finally(() => this.toggleProperty("editingName"));
},
toggleTitleEdit() {
2018-06-15 11:03:24 -04:00
this.set("userTitleValue", this.get("model.title"));
this.toggleProperty("editingTitle");
2013-09-16 14:08:55 -04:00
},
saveTitle() {
2018-06-15 11:03:24 -04:00
const prevTitle = this.get("userTitleValue");
this.set("model.title", this.get("userTitleValue"));
return ajax(
userPath(`${this.get("model.username").toLowerCase()}.json`),
{
data: { title: this.get("userTitleValue") },
type: "PUT"
}
)
.catch(e => {
this.set("model.title", prevTitle);
popupAjaxError(e);
})
.finally(() => this.toggleProperty("editingTitle"));
2013-10-22 15:53:08 -04:00
},
generateApiKey() {
2018-06-15 11:03:24 -04:00
this.get("model").generateApiKey();
2013-10-22 15:53:08 -04:00
},
groupAdded(added) {
2018-06-15 11:03:24 -04:00
this.get("model")
.groupAdded(added)
.catch(function() {
bootbox.alert(I18n.t("generic_error"));
});
},
2014-07-15 10:11:39 -04:00
groupRemoved(groupId) {
2018-06-15 11:03:24 -04:00
this.get("model")
.groupRemoved(groupId)
.catch(function() {
bootbox.alert(I18n.t("generic_error"));
});
},
savePrimaryGroup() {
const self = this;
2018-06-15 11:03:24 -04:00
return ajax("/admin/users/" + this.get("model.id") + "/primary_group", {
type: "PUT",
data: { primary_group_id: this.get("model.primary_group_id") }
})
.then(function() {
self.set(
"originalPrimaryGroupId",
self.get("model.primary_group_id")
);
})
.catch(function() {
bootbox.alert(I18n.t("generic_error"));
});
},
resetPrimaryGroup() {
2018-06-15 11:03:24 -04:00
this.set("model.primary_group_id", this.get("originalPrimaryGroupId"));
},
regenerateApiKey() {
const self = this;
bootbox.confirm(
I18n.t("admin.api.confirm_regen"),
I18n.t("no_value"),
I18n.t("yes_value"),
function(result) {
2018-06-15 11:03:24 -04:00
if (result) {
self.get("model").generateApiKey();
}
2013-10-22 15:53:08 -04:00
}
);
2013-10-22 15:53:08 -04:00
},
revokeApiKey() {
const self = this;
bootbox.confirm(
I18n.t("admin.api.confirm_revoke"),
I18n.t("no_value"),
I18n.t("yes_value"),
function(result) {
2018-06-15 11:03:24 -04:00
if (result) {
self.get("model").revokeApiKey();
}
2013-10-22 15:53:08 -04:00
}
);
2013-09-16 14:08:55 -04:00
}
}
});