-
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.
Merge pull request #122 from hathitrust/HT-3054_auth_policies
HT-3054 users with admin ht_users can't admin approval requests
- Loading branch information
Showing
14 changed files
with
139 additions
and
50 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# frozen_string_literal: true | ||
|
||
module Otis | ||
module Authorization | ||
module Resource | ||
def self.included(klass) | ||
klass.extend ClassMethods | ||
end | ||
|
||
module ClassMethods | ||
def to_resource | ||
Checkpoint::Resource::AllOfType.new to_s.underscore.to_sym | ||
end | ||
|
||
def to_resource_name | ||
to_s.underscore.to_sym | ||
end | ||
end | ||
|
||
def to_resource | ||
Checkpoint::Resource.new self | ||
end | ||
|
||
def resource_type | ||
self.class.to_resource_name | ||
end | ||
|
||
def resource_id | ||
id | ||
end | ||
end | ||
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 |
---|---|---|
@@ -1,5 +1,7 @@ | ||
# frozen_string_literal: true | ||
|
||
class ApplicationRecord < ActiveRecord::Base | ||
include Otis::Authorization::Resource | ||
|
||
self.abstract_class = true | ||
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
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 | ||
def can?(action, object, user) | ||
return false if object.nil? | ||
|
||
Checkpoint::Query::ActionPermitted.new(user, action, | ||
object.to_resource, authority: authority).true? | ||
end | ||
|
||
def authority | ||
Services.checkpoint | ||
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,7 @@ | ||
# frozen_string_literal: true | ||
|
||
class HTApprovalRequestsPolicy < ApplicationPolicy | ||
def can?(action, object, user) | ||
super(action, object, user) || super(action, object.try(:ht_user), user) | ||
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
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,24 @@ | ||
# frozen_string_literal: true | ||
|
||
class ApplicationPolicyTest < ActiveSupport::TestCase | ||
def setup | ||
@user = User.new("[email protected]") | ||
@ht_user = create(:ht_user) | ||
agent = Checkpoint::Agent::Token.new("user", @user.id) | ||
view_role = Checkpoint::Credential::Role.new(:view) | ||
res_users = Checkpoint::Resource::AllOfType.new(:ht_user) | ||
Services.checkpoint.grant!(agent, view_role, res_users) | ||
end | ||
|
||
test "view credential permits ht_users index" do | ||
assert ApplicationPolicy.new.can?(:index, HTUser, @user) | ||
end | ||
|
||
test "view credential permits showing a particular HTUser" do | ||
assert ApplicationPolicy.new.can?(:show, @ht_user, @user) | ||
end | ||
|
||
test "view credential forbids deleting a particular HTUser" do | ||
refute ApplicationPolicy.new.can?(:delete, @ht_user, @user) | ||
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,21 @@ | ||
# frozen_string_literal: true | ||
|
||
class HTApprovalRequestsPolicyTest < ActiveSupport::TestCase | ||
def setup | ||
@user = User.new("[email protected]") | ||
@editable_req = create(:ht_approval_request) | ||
@uneditable_req = create(:ht_approval_request) | ||
agent = Checkpoint::Agent::Token.new("user", @user.id) | ||
admin_role = Checkpoint::Credential::Role.new(:admin) | ||
res_user = Checkpoint::Resource.new(@editable_req.ht_user) | ||
Services.checkpoint.grant!(agent, admin_role, res_user) | ||
end | ||
|
||
test "admin credential for HTUser permits editing its HTApprovalRequest" do | ||
assert HTApprovalRequestsPolicy.new.can?(:edit, @editable_req, @user) | ||
end | ||
|
||
test "admin credential for HTUser does not permit editing unrelated HTApprovalRequest" do | ||
refute HTApprovalRequestsPolicy.new.can?(:edit, @uneditable_req, @user) | ||
end | ||
end |