diff options
author | Nuo Yan <nuo@opscode.com> | 2010-02-26 16:05:50 -0800 |
---|---|---|
committer | Nuo Yan <nuo@opscode.com> | 2010-02-26 16:05:50 -0800 |
commit | 096dd1da9ee04b0af47839860324d302f4d38222 (patch) | |
tree | bd16f89c56b646487bad6a6b32d5b40f2a0bf65b /chef-server-webui | |
parent | dbdf70214cc0bbaa0bb3fa3f3b771f6fc55619c8 (diff) | |
download | chef-096dd1da9ee04b0af47839860324d302f4d38222.tar.gz |
CHEF-971
Diffstat (limited to 'chef-server-webui')
-rw-r--r-- | chef-server-webui/app/controllers/clients.rb | 89 | ||||
-rw-r--r-- | chef-server-webui/app/controllers/cookbooks.rb | 23 | ||||
-rw-r--r-- | chef-server-webui/app/controllers/databag_items.rb | 22 | ||||
-rw-r--r-- | chef-server-webui/app/controllers/databags.rb | 26 | ||||
-rw-r--r-- | chef-server-webui/app/controllers/nodes.rb | 81 | ||||
-rw-r--r-- | chef-server-webui/app/controllers/roles.rb | 90 | ||||
-rw-r--r-- | chef-server-webui/app/controllers/search.rb | 21 | ||||
-rw-r--r-- | chef-server-webui/app/controllers/status.rb | 3 | ||||
-rw-r--r-- | chef-server-webui/app/controllers/users.rb | 57 |
9 files changed, 174 insertions, 238 deletions
diff --git a/chef-server-webui/app/controllers/clients.rb b/chef-server-webui/app/controllers/clients.rb index d173ca682a..f5fce56a9a 100644 --- a/chef-server-webui/app/controllers/clients.rb +++ b/chef-server-webui/app/controllers/clients.rb @@ -25,38 +25,38 @@ class ChefServerWebui::Clients < ChefServerWebui::Application # GET /clients def index - begin - @clients_list = Chef::ApiClient.list() - render - rescue - @clients_list = {} - @_message = {:error => $!} - render - end + @clients_list = begin + Chef::ApiClient.list() + rescue => e + Chef::Log.error("#{e}\n#{e.backtrace.join("\n")}") + @_message = {:error => "Could not list clients"} + {} + end + render end # GET /clients/:id def show - begin - load_client - render - rescue => e - @client = Chef::ApiClient.new - @_message = e.message =~ /not found/ ? {:error => "Cannot find client '#{params[:id]}'"} : { :error => $!} - render - end + @client = begin + @client = Chef::ApiClient.load(params[:id]) + rescue => e + Chef::Log.error("#{e}\n#{e.backtrace.join("\n")}") + @_message = { :error => "Could not load client #{params[:id]}"} + Chef::ApiClient.new + end + render end # GET /clients/:id/edit def edit - begin - load_client - render - rescue - @client = Chef::ApiClient.new - @_message = e.message =~ /not found/ ? {:error => "Cannot find client '#{params[:id]}'"} : { :error => $!} - render - end + @client = begin + Chef::ApiClient.load(params[:id]) + rescue => e + Chef::Log.error("#{e}\n#{e.backtrace.join("\n")}") + @_message = { :error => "Could not load client #{params[:id]}"} + Chef::ApiClient.new + end + render end # GET /clients/new @@ -71,21 +71,14 @@ class ChefServerWebui::Clients < ChefServerWebui::Application @client = Chef::ApiClient.new @client.name(params[:name]) @client.admin(str_to_bool(params[:admin])) if params[:admin] - begin - response = @client.create - rescue Net::HTTPServerException => e - if e.message =~ /403/ - raise ArgumentError, "Client already exists" - else - raise e - end - end + response = @client.create @private_key = OpenSSL::PKey::RSA.new(response["private_key"]) @_message = { :notice => "Created Client #{@client.name}. Please copy the following private key as the client's validation key." } - load_client(params[:name]) + @client = Chef::ApiClient.load(params[:name]) render :show - rescue StandardError => e - @_message = { :error => $! } + rescue => e + Chef::Log.error("#{e}\n#{e.backtrace.join("\n")}") + @_message = { :error => "Could not create client" } render :new end end @@ -93,7 +86,7 @@ class ChefServerWebui::Clients < ChefServerWebui::Application # PUT /clients/:id def update begin - load_client + @client = Chef::ApiClient.load(params[:id]) if params[:regen_private_key] @client.create_keys @private_key = @client.private_key @@ -102,8 +95,9 @@ class ChefServerWebui::Clients < ChefServerWebui::Application @client.save @_message = @private_key.nil? ? { :notice => "Updated Client" } : { :notice => "Created Client #{@client.name}. Please copy the following private key as the client's validation key." } render :show - rescue - @_message = { :error => $! } + rescue => e + Chef::Log.error("#{e}\n#{e.backtrace.join("\n")}") + @_message = { :error => "Could not update client" } render :edit end end @@ -111,25 +105,16 @@ class ChefServerWebui::Clients < ChefServerWebui::Application # DELETE /clients/:id def destroy begin - load_client + @client = Chef::ApiClient.load(params[:id]) @client.destroy redirect(absolute_slice_url(:clients), {:message => { :notice => "Client #{params[:id]} deleted successfully" }, :permanent => true}) - rescue - @_message = {:error => $!} + rescue => e + Chef::Log.error("#{e}\n#{e.backtrace.join("\n")}") + @_message = {:error => "Could not delete client #{params[:id]}" } @clients_list = Chef::ApiClient.list() render :index end end - - private - - def load_client(name=params[:id]) - begin - @client = Chef::ApiClient.load(name) - rescue Net::HTTPServerException => e - raise NotFound, "Cannot load client #{name}" - end - end end diff --git a/chef-server-webui/app/controllers/cookbooks.rb b/chef-server-webui/app/controllers/cookbooks.rb index 5f6e0eddad..9ed11c5546 100644 --- a/chef-server-webui/app/controllers/cookbooks.rb +++ b/chef-server-webui/app/controllers/cookbooks.rb @@ -26,24 +26,23 @@ class ChefServerWebui::Cookbooks < ChefServerWebui::Application before :login_required def index - begin - r = Chef::REST.new(Chef::Config[:chef_server_url]) - @cl = r.get_rest("cookbooks") - render - rescue - @_message = {:error => $!} - @cl = {} - render - end + @cl = begin + Chef::REST.new(Chef::Config[:chef_server_url]).get_rest("cookbooks") + rescue => e + Chef::Log.error("#{e}\n#{e.backtrace.join("\n")}") + @_message = {:error => $!} + {} + end + render end def show begin - r = Chef::REST.new(Chef::Config[:chef_server_url]) - @cookbook = r.get_rest("cookbooks/#{params[:id]}") + @cookbook = Chef::REST.new(Chef::Config[:chef_server_url]).get_rest("cookbooks/#{params[:id]}") raise NotFound unless @cookbook display @cookbook - rescue + rescue => e + Chef::Log.error("#{e}\n#{e.backtrace.join("\n")}") @_message = {:error => $!} @cl = {} render :index diff --git a/chef-server-webui/app/controllers/databag_items.rb b/chef-server-webui/app/controllers/databag_items.rb index ee0e46d3d0..2255695d61 100644 --- a/chef-server-webui/app/controllers/databag_items.rb +++ b/chef-server-webui/app/controllers/databag_items.rb @@ -25,13 +25,10 @@ class ChefServerWebui::DatabagItems < ChefServerWebui::Application def edit begin - begin - @databag_item = Chef::DataBagItem.load(params[:databag_id], params[:id]) - rescue Net::HTTPServerException => e - raise NotFound, "Cannot load Databag Item #{params[:id]}" - end + @databag_item = Chef::DataBagItem.load(params[:databag_id], params[:id]) @default_data = @databag_item - rescue StandardError => e + rescue => e + Chef::Log.error("#{e}\n#{e.backtrace.join("\n")}") @_message = { :error => $! } end render @@ -45,7 +42,8 @@ class ChefServerWebui::DatabagItems < ChefServerWebui::Application raise ArgumentError, "Updating id is not allowed" unless @databag_item.raw_data['id'] == params[:id] #to be consistent with other objects, changing id is not allowed. @databag_item.save redirect(slice_url(:databag_databag_items, :databag_id => params[:databag_id], :id => @databag_item.name), :message => { :notice => "Updated Databag Item #{@databag_item.name}" }) - rescue + rescue => e + Chef::Log.error("#{e}\n#{e.backtrace.join("\n")}") @_message = { :error => $! } @databag_item = Chef::DataBagItem.load(params[:databag_id], params[:id]) @default_data = @databag_item @@ -53,7 +51,6 @@ class ChefServerWebui::DatabagItems < ChefServerWebui::Application end end - def new @default_data = {'id'=>''} render @@ -67,7 +64,8 @@ class ChefServerWebui::DatabagItems < ChefServerWebui::Application @databag_item.raw_data = JSON.parse(params[:json_data]) @databag_item.create redirect(slice_url(:databag_databag_items, :databag_id => @databag_name), :message => { :notice => "Created Databag Item #{@databag_item.name}" }) - rescue StandardError => e + rescue => e + Chef::Log.error("#{e}\n#{e.backtrace.join("\n")}") @_message = { :error => $! } render :new end @@ -84,7 +82,8 @@ class ChefServerWebui::DatabagItems < ChefServerWebui::Application r = Chef::REST.new(Chef::Config[:chef_server_url]) @databag_item = r.get_rest("data/#{params[:databag_id]}/#{params[:id]}") display @databag_item - rescue + rescue => e + Chef::Log.error("#{e}\n#{e.backtrace.join("\n")}") redirect(slice_url(:databag_databag_items), {:message => { :error => $! }, :permanent => true}) end end @@ -94,7 +93,8 @@ class ChefServerWebui::DatabagItems < ChefServerWebui::Application @databag_item = Chef::DataBagItem.new @databag_item.destroy(databag_id, item_id) redirect(slice_url(:databag_databag_items), {:message => { :notice => "Databag Item #{params[:id]} deleted successfully" }, :permanent => true}) - rescue + rescue => e + Chef::Log.error("#{e}\n#{e.backtrace.join("\n")}") redirect(slice_url(:databag_databag_items), {:message => { :error => $! }, :permanent => true}) end end diff --git a/chef-server-webui/app/controllers/databags.rb b/chef-server-webui/app/controllers/databags.rb index 555ec79baf..d7172803dc 100644 --- a/chef-server-webui/app/controllers/databags.rb +++ b/chef-server-webui/app/controllers/databags.rb @@ -34,22 +34,22 @@ class ChefServerWebui::Databags < ChefServerWebui::Application @databag.name params[:name] @databag.create redirect(slice_url(:databags), :message => { :notice => "Created Databag #{@databag.name}" }) - rescue StandardError => e + rescue => e + Chef::Log.error("#{e}\n#{e.backtrace.join("\n")}") @_message = { :error => $! } render :new end end def index - begin - r = Chef::REST.new(Chef::Config[:chef_server_url]) - @databags = r.get_rest("data") - render - rescue - @_message = { :error => $! } - @databags = {} - render - end + @databags = begin + Chef::REST.new(Chef::Config[:chef_server_url]).get_rest("data") + rescue => e + Chef::Log.error("#{e}\n#{e.backtrace.join("\n")}") + @_message = { :error => $! } + {} + end + render end def show @@ -59,7 +59,8 @@ class ChefServerWebui::Databags < ChefServerWebui::Application @databag = r.get_rest("data/#{params[:id]}") raise NotFound unless @databag display @databag - rescue + rescue => e + Chef::Log.error("#{e}\n#{e.backtrace.join("\n")}") @databags = Chef::DataBag.list @_message = { :error => $!} render :index @@ -71,7 +72,8 @@ class ChefServerWebui::Databags < ChefServerWebui::Application r = Chef::REST.new(Chef::Config[:chef_server_url]) r.delete_rest("data/#{params[:id]}") redirect(absolute_slice_url(:databags), {:message => { :notice => "Data bag #{params[:id]} deleted successfully" }, :permanent => true}) - rescue + rescue => e + Chef::Log.error("#{e}\n#{e.backtrace.join("\n")}") @databags = Chef::DataBag.list @_message = { :error => $!} render :index diff --git a/chef-server-webui/app/controllers/nodes.rb b/chef-server-webui/app/controllers/nodes.rb index a696bf85e1..5391e65517 100644 --- a/chef-server-webui/app/controllers/nodes.rb +++ b/chef-server-webui/app/controllers/nodes.rb @@ -28,27 +28,24 @@ class ChefServerWebui::Nodes < ChefServerWebui::Application before :authorized_node, :only => [ :update, :destroy ] def index - begin - @node_list = Chef::Node.list - render - rescue - @node_list = {} - @_message = {:error => $!} - render - end + @node_list = begin + Chef::Node.list + rescue => e + Chef::Log.error("#{e}\n#{e.backtrace.join("\n")}") + @_message = {:error => $!} + {} + end + render end def show - begin - begin - @node = Chef::Node.load(params[:id]) - rescue Net::HTTPServerException => e - raise NotFound, "Cannot load node #{params[:id]}" - end - rescue - @node = Chef::Node.new - @_message = {:error => $!} - end + @node = begin + Chef::Node.load(params[:id]) + rescue => e + Chef::Log.error("#{e}\n#{e.backtrace.join("\n")}") + @_message = {:error => "Could not load node #{params[:id]}"} + Chef::Node.new + end render end @@ -59,7 +56,8 @@ class ChefServerWebui::Nodes < ChefServerWebui::Application @available_roles = Chef::Role.list.keys.sort @run_list = @node.run_list render - rescue + rescue => e + Chef::Log.error("#{e}\n#{e.backtrace.join("\n")}") @node_list = Chef::Node.list() @_message = {:error => $!} render :index @@ -68,21 +66,18 @@ class ChefServerWebui::Nodes < ChefServerWebui::Application def edit begin - begin - @node = Chef::Node.load(params[:id]) - rescue Net::HTTPServerException => e - raise NotFound, "Cannot load node #{params[:id]}" - end + @node = Chef::Node.load(params[:id]) @available_recipes = get_available_recipes @available_roles = Chef::Role.list.keys.sort @run_list = @node.run_list render - rescue + rescue => e + Chef::Log.error("#{e}\n#{e.backtrace.join("\n")}") @node = Chef::Node.new @available_recipes = [] @available_roles = [] @run_list = [] - @_message = {:error => $!} + @_message = {:error => "Could not load node #{params[:id]}"} render end end @@ -94,18 +89,10 @@ class ChefServerWebui::Nodes < ChefServerWebui::Application @node.attribute = JSON.parse(params[:attributes]) @node.run_list.reset!(params[:for_node] ? params[:for_node] : []) raise ArgumentError, "Node name cannot be blank" if (params[:name].nil? || params[:name].length==0) - begin - @node.create - rescue Net::HTTPServerException => e - if e.message =~ /403/ - raise ArgumentError, "Node already exists" - else - raise e - end - end + @node.create redirect(slice_url(:nodes), :message => { :notice => "Created Node #{@node.name}" }) - rescue StandardError => e - Chef::Log.error("StandardError creating node: #{e.message}") + rescue => e + Chef::Log.error("#{e}\n#{e.backtrace.join("\n")}") @node.attribute = JSON.parse(params[:attributes]) @available_recipes = get_available_recipes @available_roles = Chef::Role.list.keys.sort @@ -119,18 +106,13 @@ class ChefServerWebui::Nodes < ChefServerWebui::Application def update begin @node = Chef::Node.load(params[:id]) - rescue Net::HTTPServerException => e - raise NotFound, "Cannot load node #{params[:id]}" - end - - begin @node.run_list.reset!(params[:for_node] ? params[:for_node] : []) @node.attribute = JSON.parse(params[:attributes]) @node.save @_message = { :notice => "Updated Node" } render :show - rescue Exception => e - Chef::Log.error("Exception updating node: #{e.message}") + rescue => e + Chef::Log.error("#{e}\n#{e.backtrace.join("\n")}") @available_recipes = get_available_recipes @available_roles = Chef::Role.list.keys.sort @run_list = Chef::RunList.new @@ -142,16 +124,13 @@ class ChefServerWebui::Nodes < ChefServerWebui::Application def destroy begin - begin - @node = Chef::Node.load(params[:id]) - rescue Net::HTTPServerException => e - raise NotFound, "Cannot load node #{params[:id]}" - end + @node = Chef::Node.load(params[:id]) @node.destroy redirect(absolute_slice_url(:nodes), {:message => { :notice => "Node #{params[:id]} deleted successfully" }, :permanent => true}) - rescue + rescue => e + Chef::Log.error("#{e}\n#{e.backtrace.join("\n")}") @node_list = Chef::Node.list() - @_message = {:error => $!} + @_message = {:error => "Could not delete the node"} render :index end end diff --git a/chef-server-webui/app/controllers/roles.rb b/chef-server-webui/app/controllers/roles.rb index bc6ba21d99..545d262c56 100644 --- a/chef-server-webui/app/controllers/roles.rb +++ b/chef-server-webui/app/controllers/roles.rb @@ -26,30 +26,26 @@ class ChefServerWebui::Roles < ChefServerWebui::Application # GET /roles def index - begin - @role_list = Chef::Role.list() - render - rescue - @role_list = {} - @_message = {:error => $!} - render - end + @role_list = begin + Chef::Role.list() + rescue => e + Chef::Log.error("#{e}\n#{e.backtrace.join("\n")}") + @_message = {:error => $!} + {} + end + render end # GET /roles/:id def show - begin - begin - @role = Chef::Role.load(params[:id]) - rescue Net::HTTPServerException => e - raise NotFound, "Cannot load role #{params[:id]}" - end - render - rescue - @role = Chef::Role.new - @_message = {:error => $!} - render - end + @role = begin + Chef::Role.load(params[:id]) + rescue => e + Chef::Log.error("#{e}\n#{e.backtrace.join("\n")}") + @_message = {:error => "Could not load role #{params[:id]}"} + Chef::Role.new + end + render end # GET /roles/new @@ -60,9 +56,10 @@ class ChefServerWebui::Roles < ChefServerWebui::Application @available_roles = Chef::Role.list.keys.sort @run_list = @role.run_list render - rescue + rescue => e + Chef::Log.error("#{e}\n#{e.backtrace.join("\n")}") @role_list = Chef::Role.list() - @_message = {:error => $!} + @_message = {:error => "Could not load available recipes, roles, or the run list."} render :index end end @@ -70,23 +67,19 @@ class ChefServerWebui::Roles < ChefServerWebui::Application # GET /roles/:id/edit def edit begin - begin - @role = Chef::Role.load(params[:id]) - rescue Net::HTTPServerException => e - raise NotFound, "Cannot load role #{params[:id]}" - end + @role = Chef::Role.load(params[:id]) @available_recipes = get_available_recipes @available_roles = Chef::Role.list.keys.sort @run_list = @role.run_list - render - rescue + rescue => e + Chef::Log.error("#{e}\n#{e.backtrace.join("\n")}") @role = Chef::Role.new @available_recipes = [] @available_roles = [] @run_list = [] - @_message = {:error => $!} - render + @_message = {:error => "Could not load role #{params[:id]}, the available recipes, roles, or the run list"} end + render end # POST /roles @@ -98,23 +91,16 @@ class ChefServerWebui::Roles < ChefServerWebui::Application @role.description(params[:description]) if params[:description] != '' @role.default_attributes(JSON.parse(params[:default_attributes])) if params[:default_attributes] != '' @role.override_attributes(JSON.parse(params[:override_attributes])) if params[:override_attributes] != '' - begin - @role.create - rescue Net::HTTPServerException => e - if e.message =~ /403/ - raise ArgumentError, "Role already exists" - else - raise e - end - end + @role.create redirect(slice_url(:roles), :message => { :notice => "Created Role #{@role.name}" }) - rescue ArgumentError + rescue => e + Chef::Log.error("#{e}\n#{e.backtrace.join("\n")}") @available_recipes = get_available_recipes @role = Chef::Role.new @role.default_attributes(JSON.parse(params[:default_attributes])) if params[:default_attributes] != '' @role.override_attributes(JSON.parse(params[:override_attributes])) if params[:override_attributes] != '' @run_list = params[:for_role] ? params[:for_role] : [] - @_message = { :error => $! } + @_message = { :error => "Could not create role" } render :new end end @@ -123,11 +109,6 @@ class ChefServerWebui::Roles < ChefServerWebui::Application def update begin @role = Chef::Role.load(params[:id]) - rescue Net::HTTPServerException => e - raise NotFound, "Cannot load role #{params[:id]}" - end - - begin @role.run_list(params[:for_role] ? params[:for_role] : []) @role.description(params[:description]) if params[:description] != '' @role.default_attributes(JSON.parse(params[:default_attributes])) if params[:default_attributes] != '' @@ -135,11 +116,13 @@ class ChefServerWebui::Roles < ChefServerWebui::Application @role.save @_message = { :notice => "Updated Role" } render :show - rescue ArgumentError + rescue => e + Chef::Log.error("#{e}\n#{e.backtrace.join("\n")}") @available_recipes = get_available_recipes @run_list = params[:for_role] ? params[:for_role] : [] @role.default_attributes(JSON.parse(params[:default_attributes])) if params[:default_attributes] != '' @role.override_attributes(JSON.parse(params[:override_attributes])) if params[:override_attributes] != '' + @_message = {:error => "Could not update role #{params[:id]}"} render :edit end end @@ -147,16 +130,13 @@ class ChefServerWebui::Roles < ChefServerWebui::Application # DELETE /roles/:id def destroy begin - begin - @role = Chef::Role.load(params[:id]) - rescue Net::HTTPServerException => e - raise NotFound, "Cannot load role #{params[:id]}" - end + @role = Chef::Role.load(params[:id]) @role.destroy redirect(absolute_slice_url(:roles), :message => { :notice => "Role #{@role.name} deleted successfully." }, :permanent => true) - rescue + rescue => e + Chef::Log.error("#{e}\n#{e.backtrace.join("\n")}") @role_list = Chef::Role.list() - @_message = {:error => $!} + @_message = {:error => "Could not delete role #{params[:id]}"} render :index end end diff --git a/chef-server-webui/app/controllers/search.rb b/chef-server-webui/app/controllers/search.rb index db45538b75..75eb310407 100644 --- a/chef-server-webui/app/controllers/search.rb +++ b/chef-server-webui/app/controllers/search.rb @@ -24,15 +24,15 @@ class ChefServerWebui::Search < ChefServerWebui::Application before :login_required def index - begin - @s = Chef::Search::Query.new - @search_indexes = @s.list_indexes - render - rescue - @search_indexes = {} - @_message = {:error => $!} - render - end + @s = Chef::Search::Query.new + @search_indexes = begin + @s.list_indexes + rescue => e + Chef::Log.error("#{e}\n#{e.backtrace.join("\n")}") + @_message = {:error => $!} + {} + end + render end def show @@ -51,7 +51,8 @@ class ChefServerWebui::Search < ChefServerWebui::Application end @results render - rescue StandardError => e + rescue => e + Chef::Log.error("#{e}\n#{e.backtrace.join("\n")}") @_message = { :error => "Unable to find the #{params[:id]}. (#{$!})" } @search_indexes = @s.list_indexes render :index diff --git a/chef-server-webui/app/controllers/status.rb b/chef-server-webui/app/controllers/status.rb index 4fa3419706..7054272ec9 100644 --- a/chef-server-webui/app/controllers/status.rb +++ b/chef-server-webui/app/controllers/status.rb @@ -28,7 +28,8 @@ class ChefServerWebui::Status < ChefServerWebui::Application begin @status = Chef::Node.list(true) render - rescue + rescue => e + Chef::Log.error("#{e}\n#{e.backtrace.join("\n")}") @status = {} @_message = {:error => $!} render diff --git a/chef-server-webui/app/controllers/users.rb b/chef-server-webui/app/controllers/users.rb index 4b11803d52..32bfb519ac 100644 --- a/chef-server-webui/app/controllers/users.rb +++ b/chef-server-webui/app/controllers/users.rb @@ -32,7 +32,8 @@ class ChefServerWebui::Users < ChefServerWebui::Application authorized_user @users = Chef::WebUIUser.list render - rescue + rescue => e + Chef::Log.error("#{e}\n#{e.backtrace.join("\n")}") set_user_and_redirect end end @@ -43,7 +44,8 @@ class ChefServerWebui::Users < ChefServerWebui::Application raise Forbidden, "The current user is not an Administrator, you can only Show and Edit the user itself. To control other users, login as an Administrator." unless params[:user_id] == session[:user] unless session[:level] == :admin @user = Chef::WebUIUser.load(params[:user_id]) render - rescue + rescue => e + Chef::Log.error("#{e}\n#{e.backtrace.join("\n")}") set_user_and_redirect end end @@ -52,13 +54,10 @@ class ChefServerWebui::Users < ChefServerWebui::Application def show begin raise Forbidden, "The current user is not an Administrator, you can only Show and Edit the user itself. To control other users, login as an Administrator." unless params[:user_id] == session[:user] unless session[:level] == :admin - begin - @user = Chef::WebUIUser.load(params[:user_id]) - rescue Net::HTTPServerException => e - raise NotFound, "Cannot find user #{params[:user_id]}" - end + @user = Chef::WebUIUser.load(params[:user_id]) render - rescue + rescue => e + Chef::Log.error("#{e}\n#{e.backtrace.join("\n")}") set_user_and_redirect end end @@ -66,11 +65,8 @@ class ChefServerWebui::Users < ChefServerWebui::Application # PUT to /users/:user_id/update def update begin - begin - @user = Chef::WebUIUser.load(params[:user_id]) - rescue Net::HTTPServerException => e - raise NotFound, "Cannot find user #{params[:user_id]}" - end + @user = Chef::WebUIUser.load(params[:user_id]) + if session[:level] == :admin and ['true','false'].include? params[:admin] @user.admin = str_to_bool(params[:admin]) end @@ -91,8 +87,9 @@ class ChefServerWebui::Users < ChefServerWebui::Application @user.save @_message = { :notice => "Updated User #{@user.name}" } render :show - rescue - @_message = { :error => $! } + rescue => e + Chef::Log.error("#{e}\n#{e.backtrace.join("\n")}") + @_message = { :error => "Could not update user" } render :edit end end @@ -102,7 +99,8 @@ class ChefServerWebui::Users < ChefServerWebui::Application authorized_user @user = Chef::WebUIUser.new render - rescue + rescue => e + Chef::Log.error("#{e}\n#{e.backtrace.join("\n")}") set_user_and_redirect end end @@ -115,18 +113,11 @@ class ChefServerWebui::Users < ChefServerWebui::Application @user.set_password(params[:password], params[:password2]) @user.admin = true if params[:admin] (params[:openid].length == 0 || params[:openid].nil?) ? @user.set_openid(nil) : @user.set_openid(URI.parse(params[:openid]).normalize.to_s) - begin - @user.create - rescue Net::HTTPServerException => e - if e.message =~ /403/ - raise ArgumentError, "User already exists" - else - raise e - end - end + @user.create redirect(slice_url(:users), :message => { :notice => "Created User #{params[:name]}" }) - rescue - @_message = { :error => $! } + rescue => e + Chef::Log.error("#{e}\n#{e.backtrace.join("\n")}") + @_message = { :error => "Could not create user" } session[:level] != :admin ? set_user_and_redirect : (render :new) end end @@ -138,14 +129,11 @@ class ChefServerWebui::Users < ChefServerWebui::Application def login_exec begin - begin - @user = Chef::WebUIUser.load(params[:name]) - rescue Net::HTTPServerException => e - raise(Unauthorized, "Wrong username or password.") - end + @user = Chef::WebUIUser.load(params[:name]) raise(Unauthorized, "Wrong username or password.") unless @user.verify_password(params[:password]) complete - rescue + rescue => e + Chef::Log.error("#{e}\n#{e.backtrace.join("\n")}") @user = Chef::WebUIUser.new @_message = { :error => $! } render :login @@ -171,7 +159,8 @@ class ChefServerWebui::Users < ChefServerWebui::Application @user.destroy logout if params[:user_id] == session[:user] redirect(absolute_slice_url(:users), {:message => { :notice => "User #{params[:user_id]} deleted successfully" }, :permanent => true}) - rescue + rescue => e + Chef::Log.error("#{e}\n#{e.backtrace.join("\n")}") session[:level] != :admin ? set_user_and_redirect : redirect_to_list_users({ :error => $! }) end end |