Skip to content

Commit

Permalink
Simplify the check for forbidden access
Browse files Browse the repository at this point in the history
This consolidates two separate `before_filter`s that both rendered the same
failure notice into a single callback method. Since we depend on the token to
find the user anyway, we'll only need to check if the user exists.
  • Loading branch information
mxie committed Jun 12, 2015
1 parent b9700d1 commit 8bdc82d
Showing 1 changed file with 8 additions and 16 deletions.
24 changes: 8 additions & 16 deletions app/controllers/clearance/passwords_controller.rb
Expand Up @@ -3,8 +3,7 @@
class Clearance::PasswordsController < Clearance::BaseController
skip_before_filter :require_login, only: [:create, :edit, :new, :update]
skip_before_filter :authorize, only: [:create, :edit, :new, :update]
before_filter :forbid_missing_token, only: [:edit, :update]
before_filter :forbid_non_existent_user, only: [:edit, :update]
before_filter :ensure_existing_user, only: [:edit, :update]

def create
if user = find_user_for_create
Expand Down Expand Up @@ -76,6 +75,13 @@ def find_user_for_update
find_user_by_id_and_confirmation_token
end

def ensure_existing_user
unless find_user_by_id_and_confirmation_token
flash_failure_when_forbidden
render template: "passwords/new"
end
end

def flash_failure_when_forbidden
flash.now[:notice] = translate(:forbidden,
scope: [:clearance, :controllers, :passwords],
Expand All @@ -88,20 +94,6 @@ def flash_failure_after_update
default: t('flashes.failure_after_update'))
end

def forbid_missing_token
if params[:token].to_s.blank?
flash_failure_when_forbidden
render template: 'passwords/new'
end
end

def forbid_non_existent_user
unless find_user_by_id_and_confirmation_token
flash_failure_when_forbidden
render template: 'passwords/new'
end
end

def url_after_create
sign_in_url
end
Expand Down

0 comments on commit 8bdc82d

Please sign in to comment.