Skip to content

Commit 4e72624

Browse files
fix(permission): refetch permission if collection name doesn't exist (#680)
1 parent a28e9ec commit 4e72624

File tree

2 files changed

+51
-2
lines changed

2 files changed

+51
-2
lines changed

app/services/forest_liana/ability/permission.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ def is_crud_authorized?(action, user, collection)
1616
collection_name = ForestLiana.name_for(collection)
1717

1818
begin
19-
is_allowed = collections_data[collection_name][action].include? user_data['roleId']
19+
is_allowed = (collections_data.key?(collection_name) && collections_data[collection_name][action].include?(user_data['roleId']))
20+
2021
# re-fetch if user permission is not allowed (may have been changed)
2122
unless is_allowed
2223
collections_data = get_collections_permissions_data(true)
@@ -25,7 +26,7 @@ def is_crud_authorized?(action, user, collection)
2526

2627
is_allowed
2728
rescue
28-
raise ForestLiana::Errors::ExpectedError.new(409, :conflict, "The collection #{collection} doesn't exist", 'collection not found')
29+
raise ForestLiana::Errors::ExpectedError.new(409, :conflict, "The collection #{collection_name} doesn't exist", 'collection not found')
2930
end
3031
end
3132

spec/services/forest_liana/ability/permission_spec.rb

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ module Ability
7979
end
8080
end
8181

82+
8283
it 'should throw an exception when the collection doesn\'t exist' do
8384
expect {dummy_class.is_crud_authorized?('browse', user, String)}.to raise_error(ForestLiana::Errors::ExpectedError, 'The collection String doesn\'t exist')
8485
end
@@ -163,6 +164,53 @@ module Ability
163164
expect(dummy_class.is_crud_authorized?('browse', user, Island)).to equal true
164165
end
165166

167+
it 'should re-fetch the permission once when collection_name doesn\'t exist' do
168+
Rails.cache.write(
169+
'forest.collections',
170+
{
171+
"collections" => {
172+
"Fake_collection_name" => {
173+
"collection" => {
174+
"browseEnabled" => { "roles" => [1] },
175+
"readEnabled" => { "roles" => [1] },
176+
"editEnabled" => { "roles" => [1] },
177+
"addEnabled" => { "roles" => [1] },
178+
"deleteEnabled" => { "roles" => [1] },
179+
"exportEnabled" => { "roles" => [1] }
180+
},
181+
"actions" => {
182+
183+
}
184+
}
185+
}
186+
}
187+
)
188+
189+
allow_any_instance_of(ForestLiana::Ability::Fetch)
190+
.to receive(:get_permissions)
191+
.and_return(
192+
{
193+
"collections" => {
194+
"Island" => {
195+
"collection" => {
196+
"browseEnabled" => { "roles" => [1] },
197+
"readEnabled" => { "roles" => [1] },
198+
"editEnabled" => { "roles" => [1] },
199+
"addEnabled" => { "roles" => [1] },
200+
"deleteEnabled" => { "roles" => [1] },
201+
"exportEnabled" => { "roles" => [1] }
202+
},
203+
"actions" => {
204+
205+
}
206+
}
207+
}
208+
}
209+
)
210+
211+
expect(dummy_class.is_crud_authorized?('browse', user, Island)).to equal true
212+
end
213+
166214
it 'should return false when user permission is not allowed' do
167215
Rails.cache.delete('forest.users')
168216

0 commit comments

Comments
 (0)