2021-01-20 13:04:21 +08:00
|
|
|
import DiscourseURL, { userPath } from "discourse/lib/url";
|
2019-10-31 04:28:29 +08:00
|
|
|
import { and, notEmpty } from "@ember/object/computed";
|
2019-10-30 01:31:44 +08:00
|
|
|
import { fmt, propertyNotEqual, setting } from "discourse/lib/computed";
|
2021-01-20 13:04:21 +08:00
|
|
|
import AdminUser from "admin/models/admin-user";
|
2019-10-30 01:31:44 +08:00
|
|
|
import CanCheckEmails from "discourse/mixins/can-check-emails";
|
2019-10-24 01:06:54 +08:00
|
|
|
import Controller from "@ember/controller";
|
2020-05-14 04:23:41 +08:00
|
|
|
import I18n from "I18n";
|
2016-07-01 01:55:44 +08:00
|
|
|
import { ajax } from "discourse/lib/ajax";
|
2020-01-17 01:56:53 +08:00
|
|
|
import discourseComputed from "discourse-common/utils/decorators";
|
2021-01-20 13:04:21 +08:00
|
|
|
import getURL from "discourse-common/lib/get-url";
|
2019-11-06 02:43:49 +08:00
|
|
|
import { htmlSafe } from "@ember/template";
|
2021-08-25 12:44:22 +08:00
|
|
|
import { extractError, popupAjaxError } from "discourse/lib/ajax-error";
|
2019-11-06 02:43:49 +08:00
|
|
|
import { inject as service } from "@ember/service";
|
2020-04-22 16:37:51 +08:00
|
|
|
import showModal from "discourse/lib/show-modal";
|
2014-08-13 07:04:36 +08:00
|
|
|
|
2019-10-24 01:06:54 +08:00
|
|
|
export default Controller.extend(CanCheckEmails, {
|
FEATURE: Centralized 2FA page (#15377)
2FA support in Discourse was added and grown gradually over the years: we first
added support for TOTP for logins, then we implemented backup codes, and last
but not least, security keys. 2FA usage was initially limited to logging in,
but it has been expanded and we now require 2FA for risky actions such as
adding a new admin to the site.
As a result of this gradual growth of the 2FA system, technical debt has
accumulated to the point where it has become difficult to require 2FA for more
actions. We now have 5 different 2FA UI implementations and each one has to
support all 3 2FA methods (TOTP, backup codes, and security keys) which makes
it difficult to maintain a consistent UX for these different implementations.
Moreover, there is a lot of repeated logic in the server-side code behind these
5 UI implementations which hinders maintainability even more.
This commit is the first step towards repaying the technical debt: it builds a
system that centralizes as much as possible of the 2FA server-side logic and
UI. The 2 main components of this system are:
1. A dedicated page for 2FA with support for all 3 methods.
2. A reusable server-side class that centralizes the 2FA logic (the
`SecondFactor::AuthManager` class).
From a top-level view, the 2FA flow in this new system looks like this:
1. User initiates an action that requires 2FA;
2. Server is aware that 2FA is required for this action, so it redirects the
user to the 2FA page if the user has a 2FA method, otherwise the action is
performed.
3. User submits the 2FA form on the page;
4. Server validates the 2FA and if it's successful, the action is performed and
the user is redirected to the previous page.
A more technically-detailed explanation/documentation of the new system is
available as a comment at the top of the `lib/second_factor/auth_manager.rb`
file. Please note that the details are not set in stone and will likely change
in the future, so please don't use the system in your plugins yet.
Since this is a new system that needs to be tested, we've decided to migrate
only the 2FA for adding a new admin to the new system at this time (in this
commit). Our plan is to gradually migrate the remaining 2FA implementations to
the new system.
For screenshots of the 2FA page, see PR #15377 on GitHub.
2022-02-17 17:12:59 +08:00
|
|
|
router: service(),
|
2022-08-30 01:59:57 +08:00
|
|
|
dialog: service(),
|
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,
|
2020-11-11 03:12:44 +08:00
|
|
|
ssoExternalEmail: null,
|
2021-02-17 23:57:51 +08:00
|
|
|
ssoLastPayload: null,
|
2013-06-26 06:39:20 +08:00
|
|
|
|
2015-08-08 03:08:27 +08:00
|
|
|
showBadges: setting("enable_badges"),
|
2019-10-31 07:18:29 +08:00
|
|
|
hasLockedTrustLevel: notEmpty("model.manual_locked_trust_level"),
|
2014-03-05 20:52:20 +08:00
|
|
|
|
2015-08-08 03:08:27 +08:00
|
|
|
primaryGroupDirty: propertyNotEqual(
|
|
|
|
"originalPrimaryGroupId",
|
|
|
|
"model.primary_group_id"
|
|
|
|
),
|
2014-02-11 05:59:36 +08:00
|
|
|
|
2019-10-31 04:28:29 +08:00
|
|
|
canDisableSecondFactor: and(
|
2018-02-20 14:44:51 +08:00
|
|
|
"model.second_factor_enabled",
|
|
|
|
"model.can_disable_second_factor"
|
|
|
|
),
|
|
|
|
|
2019-11-08 05:38:28 +08:00
|
|
|
@discourseComputed("model.customGroups")
|
2018-10-02 12:34:08 +08:00
|
|
|
customGroupIds(customGroups) {
|
|
|
|
return customGroups.mapBy("id");
|
|
|
|
},
|
|
|
|
|
2019-11-08 05:38:28 +08:00
|
|
|
@discourseComputed("customGroupIdsBuffer", "customGroupIds")
|
2018-10-02 12:34:08 +08:00
|
|
|
customGroupsDirty(buffer, original) {
|
2020-09-22 22:28:28 +08:00
|
|
|
if (buffer === null) {
|
|
|
|
return false;
|
|
|
|
}
|
2018-10-02 12:34:08 +08:00
|
|
|
|
|
|
|
return buffer.length === original.length
|
|
|
|
? buffer.any((id) => !original.includes(id))
|
|
|
|
: true;
|
|
|
|
},
|
|
|
|
|
2019-11-08 05:38:28 +08:00
|
|
|
@discourseComputed("model.automaticGroups")
|
2018-04-13 10:41:29 +08:00
|
|
|
automaticGroups(automaticGroups) {
|
|
|
|
return automaticGroups
|
|
|
|
.map((group) => {
|
2019-11-06 02:43:49 +08:00
|
|
|
const name = htmlSafe(group.name);
|
2019-02-21 13:44:25 +08:00
|
|
|
return `<a href="/g/${name}">${name}</a>`;
|
2018-04-13 10:41:29 +08:00
|
|
|
})
|
|
|
|
.join(", ");
|
|
|
|
},
|
2014-07-14 02:11:38 +08:00
|
|
|
|
2019-11-08 05:38:28 +08:00
|
|
|
@discourseComputed("model.associated_accounts")
|
2018-07-23 23:51:57 +08:00
|
|
|
associatedAccountsLoaded(associatedAccounts) {
|
|
|
|
return typeof associatedAccounts !== "undefined";
|
|
|
|
},
|
|
|
|
|
2019-11-08 05:38:28 +08:00
|
|
|
@discourseComputed("model.associated_accounts")
|
2018-07-23 23:51:57 +08:00
|
|
|
associatedAccounts(associatedAccounts) {
|
|
|
|
return associatedAccounts
|
|
|
|
.map((provider) => `${provider.name} (${provider.description})`)
|
|
|
|
.join(", ");
|
|
|
|
},
|
|
|
|
|
2019-11-08 05:38:28 +08:00
|
|
|
@discourseComputed("model.user_fields.[]")
|
2019-01-22 23:44:55 +08:00
|
|
|
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
|
|
|
|
2019-11-08 05:38:28 +08:00
|
|
|
@discourseComputed(
|
|
|
|
"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) {
|
|
|
|
return I18n.t("admin.user.delete_posts_forbidden_because_staff");
|
|
|
|
}
|
|
|
|
if (postCount > this.siteSettings.delete_all_posts_max) {
|
|
|
|
return I18n.t("admin.user.cant_delete_all_too_many_posts", {
|
|
|
|
count: this.siteSettings.delete_all_posts_max,
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
return I18n.t("admin.user.cant_delete_all_posts", {
|
|
|
|
count: this.siteSettings.delete_user_max_post_age,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2019-11-08 05:38:28 +08:00
|
|
|
@discourseComputed("model.canBeDeleted", "model.staff")
|
2018-05-25 23:33:01 +08:00
|
|
|
deleteExplanation(canBeDeleted, staff) {
|
|
|
|
if (canBeDeleted) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (staff) {
|
|
|
|
return I18n.t("admin.user.delete_forbidden_because_staff");
|
|
|
|
} else {
|
|
|
|
return I18n.t("admin.user.delete_forbidden", {
|
|
|
|
count: this.siteSettings.delete_user_max_post_age,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2021-08-02 22:15:53 +08:00
|
|
|
@discourseComputed("model.username")
|
|
|
|
postEditsByEditorFilter(username) {
|
|
|
|
return { editor: username };
|
|
|
|
},
|
|
|
|
|
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)
|
2022-08-30 01:59:57 +08:00
|
|
|
.catch(() => this.dialog.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);
|
|
|
|
}
|
|
|
|
})
|
2022-08-30 01:59:57 +08:00
|
|
|
.catch(() => this.dialog.alert(I18n.t("generic_error")));
|
2018-10-02 12:34:08 +08:00
|
|
|
},
|
|
|
|
|
2021-02-17 23:57:51 +08:00
|
|
|
@discourseComputed("ssoLastPayload")
|
2020-09-15 22:00:10 +08:00
|
|
|
ssoPayload(lastPayload) {
|
|
|
|
return lastPayload.split("&");
|
|
|
|
},
|
|
|
|
|
2013-09-17 02:08:55 +08:00
|
|
|
actions: {
|
2016-12-20 19:26:53 +08:00
|
|
|
impersonate() {
|
2021-01-20 13:04:21 +08:00
|
|
|
return this.model
|
|
|
|
.impersonate()
|
|
|
|
.then(() => DiscourseURL.redirectTo("/"))
|
|
|
|
.catch((e) => {
|
|
|
|
if (e.status === 404) {
|
2022-08-30 01:59:57 +08:00
|
|
|
this.dialog.alert(I18n.t("admin.impersonate.not_found"));
|
2021-01-20 13:04:21 +08:00
|
|
|
} else {
|
2022-08-30 01:59:57 +08:00
|
|
|
this.dialog.alert(I18n.t("admin.impersonate.invalid"));
|
2021-01-20 13:04:21 +08:00
|
|
|
}
|
|
|
|
});
|
2016-12-20 19:26:53 +08:00
|
|
|
},
|
|
|
|
logOut() {
|
2021-01-20 13:04:21 +08:00
|
|
|
return this.model
|
|
|
|
.logOut()
|
2022-08-30 01:59:57 +08:00
|
|
|
.then(() => this.dialog.alert(I18n.t("admin.user.logged_out")));
|
2016-12-20 19:26:53 +08:00
|
|
|
},
|
|
|
|
resetBounceScore() {
|
2019-05-27 16:15:39 +08:00
|
|
|
return this.model.resetBounceScore();
|
2016-12-20 19:26:53 +08:00
|
|
|
},
|
|
|
|
approve() {
|
2019-07-26 17:20:11 +08:00
|
|
|
return this.model.approve(this.currentUser);
|
2016-12-20 19:26:53 +08:00
|
|
|
},
|
2021-01-20 13:04:21 +08:00
|
|
|
|
|
|
|
_formatError(event) {
|
|
|
|
return `http: ${event.status} - ${event.body}`;
|
|
|
|
},
|
|
|
|
|
2016-12-20 19:26:53 +08:00
|
|
|
deactivate() {
|
2021-01-20 13:04:21 +08:00
|
|
|
return this.model
|
|
|
|
.deactivate()
|
|
|
|
.then(() =>
|
|
|
|
this.model.setProperties({ active: false, can_activate: true })
|
|
|
|
)
|
|
|
|
.catch((e) => {
|
|
|
|
const error = I18n.t("admin.user.deactivate_failed", {
|
|
|
|
error: this._formatError(e),
|
|
|
|
});
|
2022-08-30 01:59:57 +08:00
|
|
|
this.dialog.alert(error);
|
2021-01-20 13:04:21 +08:00
|
|
|
});
|
2016-12-20 19:26:53 +08:00
|
|
|
},
|
|
|
|
sendActivationEmail() {
|
2021-01-20 13:04:21 +08:00
|
|
|
return this.model
|
|
|
|
.sendActivationEmail()
|
2022-08-30 01:59:57 +08:00
|
|
|
.then(() =>
|
|
|
|
this.dialog.alert(I18n.t("admin.user.activation_email_sent"))
|
|
|
|
)
|
2021-01-20 13:04:21 +08:00
|
|
|
.catch(popupAjaxError);
|
2016-12-20 19:26:53 +08:00
|
|
|
},
|
|
|
|
activate() {
|
2021-01-20 13:04:21 +08:00
|
|
|
return this.model
|
|
|
|
.activate()
|
|
|
|
.then(() =>
|
|
|
|
this.model.setProperties({
|
|
|
|
active: true,
|
|
|
|
can_deactivate: !this.model.staff,
|
|
|
|
})
|
|
|
|
)
|
|
|
|
.catch((e) => {
|
|
|
|
const error = I18n.t("admin.user.activate_failed", {
|
|
|
|
error: this._formatError(e),
|
|
|
|
});
|
2022-08-30 01:59:57 +08:00
|
|
|
this.dialog.alert(error);
|
2021-01-20 13:04:21 +08:00
|
|
|
});
|
2016-12-20 19:26:53 +08:00
|
|
|
},
|
|
|
|
revokeAdmin() {
|
2019-05-27 16:15:39 +08:00
|
|
|
return this.model.revokeAdmin();
|
2016-12-20 19:26:53 +08:00
|
|
|
},
|
|
|
|
grantAdmin() {
|
2021-01-20 13:04:21 +08:00
|
|
|
return this.model
|
|
|
|
.grantAdmin()
|
2021-09-14 20:19:28 +08:00
|
|
|
.then((result) => {
|
|
|
|
if (result.email_confirmation_required) {
|
2022-08-30 01:59:57 +08:00
|
|
|
this.dialog.alert(I18n.t("admin.user.grant_admin_confirm"));
|
2021-09-14 20:19:28 +08:00
|
|
|
}
|
2021-01-20 13:04:21 +08:00
|
|
|
})
|
FEATURE: Centralized 2FA page (#15377)
2FA support in Discourse was added and grown gradually over the years: we first
added support for TOTP for logins, then we implemented backup codes, and last
but not least, security keys. 2FA usage was initially limited to logging in,
but it has been expanded and we now require 2FA for risky actions such as
adding a new admin to the site.
As a result of this gradual growth of the 2FA system, technical debt has
accumulated to the point where it has become difficult to require 2FA for more
actions. We now have 5 different 2FA UI implementations and each one has to
support all 3 2FA methods (TOTP, backup codes, and security keys) which makes
it difficult to maintain a consistent UX for these different implementations.
Moreover, there is a lot of repeated logic in the server-side code behind these
5 UI implementations which hinders maintainability even more.
This commit is the first step towards repaying the technical debt: it builds a
system that centralizes as much as possible of the 2FA server-side logic and
UI. The 2 main components of this system are:
1. A dedicated page for 2FA with support for all 3 methods.
2. A reusable server-side class that centralizes the 2FA logic (the
`SecondFactor::AuthManager` class).
From a top-level view, the 2FA flow in this new system looks like this:
1. User initiates an action that requires 2FA;
2. Server is aware that 2FA is required for this action, so it redirects the
user to the 2FA page if the user has a 2FA method, otherwise the action is
performed.
3. User submits the 2FA form on the page;
4. Server validates the 2FA and if it's successful, the action is performed and
the user is redirected to the previous page.
A more technically-detailed explanation/documentation of the new system is
available as a comment at the top of the `lib/second_factor/auth_manager.rb`
file. Please note that the details are not set in stone and will likely change
in the future, so please don't use the system in your plugins yet.
Since this is a new system that needs to be tested, we've decided to migrate
only the 2FA for adding a new admin to the new system at this time (in this
commit). Our plan is to gradually migrate the remaining 2FA implementations to
the new system.
For screenshots of the 2FA page, see PR #15377 on GitHub.
2022-02-17 17:12:59 +08:00
|
|
|
.catch((error) => {
|
|
|
|
const nonce = error.jqXHR?.responseJSON.second_factor_challenge_nonce;
|
|
|
|
if (nonce) {
|
|
|
|
this.router.transitionTo("second-factor-auth", {
|
|
|
|
queryParams: { nonce },
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
popupAjaxError(error);
|
|
|
|
}
|
|
|
|
});
|
2016-12-20 19:26:53 +08:00
|
|
|
},
|
|
|
|
revokeModeration() {
|
2019-05-27 16:15:39 +08:00
|
|
|
return this.model.revokeModeration();
|
2016-12-20 19:26:53 +08:00
|
|
|
},
|
|
|
|
grantModeration() {
|
2019-05-27 16:15:39 +08:00
|
|
|
return this.model.grantModeration();
|
2016-12-20 19:26:53 +08:00
|
|
|
},
|
|
|
|
saveTrustLevel() {
|
2021-01-20 13:04:21 +08:00
|
|
|
return this.model
|
|
|
|
.saveTrustLevel()
|
|
|
|
.then(() => window.location.reload())
|
|
|
|
.catch((e) => {
|
|
|
|
let error;
|
|
|
|
if (e.jqXHR.responseJSON && e.jqXHR.responseJSON.errors) {
|
|
|
|
error = e.jqXHR.responseJSON.errors[0];
|
|
|
|
}
|
|
|
|
error =
|
|
|
|
error ||
|
|
|
|
I18n.t("admin.user.trust_level_change_failed", {
|
|
|
|
error: this._formatError(e),
|
|
|
|
});
|
2022-08-30 01:59:57 +08:00
|
|
|
this.dialog.alert(error);
|
2021-01-20 13:04:21 +08:00
|
|
|
});
|
2016-12-20 19:26:53 +08:00
|
|
|
},
|
|
|
|
restoreTrustLevel() {
|
2019-05-27 16:15:39 +08:00
|
|
|
return this.model.restoreTrustLevel();
|
2016-12-20 19:26:53 +08:00
|
|
|
},
|
|
|
|
lockTrustLevel(locked) {
|
2021-01-20 13:04:21 +08:00
|
|
|
return this.model
|
|
|
|
.lockTrustLevel(locked)
|
|
|
|
.then(() => window.location.reload())
|
|
|
|
.catch((e) => {
|
|
|
|
let error;
|
|
|
|
if (e.jqXHR.responseJSON && e.jqXHR.responseJSON.errors) {
|
|
|
|
error = e.jqXHR.responseJSON.errors[0];
|
|
|
|
}
|
|
|
|
error =
|
|
|
|
error ||
|
|
|
|
I18n.t("admin.user.trust_level_change_failed", {
|
|
|
|
error: this._formatError(e),
|
|
|
|
});
|
2022-08-30 01:59:57 +08:00
|
|
|
this.dialog.alert(error);
|
2021-01-20 13:04:21 +08:00
|
|
|
});
|
2017-11-11 01:18:08 +08:00
|
|
|
},
|
|
|
|
unsilence() {
|
2019-05-27 16:15:39 +08:00
|
|
|
return this.model.unsilence();
|
2017-11-11 01:18:08 +08:00
|
|
|
},
|
|
|
|
silence() {
|
2019-05-27 16:15:39 +08:00
|
|
|
return this.model.silence();
|
2016-12-20 19:26:53 +08:00
|
|
|
},
|
2021-01-20 13:04:21 +08:00
|
|
|
|
2016-12-20 19:26:53 +08:00
|
|
|
anonymize() {
|
2021-01-20 13:04:21 +08:00
|
|
|
const user = this.model;
|
|
|
|
|
|
|
|
const performAnonymize = () => {
|
|
|
|
this.model
|
|
|
|
.anonymize()
|
|
|
|
.then((data) => {
|
|
|
|
if (data.success) {
|
|
|
|
if (data.username) {
|
|
|
|
document.location = getURL(
|
|
|
|
`/admin/users/${user.get("id")}/${data.username}`
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
document.location = getURL("/admin/users/list/active");
|
|
|
|
}
|
|
|
|
} else {
|
2022-08-30 01:59:57 +08:00
|
|
|
this.dialog.alert(I18n.t("admin.user.anonymize_failed"));
|
2021-01-20 13:04:21 +08:00
|
|
|
if (data.user) {
|
|
|
|
user.setProperties(data.user);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
2022-08-30 01:59:57 +08:00
|
|
|
.catch(() =>
|
|
|
|
this.dialog.alert(I18n.t("admin.user.anonymize_failed"))
|
|
|
|
);
|
2021-01-20 13:04:21 +08:00
|
|
|
};
|
|
|
|
|
2022-08-30 01:59:57 +08:00
|
|
|
this.dialog.alert({
|
|
|
|
message: I18n.t("admin.user.anonymize_confirm"),
|
|
|
|
class: "delete-user-modal",
|
|
|
|
buttons: [
|
|
|
|
{
|
|
|
|
icon: "exclamation-triangle",
|
|
|
|
label: I18n.t("admin.user.anonymize_yes"),
|
|
|
|
class: "btn-danger",
|
|
|
|
action: () => performAnonymize(),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: I18n.t("composer.cancel"),
|
|
|
|
},
|
|
|
|
],
|
|
|
|
});
|
2017-12-22 09:18:12 +08:00
|
|
|
},
|
2021-01-20 13:04:21 +08:00
|
|
|
|
2017-12-22 09:18:12 +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-05-25 23:45:42 +08:00
|
|
|
.catch(popupAjaxError);
|
|
|
|
},
|
|
|
|
|
2018-03-05 12:32:23 +08:00
|
|
|
destroy() {
|
|
|
|
const postCount = this.get("model.post_count");
|
2020-03-16 20:51:28 +08:00
|
|
|
const maxPostCount = this.siteSettings.delete_all_posts_max;
|
2021-01-20 13:04:21 +08:00
|
|
|
const location = document.location.pathname;
|
|
|
|
|
|
|
|
const performDestroy = (block) => {
|
2022-08-30 01:59:57 +08:00
|
|
|
this.dialog.notice(I18n.t("admin.user.deleting_user"));
|
2021-01-20 13:04:21 +08:00
|
|
|
let formData = { context: location };
|
|
|
|
if (block) {
|
|
|
|
formData["block_email"] = true;
|
|
|
|
formData["block_urls"] = true;
|
|
|
|
formData["block_ip"] = true;
|
|
|
|
}
|
|
|
|
if (postCount <= maxPostCount) {
|
|
|
|
formData["delete_posts"] = true;
|
|
|
|
}
|
|
|
|
this.model
|
|
|
|
.destroy(formData)
|
|
|
|
.then((data) => {
|
|
|
|
if (data.deleted) {
|
|
|
|
if (/^\/admin\/users\/list\//.test(location)) {
|
|
|
|
document.location = location;
|
|
|
|
} else {
|
|
|
|
document.location = getURL("/admin/users/list/active");
|
|
|
|
}
|
|
|
|
} else {
|
2022-08-30 01:59:57 +08:00
|
|
|
this.dialog.alert(I18n.t("admin.user.delete_failed"));
|
2021-01-20 13:04:21 +08:00
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch(() => {
|
2022-08-30 01:59:57 +08:00
|
|
|
this.dialog.alert(I18n.t("admin.user.delete_failed"));
|
2021-01-20 13:04:21 +08:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2022-08-30 01:59:57 +08:00
|
|
|
this.dialog.alert({
|
2022-09-05 15:31:17 +08:00
|
|
|
title: I18n.t("admin.user.delete_confirm_title"),
|
2022-08-30 01:59:57 +08:00
|
|
|
message: I18n.t("admin.user.delete_confirm"),
|
|
|
|
class: "delete-user-modal",
|
|
|
|
buttons: [
|
|
|
|
{
|
|
|
|
label: I18n.t("admin.user.delete_dont_block"),
|
|
|
|
class: "btn-primary",
|
|
|
|
action: () => {
|
2022-09-05 15:31:17 +08:00
|
|
|
return performDestroy(false);
|
2022-08-30 01:59:57 +08:00
|
|
|
},
|
2021-01-20 13:04:21 +08:00
|
|
|
},
|
2022-08-30 01:59:57 +08:00
|
|
|
{
|
|
|
|
icon: "exclamation-triangle",
|
|
|
|
label: I18n.t("admin.user.delete_and_block"),
|
|
|
|
class: "btn-danger",
|
|
|
|
action: () => {
|
2022-09-05 15:31:17 +08:00
|
|
|
return performDestroy(true);
|
2022-08-30 01:59:57 +08:00
|
|
|
},
|
2021-01-20 13:04:21 +08:00
|
|
|
},
|
2022-08-30 01:59:57 +08:00
|
|
|
{
|
|
|
|
label: I18n.t("composer.cancel"),
|
|
|
|
},
|
|
|
|
],
|
|
|
|
});
|
2018-03-05 12:32:23 +08:00
|
|
|
},
|
|
|
|
|
2020-04-22 16:37:51 +08:00
|
|
|
promptTargetUser() {
|
|
|
|
showModal("admin-merge-users-prompt", {
|
|
|
|
admin: true,
|
|
|
|
model: this.model,
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
showMergeConfirmation(targetUsername) {
|
|
|
|
showModal("admin-merge-users-confirmation", {
|
|
|
|
admin: true,
|
|
|
|
model: {
|
|
|
|
username: this.model.username,
|
2020-04-22 19:10:59 +08:00
|
|
|
targetUsername,
|
2020-04-22 16:37:51 +08:00
|
|
|
},
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
merge(targetUsername) {
|
2021-01-20 13:04:21 +08:00
|
|
|
const user = this.model;
|
|
|
|
const location = document.location.pathname;
|
|
|
|
|
|
|
|
let formData = { context: location };
|
|
|
|
|
|
|
|
if (targetUsername) {
|
|
|
|
formData["target_username"] = targetUsername;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.model
|
|
|
|
.merge(formData)
|
|
|
|
.then((response) => {
|
|
|
|
if (response.success) {
|
|
|
|
showModal("admin-merge-users-progress", {
|
|
|
|
admin: true,
|
|
|
|
model: this.model,
|
|
|
|
});
|
|
|
|
} else {
|
2022-08-30 01:59:57 +08:00
|
|
|
this.dialog.alert(I18n.t("admin.user.merge_failed"));
|
2021-01-20 13:04:21 +08:00
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch(() => {
|
|
|
|
AdminUser.find(user.id).then((u) => user.setProperties(u));
|
2022-08-30 01:59:57 +08:00
|
|
|
this.dialog.alert(I18n.t("admin.user.merge_failed"));
|
2021-01-20 13:04:21 +08:00
|
|
|
});
|
2020-04-22 16:37:51 +08:00
|
|
|
},
|
|
|
|
|
2018-01-27 04:40:03 +08:00
|
|
|
viewActionLogs() {
|
2019-05-27 16:15:39 +08:00
|
|
|
this.adminTools.showActionLogs(this, {
|
2018-01-27 04:40:03 +08:00
|
|
|
target_user: this.get("model.username"),
|
|
|
|
});
|
|
|
|
},
|
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) {
|
2017-04-03 01:35:30 +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" })
|
2017-04-03 01:35:30 +08:00
|
|
|
.catch((e) => {
|
|
|
|
this.set("model.username", oldUsername);
|
|
|
|
popupAjaxError(e);
|
|
|
|
})
|
|
|
|
.finally(() => this.toggleProperty("editingUsername"));
|
|
|
|
},
|
|
|
|
|
2018-12-17 22:28:29 +08:00
|
|
|
saveName(newName) {
|
2017-04-03 01:35:30 +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" })
|
2017-04-03 01:35:30 +08:00
|
|
|
.catch((e) => {
|
|
|
|
this.set("model.name", oldName);
|
|
|
|
popupAjaxError(e);
|
|
|
|
})
|
|
|
|
.finally(() => this.toggleProperty("editingName"));
|
|
|
|
},
|
|
|
|
|
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" })
|
2017-04-03 01:35:30 +08:00
|
|
|
.catch((e) => {
|
2018-12-17 22:28:29 +08:00
|
|
|
this.set("model.title", oldTitle);
|
2017-04-03 01:35:30 +08:00
|
|
|
popupAjaxError(e);
|
|
|
|
})
|
|
|
|
.finally(() => this.toggleProperty("editingTitle"));
|
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() {
|
2020-02-03 21:22:14 +08:00
|
|
|
this.set("customGroupIdsBuffer", this.model.customGroups.mapBy("id"));
|
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, {
|
2014-02-11 05:59:36 +08:00
|
|
|
type: "PUT",
|
2019-01-22 23:44:55 +08:00
|
|
|
data: { primary_group_id: primaryGroupId },
|
2014-02-11 05:59:36 +08:00
|
|
|
})
|
2019-01-22 23:44:55 +08:00
|
|
|
.then(() => this.set("originalPrimaryGroupId", primaryGroupId))
|
2022-08-30 01:59:57 +08:00
|
|
|
.catch(() => this.dialog.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);
|
2013-09-17 02:08:55 +08:00
|
|
|
},
|
2020-09-15 22:00:10 +08:00
|
|
|
|
|
|
|
deleteSSORecord() {
|
2022-08-30 01:59:57 +08:00
|
|
|
return this.dialog.yesNoConfirm({
|
|
|
|
message: I18n.t("admin.user.discourse_connect.confirm_delete"),
|
|
|
|
didConfirm: () => this.model.deleteSSORecord(),
|
|
|
|
});
|
2020-09-15 22:00:10 +08:00
|
|
|
},
|
2020-11-11 03:12:44 +08:00
|
|
|
|
|
|
|
checkSsoEmail() {
|
|
|
|
return ajax(userPath(`${this.model.username_lower}/sso-email.json`), {
|
|
|
|
data: { context: window.location.pathname },
|
|
|
|
}).then((result) => {
|
|
|
|
if (result) {
|
|
|
|
this.set("ssoExternalEmail", result.email);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
2021-02-17 23:57:51 +08:00
|
|
|
|
|
|
|
checkSsoPayload() {
|
|
|
|
return ajax(userPath(`${this.model.username_lower}/sso-payload.json`), {
|
|
|
|
data: { context: window.location.pathname },
|
|
|
|
}).then((result) => {
|
|
|
|
if (result) {
|
|
|
|
this.set("ssoLastPayload", result.payload);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
2021-08-25 12:44:22 +08:00
|
|
|
|
|
|
|
showDeletePostsConfirmation() {
|
|
|
|
showModal("admin-delete-posts-confirmation", {
|
|
|
|
admin: true,
|
|
|
|
model: this.model,
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
deleteAllPosts() {
|
|
|
|
let deletedPosts = 0;
|
|
|
|
let deletedPercentage = 0;
|
|
|
|
const user = this.model;
|
|
|
|
|
|
|
|
const performDelete = (progressModal) => {
|
|
|
|
this.model
|
|
|
|
.deleteAllPosts()
|
|
|
|
.then(({ posts_deleted }) => {
|
|
|
|
if (posts_deleted === 0) {
|
|
|
|
user.set("post_count", 0);
|
|
|
|
progressModal.send("closeModal");
|
|
|
|
} else {
|
|
|
|
deletedPosts += posts_deleted;
|
|
|
|
deletedPercentage = Math.floor(
|
|
|
|
(deletedPosts * 100) / user.get("post_count")
|
|
|
|
);
|
|
|
|
progressModal.setProperties({
|
2021-11-10 07:31:41 +08:00
|
|
|
deletedPercentage,
|
2021-08-25 12:44:22 +08:00
|
|
|
});
|
|
|
|
performDelete(progressModal);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch((e) => {
|
|
|
|
progressModal.send("closeModal");
|
|
|
|
let error;
|
|
|
|
AdminUser.find(user.get("id")).then((u) => user.setProperties(u));
|
|
|
|
error = extractError(e) || I18n.t("admin.user.delete_posts_failed");
|
2022-08-30 01:59:57 +08:00
|
|
|
this.dialog.alert(error);
|
2021-08-25 12:44:22 +08:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const progressModal = showModal("admin-delete-user-posts-progress", {
|
|
|
|
admin: true,
|
|
|
|
});
|
|
|
|
performDelete(progressModal);
|
|
|
|
},
|
2013-09-17 02:08:55 +08:00
|
|
|
},
|
2013-07-09 07:32:16 +08:00
|
|
|
});
|