Only active instances have access to Groups.

This commit is contained in:
Thorsten Eckel 2017-06-19 10:16:46 +02:00
parent 284dbb3de8
commit b1884c82f6
2 changed files with 40 additions and 1 deletions

View file

@ -61,6 +61,8 @@ module HasGroups
#
# @return [Boolean]
def group_access?(group_id, access)
return false if !active?
group_id = self.class.ensure_group_id_parameter(group_id)
access = self.class.ensure_group_access_list_parameter(access)
@ -92,8 +94,9 @@ module HasGroups
#
# @return [Array<Integer>] Group IDs the instance has the given access(es) to.
def group_ids_access(access)
access = self.class.ensure_group_access_list_parameter(access)
return [] if !active?
access = self.class.ensure_group_access_list_parameter(access)
foreign_key = group_through.foreign_key
klass = group_through.klass
@ -124,6 +127,7 @@ module HasGroups
#
# @return [Array<Group>] Groups the instance has the given access(es) to.
def groups_access(access)
return [] if !active?
group_ids = group_ids_access(access)
Group.where(id: group_ids)
end
@ -182,6 +186,7 @@ module HasGroups
private
def groups_access_map(key)
return {} if !active?
{}.tap do |hash|
groups.access.where(active: true).pluck(key, :access).each do |entry|
hash[ entry[0] ] ||= []

View file

@ -111,6 +111,14 @@ RSpec.shared_examples 'HasGroups' do
expect(instance.group_access?(group_inactive.id, 'read')).to be false
end
it 'prevents inactive instances' do
instance_inactive.group_names_access_map = {
group_read.name => 'read',
}
expect(instance_inactive.group_access?(group_read.id, 'read')).to be false
end
end
context '#group_ids_access' do
@ -135,6 +143,14 @@ RSpec.shared_examples 'HasGroups' do
expect(result).not_to include(group_inactive.id)
end
it "doesn't list for inactive instances" do
instance_inactive.group_names_access_map = {
group_read.name => 'read',
}
expect(instance_inactive.group_ids_access('read')).to be_empty
end
context 'single access' do
it 'lists access Group IDs' do
@ -258,6 +274,15 @@ RSpec.shared_examples 'HasGroups' do
expect(instance.group_names_access_map).to eq(expected)
end
it "doesn't map for inactive instances" do
instance_inactive.group_names_access_map = {
group_full.name => ['full'],
group_read.name => ['read'],
}
expect(instance_inactive.group_names_access_map).to be_empty
end
end
context '#group_ids_access_map=' do
@ -336,6 +361,15 @@ RSpec.shared_examples 'HasGroups' do
expect(instance.group_ids_access_map).to eq(expected)
end
it "doesn't map for inactive instances" do
instance_inactive.group_ids_access_map = {
group_full.id => ['full'],
group_read.id => ['read'],
}
expect(instance_inactive.group_ids_access_map).to be_empty
end
end
context '#associations_from_param' do