2013-02-14 11:57:26 -06:00
|
|
|
require_dependency 'discourse_hub'
|
2013-06-06 09:40:10 -05:00
|
|
|
require_dependency 'user_name_suggester'
|
2013-08-18 09:37:38 -05:00
|
|
|
require_dependency 'user_activator'
|
2013-02-05 13:16:51 -06:00
|
|
|
|
|
|
|
class UsersController < ApplicationController
|
|
|
|
|
2013-08-14 05:20:05 -05:00
|
|
|
skip_before_filter :authorize_mini_profiler, only: [:avatar]
|
|
|
|
skip_before_filter :check_xhr, only: [:show, :password_reset, :update, :activate_account, :authorize_email, :user_preferences_redirect, :avatar]
|
2013-02-05 13:16:51 -06:00
|
|
|
|
2013-08-24 15:37:31 -05:00
|
|
|
before_filter :ensure_logged_in, only: [:username, :update, :change_email, :user_preferences_redirect, :upload_avatar, :toggle_avatar]
|
2013-02-07 09:45:24 -06:00
|
|
|
|
2013-05-22 10:20:16 -05:00
|
|
|
# we need to allow account creation with bad CSRF tokens, if people are caching, the CSRF token on the
|
2013-05-03 01:43:11 -05:00
|
|
|
# page is going to be empty, this means that server will see an invalid CSRF and blow the session
|
|
|
|
# once that happens you can't log in with social
|
2013-06-04 23:01:24 -05:00
|
|
|
skip_before_filter :verify_authenticity_token, only: [:create]
|
2013-07-15 11:12:54 -05:00
|
|
|
skip_before_filter :redirect_to_login_if_required, only: [:check_username,
|
|
|
|
:create,
|
|
|
|
:get_honeypot_value,
|
|
|
|
:activate_account,
|
|
|
|
:send_activation_email,
|
|
|
|
:authorize_email,
|
|
|
|
:password_reset]
|
2013-05-03 01:43:11 -05:00
|
|
|
|
2013-02-07 09:45:24 -06:00
|
|
|
def show
|
2013-02-05 13:16:51 -06:00
|
|
|
@user = fetch_user_from_params
|
2013-03-08 14:04:37 -06:00
|
|
|
user_serializer = UserSerializer.new(@user, scope: guardian, root: 'user')
|
|
|
|
respond_to do |format|
|
|
|
|
format.html do
|
|
|
|
store_preloaded("user_#{@user.username}", MultiJson.dump(user_serializer))
|
|
|
|
end
|
|
|
|
|
|
|
|
format.json do
|
|
|
|
render_json_dump(user_serializer)
|
|
|
|
end
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def user_preferences_redirect
|
|
|
|
redirect_to email_preferences_path(current_user.username_lower)
|
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
2013-03-23 10:02:59 -05:00
|
|
|
user = User.where(username_lower: params[:username].downcase).first
|
2013-02-05 13:16:51 -06:00
|
|
|
guardian.ensure_can_edit!(user)
|
2013-04-11 19:07:46 -05:00
|
|
|
json_result(user, serializer: UserSerializer) do |u|
|
2013-02-05 13:16:51 -06:00
|
|
|
|
|
|
|
website = params[:website]
|
2013-02-07 09:45:24 -06:00
|
|
|
if website
|
2013-02-05 13:16:51 -06:00
|
|
|
website = "http://" + website unless website =~ /^http/
|
|
|
|
end
|
|
|
|
|
|
|
|
u.bio_raw = params[:bio_raw] || u.bio_raw
|
|
|
|
u.name = params[:name] || u.name
|
|
|
|
u.website = website || u.website
|
|
|
|
u.digest_after_days = params[:digest_after_days] || u.digest_after_days
|
|
|
|
u.auto_track_topics_after_msecs = params[:auto_track_topics_after_msecs].to_i if params[:auto_track_topics_after_msecs]
|
2013-02-14 00:32:58 -06:00
|
|
|
u.new_topic_duration_minutes = params[:new_topic_duration_minutes].to_i if params[:new_topic_duration_minutes]
|
2013-06-25 17:39:20 -05:00
|
|
|
u.title = params[:title] || u.title if guardian.can_grant_title?(u)
|
2013-02-05 13:16:51 -06:00
|
|
|
|
2013-03-22 13:08:11 -05:00
|
|
|
[:email_digests, :email_direct, :email_private_messages,
|
2013-06-15 01:58:24 -05:00
|
|
|
:external_links_in_new_tab, :enable_quoting, :dynamic_favicon].each do |i|
|
2013-02-05 13:16:51 -06:00
|
|
|
if params[i].present?
|
|
|
|
u.send("#{i.to_s}=", params[i] == 'true')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-04-11 19:07:46 -05:00
|
|
|
if u.save
|
|
|
|
u
|
|
|
|
else
|
|
|
|
nil
|
|
|
|
end
|
2013-02-07 09:45:24 -06:00
|
|
|
end
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
def username
|
2013-06-06 02:14:32 -05:00
|
|
|
params.require(:new_username)
|
2013-02-05 13:16:51 -06:00
|
|
|
|
|
|
|
user = fetch_user_from_params
|
2013-08-12 13:54:52 -05:00
|
|
|
guardian.ensure_can_edit_username!(user)
|
2013-02-07 09:45:24 -06:00
|
|
|
|
2013-02-05 13:16:51 -06:00
|
|
|
result = user.change_username(params[:new_username])
|
|
|
|
raise Discourse::InvalidParameters.new(:new_username) unless result
|
|
|
|
|
2013-02-07 09:45:24 -06:00
|
|
|
render nothing: true
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
def preferences
|
|
|
|
render nothing: true
|
|
|
|
end
|
|
|
|
|
|
|
|
def invited
|
|
|
|
invited_list = InvitedList.new(fetch_user_from_params)
|
|
|
|
render_serialized(invited_list, InvitedListSerializer)
|
|
|
|
end
|
|
|
|
|
|
|
|
def is_local_username
|
2013-06-06 02:14:32 -05:00
|
|
|
params.require(:username)
|
2013-02-05 13:16:51 -06:00
|
|
|
u = params[:username].downcase
|
|
|
|
r = User.exec_sql('select 1 from users where username_lower = ?', u).values
|
|
|
|
render json: {valid: r.length == 1}
|
|
|
|
end
|
|
|
|
|
2013-08-25 03:57:12 -05:00
|
|
|
def render_available_true
|
|
|
|
render(json: { available: true })
|
|
|
|
end
|
|
|
|
|
|
|
|
def changing_case_of_own_username(target_user, username)
|
|
|
|
target_user and username.downcase == target_user.username.downcase
|
|
|
|
end
|
|
|
|
|
|
|
|
# Used for checking availability of a username and will return suggestions
|
|
|
|
# if the username is not available.
|
2013-02-05 13:16:51 -06:00
|
|
|
def check_username
|
2013-06-06 02:14:32 -05:00
|
|
|
params.require(:username)
|
2013-08-25 03:57:12 -05:00
|
|
|
username = params[:username]
|
2013-02-05 13:16:51 -06:00
|
|
|
|
2013-08-25 03:57:12 -05:00
|
|
|
target_user = user_from_params_or_current_user
|
2013-07-30 13:13:56 -05:00
|
|
|
|
2013-06-28 15:21:46 -05:00
|
|
|
# The special case where someone is changing the case of their own username
|
2013-08-25 03:57:12 -05:00
|
|
|
return render_available_true if changing_case_of_own_username(target_user, username)
|
2013-06-28 15:21:46 -05:00
|
|
|
|
2013-08-25 03:57:12 -05:00
|
|
|
validator = UsernameValidator.new(username)
|
2013-02-08 13:12:48 -06:00
|
|
|
if !validator.valid_format?
|
|
|
|
render json: {errors: validator.errors}
|
2013-02-14 11:57:26 -06:00
|
|
|
elsif !SiteSetting.call_discourse_hub?
|
2013-08-25 03:57:12 -05:00
|
|
|
check_username_locally(username)
|
2013-02-05 13:16:51 -06:00
|
|
|
else
|
2013-08-25 03:57:12 -05:00
|
|
|
check_username_with_hub_server(target_user, username)
|
|
|
|
end
|
|
|
|
rescue RestClient::Forbidden
|
|
|
|
render json: {errors: [I18n.t("discourse_hub.access_token_problem")]}
|
|
|
|
end
|
2013-02-05 13:16:51 -06:00
|
|
|
|
2013-08-25 03:57:12 -05:00
|
|
|
def check_username_locally(username)
|
|
|
|
if User.username_available?(username)
|
|
|
|
render_available_true
|
|
|
|
else
|
|
|
|
render_unavailable_with_suggestion(UserNameSuggester.suggest(username))
|
|
|
|
end
|
|
|
|
end
|
2013-02-05 13:16:51 -06:00
|
|
|
|
2013-08-25 03:57:12 -05:00
|
|
|
def user_from_params_or_current_user
|
|
|
|
params[:for_user_id] ? User.find(params[:for_user_id]) : current_user
|
|
|
|
end
|
|
|
|
|
|
|
|
def available_globally_and_suggestion_from_hub(target_user, username, email_given)
|
|
|
|
if email_given
|
|
|
|
global_match, available, suggestion =
|
|
|
|
DiscourseHub.nickname_match?(username, params[:email] || target_user.email)
|
|
|
|
{ available_globally: available || global_match,
|
|
|
|
suggestion_from_discourse_hub: suggestion,
|
|
|
|
global_match: global_match }
|
|
|
|
else
|
|
|
|
args = DiscourseHub.nickname_available?(username)
|
|
|
|
{ available_globally: args[0],
|
|
|
|
suggestion_from_discourse_hub: args[1],
|
|
|
|
global_match: false }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Contact the Discourse Hub server
|
|
|
|
def check_username_with_hub_server(target_user, username)
|
|
|
|
email_given = (params[:email].present? || target_user.present?)
|
|
|
|
available_locally = User.username_available?(username)
|
|
|
|
info = available_globally_and_suggestion_from_hub(target_user, username, email_given)
|
|
|
|
available_globally = info[:available_globally]
|
|
|
|
suggestion_from_discourse_hub = info[:suggestion_from_discourse_hub]
|
|
|
|
global_match = info[:global_match]
|
|
|
|
if available_globally && available_locally
|
|
|
|
render json: { available: true, global_match: (global_match ? true : false) }
|
|
|
|
elsif available_locally && !available_globally
|
|
|
|
if email_given
|
|
|
|
# Nickname and email do not match what's registered on the discourse hub.
|
|
|
|
render json: { available: false, global_match: false, suggestion: suggestion_from_discourse_hub }
|
2013-02-05 13:16:51 -06:00
|
|
|
else
|
2013-08-25 03:57:12 -05:00
|
|
|
# The nickname is available locally, but is registered on the discourse hub.
|
|
|
|
# We need an email to see if the nickname belongs to this person.
|
|
|
|
# Don't give a suggestion until we get the email and try to match it with on the discourse hub.
|
|
|
|
render json: { available: false }
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
2013-08-25 03:57:12 -05:00
|
|
|
elsif available_globally && !available_locally
|
|
|
|
# Already registered on this site with the matching nickname and email address. Why are you signing up again?
|
|
|
|
render json: { available: false, suggestion: UserNameSuggester.suggest(username) }
|
|
|
|
else
|
|
|
|
# Not available anywhere.
|
|
|
|
render_unavailable_with_suggestion(suggestion_from_discourse_hub)
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
2013-08-25 03:57:12 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def render_unavailable_with_suggestion(suggestion)
|
|
|
|
render json: { available: false, suggestion: suggestion }
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
2013-06-20 14:47:35 -05:00
|
|
|
return fake_success_response if suspicious? params
|
2013-02-06 18:25:21 -06:00
|
|
|
|
2013-04-12 17:46:55 -05:00
|
|
|
user = User.new_from_params(params)
|
2013-08-18 09:37:38 -05:00
|
|
|
auth = authenticate_user(user, params)
|
2013-08-23 04:38:01 -05:00
|
|
|
register_nickname(user)
|
2013-02-05 13:16:51 -06:00
|
|
|
|
2013-08-18 09:37:38 -05:00
|
|
|
if user.save
|
|
|
|
activator = UserActivator.new(user, session, cookies)
|
|
|
|
message = activator.activation_message
|
2013-08-25 15:18:07 -05:00
|
|
|
create_third_party_auth_records(user, auth)
|
2013-02-07 09:45:24 -06:00
|
|
|
|
|
|
|
# Clear authentication session.
|
2013-02-05 13:16:51 -06:00
|
|
|
session[:authentication] = nil
|
|
|
|
|
2013-06-05 20:19:27 -05:00
|
|
|
render json: { success: true, active: user.active?, message: message }
|
2013-02-05 13:16:51 -06:00
|
|
|
else
|
2013-04-12 17:46:55 -05:00
|
|
|
render json: {
|
|
|
|
success: false,
|
2013-07-25 12:01:27 -05:00
|
|
|
message: I18n.t("login.errors", errors: user.errors.full_messages.join("\n")),
|
|
|
|
errors: user.errors.to_hash,
|
|
|
|
values: user.attributes.slice("name", "username", "email")
|
2013-04-12 17:46:55 -05:00
|
|
|
}
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
2013-03-07 13:56:28 -06:00
|
|
|
rescue ActiveRecord::StatementInvalid
|
2013-04-12 17:46:55 -05:00
|
|
|
render json: { success: false, message: I18n.t("login.something_already_taken") }
|
2013-08-25 17:41:17 -05:00
|
|
|
rescue DiscourseHub::NicknameUnavailable=> e
|
|
|
|
render json: e.response_message
|
2013-02-05 13:16:51 -06:00
|
|
|
rescue RestClient::Forbidden
|
2013-04-12 17:46:55 -05:00
|
|
|
render json: { errors: [I18n.t("discourse_hub.access_token_problem")] }
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
|
|
|
|
2013-08-18 09:37:38 -05:00
|
|
|
def authenticate_user(user, params)
|
|
|
|
auth = session[:authentication]
|
|
|
|
if valid_session_authentication?(auth, params[:email])
|
|
|
|
user.active = true
|
|
|
|
end
|
|
|
|
user.password_required! unless auth
|
|
|
|
|
|
|
|
auth
|
|
|
|
end
|
|
|
|
|
2013-02-06 18:25:21 -06:00
|
|
|
def get_honeypot_value
|
|
|
|
render json: {value: honeypot_value, challenge: challenge_value}
|
|
|
|
end
|
|
|
|
|
2013-02-05 13:16:51 -06:00
|
|
|
def password_reset
|
|
|
|
expires_now()
|
|
|
|
|
|
|
|
@user = EmailToken.confirm(params[:token])
|
|
|
|
if @user.blank?
|
|
|
|
flash[:error] = I18n.t('password_reset.no_token')
|
|
|
|
else
|
2013-03-04 18:42:44 -06:00
|
|
|
if request.put? && params[:password].present?
|
2013-02-05 13:16:51 -06:00
|
|
|
@user.password = params[:password]
|
|
|
|
if @user.save
|
|
|
|
|
2013-04-03 11:23:28 -05:00
|
|
|
if Guardian.new(@user).can_access_forum?
|
2013-02-05 13:16:51 -06:00
|
|
|
# Log in the user
|
|
|
|
log_on_user(@user)
|
|
|
|
flash[:success] = I18n.t('password_reset.success')
|
2013-04-03 11:23:28 -05:00
|
|
|
else
|
|
|
|
@requires_approval = true
|
|
|
|
flash[:success] = I18n.t('password_reset.success_unapproved')
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
2013-02-07 09:45:24 -06:00
|
|
|
end
|
|
|
|
end
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
2013-03-22 13:08:11 -05:00
|
|
|
render layout: 'no_js'
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
2013-02-07 09:45:24 -06:00
|
|
|
|
2013-02-05 13:16:51 -06:00
|
|
|
def change_email
|
2013-06-06 02:14:32 -05:00
|
|
|
params.require(:email)
|
2013-02-05 13:16:51 -06:00
|
|
|
user = fetch_user_from_params
|
|
|
|
guardian.ensure_can_edit!(user)
|
2013-04-27 22:02:23 -05:00
|
|
|
lower_email = Email.downcase(params[:email]).strip
|
2013-02-05 13:16:51 -06:00
|
|
|
|
|
|
|
# Raise an error if the email is already in use
|
2013-04-14 19:20:33 -05:00
|
|
|
if User.where("email = ?", lower_email).exists?
|
2013-04-12 17:46:55 -05:00
|
|
|
raise Discourse::InvalidParameters.new(:email)
|
|
|
|
end
|
2013-02-05 13:16:51 -06:00
|
|
|
|
2013-04-14 19:20:33 -05:00
|
|
|
email_token = user.email_tokens.create(email: lower_email)
|
2013-04-12 17:46:55 -05:00
|
|
|
Jobs.enqueue(
|
|
|
|
:user_email,
|
2013-04-14 19:20:33 -05:00
|
|
|
to_address: lower_email,
|
2013-04-12 17:46:55 -05:00
|
|
|
type: :authorize_email,
|
|
|
|
user_id: user.id,
|
|
|
|
email_token: email_token.token
|
|
|
|
)
|
2013-02-05 13:16:51 -06:00
|
|
|
|
2013-02-07 09:45:24 -06:00
|
|
|
render nothing: true
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
def authorize_email
|
|
|
|
expires_now()
|
|
|
|
if @user = EmailToken.confirm(params[:token])
|
|
|
|
log_on_user(@user)
|
|
|
|
else
|
|
|
|
flash[:error] = I18n.t('change_email.error')
|
|
|
|
end
|
2013-03-22 13:08:11 -05:00
|
|
|
render layout: 'no_js'
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
def activate_account
|
|
|
|
expires_now()
|
|
|
|
if @user = EmailToken.confirm(params[:token])
|
|
|
|
|
|
|
|
# Log in the user unless they need to be approved
|
2013-04-03 11:23:28 -05:00
|
|
|
if Guardian.new(@user).can_access_forum?
|
2013-02-05 13:16:51 -06:00
|
|
|
@user.enqueue_welcome_message('welcome_user') if @user.send_welcome_message
|
|
|
|
log_on_user(@user)
|
2013-04-03 11:23:28 -05:00
|
|
|
else
|
|
|
|
@needs_approval = true
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
else
|
|
|
|
flash[:error] = I18n.t('activation.already_done')
|
|
|
|
end
|
2013-03-22 13:08:11 -05:00
|
|
|
render layout: 'no_js'
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
|
|
|
|
2013-02-22 10:49:48 -06:00
|
|
|
def send_activation_email
|
|
|
|
@user = fetch_user_from_params
|
|
|
|
@email_token = @user.email_tokens.unconfirmed.active.first
|
|
|
|
if @user
|
2013-06-25 10:23:23 -05:00
|
|
|
@email_token ||= @user.email_tokens.create(email: @user.email)
|
2013-02-22 10:49:48 -06:00
|
|
|
Jobs.enqueue(:user_email, type: :signup, user_id: @user.id, email_token: @email_token.token)
|
|
|
|
end
|
|
|
|
render nothing: true
|
|
|
|
end
|
|
|
|
|
2013-02-05 13:16:51 -06:00
|
|
|
def search_users
|
2013-02-07 04:59:25 -06:00
|
|
|
term = params[:term].to_s.strip
|
2013-02-05 13:16:51 -06:00
|
|
|
topic_id = params[:topic_id]
|
|
|
|
topic_id = topic_id.to_i if topic_id
|
|
|
|
|
2013-02-07 03:34:49 -06:00
|
|
|
results = UserSearch.search term, topic_id
|
2013-02-05 13:16:51 -06:00
|
|
|
|
2013-08-14 08:25:05 -05:00
|
|
|
render json: { users: results.as_json(only: [ :username, :name, :use_uploaded_avatar, :upload_avatar_template, :uploaded_avatar_id],
|
2013-04-12 17:46:55 -05:00
|
|
|
methods: :avatar_template) }
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
|
|
|
|
2013-08-14 05:20:05 -05:00
|
|
|
# [LEGACY] avatars in quotes/oneboxes might still be pointing to this route
|
|
|
|
# fixing it requires a rebake of all the posts
|
2013-08-13 15:08:29 -05:00
|
|
|
def avatar
|
2013-08-14 11:26:31 -05:00
|
|
|
user = User.where(username_lower: params[:username].downcase).first
|
2013-08-14 05:20:05 -05:00
|
|
|
if user.present?
|
|
|
|
size = determine_avatar_size(params[:size])
|
|
|
|
url = user.avatar_template.gsub("{size}", size.to_s)
|
|
|
|
expires_in 1.day
|
|
|
|
redirect_to url
|
|
|
|
else
|
|
|
|
raise ActiveRecord::RecordNotFound
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def determine_avatar_size(size)
|
|
|
|
size = size.to_i
|
|
|
|
size = 64 if size == 0
|
|
|
|
size = 10 if size < 10
|
|
|
|
size = 128 if size > 128
|
|
|
|
size
|
|
|
|
end
|
|
|
|
|
|
|
|
def upload_avatar
|
2013-08-13 15:08:29 -05:00
|
|
|
user = fetch_user_from_params
|
|
|
|
guardian.ensure_can_edit!(user)
|
|
|
|
|
|
|
|
file = params[:file] || params[:files].first
|
|
|
|
|
|
|
|
# check the file size (note: this might also be done in the web server)
|
|
|
|
filesize = File.size(file.tempfile)
|
|
|
|
max_size_kb = SiteSetting.max_image_size_kb * 1024
|
|
|
|
return render status: 413, text: I18n.t("upload.images.too_large", max_size_kb: max_size_kb) if filesize > max_size_kb
|
|
|
|
|
|
|
|
upload = Upload.create_for(user.id, file, filesize)
|
|
|
|
|
2013-08-24 15:37:31 -05:00
|
|
|
user.uploaded_avatar_template = nil
|
2013-08-13 15:08:29 -05:00
|
|
|
user.uploaded_avatar = upload
|
|
|
|
user.use_uploaded_avatar = true
|
|
|
|
user.save!
|
|
|
|
|
|
|
|
Jobs.enqueue(:generate_avatars, upload_id: upload.id)
|
|
|
|
|
2013-08-24 15:37:31 -05:00
|
|
|
render json: {
|
|
|
|
url: upload.url,
|
|
|
|
width: upload.width,
|
|
|
|
height: upload.height,
|
|
|
|
}
|
2013-08-13 15:08:29 -05:00
|
|
|
|
|
|
|
rescue FastImage::ImageFetchFailure
|
|
|
|
render status: 422, text: I18n.t("upload.images.fetch_failure")
|
|
|
|
rescue FastImage::UnknownImageType
|
|
|
|
render status: 422, text: I18n.t("upload.images.unknown_image_type")
|
|
|
|
rescue FastImage::SizeNotFound
|
|
|
|
render status: 422, text: I18n.t("upload.images.size_not_found")
|
|
|
|
end
|
|
|
|
|
|
|
|
def toggle_avatar
|
|
|
|
params.require(:use_uploaded_avatar)
|
|
|
|
user = fetch_user_from_params
|
|
|
|
guardian.ensure_can_edit!(user)
|
|
|
|
|
|
|
|
user.use_uploaded_avatar = params[:use_uploaded_avatar]
|
|
|
|
user.save!
|
|
|
|
|
2013-08-16 17:29:54 -05:00
|
|
|
render nothing: true
|
2013-08-13 15:08:29 -05:00
|
|
|
end
|
|
|
|
|
2013-02-05 13:16:51 -06:00
|
|
|
private
|
|
|
|
|
2013-02-06 18:25:21 -06:00
|
|
|
def honeypot_value
|
|
|
|
Digest::SHA1::hexdigest("#{Discourse.current_hostname}:#{Discourse::Application.config.secret_token}")[0,15]
|
|
|
|
end
|
|
|
|
|
|
|
|
def challenge_value
|
2013-08-23 01:19:23 -05:00
|
|
|
challenge = $redis.get('SECRET_CHALLENGE')
|
|
|
|
unless challenge && challenge.length == 16*2
|
|
|
|
challenge = SecureRandom.hex(16)
|
|
|
|
$redis.set('SECRET_CHALLENGE',challenge)
|
|
|
|
end
|
|
|
|
|
|
|
|
challenge
|
2013-02-06 18:25:21 -06:00
|
|
|
end
|
|
|
|
|
2013-06-05 13:08:21 -05:00
|
|
|
def suspicious?(params)
|
|
|
|
honeypot_or_challenge_fails?(params) || SiteSetting.invite_only?
|
|
|
|
end
|
|
|
|
|
2013-06-20 14:47:35 -05:00
|
|
|
def fake_success_response
|
2013-06-05 13:08:21 -05:00
|
|
|
render(
|
|
|
|
json: {
|
|
|
|
success: true,
|
|
|
|
active: false,
|
|
|
|
message: I18n.t("login.activate_email", email: params[:email])
|
|
|
|
}
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2013-04-12 17:46:55 -05:00
|
|
|
def honeypot_or_challenge_fails?(params)
|
|
|
|
params[:password_confirmation] != honeypot_value ||
|
|
|
|
params[:challenge] != challenge_value.try(:reverse)
|
|
|
|
end
|
|
|
|
|
|
|
|
def valid_session_authentication?(auth, email)
|
|
|
|
auth && auth[:email] == email && auth[:email_valid]
|
|
|
|
end
|
|
|
|
|
|
|
|
def create_third_party_auth_records(user, auth)
|
2013-08-23 01:20:43 -05:00
|
|
|
return unless auth && auth[:authenticator_name]
|
2013-08-25 15:18:07 -05:00
|
|
|
|
2013-08-23 01:20:43 -05:00
|
|
|
authenticator = Users::OmniauthCallbacksController.find_authenticator(auth[:authenticator_name])
|
|
|
|
authenticator.after_create_account(user, auth)
|
2013-04-12 17:46:55 -05:00
|
|
|
end
|
2013-08-17 23:43:59 -05:00
|
|
|
|
2013-08-23 04:38:01 -05:00
|
|
|
def register_nickname(user)
|
|
|
|
if user.valid? && SiteSetting.call_discourse_hub?
|
|
|
|
DiscourseHub.register_nickname(user.username, user.email)
|
|
|
|
end
|
|
|
|
end
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|