users_controller.rb 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. class Admin::UsersController < ApplicationController
  2. before_action :authenticate_admin!
  3. before_action :find_user, only: [:edit, :destroy, :update, :deactivate, :activate]
  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. private
  70. def find_user
  71. @user = User.find(params[:id])
  72. end
  73. def resource
  74. @user
  75. end
  76. end