2013-02-06 03:16:51 +08:00
|
|
|
class SessionController < ApplicationController
|
2013-07-29 13:13:13 +08:00
|
|
|
|
2013-06-05 06:32:36 +08:00
|
|
|
skip_before_filter :redirect_to_login_if_required
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2013-07-29 13:13:13 +08:00
|
|
|
def csrf
|
|
|
|
render json: {csrf: form_authenticity_token }
|
|
|
|
end
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
def create
|
2013-06-06 15:14:32 +08:00
|
|
|
params.require(:login)
|
|
|
|
params.require(:password)
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2013-10-24 15:59:58 +08:00
|
|
|
login = params[:login].strip
|
|
|
|
password = params[:password]
|
|
|
|
login = login[1..-1] if login[0] == "@"
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2013-10-24 15:59:58 +08:00
|
|
|
@user = User.find_by_username_or_email(login)
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2013-02-07 23:45:24 +08:00
|
|
|
if @user.present?
|
2013-02-06 03:16:51 +08:00
|
|
|
|
|
|
|
# If the site requires user approval and the user is not approved yet
|
2013-08-07 04:51:29 +08:00
|
|
|
if SiteSetting.must_approve_users? && !@user.approved? && !@user.admin?
|
2013-03-23 02:08:11 +08:00
|
|
|
render json: {error: I18n.t("login.not_approved")}
|
2013-02-06 03:16:51 +08:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
# If their password is correct
|
2013-10-24 15:59:58 +08:00
|
|
|
if @user.confirm_password?(password)
|
2013-06-28 03:14:42 +08:00
|
|
|
|
2013-11-08 02:53:32 +08:00
|
|
|
if @user.suspended?
|
|
|
|
if reason = @user.suspend_reason
|
|
|
|
render json: { error: I18n.t("login.suspended_with_reason", {date: I18n.l(@user.suspended_till, format: :date_only), reason: reason}) }
|
2013-11-01 22:47:03 +08:00
|
|
|
else
|
2013-11-08 02:53:32 +08:00
|
|
|
render json: { error: I18n.t("login.suspended", {date: I18n.l(@user.suspended_till, format: :date_only)}) }
|
2013-11-01 22:47:03 +08:00
|
|
|
end
|
2013-06-28 03:14:42 +08:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2013-02-12 00:18:26 +08:00
|
|
|
if @user.email_confirmed?
|
|
|
|
log_on_user(@user)
|
|
|
|
render_serialized(@user, UserSerializer)
|
|
|
|
return
|
|
|
|
else
|
2013-04-19 04:44:56 +08: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-12 00:18:26 +08:00
|
|
|
return
|
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-03-23 02:08:11 +08:00
|
|
|
render json: {error: I18n.t("login.incorrect_username_email_or_password")}
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def forgot_password
|
2013-06-06 15:14:32 +08:00
|
|
|
params.require(:login)
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2013-10-24 15:59:58 +08:00
|
|
|
user = User.find_by_username_or_email(params[:login])
|
2013-02-06 03:16:51 +08: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-23 02:08:11 +08:00
|
|
|
render json: {result: "ok"}
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def destroy
|
2013-08-27 13:56:12 +08:00
|
|
|
reset_session
|
2013-10-09 12:10:37 +08:00
|
|
|
log_off_user
|
2013-02-06 03:16:51 +08:00
|
|
|
render nothing: true
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|