From 13af2f1d0b11475027bebc016ad76e8124482694 Mon Sep 17 00:00:00 2001 From: Nicolae Claudius Date: Sun, 26 Jan 2014 23:22:24 +0200 Subject: [PATCH] ban/unban users --- app/models/admin_ability.rb | 10 +++++ app/models/user.rb | 13 ++++++ app/views/rails_admin/main/ban.html.erb | 22 ++++++++++ config/initializers/rails_admin.rb | 39 +++++++++++++++-- config/locales/rails_admin.en.yml | 15 +++++++ lib/rails_admin/config/actions/ban.rb | 57 +++++++++++++++++++++++++ lib/rails_admin/config/actions/unban.rb | 33 ++++++++++++++ 7 files changed, 186 insertions(+), 3 deletions(-) create mode 100644 app/models/admin_ability.rb create mode 100644 app/views/rails_admin/main/ban.html.erb create mode 100644 config/locales/rails_admin.en.yml create mode 100644 lib/rails_admin/config/actions/ban.rb create mode 100644 lib/rails_admin/config/actions/unban.rb diff --git a/app/models/admin_ability.rb b/app/models/admin_ability.rb new file mode 100644 index 0000000..8f3c000 --- /dev/null +++ b/app/models/admin_ability.rb @@ -0,0 +1,10 @@ +class AdminAbility + include CanCan::Ability + + def initialize(admin) + can :access, :rails_admin + can :manage, :all + cannot [:ban, :unban], :all + can [:ban, :unban], User + end +end \ No newline at end of file diff --git a/app/models/user.rb b/app/models/user.rb index b4eb4cc..d66b29b 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -48,6 +48,19 @@ class User < ActiveRecord::Base !active? ? :deactivated : super end + def ban! + self.class.transaction do + update_column :active, false + domains.each &:destroy + records.each &:destroy + permissions.each &:destroy + end + end + + def unban! + update_column :active, true + end + def to_paper_trail "#{id} #{email} name:#{full_name} ip:#{current_sign_in_ip} last_ip:#{last_sign_in_ip}" end diff --git a/app/views/rails_admin/main/ban.html.erb b/app/views/rails_admin/main/ban.html.erb new file mode 100644 index 0000000..bb6ec5f --- /dev/null +++ b/app/views/rails_admin/main/ban.html.erb @@ -0,0 +1,22 @@ +

+ <%= t("admin.form.are_you_sure_you_want_to_ban", + :model_name => @abstract_model.pretty_name.downcase) %> + “ + <%= @model_config.with(:object => @object).object_label %> + ” + <%= t("admin.form.all_of_the_following_related_items_will_be_deleted") %> +

