Browse Source

ban/unban users

pull/1/head
Nicolae Claudius 11 years ago
parent
commit
13af2f1d0b
  1. 10
      app/models/admin_ability.rb
  2. 13
      app/models/user.rb
  3. 22
      app/views/rails_admin/main/ban.html.erb
  4. 39
      config/initializers/rails_admin.rb
  5. 15
      config/locales/rails_admin.en.yml
  6. 57
      lib/rails_admin/config/actions/ban.rb
  7. 33
      lib/rails_admin/config/actions/unban.rb

10
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

13
app/models/user.rb

@ -48,6 +48,19 @@ class User < ActiveRecord::Base
!active? ? :deactivated : super !active? ? :deactivated : super
end 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 def to_paper_trail
"#{id} #{email} name:#{full_name} ip:#{current_sign_in_ip} last_ip:#{last_sign_in_ip}" "#{id} #{email} name:#{full_name} ip:#{current_sign_in_ip} last_ip:#{last_sign_in_ip}"
end end

22
app/views/rails_admin/main/ban.html.erb

@ -0,0 +1,22 @@
<h4>
<%= t("admin.form.are_you_sure_you_want_to_ban",
:model_name => @abstract_model.pretty_name.downcase) %>
&ldquo;
<strong><%= @model_config.with(:object => @object).object_label %></strong>
&rdquo;
<%= t("admin.form.all_of_the_following_related_items_will_be_deleted") %>
</h4>
<ul>
<%= render :partial => "delete_notice", :object => @object %>
</ul>
<%= form_for(@object, :url => ban_path(:model_name => @abstract_model.to_param, :id => @object.id), :html => {:method => "patch"}) do %>
<input name="return_to" type="<%= :hidden %>" value="<%= (params[:return_to].presence || request.referer) %>"></input>
<div class="form-actions">
<button class="btn btn-danger" data-disable-with="<%= t("admin.form.confirmation") %>" type="submit">
<i class="icon-white icon-ok"></i> <%= t("admin.form.confirmation") %>
</button>
<button class="btn" data-disable-with="<%= t("admin.form.cancel") %>" name="_continue" type="submit">
<i class="icon-remove"></i> <%= t("admin.form.cancel") %>
</button>
</div>
<% end %>

39
config/initializers/rails_admin.rb

@ -1,5 +1,5 @@
# RailsAdmin config file. Generated on September 10, 2012 23:57 require 'rails_admin/config/actions/ban'
# See github.com/sferik/rails_admin for more informations require 'rails_admin/config/actions/unban'
RailsAdmin.config do |config| RailsAdmin.config do |config|
@ -15,11 +15,13 @@ RailsAdmin.config do |config|
# Or with a PaperTrail: (you need to install it first) # Or with a PaperTrail: (you need to install it first)
# config.audit_with :paper_trail, Admin # config.audit_with :paper_trail, Admin
# Set the admin name here (optional second array element will appear in a beautiful RailsAdmin red ©) # Set the admin name here (optional second array element will appear in a beautiful RailsAdmin red ©)
config.main_app_name = ['Entrydns', 'Admin'] config.main_app_name = ['Entrydns', 'Admin']
# or for a dynamic name: # or for a dynamic name:
# config.main_app_name = Proc.new { |controller| [Rails.application.engine_name.titleize, controller.params['action'].titleize] } # 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 # ==> Global show view settings
# Display empty fields in show views # Display empty fields in show views
@ -35,7 +37,38 @@ RailsAdmin.config do |config|
# Add models here if you want to go 'whitelist mode': # Add models here if you want to go 'whitelist mode':
config.included_models = [A, AAAA, Admin, CNAME, Domain, MX, NS, Permission, 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 # Application wide tried label methods for models' instances
# config.label_methods << :description # Default is [:name, :title] # config.label_methods << :description # Default is [:name, :title]

15
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"

57
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

33
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
Loading…
Cancel
Save