From 02782673018b0c696c50643f33c6bb664b1dfb14 Mon Sep 17 00:00:00 2001 From: Nicolae Claudius Date: Thu, 1 Mar 2012 03:17:37 -0800 Subject: [PATCH] regenerate token, fix toke uniqueness scope --- app/controllers/as_controller.rb | 13 +++++++++++++ app/controllers/hosts_controller.rb | 19 ++++++++++++++++--- app/controllers/records_controller.rb | 9 ++++++++- app/helpers/records_helper.rb | 3 +++ app/models/record.rb | 2 +- app/views/as/_list_record.html.erb | 16 ++++++++++++++++ app/views/as/on_action_update.js.erb | 15 +++++++++++++++ config/routes.rb | 6 ++++++ 8 files changed, 78 insertions(+), 5 deletions(-) create mode 100644 app/views/as/_list_record.html.erb create mode 100644 app/views/as/on_action_update.js.erb diff --git a/app/controllers/as_controller.rb b/app/controllers/as_controller.rb index 66be7cb..b0c6a86 100644 --- a/app/controllers/as_controller.rb +++ b/app/controllers/as_controller.rb @@ -8,9 +8,22 @@ class AsController < ApplicationController conf.columns[:change_date].list_ui = :timestamp conf.columns[:ttl].options = {:i18n_number => {:delimiter => ''}} conf.actions.exclude :show + conf.action_links.add 'new_token', label: 'New Token', method: :put, + type: :member, position: false, confirm: 'Are you sure?' end include RecordsControllerCommon + def new_token + process_action_link_action do |record| + record.instance_variable_set(:@readonly, false) + record.generate_token + update_save(no_record_param_update: true) + if successful? + flash[:info] = "Token was updated successfully to #{record.authentication_token}" + end + end + end + protected # override to use :mx_records instead of :records assoc diff --git a/app/controllers/hosts_controller.rb b/app/controllers/hosts_controller.rb index 1ac1292..019cea2 100644 --- a/app/controllers/hosts_controller.rb +++ b/app/controllers/hosts_controller.rb @@ -5,17 +5,30 @@ class HostsController < ApplicationController conf.create.columns = [:name, :host_domain, :content, :ttl] conf.update.columns = [:name, :host_domain, :content, :ttl] conf.list.label = 'Hosts' - conf.list.sorting = {:name => :asc} + conf.list.sorting = {name: :asc} conf.create.link.label = "Add Host" conf.columns[:host_domain].form_ui = :select - conf.columns[:host_domain].options = {:options => Settings.host_domains} + conf.columns[:host_domain].options = {options: Settings.host_domains} conf.columns[:name].label = 'Host' conf.columns[:name].description = 'Ex. "your-name"' conf.columns[:content].label = 'IP' conf.columns[:content].description = 'Ex. "10.10.5.12"' conf.columns[:change_date].list_ui = :timestamp - conf.columns[:ttl].options = {:i18n_number => {:delimiter => ''}} + conf.columns[:ttl].options = {i18n_number: {delimiter: ''}} conf.actions.exclude :show + conf.action_links.add 'new_token', label: 'New Token', method: :put, + type: :member, position: false, confirm: 'Are you sure?' + end + + def new_token + process_action_link_action do |record| + record.instance_variable_set(:@readonly, false) + record.generate_token + update_save(no_record_param_update: true) + if successful? + flash[:info] = "Token was updated successfully to #{record.authentication_token}" + end + end end protected diff --git a/app/controllers/records_controller.rb b/app/controllers/records_controller.rb index b70d00d..6fa314e 100644 --- a/app/controllers/records_controller.rb +++ b/app/controllers/records_controller.rb @@ -25,6 +25,8 @@ class RecordsController < ApplicationController conf.columns[:ttl].options = {:i18n_number => {:delimiter => ''}} # conf.create.link.label = "Add Record" conf.actions.exclude :show + conf.action_links.add 'new_token', label: 'New Token', method: :put, + security_method: :a_record?, type: :member, position: false, confirm: 'Are you sure?' end include RecordsControllerCommon skip_before_filter :ensure_nested_under_domain, :only => 'modify' @@ -45,7 +47,7 @@ class RecordsController < ApplicationController format.html {render(:text => MODIFY_OK)} end end - + protected def new_model @@ -53,4 +55,9 @@ class RecordsController < ApplicationController before_create_save(record) record end + + # just to limit the action to A type records + def a_record?(record) + record.class == A + end end \ No newline at end of file diff --git a/app/helpers/records_helper.rb b/app/helpers/records_helper.rb index 4493314..1d09970 100644 --- a/app/helpers/records_helper.rb +++ b/app/helpers/records_helper.rb @@ -1,2 +1,5 @@ module RecordsHelper + def record_authentication_token_column(record) + record.type == 'A' ? record.authentication_token : '-' + end end \ No newline at end of file diff --git a/app/models/record.rb b/app/models/record.rb index cfd3ea7..3f61135 100644 --- a/app/models/record.rb +++ b/app/models/record.rb @@ -47,7 +47,7 @@ class Record < ActiveRecord::Base def generate_token self.authentication_token = loop do token = Devise.friendly_token - break token unless self.class.exists?(:authentication_token => token) + break token unless Record.exists?(authentication_token: token) end end diff --git a/app/views/as/_list_record.html.erb b/app/views/as/_list_record.html.erb new file mode 100644 index 0000000..af97ccf --- /dev/null +++ b/app/views/as/_list_record.html.erb @@ -0,0 +1,16 @@ +<%# add :controller_id => :as_records to respond in records list, not a's %> + +<% +record = list_record if list_record # compat with render :partial :collection +columns ||= list_columns +tr_class = cycle("", "even-record") +tr_class += " #{list_row_class(record)}" if respond_to? :list_row_class +url_options = params_for(:action => :list, :id => record.id) +action_links ||= active_scaffold_config.action_links.member +-%> + + + <%= render :partial => 'list_record_columns', :locals => {:record => record, :columns => columns} %> + <%= render :partial => 'list_actions', :locals => {:record => record, :url_options => url_options, :action_links => action_links} unless action_links.empty? %> + <%= render_nested_view(action_links, url_options, record) unless @nested_auto_open.nil? %> + diff --git a/app/views/as/on_action_update.js.erb b/app/views/as/on_action_update.js.erb new file mode 100644 index 0000000..0aa7a08 --- /dev/null +++ b/app/views/as/on_action_update.js.erb @@ -0,0 +1,15 @@ +<%# add :controller_id => :as_records to respond in records list, not a's %> + +<%if controller.send :successful?%> + ActiveScaffold.replace_html('<%=active_scaffold_messages_id(:controller_id => :as_records)%>','<%=escape_javascript(render(:partial => 'messages'))%>'); + <%if @record%> + ActiveScaffold.update_row('<%=element_row_id(:controller_id => :as_records, :action => :list, :id => @record.id)%>','<%=escape_javascript(render(:partial => 'list_record', :locals => {:record => @record}))%>'); + <%end%> + <% if active_scaffold_config.list.columns.any? {|c| c.calculation?}%> + ActiveScaffold.replace('<%=active_scaffold_calculations_id(:controller_id => :as_records)%>', '<%=escape_javascript(render(:partial => 'list_calculations'))%>'); + <% end %> +<%else%> + <%flash[:error] = active_scaffold_error_messages_for(@record, :object_name => "#{@record.class.model_name.human.downcase}#{@record.new_record? ? '' : ": #{@record.to_label}"}", :header_message => '', :message => "#{@record.class.model_name.human.downcase}#{@record.new_record? ? '' : ": #{@record.to_label}"}", :container_tag => nil, :list_type => :br)%> + ActiveScaffold.replace_html('<%=active_scaffold_messages_id(:controller_id => :as_records)%>','<%=escape_javascript(render(:partial => 'messages'))%>'); + ActiveScaffold.scroll_to('<%=active_scaffold_messages_id(:controller_id => :as_records)%>'); +<%end%> \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index df9afea..cfb083a 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -11,6 +11,9 @@ Entrydns::Application.routes.draw do resources :hosts do as_routes + member do + put 'new_token' + end end put '/records/modify/:authentication_token', :to => 'records#modify', :as => :modify_record @@ -32,6 +35,9 @@ Entrydns::Application.routes.draw do resources :as do as_routes + member do + put 'new_token' + end end resources :cnames do