From e2a38ebc1769af3994e7f53c08d7b9ca65097c85 Mon Sep 17 00:00:00 2001 From: r888888888 Date: Sun, 21 Apr 2013 11:36:43 -0700 Subject: [PATCH] fixes #1284 --- app/assets/stylesheets/common/main_layout.css.scss | 2 +- app/controllers/application_controller.rb | 2 +- app/controllers/bans_controller.rb | 2 +- app/logical/anonymous_user.rb | 4 ++++ app/models/ban.rb | 10 ++++++++++ app/views/bans/show.html.erb | 6 +++++- app/views/layouts/default.html.erb | 4 ++++ app/views/users/_ban_notice.html.erb | 5 +++++ app/views/users/_secondary_links.html.erb | 5 +++++ db/seeds.rb | 4 ++-- test/unit/ban_test.rb | 8 ++++++++ 11 files changed, 46 insertions(+), 6 deletions(-) create mode 100644 app/views/users/_ban_notice.html.erb diff --git a/app/assets/stylesheets/common/main_layout.css.scss b/app/assets/stylesheets/common/main_layout.css.scss index d21ab394e..e4a607b4b 100644 --- a/app/assets/stylesheets/common/main_layout.css.scss +++ b/app/assets/stylesheets/common/main_layout.css.scss @@ -8,7 +8,7 @@ div#page { overflow: visible; margin: 0 30px; - div#upgrade-account-notice, div#sign-up-notice, div#tos-notice { + div#upgrade-account-notice, div#sign-up-notice, div#tos-notice, div#ban-notice { margin: 1em 0; padding: 1em; text-align: center; diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index f7ba73b2b..456de2626 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -88,7 +88,7 @@ protected %w(member banned builder privileged platinum contributor janitor moderator admin).each do |level| define_method("#{level}_only") do - if CurrentUser.user.__send__("is_#{level}?") + if !CurrentUser.user.is_banned? && CurrentUser.user.__send__("is_#{level}?") true else access_denied() diff --git a/app/controllers/bans_controller.rb b/app/controllers/bans_controller.rb index b0dd2b44f..d31787883 100644 --- a/app/controllers/bans_controller.rb +++ b/app/controllers/bans_controller.rb @@ -2,7 +2,7 @@ class BansController < ApplicationController before_filter :moderator_only, :except => [:show, :index] def new - @ban = Ban.new + @ban = Ban.new(params[:ban]) end def edit diff --git a/app/logical/anonymous_user.rb b/app/logical/anonymous_user.rb index a4d49219b..c2590a0d5 100644 --- a/app/logical/anonymous_user.rb +++ b/app/logical/anonymous_user.rb @@ -32,6 +32,10 @@ class AnonymousUser true end + def is_banned? + false + end + def has_mail? false end diff --git a/app/models/ban.rb b/app/models/ban.rb index 223705f2a..eaf5424e6 100644 --- a/app/models/ban.rb +++ b/app/models/ban.rb @@ -1,5 +1,7 @@ class Ban < ActiveRecord::Base after_create :update_feedback + after_create :update_user_on_create + after_destroy :update_user_on_destroy belongs_to :user belongs_to :banner, :class_name => "User" attr_accessible :reason, :duration, :user_id, :user_name @@ -67,6 +69,14 @@ class Ban < ActiveRecord::Base end end + def update_user_on_create + user.update_attribute(:is_banned, true) + end + + def update_user_on_destroy + user.update_attribute(:is_banned, false) + end + def user_name user ? user.name : nil end diff --git a/app/views/bans/show.html.erb b/app/views/bans/show.html.erb index e76fb3249..a6edd264e 100644 --- a/app/views/bans/show.html.erb +++ b/app/views/bans/show.html.erb @@ -1,11 +1,15 @@

Show Ban

-
    +
    • User: <%= link_to_user(@ban.user) %>
    • Expires: <%= compact_time @ban.expires_at %>
    • Reason: <%= @ban.reason %>
    + + <%= form_tag(ban_path(@ban), :method => :delete) do %> + <%= submit_tag "Unban" %> + <% end %>
diff --git a/app/views/layouts/default.html.erb b/app/views/layouts/default.html.erb index 97620c8ba..5616d07d7 100644 --- a/app/views/layouts/default.html.erb +++ b/app/views/layouts/default.html.erb @@ -44,6 +44,10 @@ <%= render "users/upgrade_notice" %> <% end %> + <% if CurrentUser.user.is_banned? %> + <%= render "users/ban_notice" %> + <% end %> + <% if cookies["accepted_tos"].blank? && !CurrentUser.is_privileged? %> <%= render "users/tos" %> <% end %> diff --git a/app/views/users/_ban_notice.html.erb b/app/views/users/_ban_notice.html.erb new file mode 100644 index 000000000..4f9de5b75 --- /dev/null +++ b/app/views/users/_ban_notice.html.erb @@ -0,0 +1,5 @@ +
+

Your account has been temporarily banned

+

Reason: <%= CurrentUser.user.ban.reason %>

+

Your ban will expire in <%= time_ago_in_words(CurrentUser.user.ban.expires_at) %>

+
diff --git a/app/views/users/_secondary_links.html.erb b/app/views/users/_secondary_links.html.erb index 38135c06e..3baf3dae1 100644 --- a/app/views/users/_secondary_links.html.erb +++ b/app/views/users/_secondary_links.html.erb @@ -27,6 +27,11 @@ <% if CurrentUser.is_moderator? %>
  • <%= link_to "Promote", edit_admin_user_path(@user) %>
  • + <% if @user.is_banned? %> +
  • <%= link_to "Unban", ban_path(@user.ban) %>
  • + <% else %> +
  • <%= link_to "Ban", new_ban_path(:ban => {:user_id => @user.id}) %>
  • + <% end %> <% end %>
  • |
  • diff --git a/db/seeds.rb b/db/seeds.rb index 42712ff98..ae48aede0 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -47,8 +47,8 @@ if User.count == 0 0.upto(10) do |i| User.create( :name => rand_string(8, true), - :password => i.to_s * 5, - :password_confirmation => i.to_s * 5 + :password => "password1", + :password_confirmation => "password1" ) end $used_names = Set.new([""]) diff --git a/test/unit/ban_test.rb b/test/unit/ban_test.rb index cc86567a4..2d856e269 100644 --- a/test/unit/ban_test.rb +++ b/test/unit/ban_test.rb @@ -15,6 +15,14 @@ class BanTest < ActiveSupport::TestCase CurrentUser.ip_addr = nil end + should "set the is_banned flag on the user" do + user = FactoryGirl.create(:user) + ban = FactoryGirl.build(:ban, :user => user, :banner => @banner) + ban.save + user.reload + assert(user.is_banned?) + end + should "not be valid against another admin" do user = FactoryGirl.create(:admin_user) ban = FactoryGirl.build(:ban, :user => user, :banner => @banner)