Skip to content

Commit

Permalink
Add unauthorized_entity to authenticate_for's default for called by c…
Browse files Browse the repository at this point in the history
…allback (nsarno#228)

- Add unauthorized_entity to authenticate_for's default

- Remove unused argument from unauthorized_entity
  • Loading branch information
sakuraineed committed Oct 28, 2018
1 parent 749ce01 commit 1550a0b
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 6 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
All notable changes to this project will be documented in this file.
This project adheres to [Semantic Versioning](http://semver.org/).

## Unreleased
### Fixed
- Add unauthorized_entity to authenticate_for's default for called by callback

## [2.1.1] - 2017-02-11
### Fixed
- Stop trying to retrieve user from empty payload when no token is given
Expand Down
14 changes: 9 additions & 5 deletions lib/knock/authenticable.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
module Knock::Authenticable
def authenticate_for entity_class
def authenticate_for entity_class, by_callback: true
getter_name = "current_#{entity_class.to_s.parameterize.underscore}"
define_current_entity_getter(entity_class, getter_name)
public_send(getter_name)
if by_callback
unauthorized_entity unless public_send(getter_name)
else
public_send(getter_name)
end
end

private
Expand All @@ -15,7 +19,7 @@ def method_missing(method, *args)
prefix, entity_name = method.to_s.split('_', 2)
case prefix
when 'authenticate'
unauthorized_entity(entity_name) unless authenticate_entity(entity_name)
unauthorized_entity unless authenticate_entity(entity_name)
when 'current'
authenticate_entity(entity_name)
else
Expand All @@ -26,11 +30,11 @@ def method_missing(method, *args)
def authenticate_entity(entity_name)
if token
entity_class = entity_name.camelize.constantize
send(:authenticate_for, entity_class)
send(:authenticate_for, entity_class, by_callback: false)
end
end

def unauthorized_entity(entity_name)
def unauthorized_entity
head(:unauthorized)
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def index

private

def unauthorized_entity(entity)
def unauthorized_entity
head :not_found
end
end

0 comments on commit 1550a0b

Please sign in to comment.