2013-02-05 14:16:51 -05:00
|
|
|
class SessionController < ApplicationController
|
2013-07-29 01:13:13 -04:00
|
|
|
|
2013-06-04 18:32:36 -04:00
|
|
|
skip_before_filter :redirect_to_login_if_required
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-07-29 01:13:13 -04:00
|
|
|
def csrf
|
|
|
|
render json: {csrf: form_authenticity_token }
|
|
|
|
end
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
def create
|
2013-06-06 03:14:32 -04:00
|
|
|
params.require(:login)
|
|
|
|
params.require(:password)
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-07-23 23:02:42 -04:00
|
|
|
login = params[:login].strip
|
2013-02-05 14:16:51 -05:00
|
|
|
login = login[1..-1] if login[0] == "@"
|
|
|
|
|
|
|
|
if login =~ /@/
|
2013-04-14 20:20:33 -04:00
|
|
|
@user = User.where(email: Email.downcase(login)).first
|
2013-02-05 14:16:51 -05:00
|
|
|
else
|
2013-04-14 20:20:33 -04:00
|
|
|
@user = User.where(username_lower: login.downcase).first
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2013-02-07 10:45:24 -05:00
|
|
|
if @user.present?
|
2013-02-05 14:16:51 -05:00
|
|
|
|
|
|
|
# If the site requires user approval and the user is not approved yet
|
2013-08-06 16:51:29 -04:00
|
|
|
if SiteSetting.must_approve_users? && !@user.approved? && !@user.admin?
|
2013-03-22 14:08:11 -04:00
|
|
|
render json: {error: I18n.t("login.not_approved")}
|
2013-02-05 14:16:51 -05:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
# If their password is correct
|
|
|
|
if @user.confirm_password?(params[:password])
|
2013-06-27 15:14:42 -04:00
|
|
|
|
|
|
|
if @user.is_banned?
|
2013-06-30 12:49:34 -04:00
|
|
|
render json: { error: I18n.t("login.banned", {date: I18n.l(@user.banned_till, format: :date_only)}) }
|
2013-06-27 15:14:42 -04:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2013-02-11 11:18:26 -05:00
|
|
|
if @user.email_confirmed?
|
|
|
|
log_on_user(@user)
|
|
|
|
render_serialized(@user, UserSerializer)
|
|
|
|
return
|
|
|
|
else
|
2013-04-18 16:44:56 -04:00
|
|
|
render json: {
|
|
|
|
error: I18n.t("login.not_activated"),
|
|
|
|
reason: 'not_activated',
|
|
|
|
sent_to_email: @user.email_logs.where(email_type: 'signup').order('created_at DESC').first.try(:to_address) || @user.email,
|
|
|
|
current_email: @user.email
|
|
|
|
}
|
2013-02-11 11:18:26 -05:00
|
|
|
return
|
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-03-22 14:08:11 -04:00
|
|
|
render json: {error: I18n.t("login.incorrect_username_email_or_password")}
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def forgot_password
|
2013-06-06 03:14:32 -04:00
|
|
|
params.require(:login)
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-04-14 20:20:33 -04:00
|
|
|
user = User.where('username_lower = :username or email = :email', username: params[:login].downcase, email: Email.downcase(params[:login])).first
|
2013-02-05 14:16:51 -05:00
|
|
|
if user.present?
|
|
|
|
email_token = user.email_tokens.create(email: user.email)
|
|
|
|
Jobs.enqueue(:user_email, type: :forgot_password, user_id: user.id, email_token: email_token.token)
|
|
|
|
end
|
|
|
|
# always render of so we don't leak information
|
2013-03-22 14:08:11 -04:00
|
|
|
render json: {result: "ok"}
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def destroy
|
2013-08-27 01:56:12 -04:00
|
|
|
reset_session
|
2013-10-09 00:10:37 -04:00
|
|
|
log_off_user
|
2013-02-05 14:16:51 -05:00
|
|
|
render nothing: true
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|