discourse/app/assets/javascripts/admin/models/admin-user.js.es6

568 lines
15 KiB
Plaintext
Raw Normal View History

2018-06-15 23:03:24 +08:00
import { iconHTML } from "discourse-common/lib/icon-library";
import { ajax } from "discourse/lib/ajax";
import computed from "ember-addons/ember-computed-decorators";
import { propertyNotEqual } from "discourse/lib/computed";
import { popupAjaxError } from "discourse/lib/ajax-error";
import ApiKey from "admin/models/api-key";
import Group from "discourse/models/group";
import { userPath } from "discourse/lib/url";
2018-06-15 23:03:24 +08:00
const wrapAdmin = user => (user ? AdminUser.create(user) : null);
const AdminUser = Discourse.User.extend({
2017-09-15 02:10:39 +08:00
adminUserView: true,
2018-06-15 23:03:24 +08:00
customGroups: Ember.computed.filter(
"groups",
g => !g.automatic && Group.create(g)
),
automaticGroups: Ember.computed.filter(
"groups",
g => g.automatic && Group.create(g)
),
canViewProfile: Ember.computed.or("active", "staged"),
@computed("bounce_score", "reset_bounce_score_after")
bounceScore(bounce_score, reset_bounce_score_after) {
if (bounce_score > 0) {
2018-06-15 23:03:24 +08:00
return `${bounce_score} - ${moment(reset_bounce_score_after).format(
"LL"
)}`;
} else {
return bounce_score;
}
},
@computed("bounce_score")
bounceScoreExplanation(bounce_score) {
if (bounce_score === 0) {
return I18n.t("admin.user.bounce_score_explanation.none");
} else if (bounce_score < Discourse.SiteSettings.bounce_score_threshold) {
return I18n.t("admin.user.bounce_score_explanation.some");
} else {
return I18n.t("admin.user.bounce_score_explanation.threshold_reached");
}
},
@computed
bounceLink() {
return Discourse.getURL("/admin/email/bounced");
},
canResetBounceScore: Ember.computed.gt("bounce_score", 0),
resetBounceScore() {
2019-05-07 16:53:21 +08:00
return ajax(`/admin/users/${this.id}/reset_bounce_score`, {
2018-06-15 23:03:24 +08:00
type: "POST"
}).then(() =>
this.setProperties({
bounce_score: 0,
reset_bounce_score_after: null
})
);
},
generateApiKey() {
2019-05-07 16:53:21 +08:00
return ajax(`/admin/users/${this.id}/generate_api_key`, {
2018-06-15 23:03:24 +08:00
type: "POST"
2019-05-07 16:53:21 +08:00
}).then(result => {
const apiKey = ApiKey.create(result.api_key);
2019-05-07 16:53:21 +08:00
this.set("api_key", apiKey);
2013-10-23 03:53:08 +08:00
return apiKey;
});
},
groupAdded(added) {
2019-05-07 16:53:21 +08:00
return ajax(`/admin/users/${this.id}/groups`, {
2018-06-15 23:03:24 +08:00
type: "POST",
data: { group_id: added.id }
}).then(() => this.groups.pushObject(added));
2014-07-15 22:11:39 +08:00
},
groupRemoved(groupId) {
2019-05-07 16:53:21 +08:00
return ajax(`/admin/users/${this.id}/groups/${groupId}`, {
2018-06-15 23:03:24 +08:00
type: "DELETE"
}).then(() => {
this.set("groups.[]", this.groups.rejectBy("id", groupId));
if (this.primary_group_id === groupId) {
2018-06-15 23:03:24 +08:00
this.set("primary_group_id", null);
}
});
2014-07-15 22:11:39 +08:00
},
revokeApiKey() {
2019-05-07 16:53:21 +08:00
return ajax(`/admin/users/${this.id}/revoke_api_key`, {
2018-06-15 23:03:24 +08:00
type: "DELETE"
}).then(() => this.set("api_key", null));
2013-10-23 03:53:08 +08:00
},
deleteAllPosts() {
let deletedPosts = 0;
2019-05-07 16:53:21 +08:00
const user = this;
const message = I18n.messageFormat(
"admin.user.delete_all_posts_confirm_MF",
{
2018-06-15 23:03:24 +08:00
POSTS: user.get("post_count"),
TOPICS: user.get("topic_count")
2019-05-07 16:53:21 +08:00
}
);
const buttons = [
{
label: I18n.t("composer.cancel"),
class: "d-modal-cancel",
link: true
},
{
label:
`${iconHTML("exclamation-triangle")} ` +
I18n.t("admin.user.delete_all_posts"),
class: "btn btn-danger",
callback: () => {
openProgressModal();
performDelete();
}
}
];
const openProgressModal = () => {
bootbox.dialog(
`<p>${I18n.t(
"admin.user.delete_posts_progress"
)}</p><div class='progress-bar'><span></span></div>`,
[],
{ classes: "delete-posts-progress" }
);
};
const performDelete = () => {
let deletedPercentage = 0;
return ajax(`/admin/users/${user.get("id")}/delete_posts_batch`, {
type: "PUT"
})
.then(({ posts_deleted }) => {
if (posts_deleted === 0) {
user.set("post_count", 0);
bootbox.hideAll();
} else {
deletedPosts += posts_deleted;
deletedPercentage = Math.floor(
(deletedPosts * 100) / user.get("post_count")
);
$(".delete-posts-progress .progress-bar > span").css({
width: `${deletedPercentage}%`
});
performDelete();
2018-06-15 23:03:24 +08:00
}
})
2019-05-07 16:53:21 +08:00
.catch(e => {
bootbox.hideAll();
let error;
AdminUser.find(user.get("id")).then(u => user.setProperties(u));
if (e.responseJSON && e.responseJSON.errors) {
error = e.responseJSON.errors[0];
}
error = error || I18n.t("admin.user.delete_posts_failed");
bootbox.alert(error);
});
};
2018-06-15 23:03:24 +08:00
bootbox.dialog(message, buttons, { classes: "delete-all-posts" });
},
revokeAdmin() {
2019-05-07 16:53:21 +08:00
return ajax(`/admin/users/${this.id}/revoke_admin`, {
2018-06-15 23:03:24 +08:00
type: "PUT"
}).then(() => {
this.setProperties({
admin: false,
can_grant_admin: true,
can_revoke_admin: false
});
});
},
grantAdmin() {
2019-05-07 16:53:21 +08:00
return ajax(`/admin/users/${this.id}/grant_admin`, {
2018-06-15 23:03:24 +08:00
type: "PUT"
})
.then(() => {
bootbox.alert(I18n.t("admin.user.grant_admin_confirm"));
})
.catch(popupAjaxError);
},
revokeModeration() {
2019-05-07 16:53:21 +08:00
return ajax(`/admin/users/${this.id}/revoke_moderation`, {
2018-06-15 23:03:24 +08:00
type: "PUT"
})
2019-05-07 16:53:21 +08:00
.then(() => {
this.setProperties({
2018-06-15 23:03:24 +08:00
moderator: false,
can_grant_moderation: true,
can_revoke_moderation: false
});
})
.catch(popupAjaxError);
},
grantModeration() {
2019-05-07 16:53:21 +08:00
return ajax(`/admin/users/${this.id}/grant_moderation`, {
2018-06-15 23:03:24 +08:00
type: "PUT"
})
2019-05-07 16:53:21 +08:00
.then(() => {
this.setProperties({
2018-06-15 23:03:24 +08:00
moderator: true,
can_grant_moderation: false,
can_revoke_moderation: true
});
})
.catch(popupAjaxError);
},
disableSecondFactor() {
2019-05-07 16:53:21 +08:00
return ajax(`/admin/users/${this.id}/disable_second_factor`, {
2018-06-15 23:03:24 +08:00
type: "PUT"
})
.then(() => {
this.set("second_factor_enabled", false);
})
.catch(popupAjaxError);
},
approve() {
2019-05-07 16:53:21 +08:00
return ajax(`/admin/users/${this.id}/approve`, {
2018-06-15 23:03:24 +08:00
type: "PUT"
2019-05-07 16:53:21 +08:00
}).then(() => {
this.setProperties({
can_approve: false,
approved: true,
approved_by: Discourse.User.current()
});
});
},
setOriginalTrustLevel() {
this.set("originalTrustLevel", this.trust_level);
},
2018-06-15 23:03:24 +08:00
dirty: propertyNotEqual("originalTrustLevel", "trustLevel.id"),
saveTrustLevel() {
2019-05-07 16:53:21 +08:00
return ajax(`/admin/users/${this.id}/trust_level`, {
2018-06-15 23:03:24 +08:00
type: "PUT",
data: { level: this.get("trustLevel.id") }
})
2019-05-07 16:53:21 +08:00
.then(() => window.location.reload())
.catch(e => {
2018-06-15 23:03:24 +08:00
let error;
if (e.responseJSON && e.responseJSON.errors) {
error = e.responseJSON.errors[0];
}
error =
error ||
I18n.t("admin.user.trust_level_change_failed", {
2019-05-07 16:53:21 +08:00
error: this._formatError(e)
2018-06-15 23:03:24 +08:00
});
bootbox.alert(error);
});
},
restoreTrustLevel() {
this.set("trustLevel.id", this.originalTrustLevel);
},
lockTrustLevel(locked) {
2019-05-07 16:53:21 +08:00
return ajax(`/admin/users/${this.id}/trust_level_lock`, {
2018-06-15 23:03:24 +08:00
type: "PUT",
data: { locked: !!locked }
2018-06-15 23:03:24 +08:00
})
2019-05-07 16:53:21 +08:00
.then(() => window.location.reload())
.catch(e => {
2018-06-15 23:03:24 +08:00
let error;
if (e.responseJSON && e.responseJSON.errors) {
error = e.responseJSON.errors[0];
}
error =
error ||
I18n.t("admin.user.trust_level_change_failed", {
2019-05-07 16:53:21 +08:00
error: this._formatError(e)
2018-06-15 23:03:24 +08:00
});
bootbox.alert(error);
});
},
2019-05-07 16:53:21 +08:00
canLockTrustLevel: Ember.computed.lt("trust_level", 4),
2019-01-12 00:54:23 +08:00
canSuspend: Ember.computed.not("staff"),
@computed("suspended_till", "suspended_at")
suspendDuration(suspendedTill, suspendedAt) {
suspendedAt = moment(suspendedAt);
suspendedTill = moment(suspendedTill);
return suspendedAt.format("L") + " - " + suspendedTill.format("L");
},
suspend(data) {
return ajax(`/admin/users/${this.id}/suspend`, {
2018-06-15 23:03:24 +08:00
type: "PUT",
data
}).then(result => this.setProperties(result.suspension));
},
unsuspend() {
return ajax(`/admin/users/${this.id}/unsuspend`, {
2018-06-15 23:03:24 +08:00
type: "PUT"
}).then(result => this.setProperties(result.suspension));
},
logOut() {
2016-07-01 01:55:44 +08:00
return ajax("/admin/users/" + this.id + "/log_out", {
2018-06-15 23:03:24 +08:00
type: "POST",
data: { username_or_email: this.username }
2019-05-07 16:53:21 +08:00
}).then(() => bootbox.alert(I18n.t("admin.user.logged_out")));
2014-06-06 11:02:52 +08:00
},
impersonate() {
2016-07-01 01:55:44 +08:00
return ajax("/admin/impersonate", {
2018-06-15 23:03:24 +08:00
type: "POST",
data: { username_or_email: this.username }
2018-06-15 23:03:24 +08:00
})
2019-05-07 16:53:21 +08:00
.then(() => (document.location = Discourse.getURL("/")))
.catch(e => {
2018-06-15 23:03:24 +08:00
if (e.status === 404) {
bootbox.alert(I18n.t("admin.impersonate.not_found"));
} else {
bootbox.alert(I18n.t("admin.impersonate.invalid"));
}
});
},
activate() {
2019-05-07 16:53:21 +08:00
return ajax(`/admin/users/${this.id}/activate`, {
2018-06-15 23:03:24 +08:00
type: "PUT"
})
2019-05-07 16:53:21 +08:00
.then(() => window.location.reload())
.catch(e => {
const error = I18n.t("admin.user.activate_failed", {
error: this._formatError(e)
2018-06-15 23:03:24 +08:00
});
bootbox.alert(error);
});
},
deactivate() {
2019-05-07 16:53:21 +08:00
return ajax(`/admin/users/${this.id}/deactivate`, {
2018-06-15 23:03:24 +08:00
type: "PUT",
data: { context: document.location.pathname }
2018-06-15 23:03:24 +08:00
})
2019-05-07 16:53:21 +08:00
.then(() => window.location.reload())
.catch(e => {
const error = I18n.t("admin.user.deactivate_failed", {
error: this._formatError(e)
2018-06-15 23:03:24 +08:00
});
bootbox.alert(error);
});
},
2017-11-11 01:18:08 +08:00
unsilence() {
2018-06-15 23:03:24 +08:00
this.set("silencingUser", true);
return ajax(`/admin/users/${this.id}/unsilence`, {
2018-06-15 23:03:24 +08:00
type: "PUT"
})
2019-05-07 16:53:21 +08:00
.then(result => this.setProperties(result.unsilence))
2018-06-15 23:03:24 +08:00
.catch(e => {
2019-05-07 16:53:21 +08:00
const error = I18n.t("admin.user.unsilence_failed", {
error: this._formatError(e)
2018-06-15 23:03:24 +08:00
});
bootbox.alert(error);
})
2019-05-07 16:53:21 +08:00
.finally(() => this.set("silencingUser", false));
},
silence(data) {
2018-06-15 23:03:24 +08:00
this.set("silencingUser", true);
return ajax(`/admin/users/${this.id}/silence`, {
2018-06-15 23:03:24 +08:00
type: "PUT",
data
2018-06-15 23:03:24 +08:00
})
2019-05-07 16:53:21 +08:00
.then(result => this.setProperties(result.silence))
2018-06-15 23:03:24 +08:00
.catch(e => {
2019-05-07 16:53:21 +08:00
const error = I18n.t("admin.user.silence_failed", {
error: this._formatError(e)
2018-06-15 23:03:24 +08:00
});
bootbox.alert(error);
})
2019-05-07 16:53:21 +08:00
.finally(() => this.set("silencingUser", false));
},
sendActivationEmail() {
2018-06-15 23:03:24 +08:00
return ajax(userPath("action/send_activation_email"), {
type: "POST",
data: { username: this.username }
2018-06-15 23:03:24 +08:00
})
2019-05-07 16:53:21 +08:00
.then(() => bootbox.alert(I18n.t("admin.user.activation_email_sent")))
2018-06-15 23:03:24 +08:00
.catch(popupAjaxError);
},
anonymize() {
2019-05-07 16:53:21 +08:00
const user = this;
const message = I18n.t("admin.user.anonymize_confirm");
const performAnonymize = function() {
2019-05-07 16:53:21 +08:00
return ajax(`/admin/users/${user.get("id")}/anonymize.json`, {
2018-06-15 23:03:24 +08:00
type: "PUT"
})
.then(function(data) {
if (data.success) {
if (data.username) {
document.location = Discourse.getURL(
2019-05-07 16:53:21 +08:00
`/admin/users/${user.get("id")}/${data.username}`
2018-06-15 23:03:24 +08:00
);
} else {
document.location = Discourse.getURL("/admin/users/list/active");
}
} else {
2018-06-15 23:03:24 +08:00
bootbox.alert(I18n.t("admin.user.anonymize_failed"));
if (data.user) {
user.setProperties(data.user);
}
}
2018-06-15 23:03:24 +08:00
})
2019-05-07 16:53:21 +08:00
.catch(() => bootbox.alert(I18n.t("admin.user.anonymize_failed")));
};
2018-06-15 23:03:24 +08:00
const buttons = [
{
label: I18n.t("composer.cancel"),
class: "cancel",
link: true
},
{
label:
`${iconHTML("exclamation-triangle")} ` +
I18n.t("admin.user.anonymize_yes"),
class: "btn btn-danger",
callback: function() {
performAnonymize();
}
}
];
2018-06-15 23:03:24 +08:00
bootbox.dialog(message, buttons, { classes: "delete-user-modal" });
},
destroy(opts) {
2019-05-07 16:53:21 +08:00
const user = this;
const message = I18n.t("admin.user.delete_confirm");
const location = document.location.pathname;
const performDestroy = function(block) {
2018-06-15 23:03:24 +08:00
bootbox.dialog(I18n.t("admin.user.deleting_user"));
let formData = { context: location };
if (block) {
formData["block_email"] = true;
formData["block_urls"] = true;
formData["block_ip"] = true;
}
if (opts && opts.deletePosts) {
formData["delete_posts"] = true;
}
2019-05-07 16:53:21 +08:00
return ajax(`/admin/users/${user.get("id")}.json`, {
2018-06-15 23:03:24 +08:00
type: "DELETE",
data: formData
2018-06-15 23:03:24 +08:00
})
.then(function(data) {
if (data.deleted) {
if (/^\/admin\/users\/list\//.test(location)) {
document.location = location;
} else {
document.location = Discourse.getURL("/admin/users/list/active");
}
} else {
2018-06-15 23:03:24 +08:00
bootbox.alert(I18n.t("admin.user.delete_failed"));
if (data.user) {
user.setProperties(data.user);
}
}
2018-06-15 23:03:24 +08:00
})
.catch(function() {
AdminUser.find(user.get("id")).then(u => user.setProperties(u));
bootbox.alert(I18n.t("admin.user.delete_failed"));
2018-06-15 23:03:24 +08:00
});
};
2018-06-15 23:03:24 +08:00
const buttons = [
{
label: I18n.t("composer.cancel"),
class: "btn",
link: true
},
{
label:
`${iconHTML("exclamation-triangle")} ` +
I18n.t("admin.user.delete_and_block"),
class: "btn btn-danger",
callback: function() {
performDestroy(true);
}
},
{
label: I18n.t("admin.user.delete_dont_block"),
class: "btn btn-primary",
callback: function() {
performDestroy(false);
}
}
];
2018-06-15 23:03:24 +08:00
bootbox.dialog(message, buttons, { classes: "delete-user-modal" });
},
loadDetails() {
if (this.loadedDetails) {
2019-05-07 16:53:21 +08:00
return Ember.RSVP.resolve(this);
2018-06-15 23:03:24 +08:00
}
2019-05-07 16:53:21 +08:00
return AdminUser.find(this.id).then(result => {
const userProperties = Object.assign(result, { loadedDetails: true });
this.setProperties(userProperties);
});
},
2018-06-15 23:03:24 +08:00
@computed("tl3_requirements")
tl3Requirements(requirements) {
if (requirements) {
2018-06-15 23:03:24 +08:00
return this.store.createRecord("tl3Requirements", requirements);
}
},
2018-07-30 23:56:48 +08:00
@computed("suspended_by")
suspendedBy: wrapAdmin,
2018-07-30 23:56:48 +08:00
@computed("silenced_by")
silencedBy: wrapAdmin,
2018-07-30 23:56:48 +08:00
@computed("approved_by")
2019-05-07 16:53:21 +08:00
approvedBy: wrapAdmin,
_formatError(event) {
return `http: ${event.status} - ${event.body}`;
}
});
AdminUser.reopenClass({
find(user_id) {
2019-05-07 16:53:21 +08:00
return ajax(`/admin/users/${user_id}.json`).then(result => {
result.loadedDetails = true;
return AdminUser.create(result);
});
},
findAll(query, filter) {
2019-05-07 16:53:21 +08:00
return ajax(`/admin/users/list/${query}.json`, {
data: filter
2019-05-07 16:53:21 +08:00
}).then(users => users.map(u => AdminUser.create(u)));
}
});
export default AdminUser;