2013-04-11 16:04:20 -04:00
|
|
|
require_dependency 'user_destroyer'
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
class Admin::UsersController < Admin::AdminController
|
|
|
|
|
|
|
|
def index
|
|
|
|
# Sort order
|
|
|
|
if params[:query] == "active"
|
2013-02-10 17:11:02 -05:00
|
|
|
@users = User.order("COALESCE(last_seen_at, to_date('1970-01-01', 'YYYY-MM-DD')) DESC, username")
|
2013-02-05 14:16:51 -05:00
|
|
|
else
|
|
|
|
@users = User.order("created_at DESC, username")
|
2013-02-07 10:45:24 -05:00
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-04-23 10:29:18 -04:00
|
|
|
if ['newuser', 'basic', 'regular', 'leader', 'elder'].include?(params[:query])
|
|
|
|
@users = @users.where('trust_level = ?', TrustLevel.levels[params[:query].to_sym])
|
|
|
|
end
|
|
|
|
|
2013-04-23 12:07:58 -04:00
|
|
|
@users = @users.where('admin = ?', true) if params[:query] == 'admins'
|
|
|
|
@users = @users.where('moderator = ?', true) if params[:query] == 'moderators'
|
|
|
|
@users = @users.where('approved = false') if params[:query] == 'pending'
|
2013-02-05 14:16:51 -05:00
|
|
|
@users = @users.where('username_lower like :filter or email like :filter', filter: "%#{params[:filter]}%") if params[:filter].present?
|
|
|
|
@users = @users.take(100)
|
|
|
|
render_serialized(@users, AdminUserSerializer)
|
|
|
|
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
|
|
|
|
@user = User.where(id: params[:user_id]).first
|
|
|
|
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
|
|
|
|
@user = User.where(id: params[:user_id]).first
|
|
|
|
guardian.ensure_can_ban!(@user)
|
|
|
|
@user.banned_till = nil
|
|
|
|
@user.banned_at = nil
|
|
|
|
@user.save!
|
|
|
|
# TODO logging
|
|
|
|
render nothing: true
|
|
|
|
end
|
|
|
|
|
|
|
|
def refresh_browsers
|
|
|
|
@user = User.where(id: params[:user_id]).first
|
|
|
|
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
|
|
|
|
@admin = User.where(id: params[:user_id]).first
|
|
|
|
guardian.ensure_can_revoke_admin!(@admin)
|
2013-05-06 00:49:56 -04:00
|
|
|
@admin.revoke_admin!
|
2013-02-05 14:16:51 -05:00
|
|
|
render nothing: true
|
|
|
|
end
|
|
|
|
|
|
|
|
def grant_admin
|
|
|
|
@user = User.where(id: params[:user_id]).first
|
|
|
|
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
|
|
|
|
@moderator = User.where(id: params[:user_id]).first
|
|
|
|
guardian.ensure_can_revoke_moderation!(@moderator)
|
2013-05-06 00:49:56 -04:00
|
|
|
@moderator.revoke_moderation!
|
2013-02-12 17:58:08 -05:00
|
|
|
render nothing: true
|
|
|
|
end
|
|
|
|
|
|
|
|
def grant_moderation
|
|
|
|
@user = User.where(id: params[:user_id]).first
|
|
|
|
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-02-05 14:16:51 -05:00
|
|
|
def approve
|
|
|
|
@user = User.where(id: params[:user_id]).first
|
|
|
|
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
|
|
|
|
@user = User.where(id: params[:user_id]).first
|
|
|
|
guardian.ensure_can_activate!(@user)
|
|
|
|
@user.activate
|
|
|
|
render nothing: true
|
|
|
|
end
|
|
|
|
|
|
|
|
def deactivate
|
|
|
|
@user = User.where(id: params[:user_id]).first
|
|
|
|
guardian.ensure_can_deactivate!(@user)
|
|
|
|
@user.deactivate
|
|
|
|
render nothing: true
|
|
|
|
end
|
|
|
|
|
2013-04-11 16:04:20 -04: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-05 14:16:51 -05:00
|
|
|
|
2013-04-11 16:04:20 -04:00
|
|
|
end
|