2016-09-08 10:25:33 -05:00
|
|
|
# frozen_string_literal: true
|
2016-08-17 17:49:09 -05:00
|
|
|
module Admin
|
|
|
|
class UserController < AdminController
|
|
|
|
def index
|
2016-09-20 14:22:20 -05:00
|
|
|
@users = policy_scope User.order(:name)
|
2016-08-17 17:49:09 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def new
|
2016-08-18 15:35:17 -05:00
|
|
|
@user = User.new
|
2016-09-20 14:22:20 -05:00
|
|
|
authorize @user
|
2016-08-17 17:49:09 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
2016-09-20 14:22:20 -05:00
|
|
|
authorize User
|
2016-08-18 15:35:17 -05:00
|
|
|
default_passwd = SecureRandom.urlsafe_base64(12)
|
|
|
|
@user = User.create({ password: default_passwd }.merge(user_params.to_h))
|
|
|
|
|
|
|
|
if @user.persisted?
|
2016-09-14 14:38:26 -05:00
|
|
|
UserMailer.welcome(@user, default_passwd).deliver_later
|
2016-08-24 12:15:12 -05:00
|
|
|
redirect_to admin_users_path, flash: { success: "Sucessfully created user #{@user.name}" }
|
2016-08-18 15:35:17 -05:00
|
|
|
else
|
|
|
|
flash[:error] = "Failed to save user."
|
|
|
|
render :new
|
|
|
|
end
|
2016-08-17 17:49:09 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def view
|
2016-08-18 15:35:17 -05:00
|
|
|
@user = User.find(params[:user_id])
|
2016-09-20 14:22:20 -05:00
|
|
|
authorize @user
|
2016-08-17 17:49:09 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def edit
|
2016-08-18 15:35:17 -05:00
|
|
|
@user = User.find(params[:user_id])
|
2016-09-20 14:22:20 -05:00
|
|
|
authorize @user
|
2016-08-17 17:49:09 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
2016-08-18 15:35:17 -05:00
|
|
|
@user = User.find(params[:user_id])
|
2016-09-20 14:22:20 -05:00
|
|
|
authorize @user
|
2016-08-18 15:35:17 -05:00
|
|
|
|
|
|
|
if @user.update_attributes(user_params)
|
|
|
|
redirect_to admin_user_path(@user.to_i),
|
2016-08-24 12:15:12 -05:00
|
|
|
flash: { success: "Sucessfully updated #{@user.name}" }
|
2016-08-18 15:35:17 -05:00
|
|
|
else
|
|
|
|
flash[:error] = "Failed to update user."
|
|
|
|
render :edit
|
|
|
|
end
|
2016-08-17 17:49:09 -05:00
|
|
|
end
|
2016-08-18 15:35:17 -05:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def user_params
|
2016-09-15 16:11:44 -05:00
|
|
|
params.require(:user).permit(:name, :email, :role, :password, quiz_ids: [])
|
2016-08-18 15:35:17 -05:00
|
|
|
end
|
2016-08-17 17:49:09 -05:00
|
|
|
end
|
|
|
|
end
|