2019-08-21 16:13:39 +02:00
|
|
|
module Admin
|
|
|
|
|
class UsersController < Admin::ApplicationController
|
2019-09-24 21:16:51 +02:00
|
|
|
before_action :default_order
|
2019-08-21 16:13:39 +02:00
|
|
|
|
2019-09-24 21:16:51 +02:00
|
|
|
def default_order
|
|
|
|
|
@order ||= Administrate::Order.new(
|
|
|
|
|
params.fetch(resource_name, {}).fetch(:order, 'updated_at'),
|
|
|
|
|
params.fetch(resource_name, {}).fetch(:direction, 'desc'),
|
|
|
|
|
)
|
|
|
|
|
end
|
2019-08-21 16:13:39 +02:00
|
|
|
|
|
|
|
|
def authenticate_admin
|
2019-08-23 15:58:43 +02:00
|
|
|
unless user_signed_in?
|
2022-06-05 11:40:43 +02:00
|
|
|
flash[:alert] = t('backend.errors.not_logged_in')
|
2019-08-23 15:58:43 +02:00
|
|
|
redirect_to new_user_session_path
|
|
|
|
|
return
|
|
|
|
|
end
|
|
|
|
|
|
2019-08-21 16:13:39 +02:00
|
|
|
unless current_user.admin?
|
2022-06-05 11:40:43 +02:00
|
|
|
flash[:alert] = t('backend.errors.not_enough_privileges')
|
2019-08-21 16:13:39 +02:00
|
|
|
redirect_to root_path
|
|
|
|
|
return
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
# overwrite default create
|
|
|
|
|
def create
|
|
|
|
|
user = User.new(user_params)
|
|
|
|
|
user.skip_confirmation! # automatically confirm user email
|
|
|
|
|
|
|
|
|
|
if user.save
|
2019-08-24 20:04:53 +02:00
|
|
|
flash[:notice] = translate_with_resource('create.success')
|
2019-08-21 16:13:39 +02:00
|
|
|
redirect_to admin_user_path(user)
|
|
|
|
|
else
|
|
|
|
|
render :new, locals: {
|
|
|
|
|
page: Administrate::Page::Form.new(dashboard, user),
|
|
|
|
|
}
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
# overwrite default update
|
|
|
|
|
def update
|
2019-08-29 12:11:18 +02:00
|
|
|
user = User.find(params[:id])
|
2019-08-21 16:13:39 +02:00
|
|
|
|
|
|
|
|
if params[:user][:password].empty?
|
|
|
|
|
user.assign_attributes(user_params.except(:password))
|
|
|
|
|
else
|
|
|
|
|
user.assign_attributes(user_params)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
user.skip_reconfirmation! # automatically reconfirm user email
|
|
|
|
|
|
|
|
|
|
if user.save
|
2019-08-24 20:04:53 +02:00
|
|
|
flash[:notice] = translate_with_resource('update.success')
|
2019-08-21 16:13:39 +02:00
|
|
|
redirect_to admin_user_path(user)
|
|
|
|
|
else
|
|
|
|
|
render :new, locals: {
|
|
|
|
|
page: Administrate::Page::Form.new(dashboard, user),
|
|
|
|
|
}
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
|
|
def user_params
|
|
|
|
|
params.require(:user).permit(:full_name, :email, :role, :password)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|