+ +<%= form_for(@object, :url => ban_path(:model_name => @abstract_model.to_param, :id => @object.id), :html => {:method => "patch"}) do %> + +
+ + +
+<% end %> diff --git a/config/initializers/rails_admin.rb b/config/initializers/rails_admin.rb index c8a17f8..d06d252 100644 --- a/config/initializers/rails_admin.rb +++ b/config/initializers/rails_admin.rb @@ -1,5 +1,5 @@ -# RailsAdmin config file. Generated on September 10, 2012 23:57 -# See github.com/sferik/rails_admin for more informations +require 'rails_admin/config/actions/ban' +require 'rails_admin/config/actions/unban' RailsAdmin.config do |config| @@ -15,11 +15,13 @@ RailsAdmin.config do |config| # Or with a PaperTrail: (you need to install it first) # config.audit_with :paper_trail, Admin + # Set the admin name here (optional second array element will appear in a beautiful RailsAdmin red ©) config.main_app_name = ['Entrydns', 'Admin'] # or for a dynamic name: # config.main_app_name = Proc.new { |controller| [Rails.application.engine_name.titleize, controller.params['action'].titleize] } + config.authorize_with :cancan, AdminAbility # ==> Global show view settings # Display empty fields in show views @@ -35,8 +37,39 @@ RailsAdmin.config do |config| # Add models here if you want to go 'whitelist mode': config.included_models = [A, AAAA, Admin, CNAME, Domain, MX, NS, Permission, - Record, SOA, SRV, TXT, User, BlacklistedDomain, PaperTrail::Version] + Record, SOA, SRV, TXT, User, Authentication, BlacklistedDomain, PaperTrail::Version] + + config.model Authentication do |conf| + parent User + end + config.model Permission do |conf| + parent User + end + + config.model Record do |conf| + parent Domain + end + + config.actions do + # root actions + dashboard # mandatory + # collection actions + index # mandatory + new + export + history_index + bulk_delete + # member actions + show + edit + delete + history_show + show_in_app + ban + unban + end + # Application wide tried label methods for models' instances # config.label_methods << :description # Default is [:name, :title] diff --git a/config/locales/rails_admin.en.yml b/config/locales/rails_admin.en.yml new file mode 100644 index 0000000..782301f --- /dev/null +++ b/config/locales/rails_admin.en.yml @@ -0,0 +1,15 @@ +en: + admin: + form: + are_you_sure_you_want_to_ban: "Are you sure you want to ban this %{model_name}" + actions: + ban: + menu: "Ban" + title: "Ban" + breadcrumb: "Ban" + done: "User successfully banned" + unban: + menu: "Unban" + title: "Unban" + breadcrumb: "Unban" + done: "User successfully unbanned" diff --git a/lib/rails_admin/config/actions/ban.rb b/lib/rails_admin/config/actions/ban.rb new file mode 100644 index 0000000..d880e29 --- /dev/null +++ b/lib/rails_admin/config/actions/ban.rb @@ -0,0 +1,57 @@ +require 'rails_admin/config/actions' +require 'rails_admin/config/actions/base' + +module RailsAdmin + module Config + module Actions + + class Ban < RailsAdmin::Config::Actions::Base + RailsAdmin::Config::Actions.register(self) + + register_instance_option :member do + true + end + + register_instance_option :http_methods do + [:get, :patch] + end + + register_instance_option :visible? do + authorized? && bindings[:object].active? + end + + register_instance_option :controller do + Proc.new do + if request.get? # BAN + + respond_to do |format| + format.html { render 'ban' } + format.js { render 'ban', :layout => false } + end + + elsif request.patch? # PATCH + + redirect_path = nil + @auditing_adapter && @auditing_adapter.delete_object(@object, @abstract_model, _current_user) + if @object.ban! + flash[:success] = t("admin.flash.successful", :name => @model_config.label, :action => t("admin.actions.ban.done")) + redirect_path = index_path + else + flash[:error] = t("admin.flash.error", :name => @model_config.label, :action => t("admin.actions.ban.done")) + redirect_path = back_or_index + end + + redirect_to redirect_path + + end + end + end + + register_instance_option :link_icon do + 'icon- fa-ban' + end + end + + end + end +end \ No newline at end of file diff --git a/lib/rails_admin/config/actions/unban.rb b/lib/rails_admin/config/actions/unban.rb new file mode 100644 index 0000000..58a6835 --- /dev/null +++ b/lib/rails_admin/config/actions/unban.rb @@ -0,0 +1,33 @@ +require 'rails_admin/config/actions' +require 'rails_admin/config/actions/base' + +module RailsAdmin + module Config + module Actions + + class Unban < RailsAdmin::Config::Actions::Base + RailsAdmin::Config::Actions.register(self) + + register_instance_option :member do + true + end + + register_instance_option :visible? do + authorized? && !bindings[:object].active? + end + + register_instance_option :controller do + Proc.new do + @object.unban! + redirect_to back_or_index + end + end + + register_instance_option :link_icon do + 'icon-ok' + end + end + + end + end +end \ No newline at end of file