-
Notifications
You must be signed in to change notification settings - Fork 2
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
Use token list of accounts for user authorization #240
Changes from all commits
b11c0f0
862f874
cde1998
5cb4e8b
a1d1bb6
ad87223
c9ade9b
3dcd64a
dded0e4
af9cafb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# encoding: utf-8 | ||
|
||
class Authorization | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I like this whole class. |
||
attr_accessor :token | ||
attr_reader :token_auth_accounts | ||
|
||
def initialize(token) | ||
@token = token | ||
end | ||
|
||
def id | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I added this because the AuthorizationRepresenter wants an id to build links ... misguided? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what does it want the id for? the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if it is the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. bug in hal gem - PRX/hal_api-rails#8 |
||
default_account.id | ||
end | ||
|
||
def default_account | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. similarly, added this because AuthorizationRepresenter wants to return a default_account There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yeah, makes sense to me |
||
User.find(token.user_id).default_account | ||
end | ||
|
||
def token_auth_accounts | ||
token_ids = token.authorized_resources.try(:keys) | ||
@token_auth_accounts = Account.where(id: token_ids) if token_ids | ||
end | ||
|
||
def token_auth_stories | ||
Story.where(account_id: token_auth_accounts.try(:ids)) unless token_auth_accounts.nil? | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. do you need the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. just doing belt + suspenders! |
||
end | ||
|
||
def token_auth_series | ||
Series.where(account_id: token_auth_accounts.try(:ids)) unless token_auth_accounts.nil? | ||
end | ||
|
||
def authorized?(account) | ||
token_auth_accounts.include?(account) | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
require 'test_helper' | ||
|
||
describe Authorization do | ||
let(:account) { create(:account) } | ||
let(:token) { StubToken.new(account.id, ['member'], 456) } | ||
let(:authorization) { Authorization.new(token) } | ||
let(:unauth_account) { create(:account) } | ||
|
||
it 'has a token' do | ||
authorization.token.wont_be_nil | ||
end | ||
|
||
it 'has a list of accounts authorized on token' do | ||
authorization.token_auth_accounts.must_include account | ||
end | ||
|
||
it 'has lists of stories and series belonging to accounts authorized on token' do | ||
authorization.token_auth_stories.must_equal account.stories | ||
authorization.token_auth_series.must_equal account.series | ||
end | ||
|
||
it 'checks against token to see if accounts are authorized' do | ||
authorization.authorized?(account).must_equal true | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe you could also write this as: just a point of information - this test is great |
||
authorization.authorized?(unauth_account).must_equal false | ||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Line is too long. [109/100]
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks for adding that check - clever to use the
authorized?
method, I was thinking of doing a detect on the account list - this is much better