2014-03-23 00:22:25 +00:00
|
|
|
class UsersController < ApplicationController
|
2020-03-16 23:57:47 +00:00
|
|
|
before_action :get_user, only: [:show, :history, :popup, :agenda, :edit, :update, :destroy]
|
2014-03-23 00:22:25 +00:00
|
|
|
respond_to :html, :js
|
|
|
|
|
2020-03-21 20:33:44 +00:00
|
|
|
PAGES = ["general", "favorites", "computer", "articles", "movies", "teams", "matches", "predictions", "comments"]
|
|
|
|
|
2014-03-23 00:22:25 +00:00
|
|
|
def index
|
2017-10-16 17:34:28 +00:00
|
|
|
search = params[:search]
|
2020-03-21 20:33:44 +00:00
|
|
|
if search && search.match(/^ip:(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})$/) && cuser&.admin?
|
2017-10-16 17:34:28 +00:00
|
|
|
@users = User.where(lastip: $1).paginate(per_page: 40, page: params[:page])
|
2017-07-02 16:34:19 +00:00
|
|
|
else
|
2017-10-16 17:34:28 +00:00
|
|
|
if params[:filter] == 'lately'
|
|
|
|
@users = User.search(params[:search]).lately.paginate(per_page: 40, page: params[:page])
|
|
|
|
else
|
|
|
|
@users = User.search(params[:search]).paginate(per_page: 40, page: params[:page])
|
|
|
|
end
|
2017-07-02 16:34:19 +00:00
|
|
|
end
|
2014-03-23 00:22:25 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def show
|
|
|
|
@page = "general"
|
|
|
|
respond_to do |format|
|
|
|
|
format.js do
|
2020-03-26 00:34:27 +00:00
|
|
|
@page = params[:page] if PAGES.include?(params[:page])
|
2014-03-23 00:22:25 +00:00
|
|
|
end
|
|
|
|
format.html {}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-03-21 20:33:44 +00:00
|
|
|
# FIXME: consider merging
|
|
|
|
def popup
|
|
|
|
render layout: false
|
|
|
|
end
|
|
|
|
|
2014-03-23 00:22:25 +00:00
|
|
|
def agenda
|
2020-03-21 20:33:44 +00:00
|
|
|
raise AccessError unless @user == cuser or cuser&.admin?
|
2014-03-23 00:22:25 +00:00
|
|
|
@teamer = Teamer.new
|
|
|
|
@teamer.user = @user
|
|
|
|
end
|
|
|
|
|
|
|
|
def history
|
2020-03-21 20:33:44 +00:00
|
|
|
raise AccessError unless cuser&.admin?
|
2014-03-23 00:22:25 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def new
|
|
|
|
@user = User.new
|
|
|
|
@user.profile = Profile.new
|
|
|
|
@user.lastip = request.env['REMOTE_ADDR']
|
|
|
|
@user.can_create? cuser
|
|
|
|
end
|
|
|
|
|
|
|
|
def edit
|
|
|
|
raise AccessError unless @user.can_update? cuser
|
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
2020-03-18 21:23:30 +00:00
|
|
|
@user = User.new(User.params(params, cuser, "create"))
|
2014-03-23 00:22:25 +00:00
|
|
|
@user.lastip = request.env['REMOTE_ADDR']
|
|
|
|
|
|
|
|
raise AccessError unless @user.can_create? cuser
|
|
|
|
|
|
|
|
if @user.valid? and @user.save
|
2014-03-31 21:33:16 +00:00
|
|
|
redirect_to action: :show, id: @user.id
|
2014-03-23 00:22:25 +00:00
|
|
|
save_session @user
|
|
|
|
else
|
2014-03-31 21:33:16 +00:00
|
|
|
render :new
|
2014-03-23 00:22:25 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
2020-03-21 20:33:44 +00:00
|
|
|
raise AccessError unless @user.can_update? cuser
|
2020-03-18 03:38:17 +00:00
|
|
|
# FIXME: use permit
|
2015-06-27 12:23:37 +00:00
|
|
|
params[:user].delete(:username) unless @user.can_change_name? cuser
|
2020-03-18 21:23:30 +00:00
|
|
|
if @user.update_attributes(User.params(params, cuser, "update"))
|
2014-03-23 00:22:25 +00:00
|
|
|
flash[:notice] = t(:users_update)
|
|
|
|
redirect_to_back
|
|
|
|
else
|
2014-03-31 21:33:16 +00:00
|
|
|
render :edit
|
2014-03-23 00:22:25 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def destroy
|
|
|
|
raise AccessError unless @user.can_destroy? cuser
|
|
|
|
@user.destroy
|
|
|
|
redirect_to users_url
|
|
|
|
end
|
|
|
|
|
2020-04-10 15:32:18 +00:00
|
|
|
def callback
|
|
|
|
@user = User.focfah(auth_hash, request.ip)
|
|
|
|
login_user(@user)
|
2020-04-10 17:46:52 +00:00
|
|
|
if @user.created_at > (Time.zone.now - 1.week)
|
|
|
|
flash[:notice] << t(:users_signup_steam)
|
2020-04-10 15:32:18 +00:00
|
|
|
render :edit
|
|
|
|
else
|
|
|
|
return_back
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-03-21 20:33:44 +00:00
|
|
|
# FIXME: maybe move to session controller
|
2014-03-23 00:22:25 +00:00
|
|
|
def login
|
2020-04-02 00:08:19 +00:00
|
|
|
if params[:login]
|
|
|
|
if (u = User.authenticate(params[:login]))
|
2020-04-10 15:32:18 +00:00
|
|
|
login_user(u)
|
2014-03-23 00:22:25 +00:00
|
|
|
else
|
2020-04-02 00:08:19 +00:00
|
|
|
flash[:error] = t(:login_unsuccessful)
|
2014-03-23 00:22:25 +00:00
|
|
|
end
|
2020-03-21 20:33:44 +00:00
|
|
|
end
|
2020-04-10 15:32:18 +00:00
|
|
|
return_back
|
2014-03-23 00:22:25 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def logout
|
2020-03-21 20:33:44 +00:00
|
|
|
session[:user] = nil
|
|
|
|
flash[:notice] = t(:login_out)
|
|
|
|
redirect_to :root
|
2014-03-23 00:22:25 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def forgot
|
|
|
|
if request.post?
|
2020-03-21 20:33:44 +00:00
|
|
|
if (user1 = User.where(username: params[:username], email: params[:email]).first) && user1.send_new_password
|
2014-03-23 00:22:25 +00:00
|
|
|
flash[:notice] = t(:passwords_sent)
|
|
|
|
else
|
|
|
|
flash[:error] = t(:incorrect_information)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def get_user
|
|
|
|
@user = User.find(params[:id])
|
|
|
|
end
|
|
|
|
|
2020-04-10 15:32:18 +00:00
|
|
|
def login_user(user)
|
|
|
|
if user.banned? Ban::TYPE_SITE
|
|
|
|
flash[:error] = t(:accounts_locked)
|
|
|
|
else
|
|
|
|
flash[:notice] = "%s (%s)" % [t(:login_successful), user.password_hash_s]
|
|
|
|
# FIXME: this doesn't work because model is saved before
|
|
|
|
flash[:notice] << " \n%s" % I18n.t(:password_md5_scrypt) if user.password_hash_changed?
|
|
|
|
save_session user
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-03-23 00:22:25 +00:00
|
|
|
def save_session user
|
|
|
|
session[:user] = user.id
|
|
|
|
user.lastip = request.ip
|
2020-03-25 01:13:38 +00:00
|
|
|
user.lastvisit = Time.now.utc
|
2020-04-10 15:32:18 +00:00
|
|
|
user.save!
|
|
|
|
end
|
|
|
|
|
|
|
|
def auth_hash
|
|
|
|
request.env['omniauth.auth']
|
2014-03-23 00:22:25 +00:00
|
|
|
end
|
|
|
|
end
|