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] # @return [Boolean]
def group_access?(group_id, access) def group_access?(group_id, access)
return false if !active?
group_id = self.class.ensure_group_id_parameter(group_id) group_id = self.class.ensure_group_id_parameter(group_id)
access = self.class.ensure_group_access_list_parameter(access) 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. # @return [Array<Integer>] Group IDs the instance has the given access(es) to.
def group_ids_access(access) 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 foreign_key = group_through.foreign_key
klass = group_through.klass klass = group_through.klass
@ -124,6 +127,7 @@ module HasGroups
# #
# @return [Array<Group>] Groups the instance has the given access(es) to. # @return [Array<Group>] Groups the instance has the given access(es) to.
def groups_access(access) def groups_access(access)
return [] if !active?
group_ids = group_ids_access(access) group_ids = group_ids_access(access)
Group.where(id: group_ids) Group.where(id: group_ids)
end end
@ -182,6 +186,7 @@ module HasGroups
private private
def groups_access_map(key) def groups_access_map(key)
return {} if !active?
{}.tap do |hash| {}.tap do |hash|
groups.access.where(active: true).pluck(key, :access).each do |entry| groups.access.where(active: true).pluck(key, :access).each do |entry|
hash[ entry[0] ] ||= [] 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 expect(instance.group_access?(group_inactive.id, 'read')).to be false
end 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 end
context '#group_ids_access' do context '#group_ids_access' do
@ -135,6 +143,14 @@ RSpec.shared_examples 'HasGroups' do
expect(result).not_to include(group_inactive.id) expect(result).not_to include(group_inactive.id)
end 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 context 'single access' do
it 'lists access Group IDs' 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) expect(instance.group_names_access_map).to eq(expected)
end 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 end
context '#group_ids_access_map=' do context '#group_ids_access_map=' do
@ -336,6 +361,15 @@ RSpec.shared_examples 'HasGroups' do
expect(instance.group_ids_access_map).to eq(expected) expect(instance.group_ids_access_map).to eq(expected)
end 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 end
context '#associations_from_param' do context '#associations_from_param' do