2013-04-11 16:04:20 -04:00
|
|
|
require_dependency 'user_destroyer'
|
2013-06-19 12:11:04 -04:00
|
|
|
require_dependency 'admin_user_index_query'
|
2013-07-03 04:27:40 -04:00
|
|
|
require_dependency 'boost_trust_level'
|
2013-04-11 16:04:20 -04:00
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
class Admin::UsersController < Admin::AdminController
|
|
|
|
|
2013-10-22 15:53:08 -04:00
|
|
|
before_filter :fetch_user, only: [:ban,
|
|
|
|
:unban,
|
|
|
|
:refresh_browsers,
|
|
|
|
:revoke_admin,
|
|
|
|
:grant_admin,
|
|
|
|
:revoke_moderation,
|
|
|
|
:grant_moderation,
|
|
|
|
:approve,
|
|
|
|
:activate,
|
|
|
|
:deactivate,
|
|
|
|
:block,
|
|
|
|
:unblock,
|
|
|
|
:trust_level,
|
|
|
|
:generate_api_key,
|
|
|
|
:revoke_api_key]
|
2013-05-31 11:41:40 -04:00
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
def index
|
2013-06-19 12:11:04 -04:00
|
|
|
query = ::AdminUserIndexQuery.new(params)
|
|
|
|
render_serialized(query.find_users, AdminUserSerializer)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def show
|
|
|
|
@user = User.where(username_lower: params[:id]).first
|
2013-03-05 17:02:23 -05:00
|
|
|
raise Discourse::NotFound.new unless @user
|
2013-02-05 14:16:51 -05:00
|
|
|
render_serialized(@user, AdminDetailedUserSerializer, root: false)
|
|
|
|
end
|
|
|
|
|
2013-02-07 02:11:56 -05:00
|
|
|
def delete_all_posts
|
|
|
|
@user = User.where(id: params[:user_id]).first
|
|
|
|
@user.delete_all_posts!(guardian)
|
|
|
|
render nothing: true
|
|
|
|
end
|
2013-04-04 12:59:44 -04:00
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
def ban
|
|
|
|
guardian.ensure_can_ban!(@user)
|
|
|
|
@user.banned_till = params[:duration].to_i.days.from_now
|
|
|
|
@user.banned_at = DateTime.now
|
|
|
|
@user.save!
|
|
|
|
# TODO logging
|
|
|
|
render nothing: true
|
|
|
|
end
|
|
|
|
|
|
|
|
def unban
|
|
|
|
guardian.ensure_can_ban!(@user)
|
|
|
|
@user.banned_till = nil
|
|
|
|
@user.banned_at = nil
|
|
|
|
@user.save!
|
|
|
|
# TODO logging
|
|
|
|
render nothing: true
|
|
|
|
end
|
|
|
|
|
|
|
|
def refresh_browsers
|
|
|
|
MessageBus.publish "/file-change", ["refresh"], user_ids: [@user.id]
|
2013-03-23 17:37:37 -04:00
|
|
|
render nothing: true
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def revoke_admin
|
2013-05-31 11:41:40 -04:00
|
|
|
guardian.ensure_can_revoke_admin!(@user)
|
|
|
|
@user.revoke_admin!
|
2013-02-05 14:16:51 -05:00
|
|
|
render nothing: true
|
|
|
|
end
|
|
|
|
|
2013-10-22 15:53:08 -04:00
|
|
|
def generate_api_key
|
|
|
|
api_key = @user.generate_api_key(current_user)
|
|
|
|
render_serialized(api_key, ApiKeySerializer)
|
|
|
|
end
|
|
|
|
|
|
|
|
def revoke_api_key
|
|
|
|
@user.revoke_api_key
|
|
|
|
render nothing: true
|
|
|
|
end
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
def grant_admin
|
|
|
|
guardian.ensure_can_grant_admin!(@user)
|
2013-05-06 00:49:56 -04:00
|
|
|
@user.grant_admin!
|
2013-02-07 10:45:24 -05:00
|
|
|
render_serialized(@user, AdminUserSerializer)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2013-02-12 17:58:08 -05:00
|
|
|
def revoke_moderation
|
2013-05-31 11:41:40 -04:00
|
|
|
guardian.ensure_can_revoke_moderation!(@user)
|
|
|
|
@user.revoke_moderation!
|
2013-02-12 17:58:08 -05:00
|
|
|
render nothing: true
|
|
|
|
end
|
|
|
|
|
|
|
|
def grant_moderation
|
|
|
|
guardian.ensure_can_grant_moderation!(@user)
|
2013-05-06 00:49:56 -04:00
|
|
|
@user.grant_moderation!
|
2013-02-12 17:58:08 -05:00
|
|
|
render_serialized(@user, AdminUserSerializer)
|
|
|
|
end
|
|
|
|
|
2013-07-03 04:27:40 -04:00
|
|
|
def trust_level
|
|
|
|
guardian.ensure_can_change_trust_level!(@user)
|
2013-07-23 17:58:26 -04:00
|
|
|
logger = StaffActionLogger.new(current_user)
|
2013-07-08 05:53:22 -04:00
|
|
|
BoostTrustLevel.new(user: @user, level: params[:level], logger: logger).save!
|
2013-07-03 04:27:40 -04:00
|
|
|
render_serialized(@user, AdminUserSerializer)
|
|
|
|
end
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
def approve
|
|
|
|
guardian.ensure_can_approve!(@user)
|
|
|
|
@user.approve(current_user)
|
|
|
|
render nothing: true
|
|
|
|
end
|
|
|
|
|
|
|
|
def approve_bulk
|
|
|
|
User.where(id: params[:users]).each do |u|
|
|
|
|
u.approve(current_user) if guardian.can_approve?(u)
|
|
|
|
end
|
|
|
|
render nothing: true
|
|
|
|
end
|
|
|
|
|
2013-05-07 21:58:34 -04:00
|
|
|
def activate
|
|
|
|
guardian.ensure_can_activate!(@user)
|
|
|
|
@user.activate
|
|
|
|
render nothing: true
|
|
|
|
end
|
|
|
|
|
|
|
|
def deactivate
|
|
|
|
guardian.ensure_can_deactivate!(@user)
|
|
|
|
@user.deactivate
|
|
|
|
render nothing: true
|
|
|
|
end
|
|
|
|
|
2013-05-31 11:41:40 -04:00
|
|
|
def block
|
|
|
|
guardian.ensure_can_block_user! @user
|
2013-07-02 14:42:30 -04:00
|
|
|
UserBlocker.block(@user, current_user)
|
2013-05-31 11:41:40 -04:00
|
|
|
render nothing: true
|
|
|
|
end
|
|
|
|
|
|
|
|
def unblock
|
|
|
|
guardian.ensure_can_unblock_user! @user
|
2013-07-02 14:42:30 -04:00
|
|
|
UserBlocker.unblock(@user, current_user)
|
2013-05-31 11:41:40 -04:00
|
|
|
render nothing: true
|
|
|
|
end
|
|
|
|
|
2013-08-16 11:42:24 -04:00
|
|
|
def reject_bulk
|
|
|
|
d = UserDestroyer.new(current_user)
|
|
|
|
success_count = 0
|
|
|
|
User.where(id: params[:users]).each do |u|
|
|
|
|
success_count += 1 if guardian.can_delete_user?(u) and d.destroy(u, params.slice(:context)) rescue UserDestroyer::PostsExistError
|
|
|
|
end
|
|
|
|
render json: {success: success_count, failed: (params[:users].try(:size) || 0) - success_count}
|
|
|
|
end
|
|
|
|
|
2013-04-11 16:04:20 -04:00
|
|
|
def destroy
|
|
|
|
user = User.where(id: params[:id]).first
|
|
|
|
guardian.ensure_can_delete_user!(user)
|
2013-07-24 13:48:55 -04:00
|
|
|
begin
|
2013-10-21 14:49:51 -04:00
|
|
|
if UserDestroyer.new(current_user).destroy(user, params.slice(:delete_posts, :block_email, :block_urls, :block_ip, :context))
|
2013-07-24 13:48:55 -04:00
|
|
|
render json: {deleted: true}
|
|
|
|
else
|
|
|
|
render json: {deleted: false, user: AdminDetailedUserSerializer.new(user, root: false).as_json}
|
|
|
|
end
|
|
|
|
rescue UserDestroyer::PostsExistError
|
|
|
|
raise Discourse::InvalidAccess.new("User #{user.username} has #{user.post_count} posts, so can't be deleted.")
|
2013-04-11 16:04:20 -04:00
|
|
|
end
|
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-05-31 11:41:40 -04:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def fetch_user
|
|
|
|
@user = User.where(id: params[:user_id]).first
|
|
|
|
end
|
|
|
|
|
2013-04-11 16:04:20 -04:00
|
|
|
end
|