2013-04-12 04:04:20 +08:00
|
|
|
require_dependency 'user_destroyer'
|
2013-06-20 00:11:04 +08:00
|
|
|
require_dependency 'admin_user_index_query'
|
2013-04-12 04:04:20 +08:00
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
class Admin::UsersController < Admin::AdminController
|
|
|
|
|
2013-05-31 23:41:40 +08:00
|
|
|
before_filter :fetch_user, only: [:ban, :unban, :refresh_browsers, :revoke_admin, :grant_admin, :revoke_moderation, :grant_moderation, :approve, :activate, :deactivate, :block, :unblock]
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
def index
|
2013-06-20 00:11:04 +08:00
|
|
|
query = ::AdminUserIndexQuery.new(params)
|
|
|
|
render_serialized(query.find_users, AdminUserSerializer)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def show
|
|
|
|
@user = User.where(username_lower: params[:id]).first
|
2013-03-06 06:02:23 +08:00
|
|
|
raise Discourse::NotFound.new unless @user
|
2013-02-06 03:16:51 +08:00
|
|
|
render_serialized(@user, AdminDetailedUserSerializer, root: false)
|
|
|
|
end
|
|
|
|
|
2013-02-07 15:11:56 +08:00
|
|
|
def delete_all_posts
|
|
|
|
@user = User.where(id: params[:user_id]).first
|
|
|
|
@user.delete_all_posts!(guardian)
|
|
|
|
render nothing: true
|
|
|
|
end
|
2013-04-05 00:59:44 +08:00
|
|
|
|
2013-02-06 03:16:51 +08: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-24 05:37:37 +08:00
|
|
|
render nothing: true
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def revoke_admin
|
2013-05-31 23:41:40 +08:00
|
|
|
guardian.ensure_can_revoke_admin!(@user)
|
|
|
|
@user.revoke_admin!
|
2013-02-06 03:16:51 +08:00
|
|
|
render nothing: true
|
|
|
|
end
|
|
|
|
|
|
|
|
def grant_admin
|
|
|
|
guardian.ensure_can_grant_admin!(@user)
|
2013-05-06 12:49:56 +08:00
|
|
|
@user.grant_admin!
|
2013-02-07 23:45:24 +08:00
|
|
|
render_serialized(@user, AdminUserSerializer)
|
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!
|
2013-02-13 06:58:08 +08:00
|
|
|
render nothing: true
|
|
|
|
end
|
|
|
|
|
|
|
|
def grant_moderation
|
|
|
|
guardian.ensure_can_grant_moderation!(@user)
|
2013-05-06 12:49:56 +08:00
|
|
|
@user.grant_moderation!
|
2013-02-13 06:58:08 +08:00
|
|
|
render_serialized(@user, AdminUserSerializer)
|
|
|
|
end
|
|
|
|
|
2013-02-06 03:16:51 +08: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-08 09:58:34 +08: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 23:41:40 +08:00
|
|
|
def block
|
|
|
|
guardian.ensure_can_block_user! @user
|
|
|
|
SpamRulesEnforcer.punish! @user
|
|
|
|
render nothing: true
|
|
|
|
end
|
|
|
|
|
|
|
|
def unblock
|
|
|
|
guardian.ensure_can_unblock_user! @user
|
|
|
|
SpamRulesEnforcer.clear @user
|
|
|
|
render nothing: true
|
|
|
|
end
|
|
|
|
|
2013-04-12 04:04:20 +08:00
|
|
|
def destroy
|
|
|
|
user = User.where(id: params[:id]).first
|
|
|
|
guardian.ensure_can_delete_user!(user)
|
|
|
|
if UserDestroyer.new(current_user).destroy(user)
|
|
|
|
render json: {deleted: true}
|
|
|
|
else
|
|
|
|
render json: {deleted: false, user: AdminDetailedUserSerializer.new(user, root: false).as_json}
|
|
|
|
end
|
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2013-05-31 23:41:40 +08:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def fetch_user
|
|
|
|
@user = User.where(id: params[:user_id]).first
|
|
|
|
end
|
|
|
|
|
2013-04-12 04:04:20 +08:00
|
|
|
end
|