2013-02-14 12:57:26 -05:00
|
|
|
require_dependency 'discourse_hub'
|
2013-06-06 10:40:10 -04:00
|
|
|
require_dependency 'user_name_suggester'
|
2014-09-18 10:48:56 -04:00
|
|
|
require_dependency 'rate_limiter'
|
2016-10-18 11:44:25 -04:00
|
|
|
require_dependency 'wizard'
|
|
|
|
require_dependency 'wizard/builder'
|
2017-04-04 13:59:22 -04:00
|
|
|
require_dependency 'admin_confirmation'
|
2013-02-05 14:16:51 -05:00
|
|
|
|
|
|
|
class UsersController < ApplicationController
|
|
|
|
|
2017-08-31 00:06:56 -04:00
|
|
|
skip_before_action :authorize_mini_profiler, only: [:avatar]
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2018-01-31 23:17:59 -05:00
|
|
|
requires_login only: [
|
2018-01-31 20:26:45 -05:00
|
|
|
:username, :update, :user_preferences_redirect, :upload_user_image,
|
|
|
|
:pick_avatar, :destroy_user_image, :destroy, :check_emails, :topic_tracking_state,
|
2018-06-28 04:12:32 -04:00
|
|
|
:preferences, :create_second_factor, :update_second_factor, :create_second_factor_backup
|
2018-01-31 20:26:45 -05:00
|
|
|
]
|
|
|
|
|
|
|
|
skip_before_action :check_xhr, only: [
|
|
|
|
:show, :badges, :password_reset, :update, :account_created,
|
|
|
|
:activate_account, :perform_account_activation, :user_preferences_redirect, :avatar,
|
2017-04-20 11:17:24 -04:00
|
|
|
:my_redirect, :toggle_anon, :admin_login, :confirm_admin, :email_login
|
2018-01-31 20:26:45 -05:00
|
|
|
]
|
2016-08-12 02:35:10 -04:00
|
|
|
|
2017-08-31 00:06:56 -04:00
|
|
|
before_action :respond_to_suspicious_request, only: [:create]
|
2013-02-07 10:45:24 -05:00
|
|
|
|
2013-05-22 11:20:16 -04:00
|
|
|
# we need to allow account creation with bad CSRF tokens, if people are caching, the CSRF token on the
|
2013-05-03 02:43:11 -04: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
|
2017-08-31 00:06:56 -04:00
|
|
|
skip_before_action :verify_authenticity_token, only: [:create]
|
|
|
|
skip_before_action :redirect_to_login_if_required, only: [:check_username,
|
2013-07-15 12:12:54 -04:00
|
|
|
:create,
|
|
|
|
:get_honeypot_value,
|
2014-09-23 09:50:57 -04:00
|
|
|
:account_created,
|
2013-07-15 12:12:54 -04:00
|
|
|
:activate_account,
|
2014-07-15 13:47:47 -04:00
|
|
|
:perform_account_activation,
|
2013-07-15 12:12:54 -04:00
|
|
|
:send_activation_email,
|
2017-04-05 16:14:22 -04:00
|
|
|
:update_activation_email,
|
2015-04-27 06:59:48 -04:00
|
|
|
:password_reset,
|
2016-01-04 11:48:54 -05:00
|
|
|
:confirm_email_token,
|
2017-04-20 11:17:24 -04:00
|
|
|
:email_login,
|
2017-04-04 13:59:22 -04:00
|
|
|
:admin_login,
|
|
|
|
:confirm_admin]
|
2013-05-03 02:43:11 -04:00
|
|
|
|
2015-03-19 11:48:16 -04:00
|
|
|
def index
|
|
|
|
end
|
|
|
|
|
2013-02-07 10:45:24 -05:00
|
|
|
def show
|
2017-04-20 03:30:45 -04:00
|
|
|
return redirect_to path('/login') if SiteSetting.hide_user_profiles_from_public && !current_user
|
2015-10-28 14:56:08 -04:00
|
|
|
|
2016-09-23 00:44:08 -04:00
|
|
|
@user = fetch_user_from_params(
|
2018-04-24 09:46:57 -04:00
|
|
|
include_inactive: current_user.try(:staff?) || (current_user && SiteSetting.show_inactive_accounts)
|
2016-09-23 00:44:08 -04:00
|
|
|
)
|
|
|
|
|
2013-03-08 15:04:37 -05:00
|
|
|
user_serializer = UserSerializer.new(@user, scope: guardian, root: 'user')
|
2015-12-17 02:06:04 -05:00
|
|
|
|
|
|
|
# TODO remove this options from serializer
|
|
|
|
user_serializer.omit_stats = true
|
|
|
|
|
2015-03-24 12:33:17 -04:00
|
|
|
topic_id = params[:include_post_count_for].to_i
|
|
|
|
if topic_id != 0
|
2017-09-14 14:08:16 -04:00
|
|
|
user_serializer.topic_post_count = { topic_id => Post.secured(guardian).where(topic_id: topic_id, user_id: @user.id).count }
|
2015-03-24 12:33:17 -04:00
|
|
|
end
|
|
|
|
|
2015-09-14 03:51:17 -04:00
|
|
|
if !params[:skip_track_visit] && (@user != current_user)
|
|
|
|
track_visit_to_user_profile
|
|
|
|
end
|
|
|
|
|
2015-02-02 12:55:32 -05:00
|
|
|
# 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')
|
2015-02-02 12:55:32 -05:00
|
|
|
return render_json_dump(user_serializer)
|
|
|
|
end
|
|
|
|
|
2013-03-08 15:04:37 -05:00
|
|
|
respond_to do |format|
|
|
|
|
format.html do
|
2014-11-27 13:51:13 -05:00
|
|
|
@restrict_fields = guardian.restrict_user_fields?(@user)
|
2013-03-08 15:04:37 -05:00
|
|
|
store_preloaded("user_#{@user.username}", MultiJson.dump(user_serializer))
|
2017-11-21 12:22:24 -05:00
|
|
|
render :show
|
2013-03-08 15:04:37 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
format.json do
|
|
|
|
render_json_dump(user_serializer)
|
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-11-21 12:22:24 -05:00
|
|
|
def badges
|
|
|
|
raise Discourse::NotFound unless SiteSetting.enable_badges?
|
|
|
|
show
|
|
|
|
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
|
2013-10-07 06:19:45 -04:00
|
|
|
user = fetch_user_from_params
|
2013-02-05 14:16:51 -05:00
|
|
|
guardian.ensure_can_edit!(user)
|
2017-08-31 00:06:56 -04:00
|
|
|
attributes = user_params.merge!(custom_fields: params[:custom_fields])
|
2014-09-26 14:48:34 -04:00
|
|
|
|
2018-01-26 16:53:10 -05:00
|
|
|
# We can't update the username via this route. Use the username route
|
|
|
|
attributes.delete(:username)
|
|
|
|
|
2014-09-26 14:48:34 -04:00
|
|
|
if params[:user_fields].present?
|
2017-08-31 00:06:56 -04:00
|
|
|
attributes[:custom_fields] = {} unless params[:custom_fields].present?
|
2015-03-20 15:18:43 -04:00
|
|
|
|
|
|
|
fields = UserField.all
|
|
|
|
fields = fields.where(editable: true) unless current_user.staff?
|
|
|
|
fields.each do |f|
|
2017-12-15 21:16:22 -05:00
|
|
|
field_id = f.id.to_s
|
|
|
|
next unless params[:user_fields].has_key?(field_id)
|
|
|
|
|
|
|
|
val = params[:user_fields][field_id]
|
2014-10-02 15:56:28 -04:00
|
|
|
val = nil if val === "false"
|
2015-02-23 13:02:30 -05:00
|
|
|
val = val[0...UserField.max_length] if val
|
2014-10-08 14:38:18 -04:00
|
|
|
|
|
|
|
return render_json_error(I18n.t("login.missing_user_field")) if val.blank? && f.required?
|
2017-08-31 00:06:56 -04:00
|
|
|
attributes[:custom_fields]["user_field_#{f.id}"] = val
|
2014-09-26 14:48:34 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-09-08 15:17:31 -04:00
|
|
|
json_result(user, serializer: UserSerializer, additional_errors: [:user_profile]) do |u|
|
2013-12-10 12:46:35 -05:00
|
|
|
updater = UserUpdater.new(current_user, user)
|
2017-08-31 00:06:56 -04:00
|
|
|
updater.update(attributes.permit!)
|
2013-02-07 10:45:24 -05:00
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def username
|
2013-06-06 03:14:32 -04:00
|
|
|
params.require(:new_username)
|
2013-02-05 14:16:51 -05:00
|
|
|
|
|
|
|
user = fetch_user_from_params
|
2013-08-12 14:54:52 -04:00
|
|
|
guardian.ensure_can_edit_username!(user)
|
2013-02-07 10:45:24 -05:00
|
|
|
|
2015-03-06 16:44:54 -05:00
|
|
|
result = UsernameChanger.change(user, params[:new_username], current_user)
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2017-06-06 21:45:27 -04: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
|
|
|
|
|
2014-09-29 16:31:05 -04:00
|
|
|
def check_emails
|
2014-10-28 20:06:59 -04:00
|
|
|
user = fetch_user_from_params(include_inactive: true)
|
2014-09-29 16:31:05 -04:00
|
|
|
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
|
|
|
|
}
|
2014-10-07 19:26:18 -04:00
|
|
|
rescue Discourse::InvalidAccess
|
2014-09-29 16:31:05 -04:00
|
|
|
render json: failed_json, status: 403
|
|
|
|
end
|
|
|
|
|
2016-08-12 02:35:10 -04:00
|
|
|
def topic_tracking_state
|
|
|
|
user = fetch_user_from_params
|
|
|
|
guardian.ensure_can_edit!(user)
|
|
|
|
|
2016-12-02 01:03:31 -05:00
|
|
|
report = TopicTrackingState.report(user)
|
2016-08-12 02:35:10 -04:00
|
|
|
serializer = ActiveModel::ArraySerializer.new(report, each_serializer: TopicTrackingStateSerializer)
|
|
|
|
|
|
|
|
render json: MultiJson.dump(serializer)
|
|
|
|
end
|
|
|
|
|
2014-04-17 23:10:53 -04:00
|
|
|
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?
|
2018-01-05 10:58:15 -05:00
|
|
|
user.title = user_badge.badge.display_name
|
2014-10-07 19:26:18 -04:00
|
|
|
user.user_profile.badge_granted_title = true
|
2014-04-17 23:10:53 -04:00
|
|
|
user.save!
|
2014-10-07 19:26:18 -04:00
|
|
|
user.user_profile.save!
|
2014-07-14 04:06:50 -04:00
|
|
|
else
|
|
|
|
user.title = ''
|
|
|
|
user.save!
|
2014-04-17 23:10:53 -04:00
|
|
|
end
|
|
|
|
|
2017-08-31 00:06:56 -04:00
|
|
|
render body: nil
|
2014-04-17 23:10:53 -04:00
|
|
|
end
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
def preferences
|
2017-08-31 00:06:56 -04:00
|
|
|
render body: nil
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2014-04-21 11:52:11 -04:00
|
|
|
def my_redirect
|
2016-03-19 08:03:10 -04:00
|
|
|
raise Discourse::NotFound if params[:path] !~ /^[a-z_\-\/]+$/
|
2015-10-14 12:40:13 -04:00
|
|
|
|
|
|
|
if current_user.blank?
|
|
|
|
cookies[:destination_url] = "/my/#{params[:path]}"
|
2015-10-14 13:39:31 -04:00
|
|
|
redirect_to "/login-preferences"
|
2015-10-14 12:40:13 -04:00
|
|
|
else
|
2017-03-28 14:27:54 -04:00
|
|
|
redirect_to(path("/u/#{current_user.username}/#{params[:path]}"))
|
2014-04-21 11:52:11 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-01-19 23:11:52 -05:00
|
|
|
def summary
|
2017-12-07 01:23:27 -05:00
|
|
|
user = fetch_user_from_params(include_inactive: current_user.try(:staff?) || (current_user && SiteSetting.show_inactive_accounts))
|
2016-01-19 23:11:52 -05:00
|
|
|
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
|
2017-12-07 01:23:27 -05:00
|
|
|
inviter = fetch_user_from_params(include_inactive: current_user.try(:staff?) || (current_user && SiteSetting.show_inactive_accounts))
|
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]
|
2013-11-05 17:52:50 -05:00
|
|
|
|
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)
|
2013-11-08 14:11:41 -05:00
|
|
|
else
|
2014-08-01 07:06:31 -04:00
|
|
|
Invite.find_redeemed_invites_from(inviter, offset)
|
2013-11-05 17:52:50 -05:00
|
|
|
end
|
|
|
|
|
2015-07-11 08:09:12 -04:00
|
|
|
invites = invites.filter_by(params[:search])
|
2014-03-21 14:13:04 -04:00
|
|
|
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
|
|
|
|
|
2015-08-24 15:03:25 -04:00
|
|
|
def invited_count
|
2017-12-07 01:23:27 -05:00
|
|
|
inviter = fetch_user_from_params(include_inactive: current_user.try(:staff?) || (current_user && SiteSetting.show_inactive_accounts))
|
2015-08-24 15:03:25 -04:00
|
|
|
|
|
|
|
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) } }
|
2015-08-24 15:03:25 -04:00
|
|
|
end
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
def is_local_username
|
2015-11-27 12:16:50 -05:00
|
|
|
usernames = params[:usernames]
|
|
|
|
usernames = [params[:username]] if usernames.blank?
|
2015-06-11 10:31:43 -04:00
|
|
|
|
2015-11-30 01:12:51 -05:00
|
|
|
groups = Group.where(name: usernames).pluck(:name)
|
2015-12-03 21:40:38 -05:00
|
|
|
mentionable_groups =
|
|
|
|
if current_user
|
|
|
|
Group.mentionable(current_user)
|
|
|
|
.where(name: usernames)
|
|
|
|
.pluck(:name, :user_count)
|
2017-12-21 00:13:57 -05:00
|
|
|
.map do |name, user_count|
|
|
|
|
{
|
|
|
|
name: name,
|
|
|
|
user_count: user_count
|
|
|
|
}
|
|
|
|
end
|
2015-12-03 21:40:38 -05:00
|
|
|
end
|
|
|
|
|
2015-11-30 01:12:51 -05:00
|
|
|
usernames -= groups
|
2016-05-22 09:01:46 -04:00
|
|
|
usernames.each(&:downcase!)
|
2015-11-30 01:12:51 -05:00
|
|
|
|
2016-11-16 06:26:36 -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
|
2016-11-14 22:03:16 -05:00
|
|
|
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) }
|
2016-11-14 22:03:16 -05:00
|
|
|
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)
|
2015-11-27 12:16:50 -05:00
|
|
|
|
2017-12-21 00:13:57 -05:00
|
|
|
render json: {
|
|
|
|
valid: result,
|
|
|
|
valid_groups: groups,
|
|
|
|
mentionable_groups: mentionable_groups,
|
|
|
|
cannot_see: cannot_see,
|
|
|
|
max_users_notified_per_group_mention: SiteSetting.max_users_notified_per_group_mention
|
|
|
|
}
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2013-08-25 04:57:12 -04:00
|
|
|
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)
|
2013-08-25 04:57:12 -04:00
|
|
|
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
|
2013-11-19 14:15:05 -05:00
|
|
|
if !params[:username].present?
|
|
|
|
params.require(:username) if !params[:email].present?
|
2014-07-16 12:25:24 -04:00
|
|
|
return render(json: success_json)
|
2013-11-19 14:15:05 -05:00
|
|
|
end
|
2013-08-25 04:57:12 -04:00
|
|
|
username = params[:username]
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-08-25 04:57:12 -04:00
|
|
|
target_user = user_from_params_or_current_user
|
2013-07-30 14:13:56 -04:00
|
|
|
|
2013-06-28 16:21:46 -04:00
|
|
|
# The special case where someone is changing the case of their own username
|
2013-08-25 04:57:12 -04:00
|
|
|
return render_available_true if changing_case_of_own_username(target_user, username)
|
2013-06-28 16:21:46 -04:00
|
|
|
|
2013-09-06 05:35:29 -04:00
|
|
|
checker = UsernameCheckerService.new
|
|
|
|
email = params[:email] || target_user.try(:email)
|
2013-11-19 14:15:05 -05:00
|
|
|
render json: checker.check_username(username, email)
|
2013-08-25 04:57:12 -04:00
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-08-25 04:57:12 -04: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
|
2017-06-12 04:48:32 -04:00
|
|
|
params.require(:email)
|
2014-09-26 14:48:34 -04:00
|
|
|
params.permit(:user_fields)
|
|
|
|
|
2014-07-14 15:42:14 -04:00
|
|
|
unless SiteSetting.allow_new_registrations
|
2014-09-26 14:48:34 -04:00
|
|
|
return fail_with("login.new_registrations_disabled")
|
2014-07-14 15:42:14 -04:00
|
|
|
end
|
|
|
|
|
2014-09-11 15:22:11 -04:00
|
|
|
if params[:password] && params[:password].length > User.max_password_length
|
2014-09-26 14:48:34 -04:00
|
|
|
return fail_with("login.password_too_long")
|
2014-09-11 15:22:11 -04:00
|
|
|
end
|
|
|
|
|
2017-06-12 04:48:32 -04:00
|
|
|
if params[:email].length > 254 + 1 + 253
|
2015-07-13 16:40:52 -04:00
|
|
|
return fail_with("login.email_too_long")
|
|
|
|
end
|
|
|
|
|
2017-04-12 22:44:26 -04:00
|
|
|
if User.reserved_username?(params[:username])
|
2015-07-01 16:44:53 -04:00
|
|
|
return fail_with("login.reserved_username")
|
|
|
|
end
|
|
|
|
|
2018-01-19 09:29:15 -05:00
|
|
|
new_user_params = user_params
|
|
|
|
user = User.unstage(new_user_params)
|
|
|
|
user = User.new(new_user_params) if user.nil?
|
2013-02-06 19:25:21 -05:00
|
|
|
|
2017-08-28 15:36:46 -04:00
|
|
|
# Handle API approval
|
|
|
|
if user.approved
|
|
|
|
user.approved_by_id ||= current_user.id
|
|
|
|
user.approved_at ||= Time.zone.now
|
|
|
|
end
|
|
|
|
|
2014-09-26 14:48:34 -04:00
|
|
|
# Handle custom fields
|
2014-10-08 14:38:18 -04:00
|
|
|
user_fields = UserField.all
|
|
|
|
if user_fields.present?
|
2015-01-27 04:48:27 -05:00
|
|
|
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
|
2015-02-23 13:02:30 -05:00
|
|
|
fields["user_field_#{f.id}"] = field_val[0...UserField.max_length]
|
2014-09-26 14:48:34 -04:00
|
|
|
end
|
|
|
|
end
|
2015-01-27 04:48:27 -05:00
|
|
|
|
|
|
|
user.custom_fields = fields
|
2014-09-26 14:48:34 -04:00
|
|
|
end
|
|
|
|
|
2013-11-12 17:37:38 -05:00
|
|
|
authentication = UserAuthenticator.new(user, session)
|
2014-03-26 00:39:44 -04:00
|
|
|
|
|
|
|
if !authentication.has_authenticator? && !SiteSetting.enable_local_logins
|
2018-05-25 22:18:19 -04:00
|
|
|
return render body: nil, status: :forbidden
|
2014-03-26 00:39:44 -04:00
|
|
|
end
|
|
|
|
|
2013-11-12 17:37:38 -05:00
|
|
|
authentication.start
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2017-10-16 13:51:35 -04:00
|
|
|
if authentication.email_valid? && !authentication.authenticated?
|
|
|
|
# posted email is different that the already validated one?
|
|
|
|
return fail_with('login.incorrect_username_email_or_password')
|
|
|
|
end
|
|
|
|
|
2013-11-12 17:37:38 -05:00
|
|
|
activation = UserActivator.new(user, request, session, cookies)
|
|
|
|
activation.start
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2014-03-19 23:49:25 -04: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?
|
|
|
|
|
2013-11-12 17:37:38 -05:00
|
|
|
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
|
2014-11-04 15:47:32 -05:00
|
|
|
session["user_created_message"] = activation.message
|
2017-05-02 15:57:55 -04:00
|
|
|
session[SessionController::ACTIVATE_USER_KEY] = user.id
|
2014-09-26 14:48:34 -04:00
|
|
|
|
2013-11-12 17:37:38 -05:00
|
|
|
render json: {
|
|
|
|
success: true,
|
|
|
|
active: user.active?,
|
2014-09-22 19:06:19 -04:00
|
|
|
message: activation.message,
|
|
|
|
user_id: user.id
|
2017-10-03 14:08:37 -04:00
|
|
|
}
|
2017-10-03 15:28:15 -04:00
|
|
|
elsif SiteSetting.hide_email_address_taken && user.errors[:primary_email]&.include?(I18n.t('errors.messages.taken'))
|
2017-10-03 14:08:37 -04:00
|
|
|
session["user_created_message"] = activation.success_message
|
|
|
|
|
|
|
|
if existing_user = User.find_by_email(user.primary_email&.email)
|
|
|
|
Jobs.enqueue(:critical_user_email, type: :account_exists, user_id: existing_user.id)
|
|
|
|
end
|
|
|
|
|
|
|
|
render json: {
|
|
|
|
success: true,
|
|
|
|
active: user.active?,
|
|
|
|
message: activation.success_message,
|
|
|
|
user_id: user.id
|
2013-11-12 17:37:38 -05:00
|
|
|
}
|
|
|
|
else
|
2017-09-11 13:22:04 -04:00
|
|
|
errors = user.errors.to_hash
|
|
|
|
errors[:email] = errors.delete(:primary_email) if errors[:primary_email]
|
|
|
|
|
2013-11-12 17:37:38 -05:00
|
|
|
render json: {
|
|
|
|
success: false,
|
|
|
|
message: I18n.t(
|
|
|
|
'login.errors',
|
|
|
|
errors: user.errors.full_messages.join("\n")
|
|
|
|
),
|
2017-09-11 13:22:04 -04:00
|
|
|
errors: errors,
|
|
|
|
values: {
|
|
|
|
name: user.name,
|
|
|
|
username: user.username,
|
|
|
|
email: user.primary_email&.email
|
|
|
|
},
|
2016-08-05 11:57:13 -04:00
|
|
|
is_developer: UsernameCheckerService.is_developer?(user.email)
|
2013-11-12 17:37:38 -05:00
|
|
|
}
|
|
|
|
end
|
2013-03-07 14:56:28 -05:00
|
|
|
rescue ActiveRecord::StatementInvalid
|
2013-11-12 17:37:38 -05:00
|
|
|
render json: {
|
|
|
|
success: false,
|
|
|
|
message: I18n.t("login.something_already_taken")
|
|
|
|
}
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2013-02-06 19:25:21 -05:00
|
|
|
def get_honeypot_value
|
2017-07-27 21:20:09 -04:00
|
|
|
render json: { value: honeypot_value, challenge: challenge_value }
|
2013-02-06 19:25:21 -05:00
|
|
|
end
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
def password_reset
|
2015-02-19 18:28:38 -05:00
|
|
|
expires_now
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2016-12-19 02:00:22 -05:00
|
|
|
token = params[:token]
|
|
|
|
|
|
|
|
if EmailToken.valid_token_format?(token)
|
2017-04-04 02:17:45 -04:00
|
|
|
@user =
|
|
|
|
if request.put?
|
|
|
|
EmailToken.confirm(token)
|
|
|
|
else
|
|
|
|
EmailToken.confirmable(token)&.user
|
|
|
|
end
|
2014-08-25 15:30:52 -04:00
|
|
|
|
|
|
|
if @user
|
2016-12-19 02:00:22 -05:00
|
|
|
secure_session["password-#{token}"] = @user.id
|
2014-08-25 15:30:52 -04:00
|
|
|
else
|
2016-12-19 02:00:22 -05:00
|
|
|
user_id = secure_session["password-#{token}"].to_i
|
|
|
|
@user = User.find(user_id) if user_id > 0
|
2014-08-25 15:30:52 -04:00
|
|
|
end
|
2014-07-01 23:06:55 -04:00
|
|
|
end
|
|
|
|
|
2018-02-20 01:44:51 -05:00
|
|
|
totp_enabled = @user&.totp_enabled?
|
|
|
|
|
2018-06-28 04:12:32 -04:00
|
|
|
if !totp_enabled || @user.authenticate_second_factor(params[:second_factor_token], params[:second_factor_method].to_i)
|
2017-12-21 20:18:12 -05:00
|
|
|
secure_session["second-factor-#{token}"] = "true"
|
|
|
|
end
|
2018-02-20 01:44:51 -05:00
|
|
|
|
|
|
|
valid_second_factor = secure_session["second-factor-#{token}"] == "true"
|
2017-12-21 20:18:12 -05:00
|
|
|
|
2014-07-01 23:06:55 -04:00
|
|
|
if !@user
|
2015-02-19 18:28:38 -05:00
|
|
|
@error = I18n.t('password_reset.no_token')
|
2013-11-11 06:28:26 -05:00
|
|
|
elsif request.put?
|
2014-09-11 15:22:11 -04:00
|
|
|
@invalid_password = params[:password].blank? || params[:password].length > User.max_password_length
|
|
|
|
|
2018-02-20 01:44:51 -05:00
|
|
|
if !valid_second_factor
|
2017-12-21 20:18:12 -05:00
|
|
|
RateLimiter.new(nil, "second-factor-min-#{request.remote_ip}", 3, 1.minute).performed!
|
2018-06-28 04:12:32 -04:00
|
|
|
@user.errors.add(:user_second_factors, :invalid)
|
2017-12-21 20:18:12 -05:00
|
|
|
@error = I18n.t('login.invalid_second_factor_code')
|
|
|
|
elsif @invalid_password
|
2014-09-11 15:22:11 -04:00
|
|
|
@user.errors.add(:password, :invalid)
|
|
|
|
else
|
|
|
|
@user.password = params[:password]
|
|
|
|
@user.password_required!
|
2017-01-31 17:21:37 -05:00
|
|
|
@user.user_auth_tokens.destroy_all
|
2014-09-11 15:22:11 -04:00
|
|
|
if @user.save
|
|
|
|
Invite.invalidate_for_email(@user.email) # invite link can't be used to log in anymore
|
2016-12-19 02:00:22 -05:00
|
|
|
secure_session["password-#{token}"] = nil
|
2017-12-21 20:18:12 -05:00
|
|
|
secure_session["second-factor-#{token}"] = nil
|
2014-09-11 15:22:11 -04:00
|
|
|
logon_after_password_reset
|
2017-01-31 15:42:12 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
respond_to do |format|
|
|
|
|
format.html do
|
|
|
|
if @error
|
|
|
|
render layout: 'no_ember'
|
|
|
|
else
|
2017-11-07 16:14:47 -05:00
|
|
|
store_preloaded(
|
|
|
|
"password_reset",
|
2018-02-20 01:44:51 -05:00
|
|
|
MultiJson.dump(
|
|
|
|
is_developer: UsernameCheckerService.is_developer?(@user.email),
|
|
|
|
admin: @user.admin?,
|
2018-06-28 04:12:32 -04:00
|
|
|
second_factor_required: !valid_second_factor,
|
|
|
|
backup_enabled: @user.backup_codes_enabled?
|
2018-02-20 01:44:51 -05:00
|
|
|
)
|
2017-11-07 16:14:47 -05:00
|
|
|
)
|
2017-01-31 15:42:12 -05:00
|
|
|
end
|
2018-02-20 01:44:51 -05:00
|
|
|
|
2017-06-07 12:36:52 -04:00
|
|
|
return redirect_to(wizard_path) if request.put? && Wizard.user_requires_completion?(@user)
|
2017-01-31 15:42:12 -05:00
|
|
|
end
|
2016-10-18 11:44:25 -04:00
|
|
|
|
2017-01-31 15:42:12 -05:00
|
|
|
format.json do
|
|
|
|
if request.put?
|
2017-04-04 02:17:45 -04:00
|
|
|
if @error || @user&.errors&.any?
|
2017-01-31 15:42:12 -05:00
|
|
|
render json: {
|
|
|
|
success: false,
|
|
|
|
message: @error,
|
2017-12-12 05:54:03 -05:00
|
|
|
errors: @user&.errors&.to_hash,
|
2018-01-09 13:01:59 -05:00
|
|
|
is_developer: UsernameCheckerService.is_developer?(@user&.email),
|
|
|
|
admin: @user&.admin?
|
2017-01-31 15:42:12 -05:00
|
|
|
}
|
|
|
|
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
|
2018-02-20 01:44:51 -05:00
|
|
|
render json: {
|
|
|
|
is_developer: UsernameCheckerService.is_developer?(@user.email),
|
|
|
|
admin: @user.admin?,
|
2018-06-28 04:12:32 -04:00
|
|
|
second_factor_required: !valid_second_factor,
|
|
|
|
backup_enabled: @user.backup_codes_enabled?
|
2018-02-20 01:44:51 -05:00
|
|
|
}
|
2014-09-11 15:22:11 -04:00
|
|
|
end
|
2014-01-21 16:53:46 -05:00
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
end
|
2013-02-07 10:45:24 -05:00
|
|
|
|
2016-01-04 11:48:54 -05:00
|
|
|
def confirm_email_token
|
|
|
|
expires_now
|
|
|
|
EmailToken.confirm(params[:token])
|
|
|
|
render json: success_json
|
|
|
|
end
|
|
|
|
|
2013-10-07 06:19:45 -04:00
|
|
|
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
|
2013-11-11 12:51:14 -05:00
|
|
|
|
2015-02-19 18:28:38 -05:00
|
|
|
@success = I18n.t(message)
|
2014-07-03 03:29:44 -04:00
|
|
|
end
|
2013-10-07 06:19:45 -04:00
|
|
|
|
2015-04-27 06:59:48 -04:00
|
|
|
def admin_login
|
2018-01-22 06:20:53 -05:00
|
|
|
return redirect_to(path("/")) if current_user
|
2015-04-27 06:59:48 -04:00
|
|
|
|
2017-12-21 20:18:12 -05:00
|
|
|
if request.put? && params[:email].present?
|
2015-04-27 06:59:48 -04:00
|
|
|
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!
|
|
|
|
|
2018-01-22 06:20:53 -05:00
|
|
|
if user = User.with_email(params[:email]).admins.human_users.first
|
2015-04-27 06:59:48 -04:00
|
|
|
email_token = user.email_tokens.create(email: user.email)
|
2016-04-07 00:38:43 -04:00
|
|
|
Jobs.enqueue(:critical_user_email, type: :admin_login, user_id: user.id, email_token: email_token.token)
|
2015-04-27 06:59:48 -04:00
|
|
|
@message = I18n.t("admin_login.success")
|
|
|
|
else
|
2018-01-22 06:20:53 -05:00
|
|
|
@message = I18n.t("admin_login.errors.unknown_email_address")
|
2015-04-27 06:59:48 -04:00
|
|
|
end
|
2018-02-20 01:44:51 -05:00
|
|
|
elsif (token = params[:token]).present?
|
|
|
|
valid_token = EmailToken.valid_token_format?(token)
|
|
|
|
|
|
|
|
if valid_token
|
2017-12-21 20:18:12 -05:00
|
|
|
if params[:second_factor_token].present?
|
|
|
|
RateLimiter.new(nil, "second-factor-min-#{request.remote_ip}", 3, 1.minute).performed!
|
|
|
|
end
|
2018-02-20 01:44:51 -05:00
|
|
|
|
|
|
|
email_token_user = EmailToken.confirmable(token)&.user
|
2018-04-09 03:03:41 -04:00
|
|
|
totp_enabled = email_token_user&.totp_enabled?
|
2018-06-28 04:12:32 -04:00
|
|
|
backup_enabled = email_token_user&.backup_codes_enabled?
|
2018-02-21 20:45:57 -05:00
|
|
|
second_factor_token = params[:second_factor_token]
|
2018-06-28 04:12:32 -04:00
|
|
|
second_factor_method = params[:second_factor_method].to_i
|
2018-02-21 20:45:57 -05:00
|
|
|
confirm_email = false
|
|
|
|
|
|
|
|
confirm_email =
|
|
|
|
if totp_enabled
|
|
|
|
@second_factor_required = true
|
2018-06-28 04:12:32 -04:00
|
|
|
@backup_codes_enabled = true
|
2018-02-21 20:45:57 -05:00
|
|
|
@message = I18n.t("login.second_factor_title")
|
|
|
|
|
|
|
|
if second_factor_token.present?
|
2018-06-28 04:12:32 -04:00
|
|
|
if email_token_user.authenticate_second_factor(second_factor_token, second_factor_method)
|
2018-02-21 20:45:57 -05:00
|
|
|
true
|
|
|
|
else
|
|
|
|
@error = I18n.t("login.invalid_second_factor_code")
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
else
|
|
|
|
true
|
|
|
|
end
|
2018-02-20 01:44:51 -05:00
|
|
|
|
2018-02-21 20:45:57 -05:00
|
|
|
if confirm_email
|
2018-02-20 01:44:51 -05:00
|
|
|
@user = EmailToken.confirm(token)
|
|
|
|
|
2017-12-21 20:18:12 -05:00
|
|
|
if @user && @user.admin?
|
|
|
|
log_on_user(@user)
|
|
|
|
return redirect_to path("/")
|
|
|
|
else
|
|
|
|
@message = I18n.t("admin_login.errors.unknown_email_address")
|
|
|
|
end
|
2015-04-27 06:59:48 -04:00
|
|
|
end
|
|
|
|
else
|
2018-01-22 06:20:53 -05:00
|
|
|
@message = I18n.t("admin_login.errors.invalid_token")
|
2015-04-27 06:59:48 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
render layout: false
|
|
|
|
rescue RateLimiter::LimitExceeded
|
|
|
|
@message = I18n.t("rate_limiter.slow_down")
|
|
|
|
render layout: false
|
|
|
|
end
|
|
|
|
|
2017-04-20 11:17:24 -04:00
|
|
|
def email_login
|
|
|
|
raise Discourse::NotFound if !SiteSetting.enable_local_logins_via_email
|
|
|
|
return redirect_to path("/") if current_user
|
|
|
|
|
|
|
|
expires_now
|
|
|
|
params.require(:login)
|
|
|
|
|
|
|
|
RateLimiter.new(nil, "email-login-hour-#{request.remote_ip}", 6, 1.hour).performed!
|
|
|
|
RateLimiter.new(nil, "email-login-min-#{request.remote_ip}", 3, 1.minute).performed!
|
|
|
|
user = User.human_users.find_by_username_or_email(params[:login])
|
|
|
|
user_presence = user.present? && !user.staged
|
|
|
|
|
|
|
|
if user
|
|
|
|
RateLimiter.new(nil, "email-login-hour-#{user.id}", 6, 1.hour).performed!
|
|
|
|
RateLimiter.new(nil, "email-login-min-#{user.id}", 3, 1.minute).performed!
|
|
|
|
|
|
|
|
if user_presence
|
|
|
|
email_token = user.email_tokens.create!(email: user.email)
|
|
|
|
|
|
|
|
Jobs.enqueue(:critical_user_email,
|
|
|
|
type: :email_login,
|
|
|
|
user_id: user.id,
|
|
|
|
email_token: email_token.token
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-02-14 18:46:33 -05:00
|
|
|
json = success_json
|
2017-04-20 11:17:24 -04:00
|
|
|
json[:user_found] = user_presence unless SiteSetting.hide_email_address_taken
|
|
|
|
render json: json
|
|
|
|
rescue RateLimiter::LimitExceeded
|
|
|
|
render_json_error(I18n.t("rate_limiter.slow_down"))
|
|
|
|
end
|
|
|
|
|
2015-04-07 04:02:10 -04:00
|
|
|
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
|
|
|
|
|
2014-09-23 09:50:57 -04:00
|
|
|
def account_created
|
2018-05-13 10:03:11 -04:00
|
|
|
if current_user.present?
|
|
|
|
if SiteSetting.enable_sso_provider && payload = cookies.delete(:sso_payload)
|
|
|
|
return redirect_to(session_sso_provider_url + "?" + payload)
|
|
|
|
else
|
|
|
|
return redirect_to("/")
|
|
|
|
end
|
|
|
|
end
|
2017-05-03 11:15:54 -04:00
|
|
|
|
2016-05-05 14:37:09 -04:00
|
|
|
@custom_body_class = "static-account-created"
|
2015-05-06 21:00:22 -04:00
|
|
|
@message = session['user_created_message'] || I18n.t('activation.missing_session')
|
2018-05-13 10:03:11 -04:00
|
|
|
@account_created = { message: @message, show_controls: false }
|
2017-05-02 13:21:46 -04:00
|
|
|
|
2017-05-02 15:57:55 -04:00
|
|
|
if session_user_id = session[SessionController::ACTIVATE_USER_KEY]
|
2018-05-11 16:55:45 -04:00
|
|
|
if user = User.where(id: session_user_id.to_i).first
|
2017-05-02 15:57:55 -04:00
|
|
|
@account_created[:username] = user.username
|
|
|
|
@account_created[:email] = user.email
|
2018-01-19 09:29:15 -05:00
|
|
|
@account_created[:show_controls] = !user.from_staged?
|
2017-05-02 15:57:55 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
store_preloaded("accountCreated", MultiJson.dump(@account_created))
|
2014-09-23 09:50:57 -04:00
|
|
|
expires_now
|
2017-05-04 12:30:13 -04:00
|
|
|
|
|
|
|
respond_to do |format|
|
|
|
|
format.html { render "default/empty" }
|
|
|
|
format.json { render json: success_json }
|
|
|
|
end
|
2014-09-23 09:50:57 -04:00
|
|
|
end
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
def activate_account
|
2014-07-15 13:47:47 -04:00
|
|
|
expires_now
|
2015-01-15 15:56:53 -05:00
|
|
|
render layout: 'no_ember'
|
2014-07-14 12:25:42 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def perform_account_activation
|
2014-07-15 14:07:19 -04:00
|
|
|
raise Discourse::InvalidAccess.new if honeypot_or_challenge_fails?(params)
|
2018-05-11 16:55:45 -04:00
|
|
|
|
2018-05-11 18:41:27 -04:00
|
|
|
if @user = EmailToken.confirm(params[:token])
|
2013-02-05 14:16:51 -05:00
|
|
|
# Log in the user unless they need to be approved
|
2013-04-03 12:23:28 -04:00
|
|
|
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)
|
2017-03-15 21:48:51 -04:00
|
|
|
|
|
|
|
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)
|
2018-05-11 18:41:27 -04:00
|
|
|
elsif SiteSetting.enable_sso_provider && payload = cookies.delete(:sso_payload)
|
|
|
|
return redirect_to(session_sso_provider_url + "?" + payload)
|
2017-03-15 21:48:51 -04:00
|
|
|
end
|
2013-04-03 12:23:28 -04:00
|
|
|
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
|
2018-05-11 18:41:27 -04:00
|
|
|
|
2015-01-15 15:56:53 -05:00
|
|
|
render layout: 'no_ember'
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2017-04-05 16:14:22 -04:00
|
|
|
def update_activation_email
|
|
|
|
RateLimiter.new(nil, "activate-edit-email-hr-#{request.remote_ip}", 5, 1.hour).performed!
|
|
|
|
|
2017-05-02 15:57:55 -04:00
|
|
|
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
|
|
|
|
|
2018-01-19 09:29:15 -05:00
|
|
|
if @user.blank? || @user.active? || current_user.present? || @user.from_staged?
|
2017-12-14 01:16:49 -05:00
|
|
|
raise Discourse::InvalidAccess.new
|
|
|
|
end
|
|
|
|
|
2017-04-05 16:14:22 -04:00
|
|
|
User.transaction do
|
2017-09-12 10:03:33 -04:00
|
|
|
primary_email = @user.primary_email
|
|
|
|
primary_email.email = params[:email]
|
2017-10-25 01:02:18 -04:00
|
|
|
primary_email.skip_validate_email = false
|
2017-09-12 10:03:33 -04:00
|
|
|
|
|
|
|
if primary_email.save
|
2017-11-07 23:02:33 -05:00
|
|
|
@user.email_tokens.create!(email: @user.email)
|
2017-04-05 16:14:22 -04:00
|
|
|
enqueue_activation_email
|
|
|
|
render json: success_json
|
|
|
|
else
|
2018-05-31 00:15:28 -04:00
|
|
|
render_json_error(primary_email)
|
2017-04-05 16:14:22 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-22 11:49:48 -05:00
|
|
|
def send_activation_email
|
2015-10-27 16:25:30 -04:00
|
|
|
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
|
2014-09-25 03:42:48 -04:00
|
|
|
|
2017-05-25 15:28:39 -04:00
|
|
|
raise Discourse::InvalidAccess.new if SiteSetting.must_approve_users?
|
|
|
|
|
2017-05-02 15:57:55 -04:00
|
|
|
if params[:username].present?
|
|
|
|
@user = User.find_by_username_or_email(params[:username].to_s)
|
|
|
|
end
|
2017-03-13 08:20:25 -04:00
|
|
|
raise Discourse::NotFound unless @user
|
|
|
|
|
2017-03-13 10:32:24 -04:00
|
|
|
if !current_user&.staff? &&
|
2017-03-13 08:20:25 -04:00
|
|
|
@user.id != session[SessionController::ACTIVATE_USER_KEY]
|
2017-03-13 07:19:42 -04:00
|
|
|
|
2017-05-25 15:28:39 -04:00
|
|
|
raise Discourse::InvalidAccess.new
|
2017-03-13 07:19:42 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
session.delete(SessionController::ACTIVATE_USER_KEY)
|
|
|
|
|
2017-03-20 05:40:48 -04:00
|
|
|
if @user.active && @user.email_confirmed?
|
2017-03-13 07:19:42 -04:00
|
|
|
render_json_error(I18n.t('activation.activated'), status: 409)
|
2017-03-13 10:22:23 -04:00
|
|
|
else
|
2017-03-13 07:19:42 -04:00
|
|
|
@email_token = @user.email_tokens.unconfirmed.active.first
|
|
|
|
enqueue_activation_email
|
2017-08-31 00:06:56 -04:00
|
|
|
render body: nil
|
2017-03-13 07:19:42 -04:00
|
|
|
end
|
2013-02-22 11:49:48 -05:00
|
|
|
end
|
|
|
|
|
2013-10-07 06:19:45 -04:00
|
|
|
def enqueue_activation_email
|
2017-11-07 23:02:33 -05:00
|
|
|
@email_token ||= @user.email_tokens.create!(email: @user.email)
|
2017-07-31 13:27:39 -04:00
|
|
|
Jobs.enqueue(:critical_user_email, type: :signup, user_id: @user.id, email_token: @email_token.token, to_address: @user.email)
|
2013-10-07 06:19:45 -04:00
|
|
|
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
|
2015-04-13 11:03:13 -04:00
|
|
|
topic_allowed_users = params[:topic_allowed_users] || false
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2017-02-09 18:45:39 -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
|
|
|
|
2015-09-11 09:04:29 -04:00
|
|
|
user_fields = [:username, :upload_avatar_template]
|
2013-10-30 15:45:13 -04:00
|
|
|
user_fields << :name if SiteSetting.enable_names?
|
|
|
|
|
2015-09-11 04:14:34 -04:00
|
|
|
to_render = { users: results.as_json(only: user_fields, methods: [:avatar_template]) }
|
2013-12-23 09:46:00 -05:00
|
|
|
|
2017-11-01 22:20:14 -04:00
|
|
|
groups =
|
|
|
|
if current_user
|
2017-10-02 05:45:58 -04:00
|
|
|
if params[:include_mentionable_groups] == 'true'
|
|
|
|
Group.mentionable(current_user)
|
|
|
|
elsif params[:include_messageable_groups] == 'true'
|
|
|
|
Group.messageable(current_user)
|
|
|
|
end
|
2017-10-03 06:49:45 -04:00
|
|
|
end
|
2017-11-01 22:20:14 -04:00
|
|
|
|
2017-11-03 09:39:55 -04:00
|
|
|
include_groups = params[:include_groups] == "true"
|
|
|
|
|
|
|
|
if include_groups || groups
|
|
|
|
groups = Group.search_groups(term, groups: groups)
|
|
|
|
groups = groups.where(visibility_level: Group.visibility_levels[:public]) if include_groups
|
2018-02-12 21:28:29 -05:00
|
|
|
groups = groups.order('groups.name asc')
|
2017-11-03 09:39:55 -04:00
|
|
|
|
|
|
|
to_render[:groups] = groups.map do |m|
|
|
|
|
{ name: m.name, full_name: m.full_name }
|
|
|
|
end
|
2015-12-01 23:49:43 -05:00
|
|
|
end
|
|
|
|
|
2013-12-23 09:46:00 -05:00
|
|
|
render json: to_render
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2015-09-17 13:42:44 -04:00
|
|
|
AVATAR_TYPES_WITH_UPLOAD ||= %w{uploaded custom gravatar}
|
|
|
|
|
2014-05-26 05:46:43 -04:00
|
|
|
def pick_avatar
|
2013-08-13 16:08:29 -04:00
|
|
|
user = fetch_user_from_params
|
|
|
|
guardian.ensure_can_edit!(user)
|
2015-05-29 04:21:41 -04:00
|
|
|
|
2015-09-11 09:04:29 -04:00
|
|
|
type = params[:type]
|
2015-09-11 09:47:48 -04:00
|
|
|
upload_id = params[:upload_id]
|
2014-05-26 05:46:43 -04:00
|
|
|
|
2015-11-12 04:26:45 -05:00
|
|
|
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
|
|
|
|
|
2015-09-11 09:04:29 -04:00
|
|
|
user.uploaded_avatar_id = upload_id
|
2015-09-11 09:47:48 -04:00
|
|
|
|
2015-09-17 13:42:44 -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
|
2015-05-29 04:21:41 -04:00
|
|
|
|
2013-08-13 16:08:29 -04:00
|
|
|
user.save!
|
2015-05-29 04:21:41 -04:00
|
|
|
user.user_avatar.save!
|
2013-08-13 16:08:29 -04:00
|
|
|
|
2014-12-06 11:26:32 -05:00
|
|
|
render json: success_json
|
2013-08-13 16:08:29 -04:00
|
|
|
end
|
2014-04-14 16:55:57 -04:00
|
|
|
|
2014-06-27 14:48:39 -04:00
|
|
|
def destroy_user_image
|
2014-02-28 15:12:51 -05:00
|
|
|
user = fetch_user_from_params
|
|
|
|
guardian.ensure_can_edit!(user)
|
2014-04-14 16:55:57 -04:00
|
|
|
|
2015-05-19 19:39:58 -04:00
|
|
|
case params.require(:type)
|
|
|
|
when "profile_background"
|
2014-06-27 14:48:39 -04:00
|
|
|
user.user_profile.clear_profile_background
|
2015-05-19 19:39:58 -04:00
|
|
|
when "card_background"
|
2014-10-20 12:11:36 -04:00
|
|
|
user.user_profile.clear_card_background
|
2014-06-27 14:48:39 -04:00
|
|
|
else
|
2015-05-19 19:39:58 -04:00
|
|
|
raise Discourse::InvalidParameters.new(:type)
|
2014-06-27 14:48:39 -04:00
|
|
|
end
|
2014-04-14 16:55:57 -04:00
|
|
|
|
2015-05-19 19:39:58 -04:00
|
|
|
render json: success_json
|
2014-02-28 15:12:51 -05:00
|
|
|
end
|
2014-04-14 16:55:57 -04:00
|
|
|
|
2014-02-13 11:42:35 -05: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
|
|
|
|
2014-02-13 11:42:35 -05:00
|
|
|
render json: success_json
|
|
|
|
end
|
|
|
|
|
2014-07-11 03:32:29 -04:00
|
|
|
def read_faq
|
2015-09-04 16:56:02 -04:00
|
|
|
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
|
|
|
|
|
2015-01-05 13:49:32 -05:00
|
|
|
def staff_info
|
2015-11-27 14:02:24 -05:00
|
|
|
@user = fetch_user_from_params(include_inactive: true)
|
2015-01-05 13:49:32 -05:00
|
|
|
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|
|
2015-01-05 13:49:32 -05:00
|
|
|
result[info] = @user.send(info)
|
|
|
|
end
|
|
|
|
|
|
|
|
render json: result
|
|
|
|
end
|
|
|
|
|
2017-04-04 13:59:22 -04:00
|
|
|
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
|
|
|
|
|
2018-02-20 01:44:51 -05:00
|
|
|
def create_second_factor
|
2018-03-01 21:37:13 -05:00
|
|
|
raise Discourse::NotFound if SiteSetting.enable_sso || !SiteSetting.enable_local_logins
|
2018-02-20 01:44:51 -05:00
|
|
|
RateLimiter.new(nil, "login-hr-#{request.remote_ip}", SiteSetting.max_logins_per_ip_per_hour, 1.hour).performed!
|
|
|
|
RateLimiter.new(nil, "login-min-#{request.remote_ip}", SiteSetting.max_logins_per_ip_per_minute, 1.minute).performed!
|
|
|
|
|
|
|
|
unless current_user.confirm_password?(params[:password])
|
|
|
|
return render json: failed_json.merge(
|
|
|
|
error: I18n.t("login.incorrect_password")
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
qrcode_svg = RQRCode::QRCode.new(current_user.totp_provisioning_uri).as_svg(
|
|
|
|
offset: 0,
|
|
|
|
color: '000',
|
|
|
|
shape_rendering: 'crispEdges',
|
|
|
|
module_size: 4
|
|
|
|
)
|
|
|
|
|
|
|
|
render json: success_json.merge(
|
2018-06-28 04:12:32 -04:00
|
|
|
key: current_user.user_second_factors.totp.data.scan(/.{4}/).join(" "),
|
2018-02-20 01:44:51 -05:00
|
|
|
qr: qrcode_svg
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2018-06-28 04:12:32 -04:00
|
|
|
def create_second_factor_backup
|
|
|
|
raise Discourse::NotFound if SiteSetting.enable_sso || !SiteSetting.enable_local_logins
|
|
|
|
|
|
|
|
unless current_user.authenticate_totp(params[:second_factor_token])
|
|
|
|
return render json: failed_json.merge(
|
|
|
|
error: I18n.t("login.invalid_second_factor_code")
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
backup_codes = current_user.generate_backup_codes
|
|
|
|
|
|
|
|
render json: success_json.merge(
|
|
|
|
backup_codes: backup_codes
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2018-02-20 01:44:51 -05:00
|
|
|
def update_second_factor
|
|
|
|
params.require(:second_factor_token)
|
2018-06-28 04:12:32 -04:00
|
|
|
params.require(:second_factor_method)
|
|
|
|
|
|
|
|
second_factor_method = params[:second_factor_method].to_i
|
2018-02-20 01:44:51 -05:00
|
|
|
|
|
|
|
[request.remote_ip, current_user.id].each do |key|
|
|
|
|
RateLimiter.new(nil, "second-factor-min-#{key}", 3, 1.minute).performed!
|
|
|
|
end
|
|
|
|
|
2018-06-28 04:12:32 -04:00
|
|
|
if second_factor_method == UserSecondFactor.methods[:totp]
|
|
|
|
user_second_factor = current_user.user_second_factors.totp
|
|
|
|
elsif second_factor_method == UserSecondFactor.methods[:backup_codes]
|
|
|
|
user_second_factor = current_user.user_second_factors.backup_codes
|
|
|
|
end
|
|
|
|
|
2018-02-20 01:44:51 -05:00
|
|
|
raise Discourse::InvalidParameters unless user_second_factor
|
|
|
|
|
|
|
|
unless current_user.authenticate_totp(params[:second_factor_token])
|
|
|
|
return render json: failed_json.merge(
|
|
|
|
error: I18n.t("login.invalid_second_factor_code")
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
if params[:enable] == "true"
|
|
|
|
user_second_factor.update!(enabled: true)
|
|
|
|
else
|
2018-06-28 04:12:32 -04:00
|
|
|
# when disabling totp, backup is disabled too
|
|
|
|
if second_factor_method == UserSecondFactor.methods[:totp]
|
|
|
|
current_user.user_second_factors.destroy_all
|
|
|
|
|
|
|
|
Jobs.enqueue(
|
|
|
|
:critical_user_email,
|
|
|
|
type: :account_second_factor_disabled,
|
|
|
|
user_id: current_user.id
|
|
|
|
)
|
|
|
|
elsif second_factor_method == UserSecondFactor.methods[:backup_codes]
|
|
|
|
current_user.user_second_factors.where(method: UserSecondFactor.methods[:backup_codes]).destroy_all
|
|
|
|
end
|
2018-02-20 01:44:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
render json: success_json
|
|
|
|
end
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
private
|
|
|
|
|
2018-06-07 01:28:18 -04:00
|
|
|
def honeypot_value
|
|
|
|
Digest::SHA1::hexdigest("#{Discourse.current_hostname}:#{GlobalSetting.safe_secret_key_base}")[0, 15]
|
|
|
|
end
|
2013-08-23 02:19:23 -04:00
|
|
|
|
2018-06-07 01:28:18 -04:00
|
|
|
def challenge_value
|
|
|
|
challenge = $redis.get('SECRET_CHALLENGE')
|
|
|
|
unless challenge && challenge.length == 16 * 2
|
|
|
|
challenge = SecureRandom.hex(16)
|
|
|
|
$redis.set('SECRET_CHALLENGE', challenge)
|
2013-02-06 19:25:21 -05:00
|
|
|
end
|
|
|
|
|
2018-06-07 01:28:18 -04:00
|
|
|
challenge
|
|
|
|
end
|
2013-11-12 17:37:38 -05:00
|
|
|
|
2018-06-07 01:28:18 -04:00
|
|
|
def respond_to_suspicious_request
|
|
|
|
if suspicious?(params)
|
|
|
|
render json: {
|
|
|
|
success: true,
|
|
|
|
active: false,
|
|
|
|
message: I18n.t("login.activate_email", email: params[:email])
|
|
|
|
}
|
2013-11-12 17:37:38 -05:00
|
|
|
end
|
2018-06-07 01:28:18 -04:00
|
|
|
end
|
2013-11-12 17:37:38 -05:00
|
|
|
|
2018-06-07 01:28:18 -04:00
|
|
|
def suspicious?(params)
|
|
|
|
return false if current_user && is_api? && current_user.admin?
|
|
|
|
honeypot_or_challenge_fails?(params) || SiteSetting.invite_only?
|
|
|
|
end
|
2013-11-12 17:37:38 -05:00
|
|
|
|
2018-06-07 01:28:18 -04:00
|
|
|
def honeypot_or_challenge_fails?(params)
|
|
|
|
return false if is_api?
|
|
|
|
params[:password_confirmation] != honeypot_value ||
|
|
|
|
params[:challenge] != challenge_value.try(:reverse)
|
|
|
|
end
|
2016-08-05 11:57:13 -04:00
|
|
|
|
2018-06-07 01:28:18 -04:00
|
|
|
def user_params
|
|
|
|
permitted = [
|
|
|
|
:name,
|
|
|
|
:email,
|
|
|
|
:password,
|
|
|
|
:username,
|
|
|
|
:title,
|
|
|
|
:date_of_birth,
|
|
|
|
:muted_usernames,
|
|
|
|
:theme_key,
|
|
|
|
:locale,
|
|
|
|
:bio_raw,
|
|
|
|
:location,
|
|
|
|
:website,
|
|
|
|
:dismissed_banner_key,
|
|
|
|
:profile_background,
|
|
|
|
:card_background
|
|
|
|
]
|
|
|
|
|
|
|
|
permitted.concat UserUpdater::OPTION_ATTR
|
|
|
|
permitted.concat UserUpdater::CATEGORY_IDS.keys.map { |k| { k => [] } }
|
|
|
|
permitted.concat UserUpdater::TAG_NAMES.keys
|
|
|
|
|
|
|
|
result = params
|
|
|
|
.permit(permitted)
|
|
|
|
.reverse_merge(
|
|
|
|
ip_address: request.remote_ip,
|
|
|
|
registration_ip_address: request.remote_ip,
|
|
|
|
locale: user_locale
|
|
|
|
)
|
2016-08-05 11:57:13 -04:00
|
|
|
|
2018-06-07 01:28:18 -04:00
|
|
|
if !UsernameCheckerService.is_developer?(result['email']) &&
|
|
|
|
is_api? &&
|
|
|
|
current_user.present? &&
|
|
|
|
current_user.admin?
|
2016-08-05 11:57:13 -04:00
|
|
|
|
2018-06-07 01:28:18 -04:00
|
|
|
result.merge!(params.permit(:active, :staged, :approved))
|
2018-02-16 19:11:41 -05:00
|
|
|
end
|
|
|
|
|
2018-06-07 01:28:18 -04:00
|
|
|
modify_user_params(result)
|
|
|
|
end
|
2016-02-06 14:49:39 -05:00
|
|
|
|
2018-06-07 01:28:18 -04:00
|
|
|
# Plugins can use this to modify user parameters
|
|
|
|
def modify_user_params(attrs)
|
|
|
|
attrs
|
|
|
|
end
|
2014-09-26 14:48:34 -04:00
|
|
|
|
2018-06-07 01:28:18 -04:00
|
|
|
def user_locale
|
|
|
|
I18n.locale
|
|
|
|
end
|
|
|
|
|
|
|
|
def fail_with(key)
|
|
|
|
render json: { success: false, message: I18n.t(key) }
|
|
|
|
end
|
2014-09-26 14:48:34 -04:00
|
|
|
|
2018-06-07 01:28:18 -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)
|
2015-09-14 03:51:17 -04:00
|
|
|
|
2018-06-07 01:28:18 -04:00
|
|
|
Scheduler::Defer.later 'Track profile view visit' do
|
|
|
|
UserProfileView.add(user_profile_id, ip, user_id)
|
2015-09-14 03:51:17 -04:00
|
|
|
end
|
2018-06-07 01:28:18 -04:00
|
|
|
end
|
2015-09-14 03:51:17 -04:00
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|