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-11-15 10:27:43 -05:00
|
|
|
login = params[:login].strip
|
|
|
|
login = login[1..-1] if login[0] == "@"
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-11-15 10:27:43 -05:00
|
|
|
if user = User.find_by_username_or_email(login)
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-11-15 10:27:43 -05:00
|
|
|
# If their password is correct
|
|
|
|
unless user.confirm_password?(params[:password])
|
|
|
|
invalid_credentials
|
|
|
|
return
|
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
|
|
|
|
# If the site requires user approval and the user is not approved yet
|
2013-11-15 10:27:43 -05:00
|
|
|
if login_not_approved_for?(user)
|
|
|
|
login_not_approved
|
2013-02-05 14:16:51 -05:00
|
|
|
return
|
|
|
|
end
|
2013-11-27 20:39:59 -05:00
|
|
|
|
2014-01-21 16:53:46 -05:00
|
|
|
# User signed on with username and password, so let's prevent the invite link
|
|
|
|
# from being used to log in (if one exists).
|
|
|
|
Invite.invalidate_for_email(user.email)
|
2013-11-27 20:39:59 -05:00
|
|
|
else
|
|
|
|
invalid_credentials
|
|
|
|
return
|
2013-11-15 10:27:43 -05:00
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-11-15 10:27:43 -05:00
|
|
|
if user.suspended?
|
|
|
|
failed_to_login(user)
|
|
|
|
return
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2013-11-15 10:27:43 -05:00
|
|
|
user.email_confirmed? ? login(user) : not_activated(user)
|
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-10-24 03:59:58 -04:00
|
|
|
user = User.find_by_username_or_email(params[:login])
|
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
|
|
|
|
|
2014-02-05 13:46:24 -05:00
|
|
|
def current
|
|
|
|
if current_user.present?
|
|
|
|
render_serialized(current_user, CurrentUserSerializer)
|
|
|
|
else
|
|
|
|
render nothing: true, status: 404
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
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
|
|
|
|
|
2013-11-15 10:27:43 -05:00
|
|
|
private
|
|
|
|
|
|
|
|
def login_not_approved_for?(user)
|
|
|
|
SiteSetting.must_approve_users? && !user.approved? && !user.admin?
|
|
|
|
end
|
|
|
|
|
|
|
|
def invalid_credentials
|
|
|
|
render json: {error: I18n.t("login.incorrect_username_email_or_password")}
|
|
|
|
end
|
|
|
|
|
|
|
|
def login_not_approved
|
|
|
|
render json: {error: I18n.t("login.not_approved")}
|
|
|
|
end
|
|
|
|
|
|
|
|
def not_activated(user)
|
|
|
|
render json: {
|
|
|
|
error: I18n.t("login.not_activated"),
|
|
|
|
reason: 'not_activated',
|
|
|
|
sent_to_email: user.find_email || user.email,
|
|
|
|
current_email: user.email
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def failed_to_login(user)
|
|
|
|
message = user.suspend_reason ? "login.suspended_with_reason" : "login.suspended"
|
|
|
|
|
|
|
|
render json: { error: I18n.t(message, { date: I18n.l(user.suspended_till, format: :date_only),
|
|
|
|
reason: user.suspend_reason}) }
|
|
|
|
end
|
|
|
|
|
|
|
|
def login(user)
|
|
|
|
log_on_user(user)
|
|
|
|
render_serialized(user, UserSerializer)
|
|
|
|
end
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|