Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor Admin authentication related code #15

Merged
merged 2 commits into from
Nov 5, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions app/controllers/admin/base_controller.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
class Admin::BaseController < ApplicationController
before_action :authenticate_admin!, :set_current_site

helper_method :current_admin, :admin_signed_in?, :current_site
helper_method :current_admin, :current_site, :admin_signed_in?

layout "admin"

private

def current_admin
@current_admin ||= find_current_admin
end
Expand All @@ -26,11 +28,9 @@ def sign_out_admin
end

def authenticate_admin!
admin_not_signed_in unless admin_signed_in?
raise_admin_not_signed_in unless admin_signed_in?
end

private

def find_current_admin
Admin.confirmed.find_by(id: session[:admin_id])
end
Expand All @@ -43,16 +43,16 @@ def after_sign_out_path
admin_root_path
end

def admin_not_signed_in
def raise_admin_not_signed_in
redirect_to(
new_admin_sessions_path,
alert: "We need you to sign in to continue." # TODO. Missing localization.
)
end

def admin_not_authorized
def raise_admin_not_authorized
redirect_to(
request.referrer || root_path,
request.referrer || admin_root_path,
alert: "You are not authorized to perform this action." # TODO. Missing localization.
)
end
Expand Down
4 changes: 2 additions & 2 deletions app/models/admin.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
class Admin < ApplicationRecord
include Authenticable
include Confirmable
include Authentication::Authenticable
include Authentication::Confirmable

EMAIL_ADDRESS_REGEXP = /\A(.+)@(.+\..+)\z/

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module Authenticable
module Authentication::Authenticable
extend ActiveSupport::Concern

included do
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
module Confirmable
module Authentication::Confirmable
extend ActiveSupport::Concern

included do
has_secure_token :confirmation_token

after_commit :send_confirmation_email, on: :create

scope :confirmed, -> { where(confirmation_token: nil) }
end

Expand All @@ -14,4 +18,12 @@ def confirm!

update_columns(confirmation_token: nil)
end

private

def send_confirmation_email
# TODO. Implement confirmation email delivery logic.

true
end
end
6 changes: 6 additions & 0 deletions test/models/admin_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ def test_valid
assert admin.valid?
end

# -- Authentication::Authenticable
def test_password_authentication
assert admin.authenticate("gobierto")
end

# -- Authentication::Confirmable
def test_confirmed_scope
subject = Admin.confirmed

Expand Down