2019-05-03 06:17:27 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-10-31 20:02:26 +08:00
|
|
|
class Admin::UsersController < Admin::StaffController
|
2017-08-31 12:06:56 +08:00
|
|
|
before_action :fetch_user,
|
|
|
|
only: %i[
|
|
|
|
suspend
|
2013-11-08 02:53:32 +08:00
|
|
|
unsuspend
|
2014-06-06 11:02:52 +08:00
|
|
|
log_out
|
2024-06-07 15:50:53 +08:00
|
|
|
grant_admin
|
2013-10-23 03:53:08 +08:00
|
|
|
revoke_admin
|
|
|
|
revoke_moderation
|
|
|
|
grant_moderation
|
|
|
|
approve
|
|
|
|
activate
|
|
|
|
deactivate
|
2017-11-11 01:18:08 +08:00
|
|
|
silence
|
|
|
|
unsilence
|
2013-10-23 03:53:08 +08:00
|
|
|
trust_level
|
2014-09-14 04:55:26 +08:00
|
|
|
trust_level_lock
|
2014-07-14 02:11:38 +08:00
|
|
|
add_group
|
|
|
|
remove_group
|
2014-02-11 05:59:36 +08:00
|
|
|
primary_group
|
2016-05-07 01:34:33 +08:00
|
|
|
anonymize
|
2020-04-22 16:37:51 +08:00
|
|
|
merge
|
2017-12-22 09:18:12 +08:00
|
|
|
reset_bounce_score
|
2018-12-15 08:01:35 +08:00
|
|
|
disable_second_factor
|
2020-09-15 22:00:10 +08:00
|
|
|
delete_posts_batch
|
|
|
|
sso_record
|
2024-09-27 20:08:05 +08:00
|
|
|
delete_associated_accounts
|
2020-09-15 22:00:10 +08:00
|
|
|
]
|
2013-05-31 23:41:40 +08:00
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
def index
|
2014-11-03 19:46:08 +08:00
|
|
|
users = ::AdminUserIndexQuery.new(params).find_users
|
|
|
|
|
2024-11-25 16:13:35 +08:00
|
|
|
opts = { include_can_be_deleted: true }
|
2014-11-03 19:46:08 +08:00
|
|
|
if params[:show_emails] == "true"
|
2019-01-18 22:26:44 +08:00
|
|
|
StaffActionLogger.new(current_user).log_show_emails(users, context: request.path)
|
2019-01-12 00:10:02 +08:00
|
|
|
opts[:emails_desired] = true
|
2014-11-03 19:46:08 +08:00
|
|
|
end
|
|
|
|
|
2019-01-12 00:10:02 +08:00
|
|
|
render_serialized(users, AdminUserListSerializer, opts)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def show
|
2015-09-26 21:56:36 +08:00
|
|
|
@user = User.find_by(id: params[:id])
|
2015-05-07 09:00:51 +08:00
|
|
|
raise Discourse::NotFound unless @user
|
2022-12-08 20:42:33 +08:00
|
|
|
|
|
|
|
render_serialized(
|
|
|
|
@user,
|
|
|
|
AdminDetailedUserSerializer,
|
|
|
|
root: false,
|
2024-08-20 20:27:29 +08:00
|
|
|
similar_users_count: @user.similar_users.count,
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def similar_users
|
|
|
|
@user = User.find_by(id: params[:user_id])
|
|
|
|
raise Discourse::NotFound if !@user
|
|
|
|
|
|
|
|
render_json_dump(
|
|
|
|
{
|
|
|
|
users:
|
|
|
|
ActiveModel::ArraySerializer.new(
|
2024-08-22 19:38:56 +08:00
|
|
|
@user.similar_users.limit(User::MAX_SIMILAR_USERS),
|
2024-08-20 20:27:29 +08:00
|
|
|
each_serializer: SimilarAdminUserSerializer,
|
|
|
|
scope: guardian,
|
|
|
|
root: false,
|
|
|
|
),
|
|
|
|
},
|
2022-12-08 20:42:33 +08:00
|
|
|
)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2018-12-14 18:04:18 +08:00
|
|
|
def delete_posts_batch
|
2018-12-15 08:01:35 +08:00
|
|
|
deleted_posts = @user.delete_posts_in_batches(guardian)
|
2018-12-14 18:04:18 +08:00
|
|
|
# staff action logs will have an entry for each post
|
|
|
|
|
|
|
|
render json: { posts_deleted: deleted_posts.length }
|
2013-02-07 15:11:56 +08:00
|
|
|
end
|
2013-04-05 00:59:44 +08:00
|
|
|
|
2018-05-25 23:45:42 +08:00
|
|
|
# DELETE action to delete penalty history for a user
|
|
|
|
def penalty_history
|
|
|
|
# We don't delete any history, we merely remove the action type
|
|
|
|
# with a removed type. It can still be viewed in the logs but
|
|
|
|
# will not affect TL3 promotions.
|
|
|
|
sql = <<~SQL
|
|
|
|
UPDATE user_histories
|
|
|
|
SET action = CASE
|
|
|
|
WHEN action = :silence_user THEN :removed_silence_user
|
|
|
|
WHEN action = :unsilence_user THEN :removed_unsilence_user
|
|
|
|
WHEN action = :suspend_user THEN :removed_suspend_user
|
|
|
|
WHEN action = :unsuspend_user THEN :removed_unsuspend_user
|
|
|
|
END
|
|
|
|
WHERE target_user_id = :user_id
|
|
|
|
AND action IN (
|
|
|
|
:silence_user,
|
|
|
|
:suspend_user,
|
|
|
|
:unsilence_user,
|
|
|
|
:unsuspend_user
|
|
|
|
)
|
|
|
|
SQL
|
|
|
|
|
2018-06-19 14:13:14 +08:00
|
|
|
DB.exec(
|
2018-05-25 23:45:42 +08:00
|
|
|
sql,
|
|
|
|
UserHistory
|
|
|
|
.actions
|
|
|
|
.slice(
|
|
|
|
:silence_user,
|
|
|
|
:suspend_user,
|
|
|
|
:unsilence_user,
|
|
|
|
:unsuspend_user,
|
|
|
|
:removed_silence_user,
|
|
|
|
:removed_unsilence_user,
|
|
|
|
:removed_suspend_user,
|
|
|
|
:removed_unsuspend_user,
|
|
|
|
)
|
|
|
|
.merge(user_id: params[:user_id].to_i),
|
|
|
|
)
|
|
|
|
|
|
|
|
render json: success_json
|
|
|
|
end
|
|
|
|
|
2013-11-08 02:53:32 +08:00
|
|
|
def suspend
|
2024-10-02 22:07:57 +08:00
|
|
|
User::Suspend.call(service_params) do
|
DEV: Replace `params` by the contract object in services
This patch replaces the parameters provided to a service through
`params` by the contract object.
That way, it allows better consistency when accessing input params. For
example, if you have a service without a contract, to access a
parameter, you need to use `params[:my_parameter]`. But with a contract,
you do this through `contract.my_parameter`. Now, with this patch,
you’ll be able to access it through `params.my_parameter` or
`params[:my_parameter]`.
Some methods have been added to the contract object to better mimic a
Hash. That way, when accessing/using `params`, you don’t have to think
too much about it:
- `params.my_key` is also accessible through `params[:my_key]`.
- `params.my_key = value` can also be done through `params[:my_key] =
value`.
- `#slice` and `#merge` are available.
- `#to_hash` has been implemented, so the contract object will be
automatically cast as a hash by Ruby depending on the context. For
example, with an AR model, you can do this: `user.update(**params)`.
2024-10-23 23:57:48 +08:00
|
|
|
on_success do |params:, user:, full_reason:|
|
2024-08-22 19:38:56 +08:00
|
|
|
render_json_dump(
|
|
|
|
suspension: {
|
DEV: Replace `params` by the contract object in services
This patch replaces the parameters provided to a service through
`params` by the contract object.
That way, it allows better consistency when accessing input params. For
example, if you have a service without a contract, to access a
parameter, you need to use `params[:my_parameter]`. But with a contract,
you do this through `contract.my_parameter`. Now, with this patch,
you’ll be able to access it through `params.my_parameter` or
`params[:my_parameter]`.
Some methods have been added to the contract object to better mimic a
Hash. That way, when accessing/using `params`, you don’t have to think
too much about it:
- `params.my_key` is also accessible through `params[:my_key]`.
- `params.my_key = value` can also be done through `params[:my_key] =
value`.
- `#slice` and `#merge` are available.
- `#to_hash` has been implemented, so the contract object will be
automatically cast as a hash by Ruby depending on the context. For
example, with an AR model, you can do this: `user.update(**params)`.
2024-10-23 23:57:48 +08:00
|
|
|
suspend_reason: params.reason,
|
DEV: Stop injecting a service result object in the caller object
Currently, when calling a service with its block form, a `#result`
method is automatically created on the caller object. Even if it never
clashed so far, this could happen.
This patch removes that method, and instead use a more classical way of
doing things: the result object is now provided as an argument to the
main block. This means if we need to access the result object in an
outcome block, it will be done like this from now on:
```ruby
MyService.call(params) do |result|
on_success do
# do something with the result object
do_something(result)
end
end
```
In the same vein, this patch introduces the ability to match keys from
the result object in the outcome blocks, like we already do with step
definitions in a service. For example:
```ruby
on_success do |model:, contract:|
do_something(model, contract)
end
```
Instead of
```ruby
on_success do
do_something(result.model, result.contract)
end
```
2024-10-21 21:37:02 +08:00
|
|
|
full_suspend_reason: full_reason,
|
|
|
|
suspended_till: user.suspended_till,
|
|
|
|
suspended_at: user.suspended_at,
|
2024-08-22 19:38:56 +08:00
|
|
|
suspended_by: BasicUserSerializer.new(current_user, root: false).as_json,
|
|
|
|
},
|
2020-11-03 23:38:56 +08:00
|
|
|
)
|
2024-02-23 03:47:15 +08:00
|
|
|
end
|
2024-08-22 19:38:56 +08:00
|
|
|
on_failed_contract do |contract|
|
|
|
|
render json: failed_json.merge(errors: contract.errors.full_messages), status: 400
|
|
|
|
end
|
2024-09-06 18:56:56 +08:00
|
|
|
on_model_not_found(:user) { raise Discourse::NotFound }
|
|
|
|
on_failed_policy(:not_suspended_already) do |policy|
|
|
|
|
render json: failed_json.merge(message: policy.reason), status: 409
|
|
|
|
end
|
|
|
|
on_failed_policy(:can_suspend_all_users) { raise Discourse::InvalidAccess.new }
|
2017-09-14 02:43:36 +08:00
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2013-11-08 02:53:32 +08:00
|
|
|
def unsuspend
|
|
|
|
guardian.ensure_can_suspend!(@user)
|
|
|
|
@user.suspended_till = nil
|
|
|
|
@user.suspended_at = nil
|
2013-02-06 03:16:51 +08:00
|
|
|
@user.save!
|
2013-11-08 02:53:32 +08:00
|
|
|
StaffActionLogger.new(current_user).log_user_unsuspend(@user)
|
2017-09-14 02:11:33 +08:00
|
|
|
|
2018-11-09 05:32:59 +08:00
|
|
|
DiscourseEvent.trigger(:user_unsuspended, user: @user)
|
|
|
|
|
2019-10-18 07:49:26 +08:00
|
|
|
render_json_dump(suspension: { suspended_till: nil, suspended_at: nil })
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2014-06-06 11:02:52 +08:00
|
|
|
def log_out
|
2014-12-01 21:03:25 +08:00
|
|
|
if @user
|
2017-02-01 06:21:37 +08:00
|
|
|
@user.user_auth_tokens.destroy_all
|
2016-07-04 17:20:30 +08:00
|
|
|
@user.logged_out
|
2014-12-01 21:03:25 +08:00
|
|
|
render json: success_json
|
|
|
|
else
|
|
|
|
render json: { error: I18n.t("admin_js.admin.users.id_not_found") }, status: 404
|
|
|
|
end
|
2014-06-06 11:02:52 +08:00
|
|
|
end
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
def revoke_admin
|
2013-05-31 23:41:40 +08:00
|
|
|
guardian.ensure_can_revoke_admin!(@user)
|
|
|
|
@user.revoke_admin!
|
2016-01-27 17:38:16 +08:00
|
|
|
StaffActionLogger.new(current_user).log_revoke_admin(@user)
|
FIX: Revoking admin or moderator status doesn't require refresh to delete/anonymize/merge user (#14073)
* FIX: Revoking admin or moderator status doesn't require refresh to delete/anonymize/merge user
On the /admin/users/<id>/<username> page, there are action buttons that are either visible or hidden depending on a few fields from the AdminDetailsSerializer: `can_be_deleted`, `can_be_anonymized`, `can_be_merged`, `can_delete_all_posts`.
These fields are updated when granting/revoking admin or moderator status. However, those updates were not being reflected on the page. E.g. if a user is granted moderation privileges, the 'anonymize user' and 'merge' buttons still appear on the page, which is inconsistent with the backend state of the user. It requires refreshing the page to update the state.
This commit fixes that issue, by syncing the client model state with the server state when handling a successful response from the server. Now, when revoking privileges, the buttons automatically appear without refreshing the page. Similarly, when granting moderator privileges, the buttons automatically disappear without refreshing the page.
* Add detailed user response to spec for changed routes.
Add tests to verify that the revoke_moderation, grant_moderation, and revoke_admin routes return a response formatted according to the AdminDetailedUserSerializer.
2021-08-19 09:57:16 +08:00
|
|
|
render_serialized(@user, AdminDetailedUserSerializer, root: false)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def grant_admin
|
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
|
|
|
result = run_second_factor!(SecondFactor::Actions::GrantAdmin)
|
|
|
|
if result.no_second_factors_enabled?
|
2021-09-14 20:19:28 +08:00
|
|
|
render json: success_json.merge(email_confirmation_required: true)
|
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
|
|
|
else
|
|
|
|
render json: success_json
|
2021-09-14 20:19:28 +08:00
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2013-02-13 06:58:08 +08:00
|
|
|
def revoke_moderation
|
2013-05-31 23:41:40 +08:00
|
|
|
guardian.ensure_can_revoke_moderation!(@user)
|
|
|
|
@user.revoke_moderation!
|
2016-01-27 17:38:16 +08:00
|
|
|
StaffActionLogger.new(current_user).log_revoke_moderation(@user)
|
FIX: Revoking admin or moderator status doesn't require refresh to delete/anonymize/merge user (#14073)
* FIX: Revoking admin or moderator status doesn't require refresh to delete/anonymize/merge user
On the /admin/users/<id>/<username> page, there are action buttons that are either visible or hidden depending on a few fields from the AdminDetailsSerializer: `can_be_deleted`, `can_be_anonymized`, `can_be_merged`, `can_delete_all_posts`.
These fields are updated when granting/revoking admin or moderator status. However, those updates were not being reflected on the page. E.g. if a user is granted moderation privileges, the 'anonymize user' and 'merge' buttons still appear on the page, which is inconsistent with the backend state of the user. It requires refreshing the page to update the state.
This commit fixes that issue, by syncing the client model state with the server state when handling a successful response from the server. Now, when revoking privileges, the buttons automatically appear without refreshing the page. Similarly, when granting moderator privileges, the buttons automatically disappear without refreshing the page.
* Add detailed user response to spec for changed routes.
Add tests to verify that the revoke_moderation, grant_moderation, and revoke_admin routes return a response formatted according to the AdminDetailedUserSerializer.
2021-08-19 09:57:16 +08:00
|
|
|
render_serialized(@user, AdminDetailedUserSerializer, root: false)
|
2013-02-13 06:58:08 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def grant_moderation
|
|
|
|
guardian.ensure_can_grant_moderation!(@user)
|
2013-05-06 12:49:56 +08:00
|
|
|
@user.grant_moderation!
|
2016-01-27 17:38:16 +08:00
|
|
|
StaffActionLogger.new(current_user).log_grant_moderation(@user)
|
FIX: Revoking admin or moderator status doesn't require refresh to delete/anonymize/merge user (#14073)
* FIX: Revoking admin or moderator status doesn't require refresh to delete/anonymize/merge user
On the /admin/users/<id>/<username> page, there are action buttons that are either visible or hidden depending on a few fields from the AdminDetailsSerializer: `can_be_deleted`, `can_be_anonymized`, `can_be_merged`, `can_delete_all_posts`.
These fields are updated when granting/revoking admin or moderator status. However, those updates were not being reflected on the page. E.g. if a user is granted moderation privileges, the 'anonymize user' and 'merge' buttons still appear on the page, which is inconsistent with the backend state of the user. It requires refreshing the page to update the state.
This commit fixes that issue, by syncing the client model state with the server state when handling a successful response from the server. Now, when revoking privileges, the buttons automatically appear without refreshing the page. Similarly, when granting moderator privileges, the buttons automatically disappear without refreshing the page.
* Add detailed user response to spec for changed routes.
Add tests to verify that the revoke_moderation, grant_moderation, and revoke_admin routes return a response formatted according to the AdminDetailedUserSerializer.
2021-08-19 09:57:16 +08:00
|
|
|
render_serialized(@user, AdminDetailedUserSerializer, root: false)
|
2013-02-13 06:58:08 +08:00
|
|
|
end
|
|
|
|
|
2014-07-14 02:11:38 +08:00
|
|
|
def add_group
|
|
|
|
group = Group.find(params[:group_id].to_i)
|
2020-04-21 09:50:20 +08:00
|
|
|
raise Discourse::NotFound unless group
|
2021-07-28 19:04:04 +08:00
|
|
|
|
2020-04-21 09:50:20 +08:00
|
|
|
return render_json_error(I18n.t("groups.errors.can_not_modify_automatic")) if group.automatic
|
2021-07-28 19:04:04 +08:00
|
|
|
guardian.ensure_can_edit!(group)
|
2015-10-29 00:21:54 +08:00
|
|
|
|
2016-12-11 23:36:15 +08:00
|
|
|
group.add(@user)
|
|
|
|
GroupActionLogger.new(current_user, group).log_add_user_to_group(@user)
|
2015-10-29 00:21:54 +08:00
|
|
|
|
2017-08-31 12:06:56 +08:00
|
|
|
render body: nil
|
2014-07-14 02:11:38 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def remove_group
|
|
|
|
group = Group.find(params[:group_id].to_i)
|
2020-04-21 09:50:20 +08:00
|
|
|
raise Discourse::NotFound unless group
|
2021-07-28 19:04:04 +08:00
|
|
|
|
2020-04-21 09:50:20 +08:00
|
|
|
return render_json_error(I18n.t("groups.errors.can_not_modify_automatic")) if group.automatic
|
2021-07-28 19:04:04 +08:00
|
|
|
guardian.ensure_can_edit!(group)
|
2017-08-05 00:13:20 +08:00
|
|
|
|
2021-07-28 19:04:04 +08:00
|
|
|
if group.remove(@user)
|
|
|
|
GroupActionLogger.new(current_user, group).log_remove_user_from_group(@user)
|
|
|
|
end
|
2017-08-05 00:13:20 +08:00
|
|
|
|
2017-08-31 12:06:56 +08:00
|
|
|
render body: nil
|
2014-07-14 02:11:38 +08:00
|
|
|
end
|
|
|
|
|
2014-02-11 05:59:36 +08:00
|
|
|
def primary_group
|
2017-08-05 00:13:20 +08:00
|
|
|
if params[:primary_group_id].present?
|
|
|
|
primary_group_id = params[:primary_group_id].to_i
|
|
|
|
if group = Group.find(primary_group_id)
|
2022-11-11 19:06:05 +08:00
|
|
|
guardian.ensure_can_change_primary_group!(@user, group)
|
|
|
|
|
2017-08-05 00:13:20 +08:00
|
|
|
@user.primary_group_id = primary_group_id if group.user_ids.include?(@user.id)
|
|
|
|
end
|
|
|
|
else
|
|
|
|
@user.primary_group_id = nil
|
2017-05-18 00:42:04 +08:00
|
|
|
end
|
2017-08-05 00:13:20 +08:00
|
|
|
|
|
|
|
@user.save!
|
|
|
|
|
2017-08-31 12:06:56 +08:00
|
|
|
render body: nil
|
2014-02-11 05:59:36 +08:00
|
|
|
end
|
|
|
|
|
2013-07-03 16:27:40 +08:00
|
|
|
def trust_level
|
|
|
|
guardian.ensure_can_change_trust_level!(@user)
|
2014-09-30 11:12:33 +08:00
|
|
|
level = params[:level].to_i
|
|
|
|
|
2017-11-24 04:55:44 +08:00
|
|
|
if @user.manual_locked_trust_level.nil?
|
2019-05-07 09:27:05 +08:00
|
|
|
if [0, 1, 2].include?(level) && Promotion.public_send("tl#{level + 1}_met?", @user)
|
2017-11-24 04:55:44 +08:00
|
|
|
@user.manual_locked_trust_level = level
|
|
|
|
@user.save
|
|
|
|
elsif level == 3 && Promotion.tl3_lost?(@user)
|
|
|
|
@user.manual_locked_trust_level = level
|
|
|
|
@user.save
|
|
|
|
end
|
2014-09-30 11:12:33 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
@user.change_trust_level!(level, log_action_for: current_user)
|
2014-06-17 08:46:30 +08:00
|
|
|
|
2013-07-03 16:27:40 +08:00
|
|
|
render_serialized(@user, AdminUserSerializer)
|
2014-07-30 03:54:20 +08:00
|
|
|
rescue Discourse::InvalidAccess => e
|
|
|
|
render_json_error(e.message)
|
2013-07-03 16:27:40 +08:00
|
|
|
end
|
|
|
|
|
2014-09-14 04:55:26 +08:00
|
|
|
def trust_level_lock
|
|
|
|
guardian.ensure_can_change_trust_level!(@user)
|
|
|
|
|
2014-09-30 11:12:33 +08:00
|
|
|
new_lock = params[:locked].to_s
|
2015-05-26 21:16:55 +08:00
|
|
|
return render_json_error I18n.t("errors.invalid_boolean") unless new_lock =~ /true|false/
|
2014-09-14 04:55:26 +08:00
|
|
|
|
2017-11-24 04:55:44 +08:00
|
|
|
@user.manual_locked_trust_level = (new_lock == "true") ? @user.trust_level : nil
|
2014-09-14 04:55:26 +08:00
|
|
|
@user.save
|
2014-09-30 11:12:33 +08:00
|
|
|
|
2017-01-11 05:45:36 +08:00
|
|
|
StaffActionLogger.new(current_user).log_lock_trust_level(@user)
|
2017-11-24 04:55:44 +08:00
|
|
|
Promotion.recalculate(@user, current_user)
|
2014-09-30 11:12:33 +08:00
|
|
|
|
2017-08-31 12:06:56 +08:00
|
|
|
render body: nil
|
2014-09-14 04:55:26 +08:00
|
|
|
end
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
def approve
|
2019-04-17 02:42:47 +08:00
|
|
|
guardian.ensure_can_approve!(@user)
|
|
|
|
|
|
|
|
reviewable =
|
|
|
|
ReviewableUser.find_by(target: @user) ||
|
|
|
|
Jobs::CreateUserReviewable.new.execute(user_id: @user.id).reviewable
|
|
|
|
|
2019-04-17 23:26:43 +08:00
|
|
|
reviewable.perform(current_user, :approve_user)
|
2017-08-31 12:06:56 +08:00
|
|
|
render body: nil
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def approve_bulk
|
2019-04-17 23:26:43 +08:00
|
|
|
Reviewable.bulk_perform_targets(current_user, :approve_user, "ReviewableUser", params[:users])
|
2017-08-31 12:06:56 +08:00
|
|
|
render body: nil
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2013-05-08 09:58:34 +08:00
|
|
|
def activate
|
|
|
|
guardian.ensure_can_activate!(@user)
|
2017-08-01 00:54:09 +08:00
|
|
|
# ensure there is an active email token
|
2021-11-25 15:34:39 +08:00
|
|
|
if !@user.email_tokens.active.exists?
|
|
|
|
@user.email_tokens.create!(email: @user.email, scope: EmailToken.scopes[:signup])
|
2023-01-09 20:20:10 +08:00
|
|
|
end
|
2013-05-08 09:58:34 +08:00
|
|
|
@user.activate
|
2017-01-11 05:45:36 +08:00
|
|
|
StaffActionLogger.new(current_user).log_user_activate(@user, I18n.t("user.activated_by_staff"))
|
2014-12-09 02:16:57 +08:00
|
|
|
render json: success_json
|
2013-05-08 09:58:34 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def deactivate
|
|
|
|
guardian.ensure_can_deactivate!(@user)
|
2019-04-04 00:04:05 +08:00
|
|
|
@user.deactivate(current_user)
|
2018-05-08 10:44:49 +08:00
|
|
|
StaffActionLogger.new(current_user).log_user_deactivate(
|
|
|
|
@user,
|
|
|
|
I18n.t("user.deactivated_by_staff"),
|
|
|
|
params.slice(:context),
|
|
|
|
)
|
2014-04-29 01:46:28 +08:00
|
|
|
refresh_browser @user
|
2019-12-30 23:14:29 +08:00
|
|
|
render json: success_json
|
2013-05-08 09:58:34 +08:00
|
|
|
end
|
|
|
|
|
2017-11-11 01:18:08 +08:00
|
|
|
def silence
|
2024-10-02 22:07:57 +08:00
|
|
|
User::Silence.call(service_params) do
|
DEV: Stop injecting a service result object in the caller object
Currently, when calling a service with its block form, a `#result`
method is automatically created on the caller object. Even if it never
clashed so far, this could happen.
This patch removes that method, and instead use a more classical way of
doing things: the result object is now provided as an argument to the
main block. This means if we need to access the result object in an
outcome block, it will be done like this from now on:
```ruby
MyService.call(params) do |result|
on_success do
# do something with the result object
do_something(result)
end
end
```
In the same vein, this patch introduces the ability to match keys from
the result object in the outcome blocks, like we already do with step
definitions in a service. For example:
```ruby
on_success do |model:, contract:|
do_something(model, contract)
end
```
Instead of
```ruby
on_success do
do_something(result.model, result.contract)
end
```
2024-10-21 21:37:02 +08:00
|
|
|
on_success do |full_reason:, user:|
|
2024-08-22 19:38:56 +08:00
|
|
|
render_json_dump(
|
|
|
|
silence: {
|
|
|
|
silenced: true,
|
DEV: Stop injecting a service result object in the caller object
Currently, when calling a service with its block form, a `#result`
method is automatically created on the caller object. Even if it never
clashed so far, this could happen.
This patch removes that method, and instead use a more classical way of
doing things: the result object is now provided as an argument to the
main block. This means if we need to access the result object in an
outcome block, it will be done like this from now on:
```ruby
MyService.call(params) do |result|
on_success do
# do something with the result object
do_something(result)
end
end
```
In the same vein, this patch introduces the ability to match keys from
the result object in the outcome blocks, like we already do with step
definitions in a service. For example:
```ruby
on_success do |model:, contract:|
do_something(model, contract)
end
```
Instead of
```ruby
on_success do
do_something(result.model, result.contract)
end
```
2024-10-21 21:37:02 +08:00
|
|
|
silence_reason: full_reason,
|
|
|
|
silenced_till: user.silenced_till,
|
|
|
|
silenced_at: user.silenced_at,
|
2024-08-22 19:38:56 +08:00
|
|
|
silenced_by: BasicUserSerializer.new(current_user, root: false).as_json,
|
|
|
|
},
|
2020-11-03 23:38:56 +08:00
|
|
|
)
|
2024-02-23 03:47:15 +08:00
|
|
|
end
|
2024-08-22 19:38:56 +08:00
|
|
|
on_failed_contract do |contract|
|
|
|
|
render json: failed_json.merge(errors: contract.errors.full_messages), status: 400
|
2022-12-08 20:42:33 +08:00
|
|
|
end
|
2024-09-06 18:56:56 +08:00
|
|
|
on_model_not_found(:user) { raise Discourse::NotFound }
|
|
|
|
on_failed_policy(:not_silenced_already) do |policy|
|
|
|
|
render json: failed_json.merge(message: policy.reason), status: 409
|
|
|
|
end
|
|
|
|
on_failed_policy(:can_silence_all_users) { raise Discourse::InvalidAccess.new }
|
2017-11-14 02:41:36 +08:00
|
|
|
end
|
2013-05-31 23:41:40 +08:00
|
|
|
end
|
|
|
|
|
2017-11-11 01:18:08 +08:00
|
|
|
def unsilence
|
|
|
|
guardian.ensure_can_unsilence_user! @user
|
|
|
|
UserSilencer.unsilence(@user, current_user)
|
2017-11-14 02:41:36 +08:00
|
|
|
|
|
|
|
render_json_dump(
|
|
|
|
unsilence: {
|
|
|
|
silenced: false,
|
|
|
|
silence_reason: nil,
|
|
|
|
silenced_till: nil,
|
2018-07-23 06:08:36 +08:00
|
|
|
silenced_at: nil,
|
2017-11-14 02:41:36 +08:00
|
|
|
},
|
|
|
|
)
|
2013-05-31 23:41:40 +08:00
|
|
|
end
|
|
|
|
|
2017-12-22 09:18:12 +08:00
|
|
|
def disable_second_factor
|
2018-02-20 14:44:51 +08:00
|
|
|
guardian.ensure_can_disable_second_factor!(@user)
|
2018-06-28 16:12:32 +08:00
|
|
|
user_second_factor = @user.user_second_factors
|
2020-07-08 03:19:30 +08:00
|
|
|
user_security_key = @user.security_keys
|
|
|
|
raise Discourse::InvalidParameters if user_second_factor.empty? && user_security_key.empty?
|
2018-02-20 14:44:51 +08:00
|
|
|
|
2018-06-28 16:12:32 +08:00
|
|
|
user_second_factor.destroy_all
|
2020-07-08 03:19:30 +08:00
|
|
|
user_security_key.destroy_all
|
2018-02-20 14:44:51 +08:00
|
|
|
StaffActionLogger.new(current_user).log_disable_second_factor_auth(@user)
|
|
|
|
|
2022-02-05 07:43:53 +08:00
|
|
|
Jobs.enqueue(:critical_user_email, type: "account_second_factor_disabled", user_id: @user.id)
|
2018-02-20 14:44:51 +08:00
|
|
|
|
|
|
|
render json: success_json
|
2017-12-22 09:18:12 +08:00
|
|
|
end
|
|
|
|
|
2013-04-12 04:04:20 +08:00
|
|
|
def destroy
|
2014-07-29 01:17:37 +08:00
|
|
|
user = User.find_by(id: params[:id].to_i)
|
2013-04-12 04:04:20 +08:00
|
|
|
guardian.ensure_can_delete_user!(user)
|
2018-05-04 04:18:19 +08:00
|
|
|
|
2022-06-23 05:20:41 +08:00
|
|
|
options = params.slice(:context, :delete_as_spammer)
|
|
|
|
%i[delete_posts block_email block_urls block_ip].each do |param_name|
|
|
|
|
options[param_name] = ActiveModel::Type::Boolean.new.cast(params[param_name])
|
|
|
|
end
|
2019-01-18 23:04:29 +08:00
|
|
|
options[:prepare_for_destroy] = true
|
2018-05-04 04:18:19 +08:00
|
|
|
|
|
|
|
hijack do
|
|
|
|
begin
|
|
|
|
if UserDestroyer.new(current_user).destroy(user, options)
|
|
|
|
render json: { deleted: true }
|
|
|
|
else
|
|
|
|
render json: {
|
|
|
|
deleted: false,
|
|
|
|
user: AdminDetailedUserSerializer.new(user, root: false).as_json,
|
|
|
|
}
|
|
|
|
end
|
|
|
|
rescue UserDestroyer::PostsExistError
|
2018-05-11 03:04:36 +08:00
|
|
|
render json: {
|
|
|
|
deleted: false,
|
2021-06-22 17:29:35 +08:00
|
|
|
message:
|
|
|
|
I18n.t(
|
|
|
|
"user.cannot_delete_has_posts",
|
|
|
|
username: user.username,
|
|
|
|
count: user.posts.joins(:topic).count,
|
|
|
|
),
|
2018-05-23 06:17:44 +08:00
|
|
|
},
|
|
|
|
status: 403
|
2013-07-25 01:48:55 +08:00
|
|
|
end
|
2013-04-12 04:04:20 +08:00
|
|
|
end
|
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2024-11-25 16:13:35 +08:00
|
|
|
def destroy_bulk
|
|
|
|
hijack do
|
|
|
|
User::BulkDestroy.call(service_params) do
|
|
|
|
on_success { render json: { deleted: true } }
|
|
|
|
|
|
|
|
on_failed_contract do |contract|
|
|
|
|
render json: failed_json.merge(errors: contract.errors.full_messages), status: 400
|
|
|
|
end
|
|
|
|
|
|
|
|
on_failed_policy(:can_delete_users) do
|
|
|
|
render json: failed_json.merge(errors: [I18n.t("user.cannot_bulk_delete")]), status: 403
|
|
|
|
end
|
|
|
|
|
|
|
|
on_model_not_found(:users) { render json: failed_json, status: 404 }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-03-19 22:27:21 +08:00
|
|
|
def badges
|
|
|
|
end
|
|
|
|
|
2014-09-25 08:19:26 +08:00
|
|
|
def tl3_requirements
|
2014-01-24 05:40:10 +08:00
|
|
|
end
|
|
|
|
|
2014-07-08 04:18:18 +08:00
|
|
|
def ip_info
|
|
|
|
params.require(:ip)
|
|
|
|
|
2018-10-31 09:38:57 +08:00
|
|
|
render json: DiscourseIpInfo.get(params[:ip], resolve_hostname: true)
|
2014-07-08 04:18:18 +08:00
|
|
|
end
|
2013-05-31 23:41:40 +08:00
|
|
|
|
2014-10-28 08:25:02 +08:00
|
|
|
def sync_sso
|
2021-02-08 18:04:33 +08:00
|
|
|
return render body: nil, status: 404 unless SiteSetting.enable_discourse_connect
|
2014-10-28 08:25:02 +08:00
|
|
|
|
2018-12-07 23:01:44 +08:00
|
|
|
begin
|
2022-01-06 20:28:46 +08:00
|
|
|
sso =
|
|
|
|
DiscourseConnect.parse(
|
|
|
|
"sso=#{params[:sso]}&sig=#{params[:sig]}",
|
|
|
|
secure_session: secure_session,
|
|
|
|
)
|
|
|
|
rescue DiscourseConnect::ParseError
|
2021-02-08 18:04:33 +08:00
|
|
|
return(
|
|
|
|
render json: failed_json.merge(message: I18n.t("discourse_connect.login_error")),
|
|
|
|
status: 422
|
2023-01-09 20:20:10 +08:00
|
|
|
)
|
2018-12-07 23:01:44 +08:00
|
|
|
end
|
2014-10-28 08:25:02 +08:00
|
|
|
|
2016-03-26 13:28:49 +08:00
|
|
|
begin
|
|
|
|
user = sso.lookup_or_create_user
|
2022-03-31 03:22:22 +08:00
|
|
|
DiscourseEvent.trigger(:sync_sso, user)
|
2016-03-26 13:28:49 +08:00
|
|
|
render_serialized(user, AdminDetailedUserSerializer, root: false)
|
|
|
|
rescue ActiveRecord::RecordInvalid => ex
|
|
|
|
render json: failed_json.merge(message: ex.message), status: 403
|
2022-01-06 20:28:46 +08:00
|
|
|
rescue DiscourseConnect::BlankExternalId => ex
|
2021-02-08 18:04:33 +08:00
|
|
|
render json: failed_json.merge(message: I18n.t("discourse_connect.blank_id_error")),
|
|
|
|
status: 422
|
2016-03-26 13:28:49 +08:00
|
|
|
end
|
2014-10-28 08:25:02 +08:00
|
|
|
end
|
|
|
|
|
2014-11-21 02:59:20 +08:00
|
|
|
def delete_other_accounts_with_same_ip
|
|
|
|
params.require(:ip)
|
|
|
|
params.require(:exclude)
|
|
|
|
params.require(:order)
|
|
|
|
|
|
|
|
user_destroyer = UserDestroyer.new(current_user)
|
2017-11-22 22:43:54 +08:00
|
|
|
options = {
|
|
|
|
delete_posts: true,
|
|
|
|
block_email: true,
|
|
|
|
block_urls: true,
|
|
|
|
block_ip: true,
|
|
|
|
delete_as_spammer: true,
|
|
|
|
context: I18n.t("user.destroy_reasons.same_ip_address", ip_address: params[:ip]),
|
|
|
|
}
|
2014-11-21 02:59:20 +08:00
|
|
|
|
2014-11-25 02:34:04 +08:00
|
|
|
AdminUserIndexQuery
|
|
|
|
.new(params)
|
|
|
|
.find_users(50)
|
2018-03-28 16:20:08 +08:00
|
|
|
.each { |user| user_destroyer.destroy(user, options) }
|
2014-11-21 02:59:20 +08:00
|
|
|
|
|
|
|
render json: success_json
|
|
|
|
end
|
|
|
|
|
2014-11-25 02:34:04 +08:00
|
|
|
def total_other_accounts_with_same_ip
|
|
|
|
params.require(:ip)
|
|
|
|
params.require(:exclude)
|
|
|
|
params.require(:order)
|
|
|
|
|
|
|
|
render json: { total: AdminUserIndexQuery.new(params).count_users }
|
|
|
|
end
|
|
|
|
|
2015-03-07 05:44:54 +08:00
|
|
|
def anonymize
|
|
|
|
guardian.ensure_can_anonymize_user!(@user)
|
2020-12-16 04:48:16 +08:00
|
|
|
opts = {}
|
|
|
|
opts[:anonymize_ip] = params[:anonymize_ip] if params[:anonymize_ip].present?
|
|
|
|
|
|
|
|
if user = UserAnonymizer.new(@user, current_user, opts).make_anonymous
|
2015-03-07 05:44:54 +08:00
|
|
|
render json: success_json.merge(username: user.username)
|
|
|
|
else
|
|
|
|
render json:
|
|
|
|
failed_json.merge(user: AdminDetailedUserSerializer.new(user, root: false).as_json)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-04-22 16:37:51 +08:00
|
|
|
def merge
|
|
|
|
target_username = params.require(:target_username)
|
|
|
|
target_user = User.find_by_username(target_username)
|
2020-04-30 23:29:33 +08:00
|
|
|
raise Discourse::NotFound if target_user.blank?
|
2020-04-22 16:37:51 +08:00
|
|
|
|
2020-04-22 20:12:09 +08:00
|
|
|
guardian.ensure_can_merge_users!(@user, target_user)
|
2020-04-22 16:37:51 +08:00
|
|
|
|
2020-12-10 12:52:08 +08:00
|
|
|
Jobs.enqueue(
|
|
|
|
:merge_user,
|
|
|
|
user_id: @user.id,
|
|
|
|
target_user_id: target_user.id,
|
|
|
|
current_user_id: current_user.id,
|
|
|
|
)
|
|
|
|
render json: success_json
|
2020-04-22 16:37:51 +08:00
|
|
|
end
|
|
|
|
|
2016-05-07 01:34:33 +08:00
|
|
|
def reset_bounce_score
|
|
|
|
guardian.ensure_can_reset_bounce_score!(@user)
|
2017-02-20 17:37:01 +08:00
|
|
|
@user.user_stat&.reset_bounce_score!
|
2023-03-21 22:26:26 +08:00
|
|
|
StaffActionLogger.new(current_user).log_reset_bounce_score(@user)
|
2016-05-07 01:34:33 +08:00
|
|
|
render json: success_json
|
|
|
|
end
|
|
|
|
|
2020-09-15 22:00:10 +08:00
|
|
|
def sso_record
|
|
|
|
guardian.ensure_can_delete_sso_record!(@user)
|
|
|
|
@user.single_sign_on_record.destroy!
|
|
|
|
render json: success_json
|
|
|
|
end
|
|
|
|
|
2024-09-27 20:08:05 +08:00
|
|
|
def delete_associated_accounts
|
|
|
|
guardian.ensure_can_delete_user_associated_accounts!(@user)
|
|
|
|
previous_value =
|
|
|
|
@user
|
|
|
|
.user_associated_accounts
|
|
|
|
.select(:provider_name, :provider_uid, :info)
|
|
|
|
.map do |associated_account|
|
|
|
|
{
|
|
|
|
provider: associated_account.provider_name,
|
|
|
|
uid: associated_account.provider_uid,
|
|
|
|
info: associated_account.info,
|
|
|
|
}.to_s
|
|
|
|
end
|
|
|
|
.join(",")
|
|
|
|
StaffActionLogger.new(current_user).log_delete_associated_accounts(
|
|
|
|
@user,
|
|
|
|
previous_value:,
|
|
|
|
context: params[:context],
|
|
|
|
)
|
|
|
|
@user.user_associated_accounts.delete_all
|
|
|
|
render json: success_json
|
|
|
|
end
|
|
|
|
|
2013-05-31 23:41:40 +08:00
|
|
|
private
|
|
|
|
|
|
|
|
def fetch_user
|
2014-05-06 21:41:59 +08:00
|
|
|
@user = User.find_by(id: params[:user_id])
|
2018-12-15 08:01:35 +08:00
|
|
|
raise Discourse::NotFound unless @user
|
2013-05-31 23:41:40 +08:00
|
|
|
end
|
|
|
|
|
2014-04-29 01:46:28 +08:00
|
|
|
def refresh_browser(user)
|
2015-05-04 10:21:00 +08:00
|
|
|
MessageBus.publish "/file-change", ["refresh"], user_ids: [user.id]
|
2014-04-29 01:46:28 +08:00
|
|
|
end
|
2013-04-12 04:04:20 +08:00
|
|
|
end
|