2019-10-30 01:31:44 +08:00
|
|
|
import { inject as service } from "@ember/service";
|
2019-10-24 01:06:54 +08:00
|
|
|
import Controller from "@ember/controller";
|
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";
|
2019-01-22 23:44:55 +08:00
|
|
|
import { default as computed } from "ember-addons/ember-computed-decorators";
|
|
|
|
import { fmt } from "discourse/lib/computed";
|
2014-08-13 07:04:36 +08:00
|
|
|
|
2019-10-24 01:06:54 +08:00
|
|
|
export default Controller.extend(CanCheckEmails, {
|
2019-10-30 01:29:08 +08:00
|
|
|
adminTools: service(),
|
2014-07-24 04:54:04 +08:00
|
|
|
originalPrimaryGroupId: null,
|
2018-10-02 12:34:08 +08:00
|
|
|
customGroupIdsBuffer: null,
|
2014-07-24 04:54:04 +08:00
|
|
|
availableGroups: null,
|
2016-04-19 11:12:14 +08:00
|
|
|
userTitleValue: null,
|
2013-06-26 06:39:20 +08:00
|
|
|
|
2018-06-15 23:03:24 +08:00
|
|
|
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"
|
|
|
|
),
|
2014-02-11 05:59:36 +08:00
|
|
|
|
2018-02-20 14:44:51 +08:00
|
|
|
canDisableSecondFactor: Ember.computed.and(
|
2018-06-15 23:03:24 +08:00
|
|
|
"model.second_factor_enabled",
|
|
|
|
"model.can_disable_second_factor"
|
2018-02-20 14:44:51 +08:00
|
|
|
),
|
|
|
|
|
2018-10-02 12:34:08 +08:00
|
|
|
@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;
|
|
|
|
},
|
|
|
|
|
2018-04-13 10:41:29 +08:00
|
|
|
@computed("model.automaticGroups")
|
|
|
|
automaticGroups(automaticGroups) {
|
2018-06-15 23:03:24 +08:00
|
|
|
return automaticGroups
|
|
|
|
.map(group => {
|
|
|
|
const name = Ember.String.htmlSafe(group.name);
|
2019-02-21 13:44:25 +08:00
|
|
|
return `<a href="/g/${name}">${name}</a>`;
|
2018-06-15 23:03:24 +08:00
|
|
|
})
|
|
|
|
.join(", ");
|
2018-04-13 10:41:29 +08:00
|
|
|
},
|
2014-07-14 02:11:38 +08:00
|
|
|
|
2018-07-23 23:51:57 +08:00
|
|
|
@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(", ");
|
|
|
|
},
|
|
|
|
|
2019-01-22 23:44:55 +08:00
|
|
|
@computed("model.user_fields.[]")
|
|
|
|
userFields(userFields) {
|
2019-04-05 02:45:04 +08:00
|
|
|
return this.site.collectUserFields(userFields);
|
2017-05-27 03:00:31 +08:00
|
|
|
},
|
|
|
|
|
2019-01-24 21:09:13 +08:00
|
|
|
preferencesPath: fmt("model.username_lower", userPath("%@/preferences")),
|
2019-01-22 23:44:55 +08:00
|
|
|
|
2018-06-15 23:03:24 +08:00
|
|
|
@computed("model.can_delete_all_posts", "model.staff", "model.post_count")
|
2018-05-25 23:33:01 +08:00
|
|
|
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");
|
2018-05-25 23:33:01 +08:00
|
|
|
}
|
|
|
|
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
|
|
|
|
});
|
2018-05-25 23:33:01 +08:00
|
|
|
} 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-05-25 23:33:01 +08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2018-06-15 23:03:24 +08:00
|
|
|
@computed("model.canBeDeleted", "model.staff")
|
2018-05-25 23:33:01 +08:00
|
|
|
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");
|
2018-05-25 23:33:01 +08:00
|
|
|
} else {
|
2018-06-15 23:03:24 +08:00
|
|
|
return I18n.t("admin.user.delete_forbidden", {
|
|
|
|
count: this.siteSettings.delete_user_max_post_age
|
|
|
|
});
|
2018-05-25 23:33:01 +08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2018-10-02 12:34:08 +08:00
|
|
|
groupAdded(added) {
|
2019-05-27 16:15:39 +08:00
|
|
|
this.model
|
2018-10-02 12:34:08 +08:00
|
|
|
.groupAdded(added)
|
2019-01-22 23:44:55 +08:00
|
|
|
.catch(() => bootbox.alert(I18n.t("generic_error")));
|
2018-10-02 12:34:08 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
groupRemoved(groupId) {
|
2019-05-27 16:15:39 +08:00
|
|
|
this.model
|
2018-10-02 12:34:08 +08:00
|
|
|
.groupRemoved(groupId)
|
|
|
|
.then(() => {
|
2019-05-27 16:15:39 +08:00
|
|
|
if (groupId === this.originalPrimaryGroupId) {
|
2018-10-02 12:34:08 +08:00
|
|
|
this.set("originalPrimaryGroupId", null);
|
|
|
|
}
|
|
|
|
})
|
2019-01-22 23:44:55 +08:00
|
|
|
.catch(() => bootbox.alert(I18n.t("generic_error")));
|
2018-10-02 12:34:08 +08:00
|
|
|
},
|
|
|
|
|
2013-09-17 02:08:55 +08:00
|
|
|
actions: {
|
2018-06-15 23:03:24 +08:00
|
|
|
impersonate() {
|
2019-05-27 16:15:39 +08:00
|
|
|
return this.model.impersonate();
|
2018-06-15 23:03:24 +08:00
|
|
|
},
|
|
|
|
logOut() {
|
2019-05-27 16:15:39 +08:00
|
|
|
return this.model.logOut();
|
2018-06-15 23:03:24 +08:00
|
|
|
},
|
|
|
|
resetBounceScore() {
|
2019-05-27 16:15:39 +08:00
|
|
|
return this.model.resetBounceScore();
|
2018-06-15 23:03:24 +08:00
|
|
|
},
|
|
|
|
approve() {
|
2019-07-26 17:20:11 +08:00
|
|
|
return this.model.approve(this.currentUser);
|
2018-06-15 23:03:24 +08:00
|
|
|
},
|
|
|
|
deactivate() {
|
2019-05-27 16:15:39 +08:00
|
|
|
return this.model.deactivate();
|
2018-06-15 23:03:24 +08:00
|
|
|
},
|
|
|
|
sendActivationEmail() {
|
2019-05-27 16:15:39 +08:00
|
|
|
return this.model.sendActivationEmail();
|
2018-06-15 23:03:24 +08:00
|
|
|
},
|
|
|
|
activate() {
|
2019-05-27 16:15:39 +08:00
|
|
|
return this.model.activate();
|
2018-06-15 23:03:24 +08:00
|
|
|
},
|
|
|
|
revokeAdmin() {
|
2019-05-27 16:15:39 +08:00
|
|
|
return this.model.revokeAdmin();
|
2018-06-15 23:03:24 +08:00
|
|
|
},
|
|
|
|
grantAdmin() {
|
2019-05-27 16:15:39 +08:00
|
|
|
return this.model.grantAdmin();
|
2018-06-15 23:03:24 +08:00
|
|
|
},
|
|
|
|
revokeModeration() {
|
2019-05-27 16:15:39 +08:00
|
|
|
return this.model.revokeModeration();
|
2018-06-15 23:03:24 +08:00
|
|
|
},
|
|
|
|
grantModeration() {
|
2019-05-27 16:15:39 +08:00
|
|
|
return this.model.grantModeration();
|
2018-06-15 23:03:24 +08:00
|
|
|
},
|
|
|
|
saveTrustLevel() {
|
2019-05-27 16:15:39 +08:00
|
|
|
return this.model.saveTrustLevel();
|
2018-06-15 23:03:24 +08:00
|
|
|
},
|
|
|
|
restoreTrustLevel() {
|
2019-05-27 16:15:39 +08:00
|
|
|
return this.model.restoreTrustLevel();
|
2018-06-15 23:03:24 +08:00
|
|
|
},
|
|
|
|
lockTrustLevel(locked) {
|
2019-05-27 16:15:39 +08:00
|
|
|
return this.model.lockTrustLevel(locked);
|
2018-06-15 23:03:24 +08:00
|
|
|
},
|
|
|
|
unsilence() {
|
2019-05-27 16:15:39 +08:00
|
|
|
return this.model.unsilence();
|
2018-06-15 23:03:24 +08:00
|
|
|
},
|
|
|
|
silence() {
|
2019-05-27 16:15:39 +08:00
|
|
|
return this.model.silence();
|
2018-06-15 23:03:24 +08:00
|
|
|
},
|
|
|
|
deleteAllPosts() {
|
2019-05-27 16:15:39 +08:00
|
|
|
return this.model.deleteAllPosts();
|
2018-06-15 23:03:24 +08:00
|
|
|
},
|
|
|
|
anonymize() {
|
2019-05-27 16:15:39 +08:00
|
|
|
return this.model.anonymize();
|
2018-06-15 23:03:24 +08:00
|
|
|
},
|
|
|
|
disableSecondFactor() {
|
2019-05-27 16:15:39 +08:00
|
|
|
return this.model.disableSecondFactor();
|
2018-06-15 23:03:24 +08:00
|
|
|
},
|
2018-05-25 23:45:42 +08:00
|
|
|
|
|
|
|
clearPenaltyHistory() {
|
2019-05-27 16:15:39 +08:00
|
|
|
const user = this.model;
|
2019-01-22 23:44:55 +08:00
|
|
|
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);
|
2018-05-25 23:45:42 +08:00
|
|
|
},
|
|
|
|
|
2018-03-05 12:32:23 +08:00
|
|
|
destroy() {
|
2018-06-15 23:03:24 +08:00
|
|
|
const postCount = this.get("model.post_count");
|
2018-03-05 12:32:23 +08:00
|
|
|
if (postCount <= 5) {
|
2019-05-27 16:15:39 +08:00
|
|
|
return this.model.destroy({ deletePosts: true });
|
2018-03-05 12:32:23 +08:00
|
|
|
} else {
|
2019-05-27 16:15:39 +08:00
|
|
|
return this.model.destroy();
|
2018-03-05 12:32:23 +08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2018-01-27 04:40:03 +08:00
|
|
|
viewActionLogs() {
|
2019-05-27 16:15:39 +08:00
|
|
|
this.adminTools.showActionLogs(this, {
|
2018-06-15 23:03:24 +08:00
|
|
|
target_user: this.get("model.username")
|
2018-01-27 04:40:03 +08:00
|
|
|
});
|
|
|
|
},
|
2017-09-13 05:07:42 +08:00
|
|
|
showSuspendModal() {
|
2019-05-27 16:15:39 +08:00
|
|
|
this.adminTools.showSuspendModal(this.model);
|
2017-09-13 05:07:42 +08:00
|
|
|
},
|
2017-09-14 02:11:33 +08:00
|
|
|
unsuspend() {
|
2019-05-27 16:42:53 +08:00
|
|
|
this.model.unsuspend().catch(popupAjaxError);
|
2017-09-14 02:11:33 +08:00
|
|
|
},
|
2017-11-14 02:41:36 +08:00
|
|
|
showSilenceModal() {
|
2019-05-27 16:15:39 +08:00
|
|
|
this.adminTools.showSilenceModal(this.model);
|
2017-11-14 02:41:36 +08:00
|
|
|
},
|
2017-09-13 05:07:42 +08:00
|
|
|
|
2018-12-17 22:28:29 +08:00
|
|
|
saveUsername(newUsername) {
|
2018-06-15 23:03:24 +08:00
|
|
|
const oldUsername = this.get("model.username");
|
2018-12-17 22:28:29 +08:00
|
|
|
this.set("model.username", newUsername);
|
2017-04-03 01:35:30 +08:00
|
|
|
|
2019-01-22 23:44:55 +08:00
|
|
|
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"));
|
2017-04-03 01:35:30 +08:00
|
|
|
},
|
|
|
|
|
2018-12-17 22:28:29 +08:00
|
|
|
saveName(newName) {
|
2018-06-15 23:03:24 +08:00
|
|
|
const oldName = this.get("model.name");
|
2018-12-17 22:28:29 +08:00
|
|
|
this.set("model.name", newName);
|
2018-06-15 23:03:24 +08:00
|
|
|
|
2019-01-22 23:44:55 +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"));
|
2017-04-03 01:35:30 +08:00
|
|
|
},
|
|
|
|
|
2018-12-17 22:28:29 +08:00
|
|
|
saveTitle(newTitle) {
|
|
|
|
const oldTitle = this.get("model.title");
|
|
|
|
this.set("model.title", newTitle);
|
2019-01-22 23:44:55 +08:00
|
|
|
|
|
|
|
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 => {
|
2018-12-17 22:28:29 +08:00
|
|
|
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
|
|
|
},
|
|
|
|
|
2015-03-18 05:59:05 +08:00
|
|
|
generateApiKey() {
|
2019-05-27 16:15:39 +08:00
|
|
|
this.model.generateApiKey();
|
2013-10-23 03:53:08 +08:00
|
|
|
},
|
|
|
|
|
2018-10-02 12:34:08 +08:00
|
|
|
saveCustomGroups() {
|
2019-05-27 16:15:39 +08:00
|
|
|
const currentIds = this.customGroupIds;
|
|
|
|
const bufferedIds = this.customGroupIdsBuffer;
|
|
|
|
const availableGroups = this.availableGroups;
|
2018-10-02 12:34:08 +08:00
|
|
|
|
2019-01-18 02:05:39 +08:00
|
|
|
bufferedIds
|
|
|
|
.filter(id => !currentIds.includes(id))
|
2019-01-22 23:44:55 +08:00
|
|
|
.forEach(id => this.groupAdded(availableGroups.findBy("id", id)));
|
2018-10-02 12:34:08 +08:00
|
|
|
|
|
|
|
currentIds
|
|
|
|
.filter(id => !bufferedIds.includes(id))
|
|
|
|
.forEach(id => this.groupRemoved(id));
|
2014-07-14 02:11:38 +08:00
|
|
|
},
|
2014-07-15 22:11:39 +08:00
|
|
|
|
2018-10-02 12:34:08 +08:00
|
|
|
resetCustomGroups() {
|
|
|
|
this.set("customGroupIdsBuffer", null);
|
2014-07-14 02:11:38 +08:00
|
|
|
},
|
|
|
|
|
2015-03-18 05:59:05 +08:00
|
|
|
savePrimaryGroup() {
|
2019-01-22 23:44:55 +08:00
|
|
|
const primaryGroupId = this.get("model.primary_group_id");
|
|
|
|
const path = `/admin/users/${this.get("model.id")}/primary_group`;
|
2015-03-18 05:59:05 +08:00
|
|
|
|
2019-01-22 23:44:55 +08:00
|
|
|
return ajax(path, {
|
2018-06-15 23:03:24 +08:00
|
|
|
type: "PUT",
|
2019-01-22 23:44:55 +08:00
|
|
|
data: { primary_group_id: primaryGroupId }
|
2018-06-15 23:03:24 +08:00
|
|
|
})
|
2019-01-22 23:44:55 +08:00
|
|
|
.then(() => this.set("originalPrimaryGroupId", primaryGroupId))
|
|
|
|
.catch(() => bootbox.alert(I18n.t("generic_error")));
|
2014-02-11 05:59:36 +08:00
|
|
|
},
|
|
|
|
|
2015-03-18 05:59:05 +08:00
|
|
|
resetPrimaryGroup() {
|
2019-05-27 16:15:39 +08:00
|
|
|
this.set("model.primary_group_id", this.originalPrimaryGroupId);
|
2014-02-11 05:59:36 +08:00
|
|
|
},
|
|
|
|
|
2015-03-18 05:59:05 +08:00
|
|
|
regenerateApiKey() {
|
|
|
|
bootbox.confirm(
|
|
|
|
I18n.t("admin.api.confirm_regen"),
|
|
|
|
I18n.t("no_value"),
|
|
|
|
I18n.t("yes_value"),
|
2019-01-22 23:44:55 +08:00
|
|
|
result => {
|
2018-06-15 23:03:24 +08:00
|
|
|
if (result) {
|
2019-05-27 16:15:39 +08:00
|
|
|
this.model.generateApiKey();
|
2018-06-15 23:03:24 +08:00
|
|
|
}
|
2013-10-23 03:53:08 +08:00
|
|
|
}
|
2015-03-18 05:59:05 +08:00
|
|
|
);
|
2013-10-23 03:53:08 +08:00
|
|
|
},
|
|
|
|
|
2015-03-18 05:59:05 +08:00
|
|
|
revokeApiKey() {
|
|
|
|
bootbox.confirm(
|
|
|
|
I18n.t("admin.api.confirm_revoke"),
|
|
|
|
I18n.t("no_value"),
|
|
|
|
I18n.t("yes_value"),
|
2019-01-22 23:44:55 +08:00
|
|
|
result => {
|
2018-06-15 23:03:24 +08:00
|
|
|
if (result) {
|
2019-05-27 16:15:39 +08:00
|
|
|
this.model.revokeApiKey();
|
2018-06-15 23:03:24 +08:00
|
|
|
}
|
2013-10-23 03:53:08 +08:00
|
|
|
}
|
2015-03-18 05:59:05 +08:00
|
|
|
);
|
2013-09-17 02:08:55 +08:00
|
|
|
}
|
|
|
|
}
|
2013-07-09 07:32:16 +08:00
|
|
|
});
|