2014-08-23 16:16:09 +09:00
|
|
|
require "digest/sha2"
|
2010-04-20 23:05:11 +00:00
|
|
|
|
|
|
|
class UserController < ApplicationController
|
|
|
|
layout "default"
|
2014-08-23 16:23:32 +09:00
|
|
|
before_action :blocked_only, :only => [:authenticate, :update, :edit, :modify_blacklist]
|
|
|
|
before_action :janitor_only, :only => [:invites]
|
|
|
|
before_action :mod_only, :only => [:block, :unblock, :show_blocked_users]
|
|
|
|
before_action :post_member_only, :only => [:set_avatar]
|
|
|
|
before_action :no_anonymous, :only => [:change_password, :change_email, :show]
|
2010-09-07 03:25:28 +00:00
|
|
|
helper :post, :tag_subscription
|
2010-04-20 23:05:11 +00:00
|
|
|
helper :avatar
|
|
|
|
|
|
|
|
protected
|
2014-08-23 20:19:29 +09:00
|
|
|
|
2010-04-20 23:05:11 +00:00
|
|
|
def save_cookies(user)
|
2014-08-23 16:44:43 +09:00
|
|
|
cookies[:login] = { :value => user.name, :expires => 1.year.from_now }
|
|
|
|
cookies[:pass_hash] = { :value => user.password_hash, :expires => 1.year.from_now, :httponly => true }
|
2010-04-20 23:05:11 +00:00
|
|
|
session[:user_id] = user.id
|
|
|
|
end
|
|
|
|
|
|
|
|
public
|
2014-08-23 20:19:29 +09:00
|
|
|
|
2012-07-26 20:55:37 +02:00
|
|
|
def autocomplete_name
|
|
|
|
keyword = params[:term].to_s
|
2014-08-23 16:16:09 +09:00
|
|
|
@users = User.where(["name ILIKE ?", "*#{keyword}*".to_escaped_for_sql_like]).pluck(:name) if keyword.length >= 2
|
2012-07-26 20:55:37 +02:00
|
|
|
respond_to do |format|
|
|
|
|
format.json { render :json => (@users || []) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-07-08 23:23:33 +07:00
|
|
|
# FIXME: this method is crap and only function as temporary workaround
|
|
|
|
# until I convert the controllers to resourceful version which is
|
|
|
|
# planned for 3.2 branch (at least 3.2.1).
|
|
|
|
def remove_avatar
|
|
|
|
# When removing other user's avatar, ensure current user is mod or higher.
|
2014-08-23 18:06:02 +09:00
|
|
|
if @current_user.id != params[:id] && !@current_user.is_mod_or_higher?
|
2012-07-08 23:23:33 +07:00
|
|
|
access_denied
|
|
|
|
return
|
|
|
|
end
|
|
|
|
@user = User.find(params[:id])
|
|
|
|
@user.avatar_post_id = nil
|
|
|
|
if @user.save
|
2014-08-23 16:16:09 +09:00
|
|
|
flash[:notice] = "Avatar removed"
|
2012-07-08 23:23:33 +07:00
|
|
|
else
|
2014-08-23 16:16:09 +09:00
|
|
|
flash[:notice] = "Failed removing avatar"
|
2012-07-08 23:23:33 +07:00
|
|
|
end
|
|
|
|
redirect_to :action => :show, :id => params[:id]
|
|
|
|
end
|
|
|
|
|
2012-06-11 19:48:35 +07:00
|
|
|
def change_password
|
2014-08-23 16:16:09 +09:00
|
|
|
@title = "Change Password"
|
2014-09-08 17:24:35 +09:00
|
|
|
respond_to { |format| format.html { render :layout => "settings" } }
|
2012-06-11 19:48:35 +07:00
|
|
|
end
|
|
|
|
|
2012-06-27 21:07:15 +07:00
|
|
|
def change_email
|
2014-08-23 16:16:09 +09:00
|
|
|
@title = "Change Email"
|
2012-06-27 21:07:15 +07:00
|
|
|
@current_user.current_email = @current_user.email
|
2014-09-08 17:24:35 +09:00
|
|
|
respond_to { |format| format.html { render :layout => "settings" } }
|
2012-06-27 21:07:15 +07:00
|
|
|
end
|
|
|
|
|
2010-04-20 23:05:11 +00:00
|
|
|
def show
|
|
|
|
if params[:name]
|
|
|
|
@user = User.find_by_name(params[:name])
|
|
|
|
else
|
|
|
|
@user = User.find(params[:id])
|
|
|
|
end
|
|
|
|
|
|
|
|
if @user.nil?
|
|
|
|
redirect_to "/404"
|
|
|
|
end
|
|
|
|
if @current_user.is_mod_or_higher?
|
2014-08-23 16:16:09 +09:00
|
|
|
@user_ips = @user.user_logs.order("created_at DESC").pluck("ip_addr").uniq
|
2010-04-20 23:05:11 +00:00
|
|
|
end
|
2012-09-16 06:49:14 -07:00
|
|
|
respond_to do |format|
|
|
|
|
format.html
|
|
|
|
end
|
2010-04-20 23:05:11 +00:00
|
|
|
end
|
2012-06-04 17:13:54 +07:00
|
|
|
|
2010-04-20 23:05:11 +00:00
|
|
|
def invites
|
|
|
|
if request.post?
|
|
|
|
if params[:member]
|
|
|
|
begin
|
|
|
|
@current_user.invite!(params[:member][:name], params[:member][:level])
|
|
|
|
flash[:notice] = "User was invited"
|
2012-06-04 17:13:54 +07:00
|
|
|
|
2010-04-20 23:05:11 +00:00
|
|
|
rescue ActiveRecord::RecordNotFound
|
|
|
|
flash[:notice] = "Account not found"
|
2012-06-04 17:13:54 +07:00
|
|
|
|
2010-04-20 23:05:11 +00:00
|
|
|
rescue User::NoInvites
|
|
|
|
flash[:notice] = "You have no invites for use"
|
2012-06-04 17:13:54 +07:00
|
|
|
|
2010-04-20 23:05:11 +00:00
|
|
|
rescue User::HasNegativeRecord
|
|
|
|
flash[:notice] = "This use has a negative record and must be invited by an admin"
|
|
|
|
end
|
|
|
|
end
|
2012-06-04 17:13:54 +07:00
|
|
|
|
2010-04-20 23:05:11 +00:00
|
|
|
redirect_to :action => "invites"
|
|
|
|
else
|
|
|
|
@invited_users = User.find(:all, :conditions => ["invited_by = ?", @current_user.id], :order => "lower(name)")
|
|
|
|
end
|
|
|
|
end
|
2012-06-04 17:13:54 +07:00
|
|
|
|
2010-04-20 23:05:11 +00:00
|
|
|
def home
|
|
|
|
end
|
|
|
|
|
|
|
|
def index
|
2014-11-20 21:49:41 +09:00
|
|
|
@users = User.with_params(params).paginate(:per_page => 20, :page => page_number)
|
2010-04-20 23:05:11 +00:00
|
|
|
respond_to_list("users")
|
|
|
|
end
|
|
|
|
|
|
|
|
def authenticate
|
|
|
|
save_cookies(@current_user)
|
2012-06-04 17:13:54 +07:00
|
|
|
|
2010-04-20 23:05:11 +00:00
|
|
|
if params[:url].blank?
|
2014-08-23 16:44:43 +09:00
|
|
|
path = { :action => "home" }
|
2010-04-20 23:05:11 +00:00
|
|
|
else
|
|
|
|
path = params[:url]
|
|
|
|
end
|
2012-06-04 17:13:54 +07:00
|
|
|
|
2010-04-20 23:05:11 +00:00
|
|
|
respond_to_success("You are now logged in", path)
|
|
|
|
end
|
|
|
|
|
|
|
|
def check
|
|
|
|
if request.post?
|
|
|
|
user = User.find_by_name(params[:username])
|
|
|
|
ret = { :exists => false }
|
|
|
|
ret[:name] = params[:username]
|
|
|
|
|
2014-08-23 18:28:59 +09:00
|
|
|
unless user
|
2014-08-23 16:44:43 +09:00
|
|
|
respond_to_success("User does not exist", {}, :api => { :response => "unknown-user" }.merge(ret))
|
2010-04-20 23:05:11 +00:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
# Return some basic information about the user even if the password isn't given, for
|
|
|
|
# UI cosmetics.
|
|
|
|
ret[:exists] = true
|
|
|
|
ret[:id] = user.id
|
|
|
|
ret[:name] = user.name
|
|
|
|
ret[:no_email] = user.email.blank?
|
|
|
|
|
|
|
|
user = User.authenticate(params[:username], params[:password] || "")
|
2014-08-23 18:28:59 +09:00
|
|
|
unless user
|
2014-08-23 16:44:43 +09:00
|
|
|
respond_to_success("Wrong password", {}, :api => { :response => "wrong-password" }.merge(ret))
|
2010-04-20 23:05:11 +00:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
ret[:pass_hash] = user.password_hash
|
2010-11-30 23:48:56 +00:00
|
|
|
ret[:user_info] = user.user_info_cookie
|
2014-08-23 16:44:43 +09:00
|
|
|
respond_to_success("Successful", {}, :api => { :response => "success" }.merge(ret))
|
2012-05-12 08:48:34 -07:00
|
|
|
else
|
|
|
|
redirect_to root_path
|
2010-04-20 23:05:11 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def login
|
2014-08-05 18:00:18 +09:00
|
|
|
respond_to { |format| format.html }
|
2010-04-20 23:05:11 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
|
|
|
user = User.create(params[:user])
|
|
|
|
|
|
|
|
if user.errors.empty?
|
|
|
|
save_cookies(user)
|
|
|
|
|
|
|
|
ret = { :exists => false }
|
|
|
|
ret[:name] = user.name
|
|
|
|
ret[:id] = user.id
|
|
|
|
ret[:pass_hash] = user.password_hash
|
2010-11-30 23:48:56 +00:00
|
|
|
ret[:user_info] = user.user_info_cookie
|
2010-04-20 23:05:11 +00:00
|
|
|
|
2014-08-23 16:44:43 +09:00
|
|
|
respond_to_success("New account created", { :action => "home" }, :api => { :response => "success" }.merge(ret))
|
2010-04-20 23:05:11 +00:00
|
|
|
else
|
|
|
|
error = user.errors.full_messages.join(", ")
|
2014-08-23 16:44:43 +09:00
|
|
|
respond_to_success("Error: " + error, { :action => "signup" }, :api => { :response => "error", :errors => user.errors.full_messages })
|
2010-04-20 23:05:11 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def signup
|
|
|
|
@user = User.new
|
|
|
|
end
|
|
|
|
|
|
|
|
def logout
|
|
|
|
session[:user_id] = nil
|
|
|
|
cookies[:login] = nil
|
|
|
|
cookies[:pass_hash] = nil
|
|
|
|
|
2010-09-10 01:23:12 +00:00
|
|
|
dest = { :action => "home" }
|
|
|
|
dest = params[:from] if params[:from]
|
|
|
|
respond_to_success("You are now logged out", dest)
|
2010-04-20 23:05:11 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
|
|
|
if params[:commit] == "Cancel"
|
|
|
|
redirect_to :action => "home"
|
|
|
|
return
|
|
|
|
end
|
2012-06-04 17:13:54 +07:00
|
|
|
|
2010-04-20 23:05:11 +00:00
|
|
|
if @current_user.update_attributes(params[:user])
|
2010-10-11 03:59:58 +00:00
|
|
|
respond_to_success("Account settings saved", :action => "edit")
|
2010-04-20 23:05:11 +00:00
|
|
|
else
|
2014-08-23 18:06:02 +09:00
|
|
|
if params[:render] && params[:render][:view]
|
2012-06-30 18:04:06 +07:00
|
|
|
render get_view_name_for_edit(params[:render][:view])
|
2012-06-11 20:53:45 +07:00
|
|
|
else
|
|
|
|
respond_to_error(@current_user, :action => "edit")
|
|
|
|
end
|
2010-04-20 23:05:11 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def modify_blacklist
|
|
|
|
added_tags = params[:add] || []
|
|
|
|
removed_tags = params[:remove] || []
|
|
|
|
|
|
|
|
tags = @current_user.blacklisted_tags_array
|
2014-08-23 18:10:14 +09:00
|
|
|
added_tags.each do |tag|
|
2014-08-23 18:28:59 +09:00
|
|
|
tags << tag unless tags.include?(tag)
|
2014-08-23 18:10:14 +09:00
|
|
|
end
|
2010-04-20 23:05:11 +00:00
|
|
|
|
|
|
|
tags -= removed_tags
|
|
|
|
|
|
|
|
if @current_user.update_attribute(:blacklisted_tags, tags.join("\n"))
|
2014-08-23 16:44:43 +09:00
|
|
|
respond_to_success("Tag blacklist updated", { :action => "home" }, :api => { :result => @current_user.blacklisted_tags_array })
|
2010-04-20 23:05:11 +00:00
|
|
|
else
|
|
|
|
respond_to_error(@current_user, :action => "edit")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def remove_from_blacklist
|
|
|
|
end
|
|
|
|
|
|
|
|
def edit
|
|
|
|
@user = @current_user
|
2014-08-23 16:16:09 +09:00
|
|
|
render :layout => "settings"
|
2010-04-20 23:05:11 +00:00
|
|
|
end
|
2012-06-04 17:13:54 +07:00
|
|
|
|
2010-04-20 23:05:11 +00:00
|
|
|
def reset_password
|
|
|
|
if request.post?
|
|
|
|
@user = User.find_by_name(params[:user][:name])
|
2012-06-04 17:13:54 +07:00
|
|
|
|
2010-04-20 23:05:11 +00:00
|
|
|
if @user.nil?
|
2014-08-23 16:44:43 +09:00
|
|
|
respond_to_error("That account does not exist", { :action => "reset_password" }, :api => { :result => "unknown-user" })
|
2010-04-20 23:05:11 +00:00
|
|
|
return
|
|
|
|
end
|
2012-06-04 17:13:54 +07:00
|
|
|
|
2010-04-20 23:05:11 +00:00
|
|
|
if @user.email.blank?
|
|
|
|
respond_to_error("You never supplied an email address, therefore you cannot have your password automatically reset",
|
2014-08-23 16:44:43 +09:00
|
|
|
{ :action => "login" }, :api => { :result => "no-email" })
|
2010-04-20 23:05:11 +00:00
|
|
|
return
|
|
|
|
end
|
2012-06-04 17:13:54 +07:00
|
|
|
|
2010-04-20 23:05:11 +00:00
|
|
|
if @user.email != params[:user][:email]
|
|
|
|
respond_to_error("That is not the email address you supplied",
|
2014-08-23 16:44:43 +09:00
|
|
|
{ :action => "login" }, :api => { :result => "wrong-email" })
|
2010-04-20 23:05:11 +00:00
|
|
|
return
|
|
|
|
end
|
2012-06-04 17:13:54 +07:00
|
|
|
|
2010-04-20 23:05:11 +00:00
|
|
|
begin
|
|
|
|
User.transaction do
|
|
|
|
# If the email is invalid, abort the password reset
|
|
|
|
new_password = @user.reset_password
|
2012-07-16 19:54:47 +07:00
|
|
|
UserMailer.new_password(@user, new_password).deliver
|
2010-04-20 23:05:11 +00:00
|
|
|
respond_to_success("Password reset. Check your email in a few minutes.",
|
2014-08-23 20:14:24 +09:00
|
|
|
{ :action => "login" }, :api => { :result => "success" })
|
2010-04-20 23:05:11 +00:00
|
|
|
return
|
|
|
|
end
|
|
|
|
rescue Net::SMTPSyntaxError, Net::SMTPFatalError
|
|
|
|
respond_to_success("Your email address was invalid",
|
2014-08-23 20:14:24 +09:00
|
|
|
{ :action => "login" }, :api => { :result => "invalid-email" })
|
2010-04-20 23:05:11 +00:00
|
|
|
return
|
|
|
|
end
|
|
|
|
else
|
|
|
|
@user = User.new
|
2014-08-23 18:06:02 +09:00
|
|
|
redirect_to root_path if params[:format] && params[:format] != "html"
|
2010-04-20 23:05:11 +00:00
|
|
|
end
|
|
|
|
end
|
2012-06-04 17:13:54 +07:00
|
|
|
|
2010-04-20 23:05:11 +00:00
|
|
|
def block
|
|
|
|
@user = User.find(params[:id])
|
2012-06-04 17:13:54 +07:00
|
|
|
|
2010-04-20 23:05:11 +00:00
|
|
|
if request.post?
|
|
|
|
if @user.is_mod_or_higher?
|
|
|
|
flash[:notice] = "You can not ban other moderators or administrators"
|
|
|
|
redirect_to :action => "block"
|
|
|
|
return
|
|
|
|
end
|
2012-06-04 17:13:54 +07:00
|
|
|
|
2010-04-20 23:05:11 +00:00
|
|
|
Ban.create(params[:ban].merge(:banned_by => @current_user.id, :user_id => params[:id]))
|
|
|
|
redirect_to :action => "show_blocked_users"
|
|
|
|
else
|
|
|
|
@ban = Ban.new(:user_id => @user.id, :duration => "1")
|
|
|
|
end
|
|
|
|
end
|
2012-06-04 17:13:54 +07:00
|
|
|
|
2010-04-20 23:05:11 +00:00
|
|
|
def unblock
|
|
|
|
params[:user].keys.each do |user_id|
|
|
|
|
Ban.destroy_all(["user_id = ?", user_id])
|
|
|
|
end
|
2012-06-04 17:13:54 +07:00
|
|
|
|
2010-04-20 23:05:11 +00:00
|
|
|
redirect_to :action => "show_blocked_users"
|
|
|
|
end
|
2012-06-04 17:13:54 +07:00
|
|
|
|
2010-04-20 23:05:11 +00:00
|
|
|
def show_blocked_users
|
2014-11-20 21:21:21 +09:00
|
|
|
@users = User.includes(:ban).joins(:ban).order("bans.expires_at ASC")
|
|
|
|
@ip_bans = IpBans.all
|
2012-06-04 17:13:54 +07:00
|
|
|
end
|
|
|
|
|
2010-04-20 23:05:11 +00:00
|
|
|
if CONFIG["enable_account_email_activation"]
|
|
|
|
def resend_confirmation
|
|
|
|
if request.post?
|
|
|
|
user = User.find_by_email(params[:email])
|
2012-06-04 17:13:54 +07:00
|
|
|
|
2010-04-20 23:05:11 +00:00
|
|
|
if user.nil?
|
|
|
|
flash[:notice] = "No account exists with that email"
|
|
|
|
redirect_to :action => "home"
|
|
|
|
return
|
|
|
|
end
|
2012-06-04 17:13:54 +07:00
|
|
|
|
2010-04-20 23:05:11 +00:00
|
|
|
if user.is_blocked_or_higher?
|
|
|
|
flash[:notice] = "Your account is already activated"
|
|
|
|
redirect_to :action => "home"
|
|
|
|
return
|
|
|
|
end
|
2012-06-04 17:13:54 +07:00
|
|
|
|
2014-08-23 20:17:13 +09:00
|
|
|
UserMailer.deliver_confirmation_email(user)
|
2010-04-20 23:05:11 +00:00
|
|
|
flash[:notice] = "Confirmation email sent"
|
|
|
|
redirect_to :action => "home"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def activate_user
|
|
|
|
flash[:notice] = "Invalid confirmation code"
|
2012-06-04 17:13:54 +07:00
|
|
|
|
2010-04-20 23:05:11 +00:00
|
|
|
users = User.find(:all, :conditions => ["level = ?", CONFIG["user_levels"]["Unactivated"]])
|
|
|
|
users.each do |user|
|
|
|
|
if User.confirmation_hash(user.name) == params["hash"]
|
|
|
|
user.update_attribute(:level, CONFIG["starting_level"])
|
|
|
|
flash[:notice] = "Account has been activated"
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
redirect_to :action => "home"
|
|
|
|
end
|
|
|
|
end
|
2012-06-04 17:13:54 +07:00
|
|
|
|
2010-04-20 23:05:11 +00:00
|
|
|
def set_avatar
|
|
|
|
@user = @current_user
|
2014-11-08 22:57:37 +09:00
|
|
|
if params[:user_id]
|
2010-04-20 23:05:11 +00:00
|
|
|
@user = User.find(params[:user_id])
|
|
|
|
respond_to_error("Not found", :action => "index", :status => 404) unless @user
|
|
|
|
end
|
|
|
|
|
|
|
|
if !@user.is_anonymous? && !@current_user.has_permission?(@user, :id)
|
2014-08-23 17:24:55 +09:00
|
|
|
access_denied
|
2010-04-20 23:05:11 +00:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
if request.post?
|
2014-11-08 22:57:37 +09:00
|
|
|
if @user.set_avatar(params)
|
2010-04-20 23:05:11 +00:00
|
|
|
redirect_to :action => "show", :id => @user.id
|
|
|
|
else
|
|
|
|
respond_to_error(@user, :action => "home")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-11-08 22:57:37 +09:00
|
|
|
if !@user.is_anonymous? && params[:id] == @user.avatar_post_id
|
2010-04-20 23:05:11 +00:00
|
|
|
@old = params
|
|
|
|
end
|
|
|
|
|
|
|
|
@params = params
|
|
|
|
@post = Post.find(params[:id])
|
|
|
|
end
|
2010-12-23 05:22:21 +00:00
|
|
|
|
|
|
|
def error
|
|
|
|
report = params[:report]
|
2012-06-04 17:13:54 +07:00
|
|
|
|
2012-04-29 10:56:41 -07:00
|
|
|
file = "#{Rails.root}/log/user_errors.log"
|
2010-12-23 05:22:21 +00:00
|
|
|
File.open(file, "a") do |f|
|
2012-05-11 11:22:38 -07:00
|
|
|
f.write(report.to_s + "\n\n\n-------------------------------------------\n\n\n")
|
2010-12-23 05:22:21 +00:00
|
|
|
end
|
|
|
|
|
2014-08-23 16:44:43 +09:00
|
|
|
render :json => { :success => true }
|
2010-12-23 05:22:21 +00:00
|
|
|
end
|
2012-06-30 18:04:06 +07:00
|
|
|
|
|
|
|
private
|
2014-08-23 20:19:29 +09:00
|
|
|
|
2012-06-30 18:04:06 +07:00
|
|
|
def get_view_name_for_edit(param)
|
|
|
|
case param
|
2014-08-23 16:16:09 +09:00
|
|
|
when "change_email"
|
2012-06-30 18:04:06 +07:00
|
|
|
:change_email
|
2014-08-23 16:16:09 +09:00
|
|
|
when "change_password"
|
2012-06-30 18:04:06 +07:00
|
|
|
:change_password
|
|
|
|
else
|
|
|
|
:edit
|
|
|
|
end
|
|
|
|
end
|
2010-04-20 23:05:11 +00:00
|
|
|
end
|