add stuff to deal with banning users

This commit is contained in:
joshua stein 2014-01-12 15:09:32 -06:00
parent 68a1f02a1c
commit 287be48187
9 changed files with 100 additions and 11 deletions

View file

@ -9,7 +9,9 @@ class ApplicationController < ActionController::Base
def authenticate_user
if session[:u] &&
(@user = User.where(:session_token => session[:u].to_s).first)
(user = User.where(:session_token => session[:u].to_s).first) &&
!user.is_banned?
@user = user
Rails.logger.info " Logged in as user #{@user.id} (#{@user.username})"
end

View file

@ -15,8 +15,13 @@ class LoginController < ApplicationController
end
def login
if (user = User.where("email = ? OR username = ?", params[:email].to_s,
params[:email].to_s).first) &&
if params[:email].to_s.match(/@/)
user = User.where(:email => params[:email]).first
else
user = User.where(:username => params[:email]).first
end
if user && !user.is_banned? &&
user.try(:authenticate, params[:password].to_s)
session[:u] = user.session_token
return redirect_to "/"
@ -65,7 +70,7 @@ class LoginController < ApplicationController
# this will get reset upon save
@reset_user.session_token = nil
if @reset_user.save
if @reset_user.save && !@reset_user.is_banned?
session[:u] = @reset_user.session_token
return redirect_to "/"
end

View file

@ -0,0 +1,15 @@
class BanNotification < ActionMailer::Base
default :from => "#{Rails.application.name} " <<
"<nobody@#{Rails.application.domain}>"
def notify(user, banner, reason)
@banner = banner
@reason = reason
mail(
:from => "#{@banner.username} <#{@banner.email}>",
:to => user.email,
:subject => "[#{Rails.application.name}] You have been banned"
)
end
end

View file

@ -11,6 +11,8 @@ class User < ActiveRecord::Base
has_many :tag_filters
belongs_to :invited_by_user,
:class_name => "User"
belongs_to :banned_by_user,
:class_name => "User"
has_secure_password
@ -68,6 +70,25 @@ class User < ActiveRecord::Base
end
end
def ban_by_user_for_reason!(banner, reason)
self.banned_at = Time.now
self.banned_by_user_id = banner.id
self.banned_reason = reason
self.session_token = nil
self.check_session_token
self.save!
BanNotification.notify(self, banner, reason)
true
end
def is_banned?
banned_at?
end
def check_session_token
if self.session_token.blank?
self.session_token = Utils.random_str(60)
@ -127,6 +148,13 @@ class User < ActiveRecord::Base
username
end
def unban!
self.banned_at = nil
self.banned_by_user_id = nil
self.banned_reason = nil
self.save!
end
def undeleted_received_messages
received_messages.where(:deleted_by_recipient => false)
end

View file

@ -0,0 +1,6 @@
You have been banned from <%= Rails.application.name %> by <%= @banner.username %> for:
<%= word_wrap(@reason, :line_width => 72).gsub(/\n/, "\n ") %>
You are no longer allowed to login to the site. If you wish, you can
discuss this ban with the moderator by replying to this e-mail.

View file

@ -11,8 +11,14 @@
</div>
<label class="required">Status:</label>
<span class="d">
Active <%= @showing_user.is_admin? ? "administrator" :
<span class="d"
<%= @showing_user.is_banned? ? raw("style=\"color: red;\"") : "" %>>
<% if @showing_user.is_banned? %>
Inactive
<% else %>
Active
<% end %>
<%= @showing_user.is_admin? ? "administrator" :
(@showing_user.is_moderator? ? "moderator" : "user") %>
</span>
<br>
@ -22,12 +28,25 @@
<%= raw(time_ago_in_words_label(@showing_user.created_at)) %> ago
<% if @showing_user.invited_by_user %>
by invitation from
<a href="/u/<%= @showing_user.invited_by_user.username %>"><%=
@showing_user.invited_by_user.username %></a>
<%= link_to @showing_user.invited_by_user.try(:username),
@showing_user.invited_by_user %>
<% end %>
</span>
<br>
<% if @showing_user.is_banned? %>
<label class="required">Banned:</label>
<span class="d">
<%= raw(time_ago_in_words_label(@showing_user.banned_at)) %> ago
<% if @showing_user.banned_by_user %>
by <%= link_to @showing_user.banned_by_user.try(:username),
@showing_user.banned_by_user %>:
<em><%= @showing_user.banned_reason %></em>
<% end %>
</span>
<br>
<% end %>
<label class="required">Karma:</label>
<span class="d">
<%= @showing_user.karma %>, averaging <%=

View file

@ -10,8 +10,12 @@
<% if (user = subtree.pop) %>
<li>
<a href="/u/<%= user.username %>"
<%= (Time.now - user.created_at < 7.days ? raw("style=\"color: green;\"") :
"") %>><%= user.username %></a>&nbsp;(<%= user.karma %>)
<% if user.is_banned? %>
style="color: gray; text-decoration: line-through;"
<% elsif Time.now - user.created_at < 7.days %>
style="color: green;"
<% end %>
><%= user.username %></a>&nbsp;(<%= user.karma %>)
<% if user.is_admin? %>
(administrator)
<% elsif user.is_moderator? %>

View file

@ -0,0 +1,7 @@
class AddBanReason < ActiveRecord::Migration
def change
add_column :users, :banned_at, :datetime
add_column :users, :banned_by_user_id, :integer
add_column :users, :banned_reason, :string, :limit => 200
end
end

View file

@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20140109034338) do
ActiveRecord::Schema.define(version: 20140112192936) do
create_table "comments", force: true do |t|
t.datetime "created_at", null: false
@ -158,6 +158,9 @@ ActiveRecord::Schema.define(version: 20140109034338) do
t.string "mailing_list_token", limit: 75
t.boolean "mailing_list_enabled", default: false
t.integer "karma", default: 0, null: false
t.datetime "banned_at"
t.integer "banned_by_user_id"
t.string "banned_reason", limit: 200
end
add_index "users", ["mailing_list_enabled"], name: "mailing_list_enabled", using: :btree