discourse/app/controllers/users_controller.rb

886 lines
26 KiB
Ruby
Raw Normal View History

require_dependency 'discourse_hub'
require_dependency 'user_name_suggester'
require_dependency 'rate_limiter'
require_dependency 'wizard'
require_dependency 'wizard/builder'
require_dependency 'admin_confirmation'
2013-02-05 14:16:51 -05:00
class UsersController < ApplicationController
skip_before_filter :authorize_mini_profiler, only: [:avatar]
skip_before_filter :check_xhr, only: [:show, :password_reset, :update, :account_created, :activate_account, :perform_account_activation, :user_preferences_redirect, :avatar, :my_redirect, :toggle_anon, :admin_login, :confirm_admin]
2013-02-05 14:16:51 -05:00
before_filter :ensure_logged_in, only: [:username, :update, :user_preferences_redirect, :upload_user_image,
:pick_avatar, :destroy_user_image, :destroy, :check_emails, :topic_tracking_state]
before_filter :respond_to_suspicious_request, only: [:create]
2013-02-07 10:45:24 -05:00
# we need to allow account creation with bad CSRF tokens, if people are caching, the CSRF token on the
# 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
skip_before_filter :verify_authenticity_token, only: [:create]
skip_before_filter :redirect_to_login_if_required, only: [:check_username,
:create,
:get_honeypot_value,
:account_created,
:activate_account,
:perform_account_activation,
:send_activation_email,
:update_activation_email,
:password_reset,
:confirm_email_token,
:admin_login,
:confirm_admin]
def index
end
2013-02-07 10:45:24 -05:00
def show
return redirect_to path('/login') if SiteSetting.hide_user_profiles_from_public && !current_user
2016-09-23 00:44:08 -04:00
@user = fetch_user_from_params(
{ include_inactive: current_user.try(:staff?) },
[{ user_profile: :card_image_badge }]
)
user_serializer = UserSerializer.new(@user, scope: guardian, root: 'user')
# TODO remove this options from serializer
user_serializer.omit_stats = true
topic_id = params[:include_post_count_for].to_i
if topic_id != 0
user_serializer.topic_post_count = { topic_id => Post.secured(guardian).where(topic_id: topic_id, user_id: @user.id).count }
end
2015-09-14 03:51:17 -04:00
if !params[:skip_track_visit] && (@user != current_user)
track_visit_to_user_profile
end
# This is a hack to get around a Rails issue where values with periods aren't handled correctly
# when used as part of a route.
2017-07-27 21:20:09 -04:00
if params[:external_id] && params[:external_id].ends_with?('.json')
return render_json_dump(user_serializer)
end
respond_to do |format|
format.html do
@restrict_fields = guardian.restrict_user_fields?(@user)
store_preloaded("user_#{@user.username}", MultiJson.dump(user_serializer))
end
format.json do
render_json_dump(user_serializer)
end
2013-02-05 14:16:51 -05:00
end
end
def card_badge
end
def update_card_badge
user = fetch_user_from_params
guardian.ensure_can_edit!(user)
user_badge = UserBadge.find_by(id: params[:user_badge_id].to_i)
if user_badge && user_badge.user == user && user_badge.badge.image.present?
user.user_profile.update_column(:card_image_badge_id, user_badge.badge.id)
else
user.user_profile.update_column(:card_image_badge_id, nil)
end
render nothing: true
end
2013-02-05 14:16:51 -05:00
def user_preferences_redirect
redirect_to email_preferences_path(current_user.username_lower)
end
def update
user = fetch_user_from_params
2013-02-05 14:16:51 -05:00
guardian.ensure_can_edit!(user)
if params[:user_fields].present?
params[:custom_fields] = {} unless params[:custom_fields].present?
fields = UserField.all
fields = fields.where(editable: true) unless current_user.staff?
fields.each do |f|
val = params[:user_fields][f.id.to_s]
val = nil if val === "false"
val = val[0...UserField.max_length] if val
return render_json_error(I18n.t("login.missing_user_field")) if val.blank? && f.required?
params[:custom_fields]["user_field_#{f.id}"] = val
end
end
json_result(user, serializer: UserSerializer, additional_errors: [:user_profile]) do |u|
updater = UserUpdater.new(current_user, user)
updater.update(params)
2013-02-07 10:45:24 -05:00
end
2013-02-05 14:16:51 -05:00
end
def username
params.require(:new_username)
2013-02-05 14:16:51 -05:00
user = fetch_user_from_params
guardian.ensure_can_edit_username!(user)
2013-02-07 10:45:24 -05:00
result = UsernameChanger.change(user, params[:new_username], current_user)
2013-02-05 14:16:51 -05:00
if result
render json: { id: user.id, username: user.username }
else
render_json_error(user.errors.full_messages.join(','))
end
2013-02-05 14:16:51 -05:00
end
def check_emails
user = fetch_user_from_params(include_inactive: true)
guardian.ensure_can_check_emails!(user)
StaffActionLogger.new(current_user).log_check_email(user, context: params[:context])
render json: {
email: user.email,
associated_accounts: user.associated_accounts
}
rescue Discourse::InvalidAccess
render json: failed_json, status: 403
end
def topic_tracking_state
user = fetch_user_from_params
guardian.ensure_can_edit!(user)
report = TopicTrackingState.report(user)
serializer = ActiveModel::ArraySerializer.new(report, each_serializer: TopicTrackingStateSerializer)
render json: MultiJson.dump(serializer)
end
def badge_title
params.require(:user_badge_id)
user = fetch_user_from_params
guardian.ensure_can_edit!(user)
2014-07-14 04:06:50 -04:00
user_badge = UserBadge.find_by(id: params[:user_badge_id])
if user_badge && user_badge.user == user && user_badge.badge.allow_title?
user.title = user_badge.badge.name
user.user_profile.badge_granted_title = true
user.save!
user.user_profile.save!
2014-07-14 04:06:50 -04:00
else
user.title = ''
user.save!
end
render nothing: true
end
2013-02-05 14:16:51 -05:00
def preferences
render nothing: true
end
def my_redirect
raise Discourse::NotFound if params[:path] !~ /^[a-z_\-\/]+$/
if current_user.blank?
cookies[:destination_url] = "/my/#{params[:path]}"
redirect_to "/login-preferences"
else
2017-03-28 14:27:54 -04:00
redirect_to(path("/u/#{current_user.username}/#{params[:path]}"))
end
end
def summary
user = fetch_user_from_params
summary = UserSummary.new(user, guardian)
serializer = UserSummarySerializer.new(summary, scope: guardian)
render_json_dump(serializer)
end
2013-02-05 14:16:51 -05:00
def invited
inviter = fetch_user_from_params
2014-08-01 07:06:31 -04:00
offset = params[:offset].to_i || 0
2015-07-11 08:09:12 -04:00
filter_by = params[:filter]
2015-07-11 08:09:12 -04:00
invites = if guardian.can_see_invite_details?(inviter) && filter_by == "pending"
Invite.find_pending_invites_from(inviter, offset)
else
2014-08-01 07:06:31 -04:00
Invite.find_redeemed_invites_from(inviter, offset)
end
2015-07-11 08:09:12 -04:00
invites = invites.filter_by(params[:search])
render_json_dump invites: serialize_data(invites.to_a, InviteSerializer),
can_see_invite_details: guardian.can_see_invite_details?(inviter)
2013-02-05 14:16:51 -05:00
end
def invited_count
inviter = fetch_user_from_params
pending_count = Invite.find_pending_invites_count(inviter)
redeemed_count = Invite.find_redeemed_invites_count(inviter)
2017-07-27 21:20:09 -04:00
render json: { counts: { pending: pending_count, redeemed: redeemed_count,
total: (pending_count.to_i + redeemed_count.to_i) } }
end
2013-02-05 14:16:51 -05:00
def is_local_username
usernames = params[:usernames]
usernames = [params[:username]] if usernames.blank?
2015-11-30 01:12:51 -05:00
groups = Group.where(name: usernames).pluck(:name)
mentionable_groups =
if current_user
Group.mentionable(current_user)
.where(name: usernames)
.pluck(:name, :user_count)
2017-07-27 21:20:09 -04:00
.map { |name, user_count| { name: name, user_count: user_count } }
end
2015-11-30 01:12:51 -05:00
usernames -= groups
usernames.each(&:downcase!)
2015-11-30 01:12:51 -05:00
# Create a New Topic Scenario is not supported (per conversation with codinghorror)
# https://meta.discourse.org/t/taking-another-1-7-release-task/51986/7
cannot_see = []
topic_id = params[:topic_id]
unless topic_id.blank?
topic = Topic.find_by(id: topic_id)
2017-07-27 21:20:09 -04:00
usernames.each { |username| cannot_see.push(username) unless Guardian.new(User.find_by_username(username)).can_see?(topic) }
end
2015-11-30 01:12:51 -05:00
result = User.where(staged: false)
2017-07-27 21:20:09 -04:00
.where(username_lower: usernames)
.pluck(:username_lower)
2017-07-27 21:20:09 -04:00
render json: { valid: result, valid_groups: groups, mentionable_groups: mentionable_groups, cannot_see: cannot_see }
2013-02-05 14:16:51 -05:00
end
def render_available_true
render(json: { available: true })
end
def changing_case_of_own_username(target_user, username)
2017-07-27 21:20:09 -04:00
target_user && 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 14:16:51 -05:00
def check_username
if !params[:username].present?
params.require(:username) if !params[:email].present?
2014-07-16 12:25:24 -04:00
return render(json: success_json)
end
username = params[:username]
2013-02-05 14:16:51 -05:00
target_user = user_from_params_or_current_user
# The special case where someone is changing the case of their own username
return render_available_true if changing_case_of_own_username(target_user, username)
checker = UsernameCheckerService.new
email = params[:email] || target_user.try(:email)
render json: checker.check_username(username, email)
end
2013-02-05 14:16:51 -05:00
def user_from_params_or_current_user
params[:for_user_id] ? User.find(params[:for_user_id]) : current_user
end
2013-02-05 14:16:51 -05:00
def create
params.require(:email)
params.permit(:user_fields)
unless SiteSetting.allow_new_registrations
return fail_with("login.new_registrations_disabled")
end
if params[:password] && params[:password].length > User.max_password_length
return fail_with("login.password_too_long")
end
if params[:email].length > 254 + 1 + 253
return fail_with("login.email_too_long")
end
if User.reserved_username?(params[:username])
return fail_with("login.reserved_username")
end
if user = User.where(staged: true).with_email(params[:email].strip.downcase).first
user_params.each { |k, v| user.send("#{k}=", v) }
user.staged = false
else
user = User.new(user_params)
end
# Handle API approval
if user.approved
user.approved_by_id ||= current_user.id
user.approved_at ||= Time.zone.now
end
# Handle custom fields
user_fields = UserField.all
if user_fields.present?
field_params = params[:user_fields] || {}
fields = user.custom_fields
user_fields.each do |f|
field_val = field_params[f.id.to_s]
if field_val.blank?
return fail_with("login.missing_user_field") if f.required?
else
fields["user_field_#{f.id}"] = field_val[0...UserField.max_length]
end
end
user.custom_fields = fields
end
authentication = UserAuthenticator.new(user, session)
if !authentication.has_authenticator? && !SiteSetting.enable_local_logins
return render nothing: true, status: 500
end
authentication.start
2013-02-05 14:16:51 -05:00
activation = UserActivator.new(user, request, session, cookies)
activation.start
2013-02-05 14:16:51 -05:00
# just assign a password if we have an authenticator and no password
# this is the case for Twitter
user.password = SecureRandom.hex if user.password.blank? && authentication.has_authenticator?
if user.save
authentication.finish
activation.finish
2014-10-01 13:33:49 -04:00
# save user email in session, to show on account-created page
session["user_created_message"] = activation.message
session[SessionController::ACTIVATE_USER_KEY] = user.id
render json: {
success: true,
active: user.active?,
message: activation.message,
user_id: user.id
}
else
errors = user.errors.to_hash
errors[:email] = errors.delete(:primary_email) if errors[:primary_email]
render json: {
success: false,
message: I18n.t(
'login.errors',
errors: user.errors.full_messages.join("\n")
),
errors: errors,
values: {
name: user.name,
username: user.username,
email: user.primary_email&.email
},
is_developer: UsernameCheckerService.is_developer?(user.email)
}
end
rescue ActiveRecord::StatementInvalid
render json: {
success: false,
message: I18n.t("login.something_already_taken")
}
2013-02-05 14:16:51 -05:00
end
def get_honeypot_value
2017-07-27 21:20:09 -04:00
render json: { value: honeypot_value, challenge: challenge_value }
end
2013-02-05 14:16:51 -05:00
def password_reset
expires_now
2013-02-05 14:16:51 -05:00
token = params[:token]
if EmailToken.valid_token_format?(token)
@user =
if request.put?
EmailToken.confirm(token)
else
EmailToken.confirmable(token)&.user
end
if @user
secure_session["password-#{token}"] = @user.id
else
user_id = secure_session["password-#{token}"].to_i
@user = User.find(user_id) if user_id > 0
end
end
if !@user
@error = I18n.t('password_reset.no_token')
2013-11-11 06:28:26 -05:00
elsif request.put?
@invalid_password = params[:password].blank? || params[:password].length > User.max_password_length
if @invalid_password
@user.errors.add(:password, :invalid)
else
@user.password = params[:password]
@user.password_required!
@user.user_auth_tokens.destroy_all
if @user.save
Invite.invalidate_for_email(@user.email) # invite link can't be used to log in anymore
secure_session["password-#{token}"] = nil
logon_after_password_reset
end
end
end
respond_to do |format|
format.html do
if @error
render layout: 'no_ember'
else
2017-07-27 21:20:09 -04:00
store_preloaded("password_reset", MultiJson.dump(is_developer: UsernameCheckerService.is_developer?(@user.email)))
end
return redirect_to(wizard_path) if request.put? && Wizard.user_requires_completion?(@user)
end
format.json do
if request.put?
if @error || @user&.errors&.any?
render json: {
success: false,
message: @error,
errors: @user&.errors.to_hash,
is_developer: UsernameCheckerService.is_developer?(@user.email)
}
else
render json: {
success: true,
message: @success,
requires_approval: !Guardian.new(@user).can_access_forum?,
redirect_to: Wizard.user_requires_completion?(@user) ? wizard_path : nil
}
end
else
2017-07-27 21:20:09 -04:00
render json: { is_developer: UsernameCheckerService.is_developer?(@user.email) }
end
end
2013-02-05 14:16:51 -05:00
end
end
2013-02-07 10:45:24 -05:00
def confirm_email_token
expires_now
EmailToken.confirm(params[:token])
render json: success_json
end
def logon_after_password_reset
2017-07-27 21:20:09 -04:00
message =
if Guardian.new(@user).can_access_forum?
# Log in the user
log_on_user(@user)
'password_reset.success'
else
@requires_approval = true
'password_reset.success_unapproved'
end
@success = I18n.t(message)
end
def admin_login
2015-06-05 04:43:59 -04:00
if current_user
return redirect_to path("/")
end
if request.put?
RateLimiter.new(nil, "admin-login-hr-#{request.remote_ip}", 6, 1.hour).performed!
RateLimiter.new(nil, "admin-login-min-#{request.remote_ip}", 3, 1.minute).performed!
user = User.with_email(params[:email]).where(admin: true).human_users.first
if user
email_token = user.email_tokens.create(email: user.email)
Jobs.enqueue(:critical_user_email, type: :admin_login, user_id: user.id, email_token: email_token.token)
@message = I18n.t("admin_login.success")
else
@message = I18n.t("admin_login.error")
end
elsif params[:token].present?
# token recieved, try to login
if EmailToken.valid_token_format?(params[:token])
@user = EmailToken.confirm(params[:token])
if @user && @user.admin?
# Log in user
log_on_user(@user)
return redirect_to path("/")
else
@message = I18n.t("admin_login.error")
end
else
@message = I18n.t("admin_login.error")
end
end
render layout: false
rescue RateLimiter::LimitExceeded
@message = I18n.t("rate_limiter.slow_down")
render layout: false
end
def toggle_anon
user = AnonymousShadowCreator.get_master(current_user) ||
AnonymousShadowCreator.get(current_user)
if user
log_on_user(user)
render json: success_json
else
render json: failed_json, status: 403
end
end
def account_created
return redirect_to("/") if current_user.present?
@custom_body_class = "static-account-created"
@message = session['user_created_message'] || I18n.t('activation.missing_session')
@account_created = {
message: @message,
show_controls: false
}
if session_user_id = session[SessionController::ACTIVATE_USER_KEY]
if user = User.where(id: session_user_id.to_i).first
@account_created[:username] = user.username
@account_created[:email] = user.email
@account_created[:show_controls] = true
end
end
store_preloaded("accountCreated", MultiJson.dump(@account_created))
expires_now
respond_to do |format|
format.html { render "default/empty" }
format.json { render json: success_json }
end
end
2013-02-05 14:16:51 -05:00
def activate_account
expires_now
render layout: 'no_ember'
end
def perform_account_activation
raise Discourse::InvalidAccess.new if honeypot_or_challenge_fails?(params)
2013-02-05 14:16:51 -05:00
if @user = EmailToken.confirm(params[:token])
# Log in the user unless they need to be approved
if Guardian.new(@user).can_access_forum?
2013-02-05 14:16:51 -05:00
@user.enqueue_welcome_message('welcome_user') if @user.send_welcome_message
log_on_user(@user)
if Wizard.user_requires_completion?(@user)
return redirect_to(wizard_path)
elsif destination_url = cookies[:destination_url]
cookies[:destination_url] = nil
return redirect_to(destination_url)
end
else
@needs_approval = true
2013-02-05 14:16:51 -05:00
end
else
2015-08-23 14:59:16 -04:00
flash.now[:error] = I18n.t('activation.already_done')
2013-02-05 14:16:51 -05:00
end
render layout: 'no_ember'
2013-02-05 14:16:51 -05:00
end
def update_activation_email
RateLimiter.new(nil, "activate-edit-email-hr-#{request.remote_ip}", 5, 1.hour).performed!
if params[:username].present?
@user = User.find_by_username_or_email(params[:username])
raise Discourse::InvalidAccess.new unless @user.present?
raise Discourse::InvalidAccess.new unless @user.confirm_password?(params[:password])
elsif user_key = session[SessionController::ACTIVATE_USER_KEY]
@user = User.where(id: user_key.to_i).first
end
raise Discourse::InvalidAccess.new unless @user.present?
raise Discourse::InvalidAccess.new if @user.active?
raise Discourse::InvalidAccess.new if current_user.present?
User.transaction do
primary_email = @user.primary_email
2017-07-27 21:20:09 -04:00
primary_email.email = params[:email]
primary_email.should_validate_email = true
if primary_email.save
@user.email_tokens.create(email: @user.email)
enqueue_activation_email
render json: success_json
else
render_json_error(@user)
end
end
end
def send_activation_email
if current_user.blank? || !current_user.staff?
RateLimiter.new(nil, "activate-hr-#{request.remote_ip}", 30, 1.hour).performed!
RateLimiter.new(nil, "activate-min-#{request.remote_ip}", 6, 1.minute).performed!
end
raise Discourse::InvalidAccess.new if SiteSetting.must_approve_users?
if params[:username].present?
@user = User.find_by_username_or_email(params[:username].to_s)
end
raise Discourse::NotFound unless @user
if !current_user&.staff? &&
@user.id != session[SessionController::ACTIVATE_USER_KEY]
raise Discourse::InvalidAccess.new
end
session.delete(SessionController::ACTIVATE_USER_KEY)
if @user.active && @user.email_confirmed?
render_json_error(I18n.t('activation.activated'), status: 409)
2017-03-13 10:22:23 -04:00
else
@email_token = @user.email_tokens.unconfirmed.active.first
enqueue_activation_email
render nothing: true
end
end
def enqueue_activation_email
@email_token ||= @user.email_tokens.create(email: @user.email)
Jobs.enqueue(:critical_user_email, type: :signup, user_id: @user.id, email_token: @email_token.token, to_address: @user.email)
end
2013-02-05 14:16:51 -05:00
def search_users
2013-02-07 05:59:25 -05:00
term = params[:term].to_s.strip
2013-02-05 14:16:51 -05:00
topic_id = params[:topic_id]
topic_id = topic_id.to_i if topic_id
topic_allowed_users = params[:topic_allowed_users] || false
2013-02-05 14:16:51 -05:00
if params[:group].present?
@group = Group.find_by(name: params[:group])
end
results = UserSearch.new(term,
topic_id: topic_id,
topic_allowed_users: topic_allowed_users,
searching_user: current_user,
group: @group
).search
2013-02-05 14:16:51 -05:00
user_fields = [:username, :upload_avatar_template]
user_fields << :name if SiteSetting.enable_names?
to_render = { users: results.as_json(only: user_fields, methods: [:avatar_template]) }
if params[:include_groups] == "true"
to_render[:groups] = Group.search_group(term).map do |m|
{ name: m.name, full_name: m.full_name }
end
end
if params[:include_mentionable_groups] == "true" && current_user
to_render[:groups] = Group.mentionable(current_user)
2017-07-27 21:20:09 -04:00
.where("name ILIKE :term_like", term_like: "#{term}%")
.map do |m|
{ name: m.name, full_name: m.full_name }
end
end
render json: to_render
2013-02-05 14:16:51 -05:00
end
AVATAR_TYPES_WITH_UPLOAD ||= %w{uploaded custom gravatar}
def pick_avatar
2013-08-13 16:08:29 -04:00
user = fetch_user_from_params
guardian.ensure_can_edit!(user)
type = params[:type]
2015-09-11 09:47:48 -04:00
upload_id = params[:upload_id]
if SiteSetting.sso_overrides_avatar
return render json: failed_json, status: 422
end
if !SiteSetting.allow_uploaded_avatars
if type == "uploaded" || type == "custom"
return render json: failed_json, status: 422
end
end
user.uploaded_avatar_id = upload_id
2015-09-11 09:47:48 -04:00
if AVATAR_TYPES_WITH_UPLOAD.include?(type)
# make sure the upload exists
unless Upload.where(id: upload_id).exists?
return render_json_error I18n.t("avatar.missing")
end
if type == "gravatar"
user.user_avatar.gravatar_upload_id = upload_id
else
user.user_avatar.custom_upload_id = upload_id
end
2015-09-11 09:47:48 -04:00
end
2013-08-13 16:08:29 -04:00
user.save!
user.user_avatar.save!
2013-08-13 16:08:29 -04:00
render json: success_json
2013-08-13 16:08:29 -04:00
end
2014-04-14 16:55:57 -04:00
def destroy_user_image
user = fetch_user_from_params
guardian.ensure_can_edit!(user)
2014-04-14 16:55:57 -04:00
case params.require(:type)
when "profile_background"
user.user_profile.clear_profile_background
when "card_background"
user.user_profile.clear_card_background
else
raise Discourse::InvalidParameters.new(:type)
end
2014-04-14 16:55:57 -04:00
render json: success_json
end
2014-04-14 16:55:57 -04:00
def destroy
@user = fetch_user_from_params
guardian.ensure_can_delete_user!(@user)
2014-04-14 16:55:57 -04:00
2017-07-27 21:20:09 -04:00
UserDestroyer.new(current_user).destroy(@user, delete_posts: true, context: params[:context])
2014-04-14 16:55:57 -04:00
render json: success_json
end
2014-07-11 03:32:29 -04:00
def read_faq
if user = current_user
2014-07-11 03:32:29 -04:00
user.user_stat.read_faq = 1.second.ago
user.user_stat.save
end
render json: success_json
end
def staff_info
@user = fetch_user_from_params(include_inactive: true)
guardian.ensure_can_see_staff_info!(@user)
result = {}
2017-04-15 00:11:02 -04:00
%W{number_of_deleted_posts number_of_flagged_posts number_of_flags_given number_of_suspensions warnings_received_count}.each do |info|
result[info] = @user.send(info)
end
render json: result
end
def confirm_admin
@confirmation = AdminConfirmation.find_by_code(params[:token])
raise Discourse::NotFound unless @confirmation
raise Discourse::InvalidAccess.new unless
@confirmation.performed_by.id == (current_user&.id || @confirmation.performed_by.id)
if request.post?
@confirmation.email_confirmed!
@confirmed = true
end
render layout: 'no_ember'
end
2013-02-05 14:16:51 -05:00
private
def honeypot_value
2017-07-27 21:20:09 -04:00
Digest::SHA1::hexdigest("#{Discourse.current_hostname}:#{GlobalSetting.safe_secret_key_base}")[0, 15]
end
def challenge_value
challenge = $redis.get('SECRET_CHALLENGE')
2017-07-27 21:20:09 -04:00
unless challenge && challenge.length == 16 * 2
challenge = SecureRandom.hex(16)
2017-07-27 21:20:09 -04:00
$redis.set('SECRET_CHALLENGE', challenge)
end
challenge
end
def respond_to_suspicious_request
if suspicious?(params)
render json: {
success: true,
active: false,
message: I18n.t("login.activate_email", email: params[:email])
}
end
end
def suspicious?(params)
return false if current_user && is_api? && current_user.admin?
honeypot_or_challenge_fails?(params) || SiteSetting.invite_only?
end
def honeypot_or_challenge_fails?(params)
return false if is_api?
params[:password_confirmation] != honeypot_value ||
params[:challenge] != challenge_value.try(:reverse)
end
def user_params
result = params.permit(:name, :email, :password, :username, :date_of_birth)
2017-07-27 21:20:09 -04:00
.merge(ip_address: request.remote_ip,
registration_ip_address: request.remote_ip,
locale: user_locale)
if !UsernameCheckerService.is_developer?(result['email']) &&
is_api? &&
current_user.present? &&
current_user.admin?
result.merge!(params.permit(:active, :staged, :approved))
end
result
end
def user_locale
I18n.locale
end
def fail_with(key)
render json: { success: false, message: I18n.t(key) }
end
2015-09-14 03:51:17 -04:00
def track_visit_to_user_profile
user_profile_id = @user.user_profile.id
ip = request.remote_ip
user_id = (current_user.id if current_user)
Scheduler::Defer.later 'Track profile view visit' do
UserProfileView.add(user_profile_id, ip, user_id)
end
end
2013-02-05 14:16:51 -05:00
end