1
0

users_controller.rb 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. @user = User.new(user_params)
  17. @user.requires_no_invitation_code!
  18. respond_to do |format|
  19. if @user.save
  20. DefaultScenarioImporter.import(@user)
  21. format.html { redirect_to admin_users_path, notice: "User '#{@user.username}' was successfully created." }
  22. format.json { render json: @user, status: :ok, location: admin_users_path(@user) }
  23. else
  24. format.html { render action: 'new' }
  25. format.json { render json: @user.errors, status: :unprocessable_entity }
  26. end
  27. end
  28. end
  29. def edit
  30. end
  31. def update
  32. params[:user].extract!(:password, :password_confirmation) if params[:user][:password].blank?
  33. @user.assign_attributes(user_params)
  34. respond_to do |format|
  35. if @user.save
  36. format.html { redirect_to admin_users_path, notice: "User '#{@user.username}' was successfully updated." }
  37. format.json { render json: @user, status: :ok, location: admin_users_path(@user) }
  38. else
  39. format.html { render action: 'edit' }
  40. format.json { render json: @user.errors, status: :unprocessable_entity }
  41. end
  42. end
  43. end
  44. def destroy
  45. @user.destroy
  46. respond_to do |format|
  47. format.html { redirect_to admin_users_path, notice: "User '#{@user.username}' was deleted." }
  48. format.json { head :no_content }
  49. end
  50. end
  51. def deactivate
  52. @user.deactivate!
  53. respond_to do |format|
  54. format.html { redirect_to admin_users_path, notice: "User '#{@user.username}' was deactivated." }
  55. format.json { render json: @user, status: :ok, location: admin_users_path(@user) }
  56. end
  57. end
  58. def activate
  59. @user.activate!
  60. respond_to do |format|
  61. format.html { redirect_to admin_users_path, notice: "User '#{@user.username}' was activated." }
  62. format.json { render json: @user, status: :ok, location: admin_users_path(@user) }
  63. end
  64. end
  65. # allow an admin to sign-in as any other user
  66. def switch_to_user
  67. if current_user != @user
  68. old_user = current_user
  69. bypass_sign_in(@user)
  70. session[:original_admin_user_id] = old_user.id
  71. end
  72. redirect_to agents_path
  73. end
  74. def switch_back
  75. if session[:original_admin_user_id].present?
  76. bypass_sign_in(User.find(session[:original_admin_user_id]))
  77. session.delete(:original_admin_user_id)
  78. else
  79. redirect_to(root_path, alert: 'You must be an admin acting as a different user to do that.') and return
  80. end
  81. redirect_to admin_users_path
  82. end
  83. private
  84. def user_params
  85. params.require(:user).permit(:email, :username, :password, :password_confirmation, :admin)
  86. end
  87. def find_user
  88. @user = User.find(params[:id])
  89. end
  90. def resource
  91. @user
  92. end
  93. end