Skip to content

Commit 1d5b777

Browse files
author
etrott
committed
allow user to update user pools
1 parent f4404c7 commit 1d5b777

6 files changed

Lines changed: 40 additions & 4 deletions

File tree

app/controllers/pools_controller.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,28 @@ def update
3838
end
3939
end
4040

41+
def disable
42+
update_params = pool_params
43+
update_params[:active] = false
44+
@pool = Pool.new(update_params)
45+
num_disabled = Pool.
46+
where( name: update_params[:name],
47+
resource: update_params[:resource]).
48+
update_all(active: update_params[:active],
49+
updated_at: Time.now)
50+
if num_disabled == 1
51+
@persisted = true
52+
elsif num_disabled == 0
53+
# concurrency issue or not matching name/resource specification
54+
@persisted = false
55+
@errors = ["conflict with existing pool or not matching name/resource"]
56+
render status: :conflict
57+
else
58+
# this should be impossible
59+
raise "Unicorns ate the unique database index?"
60+
end
61+
end
62+
4163
def destroy
4264
@pool = Pool.find(params[:id])
4365
@pool.destroy

app/helpers/auth_helper.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ def authorize_role!
2727
when :user
2828
params = request.filtered_parameters
2929
if params["controller"] == "locks" &&
30-
["create", "update", "lock_from_pool"].include?(params["action"])
30+
["create", "update", "lock_from_pool"].include?(params["action"]) ||
31+
params["controller"] == "pools" &&
32+
["disable"].include?(params["action"])
3133
return
3234
else
3335
logger.warn "regular user calling forbidden methods"

app/views/pools/create.json.ruby

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
if @persisted && !(@errors && !@errors.empty?)
2+
# success response
3+
{name: @pool.name, resource: @pool.resource,
4+
active: @pool.active}
5+
else
6+
# something failed
7+
{ messages: [@errors||=[]].flatten }
8+
end.to_json

app/views/pools/disable.json.ruby

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
render template: "pools/create"

app/views/welcome/index.html.erb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ To create a lock as normal user you can issue the following calls:
3535
To update a lock as normal user you can issue the following calls:
3636
<pre><code>curl -v -H "Accept: application/json" -H "Content-type: application/json" -X PATCH -u user:password <%= File.join(root_url, url_for(update_by_values_path)) %> -d '{"namespace":"curl", "resource":"curl_1", "owner":"curl_user", "expires":"1d"}'
3737
</code></pre>
38+
To disable a pool as normal user you can issue the following calls:
39+
<pre><code>curl -v -H "Accept: application/json" -H "Content-type: application/json" -X PATCH -u user:password <%= File.join(root_url, url_for(disable_pool_path)) %> -d '{"name":"curl", "resource":"curl_1"}'
40+
</code></pre>
3841
Please note that on update, you should specify all fields although you can only
3942
update the <code>expites</code> field. This is to avoid unintentional stealing
4043
of locks. If user is worried of intentional lock stealing, adding a random

config/routes.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22
get 'welcome/index'
33
root 'welcome#index'
44

5-
resources :locks
5+
resources :locks, except: :show
66
match '/locks/by_values(.:format)' => 'locks#update', via: [:put, :patch], as: "update_by_values"
77
# put '/locks/by_values(.:format)' => 'locks#update'
88

99
post '/locks/from_pool(.:format)' => 'locks#lock_from_pool', as: "create_from_pool"
1010

11-
resources :pools
12-
11+
match '/pools/disable(.:format)' => 'pools#disable', via: [:put, :patch], as: "disable_pool"
12+
resources :pools, except: :show
1313
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
1414
end

0 commit comments

Comments
 (0)