Skip to content

Commit 60a23a0

Browse files
Site creation permission as an Ability
Fixes #811
1 parent e92c1a3 commit 60a23a0

4 files changed

Lines changed: 38 additions & 1 deletion

File tree

app/models/ability.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,11 @@ def initialize(user, format = nil)
9696
user_membership.can_update?("location")
9797
end
9898

99+
can :create_site, Collection do |collection|
100+
user_membership = user.membership_in(collection)
101+
user_membership && user_membership.can_update?("name") && user_membership.can_update?("location")
102+
end
103+
99104
### Reminders ###
100105

101106
can [:create, :update, :read, :destroy, :set_status], Reminder, :collection => { :memberships => { :user_id => user.id } }

app/models/site.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,8 @@ def update_single_property!(es_code, value)
121121
def validate_and_process_parameters(site_params, user)
122122
user_membership = user.membership_in(collection)
123123

124+
user.authorize! :create_site, collection, message: "Not authorized to create site" if new_record?
125+
124126
if site_params.has_key?("name")
125127
user.authorize! :update_name, user_membership, message: "Not authorized to update site name"
126128
self.name = site_params["name"]

spec/controllers/api/sites_controller_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
site_params = {:name => "new site"}.to_json
3232
post :create, {:id => collection.id, :site => site_params }
3333
expect(response.status).to eq(403)
34-
expect(response.body).to include('name')
34+
expect(response.body).to include('create site')
3535
end
3636

3737
end

spec/models/ability_spec.rb

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,36 @@
200200
it { expect(member_ability_with_write_permission).to be_able_to(:update_site_property, field, site) }
201201
it { expect(member_ability_with_write_permission).to be_able_to(:read_site_property, field, site) }
202202
end
203+
204+
context "Site creation" do
205+
it "can't create sites if it doesn't have write permissions" do
206+
membership.set_access({object: 'name', new_action: 'read'})
207+
membership.set_access({object: 'location', new_action: 'read'})
208+
209+
expect(member_ability).not_to be_able_to(:create_site, collection)
210+
end
211+
212+
it "can't create sites if it doesn't have both write permissions" do
213+
membership.set_access({object: 'name', new_action: 'update'})
214+
membership.set_access({object: 'location', new_action: 'read'})
215+
216+
expect(member_ability).not_to be_able_to(:create_site, collection)
217+
end
218+
219+
it "can't create sites if it doesn't have both write permissions" do
220+
membership.set_access({object: 'name', new_action: 'read'})
221+
membership.set_access({object: 'location', new_action: 'update'})
222+
223+
expect(member_ability).not_to be_able_to(:create_site, collection)
224+
end
225+
226+
it "can create sites if it has both write permissions" do
227+
membership.set_access({object: 'name', new_action: 'update'})
228+
membership.set_access({object: 'location', new_action: 'update'})
229+
230+
expect(member_ability).to be_able_to(:create_site, collection)
231+
end
232+
end
203233
end
204234

205235
describe "guest user should not be able to update site property" do

0 commit comments

Comments
 (0)