users_controller.rb 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. class Admin::UsersController < ApplicationController
  2. before_action :authenticate_admin!, except: [:switch_back]
  3. before_action :find_user, only: [:edit, :destroy, :update, :deactivate, :activate, :switch_to_user]
  4. helper_method :resource
  5. def index
  6. @users = User.reorder('created_at DESC').page(params[:page])
  7. respond_to do |format|
  8. format.html
  9. format.json { render json: @users }
  10. end
  11. end
  12. def new
  13. @user = User.new
  14. end
  15. def create
  16. admin = params[:user].delete(:admin)
  17. @user = User.new(params[:user])
  18. @user.requires_no_invitation_code!
  19. @user.admin = admin
  20. respond_to do |format|
  21. if @user.save
  22. DefaultScenarioImporter.import(@user)
  23. format.html { redirect_to admin_users_path, notice: "User '#{@user.username}' was successfully created." }
  24. format.json { render json: @user, status: :ok, location: admin_users_path(@user) }
  25. else
  26. format.html { render action: 'new' }
  27. format.json { render json: @user.errors, status: :unprocessable_entity }
  28. end
  29. end
  30. end
  31. def edit
  32. end
  33. def update
  34. admin = params[:user].delete(:admin)
  35. params[:user].except!(:password, :password_confirmation) if params[:user][:password].blank?
  36. @user.assign_attributes(params[:user])
  37. @user.admin = admin
  38. respond_to do |format|
  39. if @user.save
  40. format.html { redirect_to admin_users_path, notice: "User '#{@user.username}' was successfully updated." }
  41. format.json { render json: @user, status: :ok, location: admin_users_path(@user) }
  42. else
  43. format.html { render action: 'edit' }
  44. format.json { render json: @user.errors, status: :unprocessable_entity }
  45. end
  46. end
  47. end
  48. def destroy
  49. @user.destroy
  50. respond_to do |format|
  51. format.html { redirect_to admin_users_path, notice: "User '#{@user.username}' was deleted." }
  52. format.json { head :no_content }
  53. end
  54. end
  55. def deactivate
  56. @user.deactivate!
  57. respond_to do |format|
  58. format.html { redirect_to admin_users_path, notice: "User '#{@user.username}' was deactivated." }
  59. format.json { render json: @user, status: :ok, location: admin_users_path(@user) }
  60. end
  61. end
  62. def activate
  63. @user.activate!
  64. respond_to do |format|
  65. format.html { redirect_to admin_users_path, notice: "User '#{@user.username}' was activated." }
  66. format.json { render json: @user, status: :ok, location: admin_users_path(@user) }
  67. end
  68. end
  69. # allow an admin to sign-in as any other user
  70. def switch_to_user
  71. if current_user != @user
  72. old_user = current_user
  73. sign_in(:user, @user, { bypass: true })
  74. session[:original_admin_user_id] = old_user.id
  75. end
  76. redirect_to agents_path
  77. end
  78. def switch_back
  79. if session[:original_admin_user_id].present?
  80. sign_in(:user, User.find(session[:original_admin_user_id]), { bypass: true })
  81. session.delete(:original_admin_user_id)
  82. else
  83. redirect_to(root_path, alert: 'You must be an admin acting as a different user to do that.') and return
  84. end
  85. redirect_to admin_users_path
  86. end
  87. private
  88. def find_user
  89. @user = User.find(params[:id])
  90. end
  91. def resource
  92. @user
  93. end
  94. end