Permalink
Browse files
add stuff to deal with banning users
- Loading branch information...
|
|
@@ -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
|
|
|
|
|
|
|
|
|
@@ -15,8 +15,13 @@ def index |
|
|
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 @@ def set_new_password |
|
|
# 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
|
|
|
|
|
|
@@ -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
|
|
|
@@ -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 @@ def average_karma |
|
|
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 @@ def to_param |
|
|
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
|
|
|
|
|
|
@@ -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.
|
|
|
@@ -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 <%=
|
|
|
|
|
|
@@ -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> (<%= 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> (<%= user.karma %>)
|
|
|
<% if user.is_admin? %>
|
|
|
(administrator)
|
|
|
<% elsif user.is_moderator? %>
|
|
|
|
|
|
@@ -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
|
|
|
@@ -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 @@ |
|
|
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
|
|
|
|
0 comments on commit
287be48