Skip to content

Commit

Permalink
Add #agent_key & .from_agent_key to Group & User
Browse files Browse the repository at this point in the history
These methods help reduce the amount of knowledge that ACLs require
regarding agents.

For Users, the default implementation is just to use `#user_key` and
`User.find_by_user_key`.

(An alternate approach would be just to implement `#user_key` and
`.find_by_user_key` directly on `Hyrax::Group`, but I’m not sure we
want Groups to be treatable as Users in all contexts where `#user_key`
might be used.)
  • Loading branch information
marrus-sh authored and tamsin johnson committed Jun 29, 2022
1 parent 0ab09c7 commit e5c1de5
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 9 deletions.
11 changes: 11 additions & 0 deletions app/models/concerns/hyrax/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,13 @@ def user_key
public_send(self.class.user_key_field)
end

##
# @return [String] a local identifier for this user; for use (e.g.) in ACL
# data
def agent_key
user_key
end

# Look for, in order:
# A cached version of the agent
# A non-cached version (direct read of the database)
Expand Down Expand Up @@ -177,6 +184,10 @@ def find_or_create_system_user(user_key)
User.find_by_user_key(user_key) || User.create!(user_key_field => user_key, password: Devise.friendly_token[0, 20])
end

def from_agent_key(key)
User.find_by_user_key(key)
end

def from_url_component(component)
User.find_by_user_key(component.gsub(/-dot-/, '.'))
end
Expand Down
8 changes: 3 additions & 5 deletions app/models/hyrax/group.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def self.name_prefix

##
# @return [Hyrax::Group]
def self.from_key(key)
def self.from_agent_key(key)
new(key.delete_prefix(name_prefix))
end

Expand All @@ -22,17 +22,15 @@ def initialize(name)
##
# @return [Boolean]
def ==(other)
other.class == self.class &&
other.name == self.name
other.class == self.class && other.name == name
end

##
# @return [String] a local identifier for this group; for use (e.g.) in ACL
# data
def group_key
def agent_key
self.class.name_prefix + name
end
alias user_key group_key

def to_sipity_agent
sipity_agent || create_sipity_agent!
Expand Down
8 changes: 7 additions & 1 deletion app/services/hyrax/access_control_list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -200,8 +200,14 @@ def initialize(acl, mode)

private

##
# Returns the identifier used by ACLs to identify agents.
#
# This defaults to the `:agent_key`, but if that method doesn’t exist,
# `:user_key` will be used as a fallback.
def id_for(agent:)
agent.user_key.to_s
key = agent.try(:agent_key) || agent.user_key
key.to_s
end
end

Expand Down
12 changes: 9 additions & 3 deletions spec/models/hyrax/group_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
let(:name) { 'etaoin' }
let(:group) { described_class.new(name) }

describe '.from_key' do
describe '.from_agent_key' do
it 'returns an equivalent group' do
expect(Hyrax::Group.from_key(Hyrax::Group.name_prefix + group.name)).to eq group
expect(described_class.from_agent_key(group.agent_key)).to eq group
end
end

describe '#==' do
let (:other_group) { described_class.new(group.name) }
let(:other_group) { described_class.new(group.name) }

it 'correctly determines equality for equivalent groups' do
expect(other_group).to eq group
Expand All @@ -24,6 +24,12 @@
end
end

describe '#agent_key' do
it 'returns the name prefixed with the name prefix' do
expect(group.agent_key).to eq described_class.name_prefix + group.name
end
end

describe '#to_sipity_agent' do
subject { group.to_sipity_agent }

Expand Down
14 changes: 14 additions & 0 deletions spec/models/user_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,20 @@
end
end

describe '#agent_key' do
let(:key) { user.agent_key }

it 'is the same as the user key' do
expect(key).to eq user.user_key
end

it 'is findable by agent_key' do
user.save!

expect(described_class.from_agent_key(key)).to eq user
end
end

it "has an email" do
expect(user.email).to be_kind_of String
end
Expand Down

0 comments on commit e5c1de5

Please sign in to comment.