-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Pundit authorizations for users (#18)
* add panoptes_client and require login when querying for /user/<id> * update faraday to get panoptes-client.rb to work. remove typo on require_login on app controller * add policy to check if current user(i.e. querying user) can view stats of the queried user * Update application_controller.rb * Update user_classification_count_controller.rb * fix user_classification_count specs to take care of authorizatons * add spec for unauthorized user * remove unneeded scopes in app policies * add specs for policies * update missing token specs * adding specs for missing headers/token authenticaton * update typo * remove parens from queried_user policy and remove logged_in? method on application_policy * rename specs for logged in user * Update README.md to link to repo wiki * typo on application_controller user client had hard coded env, removing wrapper object on pundit policy in favor of headless policy, sending param to current_user, * remove error raising in application policy for cases when we expect user to be nil * remove spec that checks if error raised if user nil on application policy to allow more open scope
- Loading branch information
1 parent
7a5a261
commit dc50012
Showing
13 changed files
with
284 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# frozen_string_literal: true | ||
|
||
class ApplicationPolicy | ||
attr_reader :user, :record | ||
|
||
def initialize(user, record) | ||
@user = user | ||
@record = record | ||
end | ||
|
||
def panoptes_admin? | ||
user['admin'] == true | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# frozen_string_literal: true | ||
|
||
class QueriedUserContextPolicy < ApplicationPolicy | ||
attr_reader :user | ||
|
||
def initialize(user, _record) | ||
super | ||
@user = user | ||
end | ||
|
||
def show? | ||
current_user_is_queried_user? || panoptes_admin? | ||
end | ||
|
||
def current_user_is_queried_user? | ||
user['id']&.to_i == user['queried_user_id']&.to_i | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe ApplicationPolicy, type: :policy do | ||
let(:records) { [] } | ||
let(:user) { | ||
{ | ||
'id' => '1234', | ||
'login' => 'login', | ||
'display_name' => 'display_name' | ||
} | ||
} | ||
let(:policy) { ApplicationPolicy.new user, records } | ||
|
||
context 'with a user' do | ||
it 'sets panoptes_admin? to be false if user is not a panoptes admin' do | ||
expect(policy.panoptes_admin?).to be false | ||
end | ||
|
||
it 'sets panoptes_admin? to true if user is panoptes admin' do | ||
user['admin'] = true | ||
ApplicationPolicy.new user, records | ||
expect(policy.panoptes_admin?).to be true | ||
end | ||
end | ||
end |
Oops, something went wrong.