reduced code complexity

This commit is contained in:
Hauke Klement
2015-02-23 17:36:22 +01:00
parent ded7825de8
commit f3c5d17662

View File

@ -8,17 +8,7 @@ class InternalUsersController < ApplicationController
skip_after_action :verify_authorized, only: [:activate, :forgot_password, :reset_password] skip_after_action :verify_authorized, only: [:activate, :forgot_password, :reset_password]
def activate def activate
if request.patch? || request.put? set_up_password if request.patch? || request.put?
respond_to do |format|
if @user.update(params[:internal_user].permit(:password, :password_confirmation))
@user.activate!
format.html { redirect_to(sign_in_path, notice: t('.success')) }
format.json { render(nothing: true, status: :ok) }
else
respond_with_invalid_object(format, object: @user, template: :activate)
end
end
end
end end
def authorize! def authorize!
@ -26,6 +16,19 @@ class InternalUsersController < ApplicationController
end end
private :authorize! private :authorize!
def change_password
respond_to do |format|
if @user.update(params[:internal_user].permit(:password, :password_confirmation))
@user.change_password!(params[:internal_user][:password])
format.html { redirect_to(sign_in_path, notice: t('.success')) }
format.json { render(nothing: true, status: :ok) }
else
respond_with_invalid_object(format, object: @user, template: :reset_password)
end
end
end
private :change_password
def create def create
@user = InternalUser.new(internal_user_params) @user = InternalUser.new(internal_user_params)
authorize! authorize!
@ -33,6 +36,15 @@ class InternalUsersController < ApplicationController
create_and_respond(object: @user) { @user.send(:send_activation_needed_email!) } create_and_respond(object: @user) { @user.send(:send_activation_needed_email!) }
end end
def deliver_reset_password_instructions
if params[:email].present?
InternalUser.find_by(email: params[:email]).try(:deliver_reset_password_instructions!)
flash[:notice] = t('.success')
redirect_to(:root)
end
end
private :deliver_reset_password_instructions
def destroy def destroy
destroy_and_respond(object: @user) destroy_and_respond(object: @user)
end end
@ -41,15 +53,10 @@ class InternalUsersController < ApplicationController
end end
def forgot_password def forgot_password
if request.get? && current_user if request.get?
flash[:warning] = t('shared.already_signed_in') render_forgot_password_form
redirect_to(:root)
elsif request.post? elsif request.post?
if params[:email].present? deliver_reset_password_instructions
InternalUser.find_by(email: params[:email]).try(:deliver_reset_password_instructions!)
flash[:notice] = t('.success')
redirect_to(:root)
end
end end
end end
@ -69,6 +76,14 @@ class InternalUsersController < ApplicationController
authorize! authorize!
end end
def render_forgot_password_form
if current_user
flash[:warning] = t('shared.already_signed_in')
redirect_to(:root)
end
end
private :render_forgot_password_form
def require_activation_token def require_activation_token
require_token(:activation) require_token(:activation)
end end
@ -86,18 +101,21 @@ class InternalUsersController < ApplicationController
private :require_token private :require_token
def reset_password def reset_password
if request.patch? || request.put? change_password if request.patch? || request.put?
respond_to do |format| end
if @user.update(params[:internal_user].permit(:password, :password_confirmation))
@user.change_password!(params[:internal_user][:password]) def set_up_password
format.html { redirect_to(sign_in_path, notice: t('.success')) } respond_to do |format|
format.json { render(nothing: true, status: :ok) } if @user.update(params[:internal_user].permit(:password, :password_confirmation))
else @user.activate!
respond_with_invalid_object(format, object: @user, template: :reset_password) format.html { redirect_to(sign_in_path, notice: t('.success')) }
end format.json { render(nothing: true, status: :ok) }
else
respond_with_invalid_object(format, object: @user, template: :activate)
end end
end end
end end
private :set_up_password
def set_user def set_user
@user = InternalUser.find(params[:id]) @user = InternalUser.find(params[:id])