diff --git a/app/controllers/admin/blocked_domains_controller.rb b/app/controllers/admin/blocked_domains_controller.rb index 9394a9334..a21e01fd1 100644 --- a/app/controllers/admin/blocked_domains_controller.rb +++ b/app/controllers/admin/blocked_domains_controller.rb @@ -2,46 +2,54 @@ class Admin::BlockedDomainsController < AdminController load_and_authorize_resource def index - bd = BlockedDomain.pluck(:name) - if bd - @blocked_domains = bd.to_yaml.gsub("---\n", '').gsub("-", '').gsub(" ", '') - end + + params[:q] ||= {} + domains = BlockedDomain.all + @q = domains.search(params[:q]) + @domains = @q.result.page(params[:page]) + @domains = @domains.per(params[:results_per_page]) if params[:results_per_page].to_i > 0 + + end + + def new + + @domain = BlockedDomain.new + end def create - @blocked_domains = params[:blocked_domains] - begin - params[:blocked_domains] = "---\n" if params[:blocked_domains].blank? - names = YAML.load(params[:blocked_domains]) - fail if names == false - rescue - flash.now[:alert] = I18n.t('invalid_yaml') - logger.warn 'Invalid YAML' - render :index and return - end + @domain = BlockedDomain.new(blocked_domain_params) - names = names.split(' ') - result = true - BlockedDomain.transaction do - existing = BlockedDomain.any_of_domains(names).pluck(:id) - BlockedDomain.where.not(id: existing).destroy_all - - names.each do |name| - rec = BlockedDomain.find_or_initialize_by(name: name) - unless rec.save - result = false - raise ActiveRecord::Rollback - end - end - end - - if result - flash[:notice] = I18n.t('record_updated') - redirect_to :back + if @domain.save + flash[:notice] = I18n.t('domain_added') + redirect_to admin_blocked_domains_path else - flash.now[:alert] = I18n.t('failed_to_update_record') - render :index + flash.now[:alert] = I18n.t('failed_to_add_domain') + render 'new' + end + + end + + def delete + + if BlockedDomain.find(params[:id]).destroy + flash[:notice] = I18n.t('domain_deleted') + redirect_to admin_blocked_domains_path + else + flash.now[:alert] = I18n.t('failed_to_delete_domain') + redirect_to admin_blocked_domains_path end end -end + + + def blocked_domain_params + params.require(:blocked_domain).permit(:name) + end + + private + + def set_domain + @domain = BlockedDomain.find(params[:id]) + end +end \ No newline at end of file diff --git a/app/controllers/admin/reserved_domains_controller.rb b/app/controllers/admin/reserved_domains_controller.rb index 4cb7ba51f..319a6275c 100644 --- a/app/controllers/admin/reserved_domains_controller.rb +++ b/app/controllers/admin/reserved_domains_controller.rb @@ -1,5 +1,6 @@ class Admin::ReservedDomainsController < AdminController load_and_authorize_resource + before_action :set_domain, only: [:edit, :update] def index @@ -16,10 +17,52 @@ class Admin::ReservedDomainsController < AdminController end def edit - authorize! :update, ReservedDomain + end + + def create + + @domain = ReservedDomain.new(reserved_domain_params) + + if @domain.save + flash[:notice] = I18n.t('domain_added') + redirect_to admin_reserved_domains_path + else + flash.now[:alert] = I18n.t('failed_to_add_domain') + render 'new' + end + + end + + def update + + if @domain.update(reserved_domain_params) + flash[:notice] = I18n.t('domain_updated') + else + flash.now[:alert] = I18n.t('failed_to_update_domain') + end + render 'edit' + end def delete - authorize! :delete, ReservedDomain + + if ReservedDomain.find(params[:id]).destroy + flash[:notice] = I18n.t('domain_deleted') + redirect_to admin_reserved_domains_path + else + flash.now[:alert] = I18n.t('failed_to_delete_domain') + redirect_to admin_reserved_domains_path + end + + end + + private + + def reserved_domain_params + params.require(:reserved_domain).permit(:name, :password) + end + + def set_domain + @domain = ReservedDomain.find(params[:id]) end end diff --git a/app/models/blocked_domain.rb b/app/models/blocked_domain.rb index f5ca0371c..fd7c2fc24 100644 --- a/app/models/blocked_domain.rb +++ b/app/models/blocked_domain.rb @@ -2,6 +2,7 @@ class BlockedDomain < ActiveRecord::Base include Versions before_save :generate_data before_destroy :remove_data + validates :name, domain_name: true, uniqueness: true class << self diff --git a/app/models/reserved_domain.rb b/app/models/reserved_domain.rb index c5d0cf9f2..8b2cb49e6 100644 --- a/app/models/reserved_domain.rb +++ b/app/models/reserved_domain.rb @@ -3,6 +3,9 @@ class ReservedDomain < ActiveRecord::Base before_save :fill_empty_passwords before_save :generate_data before_destroy :remove_data + validates :name, domain_name: true, uniqueness: true + + class << self @@ -22,7 +25,12 @@ class ReservedDomain < ActiveRecord::Base def fill_empty_passwords - self.password = SecureRandom.hex unless self.password + + if self.password.empty? + + self.password = SecureRandom.hex + + end end def name= val diff --git a/app/views/admin/blocked_domains/_form.haml b/app/views/admin/blocked_domains/_form.haml new file mode 100644 index 000000000..996d52843 --- /dev/null +++ b/app/views/admin/blocked_domains/_form.haml @@ -0,0 +1,17 @@ += form_for([:admin, @domain], html: {class: 'form-horizontal'}) do |f| + = render 'shared/full_errors', object: @domain + + .row + .col-md-8 + .panel.panel-default + .panel-heading.clearfix + .pull-left= t(:general) + .panel-body + .form-group + .col-md-4.control-label + = f.label :name + .col-md-7 + = f.text_field(:name, class: 'form-control') + .row + .col-md-8.text-right + = button_tag(t(:save), class: 'btn btn-primary') diff --git a/app/views/admin/blocked_domains/edit.haml b/app/views/admin/blocked_domains/edit.haml new file mode 100644 index 000000000..51d77f0cc --- /dev/null +++ b/app/views/admin/blocked_domains/edit.haml @@ -0,0 +1,3 @@ += render 'shared/title', name: t(:edit_pw) + += render 'form' diff --git a/app/views/admin/blocked_domains/index.haml b/app/views/admin/blocked_domains/index.haml index bd5660193..5accae030 100644 --- a/app/views/admin/blocked_domains/index.haml +++ b/app/views/admin/blocked_domains/index.haml @@ -1,10 +1,67 @@ +- content_for :actions do + = link_to(t(:new), new_admin_blocked_domain_path, class: 'btn btn-primary') = render 'shared/title', name: t(:blocked_domains) -= form_tag([:admin, :blocked_domains]) do |f| - .row - .col-md-12 - = text_area_tag :blocked_domains, @blocked_domains, class: 'form-control', rows: 30 - %hr - .row - .col-md-12.text-right - %button.btn.btn-warning=t(:save) +.row + .col-md-12 + = search_form_for [:admin, @q], html: { style: 'margin-bottom: 0;', class: 'js-form', autocomplete: 'off' } do |f| + .row + .col-md-3 + .form-group + = f.label :name + = f.search_field :name_matches, value: params[:q][:name_matches], class: 'form-control', placeholder: t(:name) + .col-md-3 + .form-group + = f.label t(:created_at_from) + = f.search_field :created_at_gteq, value: params[:q][:created_at_gteq], class: 'form-control datepicker', placeholder: t(:created_at_from) + .col-md-3 + .form-group + = f.label t(:created_at_until) + = f.search_field :created_at_lteq, value: params[:q][:created_at_lteq], class: 'form-control datepicker', placeholder: t(:created_at_until) + .row + .col-md-3 + .form-group + = label_tag t(:results_per_page) + = text_field_tag :results_per_page, params[:results_per_page], class: 'form-control', placeholder: t(:results_per_page) + .col-md-3{style: 'padding-top: 25px;'} + %button.btn.btn-primary +   + %span.glyphicon.glyphicon-search +   + %button.btn.btn-default.js-reset-form + = t(:clear_fields) +%hr +.row + .col-md-12 + .table-responsive + %table.table.table-hover.table-bordered.table-condensed + %thead + %tr + %th{class: 'col-xs-2'} + = sort_link(@q, 'name') + %th{class: 'col-xs-2'} + = sort_link(@q, 'created_at', t(:created_at)) + %th{class: 'col-xs-2'} + = sort_link(@q, 'updated_at', t(:updated_at)) + %th{class: 'col-xs-2'} + = t(:actions) + %tbody + - @domains.each do |x| + %tr + %td= x.name + %td= l(x.created_at, format: :short) + %td= l(x.updated_at, format: :short) + %td + = link_to(t(:delete), delete_admin_blocked_domain_path(id: x.id), + data: { confirm: t(:are_you_sure) }, class: 'btn btn-danger btn-xs') +.row + .col-md-6 + = paginate @domains + .col-md-6.text-right + .pagination + = t(:result_count, count: @domains.total_count) + +:coffee + $(".js-reset-form").on "click", (e) -> + e.preventDefault(); + window.location = "#{admin_blocked_domains_path}" diff --git a/app/views/admin/blocked_domains/new.haml b/app/views/admin/blocked_domains/new.haml new file mode 100644 index 000000000..4461eea40 --- /dev/null +++ b/app/views/admin/blocked_domains/new.haml @@ -0,0 +1,3 @@ += render 'shared/title', name: t(:add_blocked_domain) + += render 'form' diff --git a/app/views/admin/reserved_domains/_form.haml b/app/views/admin/reserved_domains/_form.haml index bfbacbfd9..ec7492659 100644 --- a/app/views/admin/reserved_domains/_form.haml +++ b/app/views/admin/reserved_domains/_form.haml @@ -11,13 +11,12 @@ .col-md-4.control-label = f.label :name .col-md-7 - = f.text_field(:name, class: 'form-control') + = f.text_field(:name, class: 'form-control', disabled: !f.object.new_record?) .form-group .col-md-4.control-label = f.label :password .col-md-7 - = f.text_field(:password, class: 'form-control') -%hr + = f.text_field(:password, placeholder: t(:optional), class: 'form-control') .row .col-md-8.text-right = button_tag(t(:save), class: 'btn btn-primary') diff --git a/app/views/admin/reserved_domains/edit.haml b/app/views/admin/reserved_domains/edit.haml new file mode 100644 index 000000000..51d77f0cc --- /dev/null +++ b/app/views/admin/reserved_domains/edit.haml @@ -0,0 +1,3 @@ += render 'shared/title', name: t(:edit_pw) + += render 'form' diff --git a/app/views/admin/reserved_domains/index.haml b/app/views/admin/reserved_domains/index.haml index 6ac65d14b..06825b624 100644 --- a/app/views/admin/reserved_domains/index.haml +++ b/app/views/admin/reserved_domains/index.haml @@ -55,10 +55,10 @@ %td= l(x.created_at, format: :short) %td= l(x.updated_at, format: :short) %td - = link_to(t(:edit), edit_admin_reserved_domain_path(id: x.id), + = link_to(t(:edit_pw), edit_admin_reserved_domain_path(id: x.id), class: 'btn btn-primary btn-xs') = link_to(t(:delete), delete_admin_reserved_domain_path(id: x.id), - class: 'btn btn-primary btn-xs') + data: { confirm: t(:are_you_sure) }, class: 'btn btn-danger btn-xs') .row .col-md-6 = paginate @domains diff --git a/app/views/admin/reserved_domains/new.haml b/app/views/admin/reserved_domains/new.haml index 937fdebde..cd6e189f9 100644 --- a/app/views/admin/reserved_domains/new.haml +++ b/app/views/admin/reserved_domains/new.haml @@ -1,3 +1,3 @@ -= render 'shared/title', name: t(:new_reserved_domain) += render 'shared/title', name: t(:add_reserved_domain) = render 'form' diff --git a/config/locales/en.yml b/config/locales/en.yml index d86d08404..db0ca37ce 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -932,3 +932,7 @@ en: if_auth_info_is_left_empty_it_will_be_auto_generated: 'If auth info is left empty, it will be auto generated.' each_domain_name_must_end_with_colon_sign: 'Each domain name must end with colon (:) sign.' expiration_remind_subject: 'The %{name} domain has expired' + add_reserved_domain: 'Add domain to reserved list' + add_blocked_domain: 'Add domain to blocked list' + edit_pw: 'Edit Pw' + optional: 'Optional' \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index c008d44bd..e0a331256 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -221,8 +221,16 @@ Rails.application.routes.draw do resources :settings - resources :blocked_domains - resources :reserved_domains + resources :blocked_domains do + member do + get 'delete' + end + end + resources :reserved_domains do + member do + get 'delete' + end + end resources :registrars do resources :api_users