2019-05-03 06:17:27 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
class UsersController < ApplicationController
|
2017-08-31 12:06:56 +08:00
|
|
|
skip_before_action :authorize_mini_profiler, only: [:avatar]
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2018-02-01 12:17:59 +08:00
|
|
|
requires_login only: [
|
2020-06-06 00:42:12 +08:00
|
|
|
:username, :update, :upload_user_image,
|
2018-07-18 18:57:43 +08:00
|
|
|
:pick_avatar, :destroy_user_image, :destroy, :check_emails,
|
2019-06-27 07:58:06 +08:00
|
|
|
:topic_tracking_state, :preferences, :create_second_factor_totp,
|
|
|
|
:enable_second_factor_totp, :disable_second_factor, :list_second_factors,
|
2018-08-31 16:18:06 +08:00
|
|
|
:update_second_factor, :create_second_factor_backup, :select_avatar,
|
2019-10-02 10:08:41 +08:00
|
|
|
:notification_level, :revoke_auth_token, :register_second_factor_security_key,
|
2020-03-12 13:20:56 +08:00
|
|
|
:create_second_factor_security_key, :feature_topic, :clear_featured_topic,
|
2021-11-26 04:44:15 +08:00
|
|
|
:bookmarks, :invited, :check_sso_email, :check_sso_payload,
|
|
|
|
:recent_searches, :reset_recent_searches
|
2018-02-01 09:26:45 +08:00
|
|
|
]
|
|
|
|
|
|
|
|
skip_before_action :check_xhr, only: [
|
2020-01-15 18:27:12 +08:00
|
|
|
:show, :badges, :password_reset_show, :password_reset_update, :update, :account_created,
|
2020-06-06 00:42:12 +08:00
|
|
|
:activate_account, :perform_account_activation, :avatar,
|
2019-12-10 03:15:47 +08:00
|
|
|
:my_redirect, :toggle_anon, :admin_login, :confirm_admin, :email_login, :summary,
|
2020-03-12 13:20:56 +08:00
|
|
|
:feature_topic, :clear_featured_topic, :bookmarks
|
2018-02-01 09:26:45 +08:00
|
|
|
]
|
2016-08-12 14:35:10 +08:00
|
|
|
|
2019-06-27 07:58:06 +08:00
|
|
|
before_action :second_factor_check_confirmed_password, only: [
|
|
|
|
:create_second_factor_totp, :enable_second_factor_totp,
|
2019-10-02 10:08:41 +08:00
|
|
|
:disable_second_factor, :update_second_factor, :create_second_factor_backup,
|
|
|
|
:register_second_factor_security_key, :create_second_factor_security_key
|
|
|
|
]
|
2019-06-27 07:58:06 +08:00
|
|
|
|
2017-08-31 12:06:56 +08:00
|
|
|
before_action :respond_to_suspicious_request, only: [:create]
|
2013-02-07 23:45:24 +08:00
|
|
|
|
2013-05-22 23:20:16 +08:00
|
|
|
# we need to allow account creation with bad CSRF tokens, if people are caching, the CSRF token on the
|
2013-05-03 14:43:11 +08: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 12:06:56 +08:00
|
|
|
skip_before_action :verify_authenticity_token, only: [:create]
|
|
|
|
skip_before_action :redirect_to_login_if_required, only: [:check_username,
|
2021-03-22 23:46:03 +08:00
|
|
|
:check_email,
|
2013-07-16 00:12:54 +08:00
|
|
|
:create,
|
2014-09-23 21:50:57 +08:00
|
|
|
:account_created,
|
2013-07-16 00:12:54 +08:00
|
|
|
:activate_account,
|
2014-07-16 01:47:47 +08:00
|
|
|
:perform_account_activation,
|
2013-07-16 00:12:54 +08:00
|
|
|
:send_activation_email,
|
2017-04-06 04:14:22 +08:00
|
|
|
:update_activation_email,
|
2020-01-15 18:27:12 +08:00
|
|
|
:password_reset_show,
|
|
|
|
:password_reset_update,
|
2016-01-05 00:48:54 +08:00
|
|
|
:confirm_email_token,
|
2017-04-20 23:17:24 +08:00
|
|
|
:email_login,
|
2017-04-05 01:59:22 +08:00
|
|
|
:admin_login,
|
|
|
|
:confirm_admin]
|
2013-05-03 14:43:11 +08:00
|
|
|
|
2020-05-11 13:05:42 +08:00
|
|
|
after_action :add_noindex_header, only: [:show, :my_redirect]
|
2020-01-24 12:00:27 +08:00
|
|
|
|
2021-11-26 04:44:15 +08:00
|
|
|
MAX_RECENT_SEARCHES = 5
|
|
|
|
|
2015-03-19 23:48:16 +08:00
|
|
|
def index
|
|
|
|
end
|
|
|
|
|
2020-01-28 19:55:46 +08:00
|
|
|
def show(for_card: false)
|
2017-04-20 15:30:45 +08:00
|
|
|
return redirect_to path('/login') if SiteSetting.hide_user_profiles_from_public && !current_user
|
2015-10-29 02:56:08 +08:00
|
|
|
|
2016-09-23 12:44:08 +08:00
|
|
|
@user = fetch_user_from_params(
|
2018-04-24 21:46:57 +08:00
|
|
|
include_inactive: current_user.try(:staff?) || (current_user && SiteSetting.show_inactive_accounts)
|
2016-09-23 12:44:08 +08:00
|
|
|
)
|
|
|
|
|
2018-10-11 01:00:08 +08:00
|
|
|
user_serializer = nil
|
|
|
|
if guardian.can_see_profile?(@user)
|
2020-01-28 19:55:46 +08:00
|
|
|
serializer_class = for_card ? UserCardSerializer : UserSerializer
|
|
|
|
user_serializer = serializer_class.new(@user, scope: guardian, root: 'user')
|
2018-10-11 01:00:08 +08:00
|
|
|
|
|
|
|
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
|
|
|
|
else
|
|
|
|
user_serializer = HiddenProfileSerializer.new(@user, scope: guardian, root: 'user')
|
2015-03-25 00:33:17 +08:00
|
|
|
end
|
|
|
|
|
2015-09-14 15:51:17 +08:00
|
|
|
if !params[:skip_track_visit] && (@user != current_user)
|
|
|
|
track_visit_to_user_profile
|
|
|
|
end
|
|
|
|
|
2015-02-03 01:55:32 +08: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.
|
|
|
|
if params[:external_id] && params[:external_id].ends_with?('.json')
|
|
|
|
return render_json_dump(user_serializer)
|
|
|
|
end
|
|
|
|
|
2013-03-09 04:04:37 +08:00
|
|
|
respond_to do |format|
|
|
|
|
format.html do
|
2014-11-28 02:51:13 +08:00
|
|
|
@restrict_fields = guardian.restrict_user_fields?(@user)
|
2013-03-09 04:04:37 +08:00
|
|
|
store_preloaded("user_#{@user.username}", MultiJson.dump(user_serializer))
|
2017-11-22 01:22:24 +08:00
|
|
|
render :show
|
2013-03-09 04:04:37 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
format.json do
|
|
|
|
render_json_dump(user_serializer)
|
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-01-28 19:55:46 +08:00
|
|
|
def show_card
|
|
|
|
show(for_card: true)
|
|
|
|
end
|
|
|
|
|
2020-03-06 20:23:22 +08:00
|
|
|
def cards
|
|
|
|
return redirect_to path('/login') if SiteSetting.hide_user_profiles_from_public && !current_user
|
|
|
|
|
|
|
|
user_ids = params.require(:user_ids).split(",").map(&:to_i)
|
|
|
|
raise Discourse::InvalidParameters.new(:user_ids) if user_ids.length > 50
|
|
|
|
|
|
|
|
users = User.where(id: user_ids).includes(:user_option,
|
|
|
|
:user_stat,
|
|
|
|
:default_featured_user_badges,
|
|
|
|
:user_profile,
|
|
|
|
:card_background_upload,
|
|
|
|
:primary_group,
|
2021-07-08 15:46:21 +08:00
|
|
|
:flair_group,
|
2020-03-06 20:23:22 +08:00
|
|
|
:primary_email
|
|
|
|
)
|
|
|
|
|
|
|
|
users = users.filter { |u| guardian.can_see_profile?(u) }
|
|
|
|
|
2020-07-27 08:23:54 +08:00
|
|
|
preload_fields = User.allowed_user_custom_fields(guardian) + UserField.all.pluck(:id).map { |fid| "#{User::USER_FIELD_PREFIX}#{fid}" }
|
2020-03-06 20:23:22 +08:00
|
|
|
User.preload_custom_fields(users, preload_fields)
|
|
|
|
User.preload_recent_time_read(users)
|
|
|
|
|
|
|
|
render json: users, each_serializer: UserCardSerializer
|
|
|
|
end
|
|
|
|
|
2017-11-22 01:22:24 +08:00
|
|
|
def badges
|
|
|
|
raise Discourse::NotFound unless SiteSetting.enable_badges?
|
|
|
|
show
|
|
|
|
end
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
def update
|
2013-10-07 18:19:45 +08:00
|
|
|
user = fetch_user_from_params
|
2013-02-06 03:16:51 +08:00
|
|
|
guardian.ensure_can_edit!(user)
|
2018-08-30 19:59:36 +08:00
|
|
|
attributes = user_params
|
2014-09-27 02:48:34 +08:00
|
|
|
|
2018-01-27 05:53:10 +08:00
|
|
|
# We can't update the username via this route. Use the username route
|
|
|
|
attributes.delete(:username)
|
|
|
|
|
2014-09-27 02:48:34 +08:00
|
|
|
if params[:user_fields].present?
|
2018-09-04 18:45:36 +08:00
|
|
|
attributes[:custom_fields] ||= {}
|
2015-03-21 03:18:43 +08:00
|
|
|
|
|
|
|
fields = UserField.all
|
|
|
|
fields = fields.where(editable: true) unless current_user.staff?
|
2021-07-31 01:50:47 +08:00
|
|
|
fields.each do |field|
|
|
|
|
field_id = field.id.to_s
|
2017-12-16 10:16:22 +08:00
|
|
|
next unless params[:user_fields].has_key?(field_id)
|
|
|
|
|
2021-07-31 01:50:47 +08:00
|
|
|
value = clean_custom_field_values(field)
|
|
|
|
value = nil if value === "false"
|
|
|
|
value = value[0...UserField.max_length] if value
|
2014-10-09 02:38:18 +08:00
|
|
|
|
2021-07-31 01:50:47 +08:00
|
|
|
return render_json_error(I18n.t("login.missing_user_field")) if value.blank? && field.required?
|
|
|
|
attributes[:custom_fields]["#{User::USER_FIELD_PREFIX}#{field.id}"] = value
|
2014-09-27 02:48:34 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-03-04 00:17:02 +08:00
|
|
|
if params[:external_ids]&.is_a?(ActionController::Parameters) && current_user&.admin? && is_api?
|
|
|
|
attributes[:user_associated_accounts] = []
|
|
|
|
|
|
|
|
params[:external_ids].each do |provider_name, provider_uid|
|
|
|
|
authenticator = Discourse.enabled_authenticators.find { |a| a.name == provider_name }
|
|
|
|
raise Discourse::InvalidParameters.new(:external_ids) if !authenticator&.is_managed?
|
|
|
|
|
|
|
|
attributes[:user_associated_accounts] << { provider_name: provider_name, provider_uid: provider_uid }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-11-25 08:49:27 +08:00
|
|
|
json_result(user, serializer: UserSerializer, additional_errors: [:user_profile, :user_option]) do |u|
|
2013-12-11 01:46:35 +08:00
|
|
|
updater = UserUpdater.new(current_user, user)
|
2017-08-31 12:06:56 +08:00
|
|
|
updater.update(attributes.permit!)
|
2013-02-07 23:45:24 +08:00
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def username
|
2013-06-06 15:14:32 +08:00
|
|
|
params.require(:new_username)
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2019-09-14 02:11:27 +08:00
|
|
|
if clashing_with_existing_route?(params[:new_username]) || User.reserved_username?(params[:new_username])
|
|
|
|
return render_json_error(I18n.t("login.reserved_username"))
|
|
|
|
end
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
user = fetch_user_from_params
|
2013-08-13 02:54:52 +08:00
|
|
|
guardian.ensure_can_edit_username!(user)
|
2013-02-07 23:45:24 +08:00
|
|
|
|
2015-03-07 05:44:54 +08:00
|
|
|
result = UsernameChanger.change(user, params[:new_username], current_user)
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2017-06-07 09:45:27 +08:00
|
|
|
if result
|
|
|
|
render json: { id: user.id, username: user.username }
|
|
|
|
else
|
|
|
|
render_json_error(user.errors.full_messages.join(','))
|
|
|
|
end
|
2019-08-01 12:49:53 +08:00
|
|
|
rescue Discourse::InvalidAccess
|
|
|
|
if current_user&.staff?
|
2021-02-08 18:04:33 +08:00
|
|
|
render_json_error(I18n.t('errors.messages.auth_overrides_username'))
|
2019-08-01 12:49:53 +08:00
|
|
|
else
|
|
|
|
render json: failed_json, status: 403
|
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2014-09-30 04:31:05 +08:00
|
|
|
def check_emails
|
2014-10-29 08:06:59 +08:00
|
|
|
user = fetch_user_from_params(include_inactive: true)
|
2014-09-30 04:31:05 +08:00
|
|
|
|
2019-02-06 02:01:19 +08:00
|
|
|
unless user == current_user
|
|
|
|
guardian.ensure_can_check_emails!(user)
|
|
|
|
StaffActionLogger.new(current_user).log_check_email(user, context: params[:context])
|
|
|
|
end
|
2014-09-30 04:31:05 +08:00
|
|
|
|
2018-07-03 19:51:22 +08:00
|
|
|
email, *secondary_emails = user.emails
|
2020-06-18 02:41:01 +08:00
|
|
|
unconfirmed_emails = user.unconfirmed_emails
|
2018-07-03 19:51:22 +08:00
|
|
|
|
2014-09-30 04:31:05 +08:00
|
|
|
render json: {
|
2018-07-03 19:51:22 +08:00
|
|
|
email: email,
|
|
|
|
secondary_emails: secondary_emails,
|
2020-06-18 02:41:01 +08:00
|
|
|
unconfirmed_emails: unconfirmed_emails,
|
2014-09-30 04:31:05 +08:00
|
|
|
associated_accounts: user.associated_accounts
|
|
|
|
}
|
2014-10-08 07:26:18 +08:00
|
|
|
rescue Discourse::InvalidAccess
|
2014-09-30 04:31:05 +08:00
|
|
|
render json: failed_json, status: 403
|
|
|
|
end
|
|
|
|
|
2020-11-11 03:12:44 +08:00
|
|
|
def check_sso_email
|
|
|
|
user = fetch_user_from_params(include_inactive: true)
|
|
|
|
|
|
|
|
unless user == current_user
|
2021-02-17 23:57:51 +08:00
|
|
|
guardian.ensure_can_check_sso_details!(user)
|
2020-11-11 03:12:44 +08:00
|
|
|
StaffActionLogger.new(current_user).log_check_email(user, context: params[:context])
|
|
|
|
end
|
|
|
|
|
|
|
|
email = user&.single_sign_on_record&.external_email
|
|
|
|
email = I18n.t("user.email.does_not_exist") if email.blank?
|
|
|
|
|
|
|
|
render json: { email: email }
|
|
|
|
rescue Discourse::InvalidAccess
|
|
|
|
render json: failed_json, status: 403
|
|
|
|
end
|
|
|
|
|
2021-02-17 23:57:51 +08:00
|
|
|
def check_sso_payload
|
|
|
|
user = fetch_user_from_params(include_inactive: true)
|
|
|
|
|
|
|
|
guardian.ensure_can_check_sso_details!(user)
|
|
|
|
unless user == current_user
|
|
|
|
StaffActionLogger.new(current_user).log_check_email(user, context: params[:context])
|
|
|
|
end
|
|
|
|
|
|
|
|
payload = user&.single_sign_on_record&.last_payload
|
|
|
|
payload = I18n.t("user.email.does_not_exist") if payload.blank?
|
|
|
|
|
|
|
|
render json: { payload: payload }
|
|
|
|
rescue Discourse::InvalidAccess
|
|
|
|
render json: failed_json, status: 403
|
|
|
|
end
|
|
|
|
|
2020-06-11 00:11:49 +08:00
|
|
|
def update_primary_email
|
|
|
|
if !SiteSetting.enable_secondary_emails
|
|
|
|
return render json: failed_json, status: 410
|
|
|
|
end
|
|
|
|
|
|
|
|
params.require(:email)
|
|
|
|
|
|
|
|
user = fetch_user_from_params
|
|
|
|
guardian.ensure_can_edit_email!(user)
|
|
|
|
|
|
|
|
old_primary = user.primary_email
|
|
|
|
if old_primary.email == params[:email]
|
|
|
|
return render json: success_json
|
|
|
|
end
|
|
|
|
|
|
|
|
new_primary = user.user_emails.find_by(email: params[:email])
|
|
|
|
if new_primary.blank?
|
|
|
|
return render json: failed_json.merge(errors: [I18n.t("change_email.doesnt_exist")]), status: 428
|
|
|
|
end
|
|
|
|
|
|
|
|
User.transaction do
|
|
|
|
old_primary.update!(primary: false)
|
|
|
|
new_primary.update!(primary: true)
|
2020-07-16 20:51:30 +08:00
|
|
|
DiscourseEvent.trigger(:user_updated, user)
|
2020-06-11 00:11:49 +08:00
|
|
|
|
|
|
|
if current_user.staff? && current_user != user
|
|
|
|
StaffActionLogger.new(current_user).log_update_email(user)
|
|
|
|
else
|
2020-06-16 18:30:58 +08:00
|
|
|
UserHistory.create!(action: UserHistory.actions[:update_email], acting_user_id: user.id)
|
2020-06-11 00:11:49 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
render json: success_json
|
|
|
|
end
|
|
|
|
|
|
|
|
def destroy_email
|
|
|
|
if !SiteSetting.enable_secondary_emails
|
|
|
|
return render json: failed_json, status: 410
|
|
|
|
end
|
|
|
|
|
|
|
|
params.require(:email)
|
|
|
|
|
|
|
|
user = fetch_user_from_params
|
|
|
|
guardian.ensure_can_edit!(user)
|
|
|
|
|
|
|
|
ActiveRecord::Base.transaction do
|
2021-05-13 14:14:00 +08:00
|
|
|
if change_requests = user.email_change_requests.where(new_email: params[:email]).presence
|
2021-03-10 20:49:26 +08:00
|
|
|
change_requests.destroy_all
|
2021-05-13 14:14:00 +08:00
|
|
|
elsif user.user_emails.where(email: params[:email], primary: false).destroy_all.present?
|
|
|
|
DiscourseEvent.trigger(:user_updated, user)
|
2021-03-10 20:49:26 +08:00
|
|
|
else
|
|
|
|
return render json: failed_json, status: 428
|
2020-06-11 00:11:49 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
if current_user.staff? && current_user != user
|
|
|
|
StaffActionLogger.new(current_user).log_destroy_email(user)
|
|
|
|
else
|
2020-06-16 18:30:58 +08:00
|
|
|
UserHistory.create(action: UserHistory.actions[:destroy_email], acting_user_id: user.id)
|
2020-06-11 00:11:49 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
render json: success_json
|
|
|
|
end
|
|
|
|
|
2016-08-12 14:35:10 +08:00
|
|
|
def topic_tracking_state
|
|
|
|
user = fetch_user_from_params
|
|
|
|
guardian.ensure_can_edit!(user)
|
|
|
|
|
2016-12-02 14:03:31 +08:00
|
|
|
report = TopicTrackingState.report(user)
|
DEV: Topic tracking state improvements (#13218)
I merged this PR in yesterday, finally thinking this was done https://github.com/discourse/discourse/pull/12958 but then a wild performance regression occurred. These are the problem methods:
https://github.com/discourse/discourse/blob/1aa20bd681e634f7fff22953ed62d90c2573b331/app/serializers/topic_tracking_state_serializer.rb#L13-L21
Turns out date comparison is super expensive on the backend _as well as_ the frontend.
The fix was to just move the `treat_as_new_topic_start_date` into the SQL query rather than using the slower `UserOption#treat_as_new_topic_start_date` method in ruby. After this change, 1% of the total time is spent with the `created_in_new_period` comparison instead of ~20%.
----
History:
Original PR which had to be reverted **https://github.com/discourse/discourse/pull/12555**. See the description there for what this PR is achieving, plus below.
The issue with the original PR is addressed in https://github.com/discourse/discourse/pull/12958/commits/92ef54f4020111ffacb0f2a27da5d5c2855f9d5d
If you went to the `x unread` link for a tag Chrome would freeze up and possibly crash, or eventually unfreeze after nearly 10 mins. Other routes for unread/new were similarly slow. From profiling the issue was the `sync` function of `topic-tracking-state.js`, which calls down to `isNew` which in turn calls `moment`, a change I had made in the PR above. The time it takes locally with ~1400 topics in the tracking state is 2.3 seconds.
To solve this issue, I have moved these calculations for "created in new period" and "unread not too old" into the tracking state serializer.
When I was looking at the profiler I also noticed this issue which was just compounding the problem. Every time we modify topic tracking state we recalculate the sidebar tracking/everything/tag counts. However this calls `forEachTracked` and `countTags` which can be quite expensive as they go through the whole tracking state (and were also calling the removed moment functions).
I added some logs and this was being called 30 times when navigating to a new /unread route because `sync` is being called from `build-topic-route` (one for each topic loaded due to pagination). So I just added a debounce here and it makes things even faster.
Finally, I changed topic tracking state to use a Map so our counts of the state keys is faster (Maps have .size whereas objects you have to do Object.keys(obj) which is O(n).)
<!-- NOTE: All pull requests should have tests (rspec in Ruby, qunit in JavaScript). If your code does not include test coverage, please include an explanation of why it was omitted. -->
2021-06-02 07:06:29 +08:00
|
|
|
serializer = ActiveModel::ArraySerializer.new(
|
|
|
|
report, each_serializer: TopicTrackingStateSerializer, scope: guardian
|
|
|
|
)
|
2016-08-12 14:35:10 +08:00
|
|
|
|
|
|
|
render json: MultiJson.dump(serializer)
|
|
|
|
end
|
|
|
|
|
2021-08-25 11:17:56 +08:00
|
|
|
def private_message_topic_tracking_state
|
|
|
|
user = fetch_user_from_params
|
|
|
|
guardian.ensure_can_edit!(user)
|
|
|
|
|
|
|
|
report = PrivateMessageTopicTrackingState.report(user)
|
|
|
|
|
|
|
|
serializer = ActiveModel::ArraySerializer.new(
|
|
|
|
report,
|
|
|
|
each_serializer: PrivateMessageTopicTrackingStateSerializer,
|
|
|
|
scope: guardian
|
|
|
|
)
|
|
|
|
|
|
|
|
render json: MultiJson.dump(serializer)
|
|
|
|
end
|
|
|
|
|
2014-04-18 11:10:53 +08:00
|
|
|
def badge_title
|
|
|
|
params.require(:user_badge_id)
|
|
|
|
|
|
|
|
user = fetch_user_from_params
|
|
|
|
guardian.ensure_can_edit!(user)
|
|
|
|
|
2014-07-14 16:06:50 +08:00
|
|
|
user_badge = UserBadge.find_by(id: params[:user_badge_id])
|
2019-11-08 13:34:24 +08:00
|
|
|
previous_title = user.title
|
2014-07-14 16:06:50 +08:00
|
|
|
if user_badge && user_badge.user == user && user_badge.badge.allow_title?
|
2018-01-05 23:58:15 +08:00
|
|
|
user.title = user_badge.badge.display_name
|
2014-04-18 11:10:53 +08:00
|
|
|
user.save!
|
2019-11-08 13:34:24 +08:00
|
|
|
|
|
|
|
log_params = {
|
|
|
|
details: "title matching badge id #{user_badge.badge.id}",
|
|
|
|
previous_value: previous_title,
|
|
|
|
new_value: user.title
|
|
|
|
}
|
|
|
|
|
|
|
|
if current_user.staff? && current_user != user
|
|
|
|
StaffActionLogger.new(current_user).log_title_change(user, log_params)
|
|
|
|
else
|
|
|
|
UserHistory.create!(log_params.merge(target_user_id: user.id, action: UserHistory.actions[:change_title]))
|
|
|
|
end
|
2014-07-14 16:06:50 +08:00
|
|
|
else
|
|
|
|
user.title = ''
|
|
|
|
user.save!
|
2019-11-08 13:34:24 +08:00
|
|
|
|
|
|
|
log_params = {
|
|
|
|
previous_value: previous_title
|
|
|
|
}
|
|
|
|
|
|
|
|
if current_user.staff? && current_user != user
|
2019-12-24 05:03:51 +08:00
|
|
|
StaffActionLogger
|
|
|
|
.new(current_user)
|
|
|
|
.log_title_revoke(user, log_params.merge(revoke_reason: 'user title was same as revoked badge name or custom badge name'))
|
2019-11-08 13:34:24 +08:00
|
|
|
else
|
|
|
|
UserHistory.create!(log_params.merge(target_user_id: user.id, action: UserHistory.actions[:revoke_title]))
|
|
|
|
end
|
2014-04-18 11:10:53 +08:00
|
|
|
end
|
|
|
|
|
2017-08-31 12:06:56 +08:00
|
|
|
render body: nil
|
2014-04-18 11:10:53 +08:00
|
|
|
end
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
def preferences
|
2017-08-31 12:06:56 +08:00
|
|
|
render body: nil
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2014-04-21 23:52:11 +08:00
|
|
|
def my_redirect
|
2016-03-19 20:03:10 +08:00
|
|
|
raise Discourse::NotFound if params[:path] !~ /^[a-z_\-\/]+$/
|
2015-10-15 00:40:13 +08:00
|
|
|
|
|
|
|
if current_user.blank?
|
2019-12-06 05:34:42 +08:00
|
|
|
cookies[:destination_url] = path("/my/#{params[:path]}")
|
|
|
|
redirect_to path("/login-preferences")
|
2015-10-15 00:40:13 +08:00
|
|
|
else
|
2020-06-06 00:31:58 +08:00
|
|
|
redirect_to(path("/u/#{current_user.encoded_username}/#{params[:path]}"))
|
2014-04-21 23:52:11 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-10-11 01:00:08 +08:00
|
|
|
def profile_hidden
|
|
|
|
render nothing: true
|
|
|
|
end
|
|
|
|
|
2016-01-20 12:11:52 +08:00
|
|
|
def summary
|
2019-01-14 22:15:25 +08:00
|
|
|
@user = fetch_user_from_params(include_inactive: current_user.try(:staff?) || (current_user && SiteSetting.show_inactive_accounts))
|
|
|
|
raise Discourse::NotFound unless guardian.can_see_profile?(@user)
|
2018-10-11 01:00:08 +08:00
|
|
|
|
2020-07-16 05:02:55 +08:00
|
|
|
response.headers['X-Robots-Tag'] = 'noindex'
|
|
|
|
|
2019-01-14 22:15:25 +08:00
|
|
|
respond_to do |format|
|
|
|
|
format.html do
|
|
|
|
@restrict_fields = guardian.restrict_user_fields?(@user)
|
|
|
|
render :show
|
|
|
|
end
|
|
|
|
format.json do
|
2020-07-08 05:57:42 +08:00
|
|
|
summary_json = Discourse.cache.fetch(summary_cache_key(@user), expires_in: 1.hour) do
|
|
|
|
summary = UserSummary.new(@user, guardian)
|
|
|
|
serializer = UserSummarySerializer.new(summary, scope: guardian)
|
|
|
|
MultiJson.dump(serializer)
|
|
|
|
end
|
|
|
|
render json: summary_json
|
2019-01-14 22:15:25 +08:00
|
|
|
end
|
|
|
|
end
|
2016-01-20 12:11:52 +08:00
|
|
|
end
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
def invited
|
2020-10-03 15:35:26 +08:00
|
|
|
if guardian.can_invite_to_forum?
|
2021-03-03 17:45:29 +08:00
|
|
|
filter = params[:filter] || "redeemed"
|
2020-10-03 15:35:26 +08:00
|
|
|
inviter = fetch_user_from_params(include_inactive: current_user.staff? || SiteSetting.show_inactive_accounts)
|
|
|
|
|
2021-03-03 17:45:29 +08:00
|
|
|
invites = if filter == "pending" && guardian.can_see_invite_details?(inviter)
|
|
|
|
Invite.includes(:topics, :groups).pending(inviter)
|
2021-03-06 19:29:35 +08:00
|
|
|
elsif filter == "expired"
|
|
|
|
Invite.expired(inviter)
|
2021-03-03 17:45:29 +08:00
|
|
|
elsif filter == "redeemed"
|
|
|
|
Invite.redeemed_users(inviter)
|
2020-10-03 15:35:26 +08:00
|
|
|
else
|
2021-03-03 17:45:29 +08:00
|
|
|
Invite.none
|
2020-10-03 15:35:26 +08:00
|
|
|
end
|
2020-03-05 00:10:23 +08:00
|
|
|
|
2021-03-03 17:45:29 +08:00
|
|
|
invites = invites.offset(params[:offset].to_i || 0).limit(SiteSetting.invites_per_page)
|
|
|
|
|
2020-10-03 15:35:26 +08:00
|
|
|
show_emails = guardian.can_see_invite_emails?(inviter)
|
|
|
|
if params[:search].present? && invites.present?
|
|
|
|
filter_sql = '(LOWER(users.username) LIKE :filter)'
|
|
|
|
filter_sql = '(LOWER(invites.email) LIKE :filter) or (LOWER(users.username) LIKE :filter)' if show_emails
|
|
|
|
invites = invites.where(filter_sql, filter: "%#{params[:search].downcase}%")
|
|
|
|
end
|
2013-11-06 06:52:50 +08:00
|
|
|
|
2021-03-03 17:45:29 +08:00
|
|
|
pending_count = Invite.pending(inviter).reorder(nil).count.to_i
|
2021-03-06 19:29:35 +08:00
|
|
|
expired_count = Invite.expired(inviter).reorder(nil).count.to_i
|
2021-03-03 17:45:29 +08:00
|
|
|
redeemed_count = Invite.redeemed_users(inviter).reorder(nil).count.to_i
|
|
|
|
|
2020-10-03 15:35:26 +08:00
|
|
|
render json: MultiJson.dump(InvitedSerializer.new(
|
2021-03-03 17:45:29 +08:00
|
|
|
OpenStruct.new(
|
|
|
|
invite_list: invites.to_a,
|
|
|
|
show_emails: show_emails,
|
|
|
|
inviter: inviter,
|
|
|
|
type: filter,
|
|
|
|
counts: {
|
|
|
|
pending: pending_count,
|
2021-03-06 19:29:35 +08:00
|
|
|
expired: expired_count,
|
2021-03-03 17:45:29 +08:00
|
|
|
redeemed: redeemed_count,
|
2021-03-06 19:29:35 +08:00
|
|
|
total: pending_count + expired_count
|
2021-03-03 17:45:29 +08:00
|
|
|
}
|
|
|
|
),
|
2020-10-03 15:35:26 +08:00
|
|
|
scope: guardian,
|
|
|
|
root: false
|
|
|
|
))
|
2021-03-03 17:45:29 +08:00
|
|
|
elsif current_user&.staff?
|
|
|
|
message = if SiteSetting.enable_discourse_connect
|
|
|
|
I18n.t("invite.disabled_errors.discourse_connect_enabled")
|
2020-10-03 15:35:26 +08:00
|
|
|
end
|
2020-06-09 23:19:32 +08:00
|
|
|
|
2021-03-03 17:45:29 +08:00
|
|
|
render_invite_error(message)
|
2020-10-03 15:35:26 +08:00
|
|
|
else
|
2021-03-03 17:45:29 +08:00
|
|
|
render_json_error(I18n.t("invite.disabled_errors.invalid_access"))
|
2020-10-03 15:35:26 +08:00
|
|
|
end
|
2015-08-25 03:03:25 +08:00
|
|
|
end
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
def is_local_username
|
2020-02-03 18:34:36 +08:00
|
|
|
usernames = params[:usernames] if params[:usernames].present?
|
|
|
|
usernames = [params[:username]] if params[:username].present?
|
|
|
|
|
2022-01-11 15:16:20 +08:00
|
|
|
raise Discourse::InvalidParameters.new(:usernames) if !usernames.kind_of?(Array) || usernames.size > 20
|
2015-06-11 22:31:43 +08:00
|
|
|
|
2015-11-30 14:12:51 +08:00
|
|
|
groups = Group.where(name: usernames).pluck(:name)
|
2015-12-04 10:40:38 +08:00
|
|
|
mentionable_groups =
|
|
|
|
if current_user
|
2020-02-15 01:27:46 +08:00
|
|
|
Group.mentionable(current_user, include_public: false)
|
2015-12-04 10:40:38 +08:00
|
|
|
.where(name: usernames)
|
|
|
|
.pluck(:name, :user_count)
|
2017-12-21 13:13:57 +08:00
|
|
|
.map do |name, user_count|
|
|
|
|
{
|
|
|
|
name: name,
|
|
|
|
user_count: user_count
|
|
|
|
}
|
|
|
|
end
|
2015-12-04 10:40:38 +08:00
|
|
|
end
|
|
|
|
|
2015-11-30 14:12:51 +08:00
|
|
|
usernames -= groups
|
2016-05-22 21:01:46 +08:00
|
|
|
usernames.each(&:downcase!)
|
2015-11-30 14:12:51 +08:00
|
|
|
|
2022-01-11 15:16:20 +08:00
|
|
|
users = User
|
|
|
|
.where(staged: false, username_lower: usernames)
|
|
|
|
.index_by(&:username_lower)
|
|
|
|
|
|
|
|
cannot_see = {}
|
2021-11-24 04:25:54 +08:00
|
|
|
here_count = nil
|
|
|
|
|
2016-11-15 11:03:16 +08:00
|
|
|
topic_id = params[:topic_id]
|
2021-11-24 04:25:54 +08:00
|
|
|
if topic_id.present? && topic = Topic.find_by(id: topic_id)
|
2022-01-11 15:16:20 +08:00
|
|
|
topic_muted_by = TopicUser
|
|
|
|
.where(topic: topic)
|
|
|
|
.where(user_id: users.values.map(&:id))
|
|
|
|
.where(notification_level: TopicUser.notification_levels[:muted])
|
|
|
|
.pluck(:user_id)
|
|
|
|
.to_set
|
|
|
|
|
|
|
|
if topic.private_message?
|
|
|
|
topic_allowed_user_ids = TopicAllowedUser
|
|
|
|
.where(topic: topic)
|
|
|
|
.where(user_id: users.values.map(&:id))
|
|
|
|
.pluck(:user_id)
|
|
|
|
.to_set
|
|
|
|
|
|
|
|
topic_allowed_group_ids = TopicAllowedGroup
|
|
|
|
.where(topic: topic)
|
|
|
|
.pluck(:group_id)
|
|
|
|
.to_set
|
|
|
|
end
|
|
|
|
|
2021-11-24 04:25:54 +08:00
|
|
|
usernames.each do |username|
|
2022-01-11 15:16:20 +08:00
|
|
|
user = users[username]
|
|
|
|
next if user.blank?
|
|
|
|
|
|
|
|
cannot_see_reason = nil
|
|
|
|
if !user.guardian.can_see?(topic)
|
|
|
|
cannot_see_reason = topic.private_message? ? :private : :category
|
|
|
|
elsif topic_muted_by.include?(user.id)
|
|
|
|
cannot_see_reason = :muted_topic
|
|
|
|
elsif topic.private_message? && !topic_allowed_user_ids.include?(user.id) && !user.group_ids.any? { |group_id| topic_allowed_group_ids.include?(group_id) }
|
|
|
|
cannot_see_reason = :not_allowed
|
2021-11-24 04:25:54 +08:00
|
|
|
end
|
2022-01-11 15:16:20 +08:00
|
|
|
|
|
|
|
if !guardian.is_staff? && cannot_see_reason.present? && cannot_see_reason != :private && cannot_see_reason != :category
|
|
|
|
cannot_see_reason = nil # do not leak private information
|
|
|
|
end
|
|
|
|
|
|
|
|
cannot_see[username] = cannot_see_reason if cannot_see_reason.present?
|
2021-11-24 04:25:54 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
if usernames.include?(SiteSetting.here_mention) && guardian.can_mention_here?
|
|
|
|
here_count = PostAlerter.new.expand_here_mention(topic.first_post, exclude_ids: [current_user.id]).size
|
|
|
|
end
|
2016-11-15 11:03:16 +08:00
|
|
|
end
|
|
|
|
|
2017-12-21 13:13:57 +08:00
|
|
|
render json: {
|
2022-01-11 15:16:20 +08:00
|
|
|
valid: users.keys,
|
2017-12-21 13:13:57 +08:00
|
|
|
valid_groups: groups,
|
|
|
|
mentionable_groups: mentionable_groups,
|
|
|
|
cannot_see: cannot_see,
|
2021-11-24 04:25:54 +08:00
|
|
|
here_count: here_count,
|
2017-12-21 13:13:57 +08:00
|
|
|
max_users_notified_per_group_mention: SiteSetting.max_users_notified_per_group_mention
|
|
|
|
}
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2013-08-25 16:57:12 +08:00
|
|
|
def render_available_true
|
|
|
|
render(json: { available: true })
|
|
|
|
end
|
|
|
|
|
|
|
|
def changing_case_of_own_username(target_user, username)
|
|
|
|
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-06 03:16:51 +08:00
|
|
|
def check_username
|
2013-11-20 03:15:05 +08:00
|
|
|
if !params[:username].present?
|
|
|
|
params.require(:username) if !params[:email].present?
|
2014-07-17 00:25:24 +08:00
|
|
|
return render(json: success_json)
|
2013-11-20 03:15:05 +08:00
|
|
|
end
|
2019-04-23 18:22:47 +08:00
|
|
|
username = params[:username]&.unicode_normalize
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2013-08-25 16:57:12 +08:00
|
|
|
target_user = user_from_params_or_current_user
|
2013-07-31 02:13:56 +08:00
|
|
|
|
2013-06-29 04:21:46 +08:00
|
|
|
# The special case where someone is changing the case of their own username
|
2013-08-25 16:57:12 +08:00
|
|
|
return render_available_true if changing_case_of_own_username(target_user, username)
|
2013-06-29 04:21:46 +08:00
|
|
|
|
2013-09-06 17:35:29 +08:00
|
|
|
checker = UsernameCheckerService.new
|
|
|
|
email = params[:email] || target_user.try(:email)
|
2013-11-20 03:15:05 +08:00
|
|
|
render json: checker.check_username(username, email)
|
2013-08-25 16:57:12 +08:00
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2021-03-22 23:46:03 +08:00
|
|
|
def check_email
|
2021-03-24 05:44:51 +08:00
|
|
|
begin
|
|
|
|
RateLimiter.new(nil, "check-email-#{request.remote_ip}", 10, 1.minute).performed!
|
|
|
|
rescue RateLimiter::LimitExceeded
|
|
|
|
return render json: success_json
|
|
|
|
end
|
|
|
|
|
|
|
|
email = Email.downcase((params[:email] || "").strip)
|
2021-03-22 23:46:03 +08:00
|
|
|
|
2021-03-24 05:44:51 +08:00
|
|
|
if email.blank? || SiteSetting.hide_email_address_taken?
|
2021-03-22 23:46:03 +08:00
|
|
|
return render json: success_json
|
|
|
|
end
|
|
|
|
|
2022-02-18 09:12:51 +08:00
|
|
|
if !EmailAddressValidator.valid_value?(email)
|
2021-03-24 05:44:51 +08:00
|
|
|
error = User.new.errors.full_message(:email, I18n.t(:'user.email.invalid'))
|
|
|
|
return render json: failed_json.merge(errors: [error])
|
|
|
|
end
|
2021-03-22 23:46:03 +08:00
|
|
|
|
2021-03-24 05:44:51 +08:00
|
|
|
if !EmailValidator.allowed?(email)
|
|
|
|
error = User.new.errors.full_message(:email, I18n.t(:'user.email.not_allowed'))
|
|
|
|
return render json: failed_json.merge(errors: [error])
|
2021-03-22 23:46:03 +08:00
|
|
|
end
|
2021-03-24 05:44:51 +08:00
|
|
|
|
|
|
|
if ScreenedEmail.should_block?(email)
|
|
|
|
error = User.new.errors.full_message(:email, I18n.t(:'user.email.blocked'))
|
|
|
|
return render json: failed_json.merge(errors: [error])
|
|
|
|
end
|
|
|
|
|
|
|
|
if User.where(staged: false).find_by_email(email).present?
|
|
|
|
error = User.new.errors.full_message(:email, I18n.t(:'errors.messages.taken'))
|
|
|
|
return render json: failed_json.merge(errors: [error])
|
|
|
|
end
|
|
|
|
|
2021-03-22 23:46:03 +08:00
|
|
|
render json: success_json
|
|
|
|
end
|
|
|
|
|
2013-08-25 16:57:12 +08:00
|
|
|
def user_from_params_or_current_user
|
|
|
|
params[:for_user_id] ? User.find(params[:for_user_id]) : current_user
|
|
|
|
end
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
def create
|
2017-06-12 16:48:32 +08:00
|
|
|
params.require(:email)
|
2019-05-04 00:17:06 +08:00
|
|
|
params.require(:username)
|
2020-03-15 18:17:28 +08:00
|
|
|
params.require(:invite_code) if SiteSetting.require_invite_code
|
2014-09-27 02:48:34 +08:00
|
|
|
params.permit(:user_fields)
|
2022-03-04 00:17:02 +08:00
|
|
|
params.permit(:external_ids)
|
2014-09-27 02:48:34 +08:00
|
|
|
|
2014-07-15 03:42:14 +08:00
|
|
|
unless SiteSetting.allow_new_registrations
|
2014-09-27 02:48:34 +08:00
|
|
|
return fail_with("login.new_registrations_disabled")
|
2014-07-15 03:42:14 +08:00
|
|
|
end
|
|
|
|
|
2014-09-12 03:22:11 +08:00
|
|
|
if params[:password] && params[:password].length > User.max_password_length
|
2014-09-27 02:48:34 +08:00
|
|
|
return fail_with("login.password_too_long")
|
2014-09-12 03:22:11 +08:00
|
|
|
end
|
|
|
|
|
2017-06-12 16:48:32 +08:00
|
|
|
if params[:email].length > 254 + 1 + 253
|
2015-07-14 04:40:52 +08:00
|
|
|
return fail_with("login.email_too_long")
|
|
|
|
end
|
|
|
|
|
2020-03-26 10:44:02 +08:00
|
|
|
if SiteSetting.require_invite_code && SiteSetting.invite_code.strip.downcase != params[:invite_code].strip.downcase
|
2020-03-15 18:17:28 +08:00
|
|
|
return fail_with("login.wrong_invite_code")
|
|
|
|
end
|
|
|
|
|
2019-09-14 02:11:27 +08:00
|
|
|
if clashing_with_existing_route?(params[:username]) || User.reserved_username?(params[:username])
|
2015-07-02 04:44:53 +08:00
|
|
|
return fail_with("login.reserved_username")
|
|
|
|
end
|
|
|
|
|
2019-06-07 23:51:58 +08:00
|
|
|
params[:locale] ||= I18n.locale unless current_user
|
|
|
|
|
2019-11-25 08:49:27 +08:00
|
|
|
new_user_params = user_params.except(:timezone)
|
2020-03-17 23:48:24 +08:00
|
|
|
|
|
|
|
user = User.where(staged: true).with_email(new_user_params[:email].strip.downcase).first
|
|
|
|
|
|
|
|
if user
|
|
|
|
user.active = false
|
|
|
|
user.unstage!
|
|
|
|
end
|
|
|
|
|
|
|
|
user ||= User.new
|
|
|
|
user.attributes = new_user_params
|
2013-02-07 08:25:21 +08:00
|
|
|
|
2020-04-01 02:29:15 +08:00
|
|
|
# Handle API approval and
|
|
|
|
# auto approve users based on auto_approve_email_domains setting
|
|
|
|
if user.approved? || EmailValidator.can_auto_approve_user?(user.email)
|
|
|
|
ReviewableUser.set_approved_fields!(user, current_user)
|
|
|
|
end
|
2017-08-29 03:36:46 +08:00
|
|
|
|
2014-09-27 02:48:34 +08:00
|
|
|
# Handle custom fields
|
2014-10-09 02:38:18 +08:00
|
|
|
user_fields = UserField.all
|
|
|
|
if user_fields.present?
|
2015-01-27 17:48:27 +08: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
|
2018-09-07 06:02:47 +08:00
|
|
|
fields["#{User::USER_FIELD_PREFIX}#{f.id}"] = field_val[0...UserField.max_length]
|
2014-09-27 02:48:34 +08:00
|
|
|
end
|
|
|
|
end
|
2015-01-27 17:48:27 +08:00
|
|
|
|
|
|
|
user.custom_fields = fields
|
2014-09-27 02:48:34 +08:00
|
|
|
end
|
|
|
|
|
2022-03-04 00:17:02 +08:00
|
|
|
# Handle associated accounts
|
|
|
|
associations = []
|
|
|
|
if params[:external_ids]&.is_a?(ActionController::Parameters) && current_user&.admin? && is_api?
|
|
|
|
params[:external_ids].each do |provider_name, provider_uid|
|
|
|
|
authenticator = Discourse.enabled_authenticators.find { |a| a.name == provider_name }
|
|
|
|
raise Discourse::InvalidParameters.new(:external_ids) if !authenticator&.is_managed?
|
|
|
|
|
|
|
|
association = UserAssociatedAccount.find_or_initialize_by(provider_name: provider_name, provider_uid: provider_uid)
|
|
|
|
associations << association
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-11-13 06:37:38 +08:00
|
|
|
authentication = UserAuthenticator.new(user, session)
|
2014-03-26 12:39:44 +08:00
|
|
|
|
2020-04-30 09:39:24 +08:00
|
|
|
if !authentication.has_authenticator? && !SiteSetting.enable_local_logins && !(current_user&.admin? && is_api?)
|
2018-05-26 10:18:19 +08:00
|
|
|
return render body: nil, status: :forbidden
|
2014-03-26 12:39:44 +08:00
|
|
|
end
|
|
|
|
|
2013-11-13 06:37:38 +08:00
|
|
|
authentication.start
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2017-10-17 01:51:35 +08: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-13 06:37:38 +08:00
|
|
|
activation = UserActivator.new(user, request, session, cookies)
|
|
|
|
activation.start
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2014-03-20 11:49:25 +08:00
|
|
|
# just assign a password if we have an authenticator and no password
|
|
|
|
# this is the case for Twitter
|
2022-03-04 00:17:02 +08:00
|
|
|
user.password = SecureRandom.hex if user.password.blank? && (authentication.has_authenticator? || associations.present?)
|
2014-03-20 11:49:25 +08:00
|
|
|
|
2013-11-13 06:37:38 +08:00
|
|
|
if user.save
|
|
|
|
authentication.finish
|
|
|
|
activation.finish
|
2022-03-04 00:17:02 +08:00
|
|
|
associations.each { |a| a.update!(user: user) }
|
2019-11-25 08:49:27 +08:00
|
|
|
user.update_timezone_if_missing(params[:timezone])
|
2013-11-13 06:37:38 +08:00
|
|
|
|
2019-10-16 13:53:31 +08:00
|
|
|
secure_session[HONEYPOT_KEY] = nil
|
|
|
|
secure_session[CHALLENGE_KEY] = nil
|
|
|
|
|
2014-10-02 01:33:49 +08:00
|
|
|
# save user email in session, to show on account-created page
|
2014-11-05 04:47:32 +08:00
|
|
|
session["user_created_message"] = activation.message
|
2017-05-03 03:57:55 +08:00
|
|
|
session[SessionController::ACTIVATE_USER_KEY] = user.id
|
2014-09-27 02:48:34 +08:00
|
|
|
|
2020-04-15 04:30:42 +08:00
|
|
|
# If the user was created as active this will
|
|
|
|
# ensure their email is confirmed and
|
|
|
|
# add them to the review queue if they need to be approved
|
|
|
|
user.activate if user.active?
|
2019-04-13 03:25:02 +08:00
|
|
|
|
2013-11-13 06:37:38 +08:00
|
|
|
render json: {
|
|
|
|
success: true,
|
|
|
|
active: user.active?,
|
2014-09-23 07:06:19 +08:00
|
|
|
message: activation.message,
|
2020-12-08 05:25:35 +08:00
|
|
|
}.merge(SiteSetting.hide_email_address_taken ? {} : { user_id: user.id })
|
2017-10-04 03:28:15 +08:00
|
|
|
elsif SiteSetting.hide_email_address_taken && user.errors[:primary_email]&.include?(I18n.t('errors.messages.taken'))
|
2017-10-04 02:08:37 +08:00
|
|
|
session["user_created_message"] = activation.success_message
|
|
|
|
|
|
|
|
if existing_user = User.find_by_email(user.primary_email&.email)
|
2022-02-05 01:34:38 +08:00
|
|
|
Jobs.enqueue(:critical_user_email, type: "account_exists", user_id: existing_user.id)
|
2017-10-04 02:08:37 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
render json: {
|
|
|
|
success: true,
|
2020-12-08 05:25:35 +08:00
|
|
|
active: false,
|
|
|
|
message: activation.success_message
|
2013-11-13 06:37:38 +08:00
|
|
|
}
|
|
|
|
else
|
2017-09-12 01:22:04 +08:00
|
|
|
errors = user.errors.to_hash
|
|
|
|
errors[:email] = errors.delete(:primary_email) if errors[:primary_email]
|
|
|
|
|
2013-11-13 06:37:38 +08:00
|
|
|
render json: {
|
|
|
|
success: false,
|
|
|
|
message: I18n.t(
|
|
|
|
'login.errors',
|
|
|
|
errors: user.errors.full_messages.join("\n")
|
|
|
|
),
|
2017-09-12 01:22:04 +08:00
|
|
|
errors: errors,
|
|
|
|
values: {
|
|
|
|
name: user.name,
|
|
|
|
username: user.username,
|
|
|
|
email: user.primary_email&.email
|
|
|
|
},
|
2016-08-05 23:57:13 +08:00
|
|
|
is_developer: UsernameCheckerService.is_developer?(user.email)
|
2013-11-13 06:37:38 +08:00
|
|
|
}
|
|
|
|
end
|
2013-03-08 03:56:28 +08:00
|
|
|
rescue ActiveRecord::StatementInvalid
|
2013-11-13 06:37:38 +08:00
|
|
|
render json: {
|
|
|
|
success: false,
|
|
|
|
message: I18n.t("login.something_already_taken")
|
|
|
|
}
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2020-01-15 18:27:12 +08:00
|
|
|
def password_reset_show
|
2015-02-20 07:28:38 +08:00
|
|
|
expires_now
|
2016-12-19 15:00:22 +08:00
|
|
|
token = params[:token]
|
2021-06-02 03:32:51 +08:00
|
|
|
|
2020-01-15 18:27:12 +08:00
|
|
|
password_reset_find_user(token, committing_change: false)
|
|
|
|
|
|
|
|
if !@error
|
|
|
|
security_params = {
|
|
|
|
is_developer: UsernameCheckerService.is_developer?(@user.email),
|
|
|
|
admin: @user.admin?,
|
|
|
|
second_factor_required: @user.totp_enabled?,
|
|
|
|
security_key_required: @user.security_keys_enabled?,
|
|
|
|
backup_enabled: @user.backup_codes_enabled?,
|
|
|
|
multiple_second_factor_methods: @user.has_multiple_second_factor_methods?
|
|
|
|
}
|
|
|
|
end
|
2016-12-19 15:00:22 +08:00
|
|
|
|
2020-01-15 18:27:12 +08:00
|
|
|
respond_to do |format|
|
|
|
|
format.html do
|
|
|
|
return render 'password_reset', layout: 'no_ember' if @error
|
|
|
|
|
|
|
|
Webauthn.stage_challenge(@user, secure_session)
|
|
|
|
store_preloaded(
|
|
|
|
"password_reset",
|
|
|
|
MultiJson.dump(security_params.merge(Webauthn.allowed_credentials(@user, secure_session)))
|
|
|
|
)
|
|
|
|
|
|
|
|
render 'password_reset'
|
2018-10-23 01:00:30 +08:00
|
|
|
end
|
2014-08-26 03:30:52 +08:00
|
|
|
|
2020-01-15 18:27:12 +08:00
|
|
|
format.json do
|
|
|
|
return render json: { message: @error } if @error
|
|
|
|
|
|
|
|
Webauthn.stage_challenge(@user, secure_session)
|
|
|
|
render json: security_params.merge(Webauthn.allowed_credentials(@user, secure_session))
|
2014-08-26 03:30:52 +08:00
|
|
|
end
|
2014-07-02 11:06:55 +08:00
|
|
|
end
|
2020-01-15 18:27:12 +08:00
|
|
|
end
|
2014-07-02 11:06:55 +08:00
|
|
|
|
2020-01-15 18:27:12 +08:00
|
|
|
def password_reset_update
|
|
|
|
expires_now
|
|
|
|
token = params[:token]
|
|
|
|
password_reset_find_user(token, committing_change: true)
|
2018-10-23 01:00:30 +08:00
|
|
|
|
2021-02-04 07:03:30 +08:00
|
|
|
rate_limit_second_factor!(@user)
|
2020-01-15 18:27:12 +08:00
|
|
|
|
|
|
|
# no point doing anything else if we can't even find
|
|
|
|
# a user from the token
|
|
|
|
if @user
|
|
|
|
if !secure_session["second-factor-#{token}"]
|
|
|
|
second_factor_authentication_result = @user.authenticate_second_factor(params, secure_session)
|
|
|
|
if !second_factor_authentication_result.ok
|
|
|
|
user_error_key = second_factor_authentication_result.reason == "invalid_security_key" ? :user_second_factors : :security_keys
|
|
|
|
@user.errors.add(user_error_key, :invalid)
|
|
|
|
@error = second_factor_authentication_result.error
|
|
|
|
else
|
|
|
|
|
|
|
|
# this must be set because the first call we authenticate e.g. TOTP, and we do
|
|
|
|
# not want to re-authenticate on the second call to change the password as this
|
|
|
|
# will cause a TOTP error saying the code has already been used
|
|
|
|
secure_session["second-factor-#{token}"] = true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if @invalid_password = params[:password].blank? || params[:password].size > User.max_password_length
|
2014-09-12 03:22:11 +08:00
|
|
|
@user.errors.add(:password, :invalid)
|
2020-01-15 18:27:12 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
# if we have run into no errors then the user is a-ok to
|
|
|
|
# change the password
|
|
|
|
if @user.errors.empty?
|
2020-04-18 02:39:04 +08:00
|
|
|
@user.update_timezone_if_missing(params[:timezone]) if params[:timezone]
|
2014-09-12 03:22:11 +08:00
|
|
|
@user.password = params[:password]
|
|
|
|
@user.password_required!
|
2017-02-01 06:21:37 +08:00
|
|
|
@user.user_auth_tokens.destroy_all
|
2014-09-12 03:22:11 +08:00
|
|
|
if @user.save
|
|
|
|
Invite.invalidate_for_email(@user.email) # invite link can't be used to log in anymore
|
2016-12-19 15:00:22 +08:00
|
|
|
secure_session["password-#{token}"] = nil
|
2017-12-22 09:18:12 +08:00
|
|
|
secure_session["second-factor-#{token}"] = nil
|
2018-11-14 08:32:42 +08:00
|
|
|
UserHistory.create!(
|
|
|
|
target_user: @user,
|
|
|
|
acting_user: @user,
|
|
|
|
action: UserHistory.actions[:change_password]
|
|
|
|
)
|
2014-09-12 03:22:11 +08:00
|
|
|
logon_after_password_reset
|
2017-02-01 04:42:12 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
respond_to do |format|
|
|
|
|
format.html do
|
2020-01-15 18:27:12 +08:00
|
|
|
return render 'password_reset', layout: 'no_ember' if @error
|
2018-02-20 14:44:51 +08:00
|
|
|
|
2020-01-15 18:27:12 +08:00
|
|
|
Webauthn.stage_challenge(@user, secure_session)
|
|
|
|
|
|
|
|
security_params = {
|
|
|
|
is_developer: UsernameCheckerService.is_developer?(@user.email),
|
|
|
|
admin: @user.admin?,
|
|
|
|
second_factor_required: @user.totp_enabled?,
|
|
|
|
security_key_required: @user.security_keys_enabled?,
|
|
|
|
backup_enabled: @user.backup_codes_enabled?,
|
|
|
|
multiple_second_factor_methods: @user.has_multiple_second_factor_methods?
|
|
|
|
}.merge(Webauthn.allowed_credentials(@user, secure_session))
|
|
|
|
|
|
|
|
store_preloaded("password_reset", MultiJson.dump(security_params))
|
|
|
|
|
|
|
|
return redirect_to(wizard_path) if Wizard.user_requires_completion?(@user)
|
|
|
|
|
|
|
|
render 'password_reset'
|
2017-02-01 04:42:12 +08:00
|
|
|
end
|
2016-10-18 23:44:25 +08:00
|
|
|
|
2017-02-01 04:42:12 +08:00
|
|
|
format.json do
|
2020-01-15 18:27:12 +08:00
|
|
|
if @error || @user&.errors&.any?
|
|
|
|
render json: {
|
|
|
|
success: false,
|
|
|
|
message: @error,
|
|
|
|
errors: @user&.errors&.to_hash,
|
|
|
|
is_developer: UsernameCheckerService.is_developer?(@user&.email),
|
|
|
|
admin: @user&.admin?
|
|
|
|
}
|
2017-02-01 04:42:12 +08:00
|
|
|
else
|
2020-01-15 18:27:12 +08:00
|
|
|
render json: {
|
|
|
|
success: true,
|
|
|
|
message: @success,
|
|
|
|
requires_approval: !Guardian.new(@user).can_access_forum?,
|
|
|
|
redirect_to: Wizard.user_requires_completion?(@user) ? wizard_path : nil
|
|
|
|
}
|
2014-09-12 03:22:11 +08:00
|
|
|
end
|
2014-01-22 05:53:46 +08:00
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
end
|
2013-02-07 23:45:24 +08:00
|
|
|
|
2016-01-05 00:48:54 +08:00
|
|
|
def confirm_email_token
|
|
|
|
expires_now
|
2021-11-25 15:34:39 +08:00
|
|
|
EmailToken.confirm(params[:token], scope: EmailToken.scopes[:signup])
|
2016-01-05 00:48:54 +08:00
|
|
|
render json: success_json
|
|
|
|
end
|
|
|
|
|
2013-10-07 18:19:45 +08:00
|
|
|
def logon_after_password_reset
|
2013-11-12 01:51:14 +08: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
|
|
|
|
|
2015-02-20 07:28:38 +08:00
|
|
|
@success = I18n.t(message)
|
2014-07-03 15:29:44 +08:00
|
|
|
end
|
2013-10-07 18:19:45 +08:00
|
|
|
|
2015-04-27 18:59:48 +08:00
|
|
|
def admin_login
|
2018-01-22 19:20:53 +08:00
|
|
|
return redirect_to(path("/")) if current_user
|
2015-04-27 18:59:48 +08:00
|
|
|
|
2017-12-22 09:18:12 +08:00
|
|
|
if request.put? && params[:email].present?
|
2015-04-27 18:59:48 +08: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 19:20:53 +08:00
|
|
|
if user = User.with_email(params[:email]).admins.human_users.first
|
2021-11-25 15:34:39 +08:00
|
|
|
email_token = user.email_tokens.create!(email: user.email, scope: EmailToken.scopes[:email_login])
|
2022-02-05 01:34:38 +08:00
|
|
|
Jobs.enqueue(:critical_user_email, type: "admin_login", user_id: user.id, email_token: email_token.token)
|
2015-04-27 18:59:48 +08:00
|
|
|
@message = I18n.t("admin_login.success")
|
|
|
|
else
|
2018-01-22 19:20:53 +08:00
|
|
|
@message = I18n.t("admin_login.errors.unknown_email_address")
|
2015-04-27 18:59:48 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-10-02 10:08:41 +08:00
|
|
|
render layout: 'no_ember'
|
2015-04-27 18:59:48 +08:00
|
|
|
rescue RateLimiter::LimitExceeded
|
|
|
|
@message = I18n.t("rate_limiter.slow_down")
|
2019-10-02 10:08:41 +08:00
|
|
|
render layout: 'no_ember'
|
2015-04-27 18:59:48 +08:00
|
|
|
end
|
|
|
|
|
2017-04-20 23:17:24 +08: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
|
2021-12-06 09:38:37 +08:00
|
|
|
DiscourseEvent.trigger(:before_email_login, user)
|
|
|
|
|
2021-11-25 15:34:39 +08:00
|
|
|
email_token = user.email_tokens.create!(email: user.email, scope: EmailToken.scopes[:email_login])
|
2017-04-20 23:17:24 +08:00
|
|
|
|
|
|
|
Jobs.enqueue(:critical_user_email,
|
2022-02-05 01:34:38 +08:00
|
|
|
type: "email_login",
|
2017-04-20 23:17:24 +08:00
|
|
|
user_id: user.id,
|
|
|
|
email_token: email_token.token
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-02-15 07:46:33 +08:00
|
|
|
json = success_json
|
2020-03-10 00:53:32 +08:00
|
|
|
json[:hide_taken] = SiteSetting.hide_email_address_taken
|
2017-04-20 23:17:24 +08: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 16:02:10 +08: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 21:50:57 +08:00
|
|
|
def account_created
|
2018-05-13 22:03:11 +08:00
|
|
|
if current_user.present?
|
2021-02-08 18:04:33 +08:00
|
|
|
if SiteSetting.enable_discourse_connect_provider && payload = cookies.delete(:sso_payload)
|
2018-05-13 22:03:11 +08:00
|
|
|
return redirect_to(session_sso_provider_url + "?" + payload)
|
2018-09-05 03:16:54 +08:00
|
|
|
elsif destination_url = cookies.delete(:destination_url)
|
|
|
|
return redirect_to(destination_url)
|
2018-05-13 22:03:11 +08:00
|
|
|
else
|
2018-08-24 22:34:31 +08:00
|
|
|
return redirect_to(path('/'))
|
2018-05-13 22:03:11 +08:00
|
|
|
end
|
|
|
|
end
|
2017-05-03 23:15:54 +08:00
|
|
|
|
2016-05-06 02:37:09 +08:00
|
|
|
@custom_body_class = "static-account-created"
|
2015-05-07 09:00:22 +08:00
|
|
|
@message = session['user_created_message'] || I18n.t('activation.missing_session')
|
2018-05-13 22:03:11 +08:00
|
|
|
@account_created = { message: @message, show_controls: false }
|
2017-05-03 01:21:46 +08:00
|
|
|
|
2017-05-03 03:57:55 +08:00
|
|
|
if session_user_id = session[SessionController::ACTIVATE_USER_KEY]
|
2018-05-12 04:55:45 +08:00
|
|
|
if user = User.where(id: session_user_id.to_i).first
|
2017-05-03 03:57:55 +08:00
|
|
|
@account_created[:username] = user.username
|
|
|
|
@account_created[:email] = user.email
|
2018-01-19 22:29:15 +08:00
|
|
|
@account_created[:show_controls] = !user.from_staged?
|
2017-05-03 03:57:55 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
store_preloaded("accountCreated", MultiJson.dump(@account_created))
|
2014-09-23 21:50:57 +08:00
|
|
|
expires_now
|
2017-05-05 00:30:13 +08:00
|
|
|
|
|
|
|
respond_to do |format|
|
|
|
|
format.html { render "default/empty" }
|
|
|
|
format.json { render json: success_json }
|
|
|
|
end
|
2014-09-23 21:50:57 +08:00
|
|
|
end
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
def activate_account
|
2014-07-16 01:47:47 +08:00
|
|
|
expires_now
|
2015-01-16 04:56:53 +08:00
|
|
|
render layout: 'no_ember'
|
2014-07-15 00:25:42 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def perform_account_activation
|
2014-07-16 02:07:19 +08:00
|
|
|
raise Discourse::InvalidAccess.new if honeypot_or_challenge_fails?(params)
|
2018-05-12 04:55:45 +08:00
|
|
|
|
2021-11-25 15:34:39 +08:00
|
|
|
if @user = EmailToken.confirm(params[:token], scope: EmailToken.scopes[:signup])
|
2013-02-06 03:16:51 +08:00
|
|
|
# Log in the user unless they need to be approved
|
2013-04-04 00:23:28 +08:00
|
|
|
if Guardian.new(@user).can_access_forum?
|
2013-02-06 03:16:51 +08:00
|
|
|
@user.enqueue_welcome_message('welcome_user') if @user.send_welcome_message
|
|
|
|
log_on_user(@user)
|
2017-03-16 09:48:51 +08: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)
|
2021-02-08 18:04:33 +08:00
|
|
|
elsif SiteSetting.enable_discourse_connect_provider && payload = cookies.delete(:sso_payload)
|
2018-05-12 06:41:27 +08:00
|
|
|
return redirect_to(session_sso_provider_url + "?" + payload)
|
2017-03-16 09:48:51 +08:00
|
|
|
end
|
2013-04-04 00:23:28 +08:00
|
|
|
else
|
|
|
|
@needs_approval = true
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
else
|
2015-08-24 02:59:16 +08:00
|
|
|
flash.now[:error] = I18n.t('activation.already_done')
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
2018-05-12 06:41:27 +08:00
|
|
|
|
2015-01-16 04:56:53 +08:00
|
|
|
render layout: 'no_ember'
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2017-04-06 04:14:22 +08:00
|
|
|
def update_activation_email
|
|
|
|
RateLimiter.new(nil, "activate-edit-email-hr-#{request.remote_ip}", 5, 1.hour).performed!
|
|
|
|
|
2017-05-03 03:57:55 +08: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 22:29:15 +08:00
|
|
|
if @user.blank? || @user.active? || current_user.present? || @user.from_staged?
|
2017-12-14 14:16:49 +08:00
|
|
|
raise Discourse::InvalidAccess.new
|
|
|
|
end
|
|
|
|
|
2017-04-06 04:14:22 +08:00
|
|
|
User.transaction do
|
2017-09-12 22:03:33 +08:00
|
|
|
primary_email = @user.primary_email
|
|
|
|
primary_email.email = params[:email]
|
2017-10-25 13:02:18 +08:00
|
|
|
primary_email.skip_validate_email = false
|
2017-09-12 22:03:33 +08:00
|
|
|
|
|
|
|
if primary_email.save
|
2021-11-25 15:34:39 +08:00
|
|
|
@email_token = @user.email_tokens.create!(email: @user.email, scope: EmailToken.scopes[:signup])
|
|
|
|
EmailToken.enqueue_signup_email(@email_token, to_address: @user.email)
|
2017-04-06 04:14:22 +08:00
|
|
|
render json: success_json
|
|
|
|
else
|
2018-05-31 12:15:28 +08:00
|
|
|
render_json_error(primary_email)
|
2017-04-06 04:14:22 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-23 00:49:48 +08:00
|
|
|
def send_activation_email
|
2015-10-28 04:25:30 +08: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 15:42:48 +08:00
|
|
|
|
2017-05-26 03:28:39 +08:00
|
|
|
raise Discourse::InvalidAccess.new if SiteSetting.must_approve_users?
|
|
|
|
|
2017-05-03 03:57:55 +08:00
|
|
|
if params[:username].present?
|
|
|
|
@user = User.find_by_username_or_email(params[:username].to_s)
|
|
|
|
end
|
2017-03-13 20:20:25 +08:00
|
|
|
|
2021-11-25 15:34:39 +08:00
|
|
|
raise Discourse::NotFound unless @user
|
2017-03-13 19:19:42 +08:00
|
|
|
|
2021-11-25 15:34:39 +08:00
|
|
|
if !current_user&.staff? && @user.id != session[SessionController::ACTIVATE_USER_KEY]
|
2017-05-26 03:28:39 +08:00
|
|
|
raise Discourse::InvalidAccess.new
|
2017-03-13 19:19:42 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
session.delete(SessionController::ACTIVATE_USER_KEY)
|
|
|
|
|
2017-03-20 17:40:48 +08:00
|
|
|
if @user.active && @user.email_confirmed?
|
2017-03-13 19:19:42 +08:00
|
|
|
render_json_error(I18n.t('activation.activated'), status: 409)
|
2017-03-13 22:22:23 +08:00
|
|
|
else
|
2021-11-25 15:34:39 +08:00
|
|
|
@email_token = @user.email_tokens.create!(email: @user.email, scope: EmailToken.scopes[:signup])
|
|
|
|
EmailToken.enqueue_signup_email(@email_token, to_address: @user.email)
|
2017-08-31 12:06:56 +08:00
|
|
|
render body: nil
|
2017-03-13 19:19:42 +08:00
|
|
|
end
|
2013-02-23 00:49:48 +08:00
|
|
|
end
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
def search_users
|
2013-02-07 18:59:25 +08:00
|
|
|
term = params[:term].to_s.strip
|
2019-08-06 15:57:45 +08:00
|
|
|
|
2021-01-25 18:23:36 +08:00
|
|
|
topic_id = params[:topic_id].to_i if params[:topic_id].present?
|
|
|
|
category_id = params[:category_id].to_i if params[:category_id].present?
|
2019-08-06 15:57:45 +08:00
|
|
|
|
2015-04-13 23:03:13 +08:00
|
|
|
topic_allowed_users = params[:topic_allowed_users] || false
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2019-05-10 23:35:36 +08:00
|
|
|
group_names = params[:groups] || []
|
|
|
|
group_names << params[:group] if params[:group]
|
2021-01-25 18:23:36 +08:00
|
|
|
@groups = Group.where(name: group_names) if group_names.present?
|
2017-02-10 07:45:39 +08:00
|
|
|
|
2019-08-06 15:57:45 +08:00
|
|
|
options = {
|
|
|
|
topic_allowed_users: topic_allowed_users,
|
|
|
|
searching_user: current_user,
|
|
|
|
groups: @groups
|
|
|
|
}
|
|
|
|
|
2021-06-01 00:02:32 +08:00
|
|
|
options[:include_staged_users] = !!ActiveModel::Type::Boolean.new.cast(params[:include_staged_users])
|
2021-07-21 21:14:53 +08:00
|
|
|
options[:last_seen_users] = !!ActiveModel::Type::Boolean.new.cast(params[:last_seen_users])
|
2021-08-24 15:45:26 +08:00
|
|
|
if params[:limit].present?
|
|
|
|
options[:limit] = params[:limit].to_i
|
|
|
|
raise Discourse::InvalidParameters.new(:limit) if options[:limit] <= 0
|
|
|
|
end
|
2021-01-25 18:23:36 +08:00
|
|
|
options[:topic_id] = topic_id if topic_id
|
|
|
|
options[:category_id] = category_id if category_id
|
2019-08-06 15:57:45 +08:00
|
|
|
|
|
|
|
results = UserSearch.new(term, options).search
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2015-09-11 21:04:29 +08:00
|
|
|
user_fields = [:username, :upload_avatar_template]
|
2013-10-31 03:45:13 +08:00
|
|
|
user_fields << :name if SiteSetting.enable_names?
|
|
|
|
|
2015-09-11 16:14:34 +08:00
|
|
|
to_render = { users: results.as_json(only: user_fields, methods: [:avatar_template]) }
|
2013-12-23 22:46:00 +08:00
|
|
|
|
2021-01-25 18:23:36 +08:00
|
|
|
# blank term is only handy for in-topic search of users after @
|
|
|
|
# we do not want group results ever if term is blank
|
2017-11-02 10:20:14 +08:00
|
|
|
groups =
|
2021-01-25 18:23:36 +08:00
|
|
|
if term.present? && current_user
|
2020-02-12 16:11:10 +08:00
|
|
|
if params[:include_groups] == 'true'
|
|
|
|
Group.visible_groups(current_user)
|
|
|
|
elsif params[:include_mentionable_groups] == 'true'
|
2017-10-02 17:45:58 +08:00
|
|
|
Group.mentionable(current_user)
|
|
|
|
elsif params[:include_messageable_groups] == 'true'
|
|
|
|
Group.messageable(current_user)
|
|
|
|
end
|
2017-10-03 18:49:45 +08:00
|
|
|
end
|
2017-11-02 10:20:14 +08:00
|
|
|
|
2020-02-12 16:11:10 +08:00
|
|
|
if groups
|
2021-09-08 07:38:45 +08:00
|
|
|
DiscoursePluginRegistry.groups_callback_for_users_search_controller_action.each do |param_name, block|
|
|
|
|
if params[param_name.to_s]
|
|
|
|
groups = block.call(groups, current_user)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-03-03 13:57:52 +08:00
|
|
|
groups = Group.search_groups(term, groups: groups, sort: :auto)
|
2017-11-03 21:39:55 +08:00
|
|
|
|
|
|
|
to_render[:groups] = groups.map do |m|
|
|
|
|
{ name: m.name, full_name: m.full_name }
|
|
|
|
end
|
2015-12-02 12:49:43 +08:00
|
|
|
end
|
|
|
|
|
2013-12-23 22:46:00 +08:00
|
|
|
render json: to_render
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2015-09-18 01:42:44 +08:00
|
|
|
AVATAR_TYPES_WITH_UPLOAD ||= %w{uploaded custom gravatar}
|
|
|
|
|
2014-05-26 17:46:43 +08:00
|
|
|
def pick_avatar
|
2013-08-14 04:08:29 +08:00
|
|
|
user = fetch_user_from_params
|
|
|
|
guardian.ensure_can_edit!(user)
|
2015-05-29 16:21:41 +08:00
|
|
|
|
2021-02-08 18:04:33 +08:00
|
|
|
if SiteSetting.discourse_connect_overrides_avatar
|
2021-01-05 21:29:10 +08:00
|
|
|
return render json: failed_json, status: 422
|
|
|
|
end
|
|
|
|
|
2015-09-11 21:04:29 +08:00
|
|
|
type = params[:type]
|
2014-05-26 17:46:43 +08:00
|
|
|
|
2021-01-05 21:29:10 +08:00
|
|
|
invalid_type = type.present? && !AVATAR_TYPES_WITH_UPLOAD.include?(type) && type != 'system'
|
|
|
|
if invalid_type
|
2015-11-12 17:26:45 +08:00
|
|
|
return render json: failed_json, status: 422
|
|
|
|
end
|
|
|
|
|
2021-01-05 21:29:10 +08:00
|
|
|
if type.blank? || type == 'system'
|
|
|
|
upload_id = nil
|
2021-08-24 15:46:28 +08:00
|
|
|
elsif !TrustLevelAndStaffAndDisabledSetting.matches?(SiteSetting.allow_uploaded_avatars, user)
|
2021-04-28 04:28:15 +08:00
|
|
|
return render json: failed_json, status: 422
|
2021-01-05 21:29:10 +08:00
|
|
|
else
|
|
|
|
upload_id = params[:upload_id]
|
|
|
|
upload = Upload.find_by(id: upload_id)
|
2018-09-20 13:33:10 +08:00
|
|
|
|
2021-01-05 21:29:10 +08:00
|
|
|
if upload.nil?
|
|
|
|
return render_json_error I18n.t('avatar.missing')
|
|
|
|
end
|
2015-09-11 21:47:48 +08:00
|
|
|
|
2021-01-05 21:29:10 +08:00
|
|
|
# old safeguard
|
|
|
|
user.create_user_avatar unless user.user_avatar
|
2018-09-20 13:33:10 +08:00
|
|
|
|
2021-01-05 21:29:10 +08:00
|
|
|
guardian.ensure_can_pick_avatar!(user.user_avatar, upload)
|
2015-09-18 01:42:44 +08:00
|
|
|
|
2021-01-05 21:29:10 +08:00
|
|
|
if type == 'gravatar'
|
2015-09-18 01:42:44 +08:00
|
|
|
user.user_avatar.gravatar_upload_id = upload_id
|
|
|
|
else
|
|
|
|
user.user_avatar.custom_upload_id = upload_id
|
|
|
|
end
|
2015-09-11 21:47:48 +08:00
|
|
|
end
|
2015-05-29 16:21:41 +08:00
|
|
|
|
2021-04-28 04:28:15 +08:00
|
|
|
if user.is_system_user?
|
|
|
|
SiteSetting.use_site_small_logo_as_system_avatar = false
|
|
|
|
end
|
|
|
|
|
2018-09-20 13:33:10 +08:00
|
|
|
user.uploaded_avatar_id = upload_id
|
2013-08-14 04:08:29 +08:00
|
|
|
user.save!
|
2015-05-29 16:21:41 +08:00
|
|
|
user.user_avatar.save!
|
2013-08-14 04:08:29 +08:00
|
|
|
|
2014-12-07 00:26:32 +08:00
|
|
|
render json: success_json
|
2013-08-14 04:08:29 +08:00
|
|
|
end
|
2014-04-15 04:55:57 +08:00
|
|
|
|
2018-07-18 18:57:43 +08:00
|
|
|
def select_avatar
|
|
|
|
user = fetch_user_from_params
|
|
|
|
guardian.ensure_can_edit!(user)
|
|
|
|
|
|
|
|
url = params[:url]
|
|
|
|
|
|
|
|
if url.blank?
|
|
|
|
return render json: failed_json, status: 422
|
|
|
|
end
|
|
|
|
|
2022-02-25 04:57:39 +08:00
|
|
|
if SiteSetting.selectable_avatars_mode == "disabled"
|
2018-07-18 18:57:43 +08:00
|
|
|
return render json: failed_json, status: 422
|
|
|
|
end
|
|
|
|
|
|
|
|
if SiteSetting.selectable_avatars.blank?
|
|
|
|
return render json: failed_json, status: 422
|
|
|
|
end
|
|
|
|
|
2020-08-03 22:15:41 +08:00
|
|
|
unless upload = Upload.get_from_url(url)
|
2018-07-18 18:57:43 +08:00
|
|
|
return render json: failed_json, status: 422
|
|
|
|
end
|
|
|
|
|
2020-10-13 21:17:06 +08:00
|
|
|
unless SiteSetting.selectable_avatars.include?(upload)
|
2018-07-18 18:57:43 +08:00
|
|
|
return render json: failed_json, status: 422
|
|
|
|
end
|
|
|
|
|
|
|
|
user.uploaded_avatar_id = upload.id
|
2021-04-28 04:28:15 +08:00
|
|
|
|
|
|
|
if user.is_system_user?
|
|
|
|
SiteSetting.use_site_small_logo_as_system_avatar = false
|
|
|
|
end
|
|
|
|
|
2018-07-18 18:57:43 +08:00
|
|
|
user.save!
|
|
|
|
|
|
|
|
avatar = user.user_avatar || user.create_user_avatar
|
|
|
|
avatar.custom_upload_id = upload.id
|
|
|
|
avatar.save!
|
|
|
|
|
|
|
|
render json: {
|
|
|
|
avatar_template: user.avatar_template,
|
|
|
|
custom_avatar_template: user.avatar_template,
|
|
|
|
uploaded_avatar_id: upload.id,
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2014-06-28 02:48:39 +08:00
|
|
|
def destroy_user_image
|
2014-03-01 04:12:51 +08:00
|
|
|
user = fetch_user_from_params
|
|
|
|
guardian.ensure_can_edit!(user)
|
2014-04-15 04:55:57 +08:00
|
|
|
|
2015-05-20 07:39:58 +08:00
|
|
|
case params.require(:type)
|
|
|
|
when "profile_background"
|
2014-06-28 02:48:39 +08:00
|
|
|
user.user_profile.clear_profile_background
|
2015-05-20 07:39:58 +08:00
|
|
|
when "card_background"
|
2014-10-21 00:11:36 +08:00
|
|
|
user.user_profile.clear_card_background
|
2014-06-28 02:48:39 +08:00
|
|
|
else
|
2015-05-20 07:39:58 +08:00
|
|
|
raise Discourse::InvalidParameters.new(:type)
|
2014-06-28 02:48:39 +08:00
|
|
|
end
|
2014-04-15 04:55:57 +08:00
|
|
|
|
2015-05-20 07:39:58 +08:00
|
|
|
render json: success_json
|
2014-03-01 04:12:51 +08:00
|
|
|
end
|
2014-04-15 04:55:57 +08:00
|
|
|
|
2014-02-14 00:42:35 +08:00
|
|
|
def destroy
|
|
|
|
@user = fetch_user_from_params
|
|
|
|
guardian.ensure_can_delete_user!(@user)
|
2014-04-15 04:55:57 +08:00
|
|
|
|
2014-10-20 22:59:06 +08:00
|
|
|
UserDestroyer.new(current_user).destroy(@user, delete_posts: true, context: params[:context])
|
2014-04-15 04:55:57 +08:00
|
|
|
|
2014-02-14 00:42:35 +08:00
|
|
|
render json: success_json
|
|
|
|
end
|
|
|
|
|
2019-03-27 17:41:50 +08:00
|
|
|
def notification_level
|
|
|
|
user = fetch_user_from_params
|
2019-02-27 21:49:07 +08:00
|
|
|
|
2019-03-27 17:41:50 +08:00
|
|
|
if params[:notification_level] == "ignore"
|
2020-06-19 01:41:27 +08:00
|
|
|
@error_message = "ignore_error"
|
2020-01-02 21:04:08 +08:00
|
|
|
guardian.ensure_can_ignore_user!(user)
|
2019-03-27 17:41:50 +08:00
|
|
|
MutedUser.where(user: current_user, muted_user: user).delete_all
|
2019-03-29 18:14:53 +08:00
|
|
|
ignored_user = IgnoredUser.find_by(user: current_user, ignored_user: user)
|
|
|
|
if ignored_user.present?
|
|
|
|
ignored_user.update(expiring_at: DateTime.parse(params[:expiring_at]))
|
|
|
|
else
|
|
|
|
IgnoredUser.create!(user: current_user, ignored_user: user, expiring_at: Time.parse(params[:expiring_at]))
|
|
|
|
end
|
2019-03-27 17:41:50 +08:00
|
|
|
elsif params[:notification_level] == "mute"
|
2020-06-19 01:41:27 +08:00
|
|
|
@error_message = "mute_error"
|
2020-01-02 21:04:08 +08:00
|
|
|
guardian.ensure_can_mute_user!(user)
|
2019-03-27 17:41:50 +08:00
|
|
|
IgnoredUser.where(user: current_user, ignored_user: user).delete_all
|
|
|
|
MutedUser.find_or_create_by!(user: current_user, muted_user: user)
|
|
|
|
elsif params[:notification_level] == "normal"
|
|
|
|
MutedUser.where(user: current_user, muted_user: user).delete_all
|
|
|
|
IgnoredUser.where(user: current_user, ignored_user: user).delete_all
|
|
|
|
end
|
2019-02-27 21:49:07 +08:00
|
|
|
|
|
|
|
render json: success_json
|
2022-03-03 13:57:52 +08:00
|
|
|
rescue Discourse::InvalidAccess
|
2020-06-19 01:41:27 +08:00
|
|
|
render_json_error(I18n.t("notification_level.#{@error_message}"))
|
2019-02-27 21:49:07 +08:00
|
|
|
end
|
|
|
|
|
2014-07-11 15:32:29 +08:00
|
|
|
def read_faq
|
2015-09-05 04:56:02 +08:00
|
|
|
if user = current_user
|
2014-07-11 15:32:29 +08:00
|
|
|
user.user_stat.read_faq = 1.second.ago
|
|
|
|
user.user_stat.save
|
|
|
|
end
|
|
|
|
|
|
|
|
render json: success_json
|
|
|
|
end
|
|
|
|
|
2021-11-26 04:44:15 +08:00
|
|
|
def recent_searches
|
|
|
|
if !SiteSetting.log_search_queries
|
|
|
|
return render json: failed_json.merge(
|
|
|
|
error: I18n.t("user_activity.no_log_search_queries")
|
|
|
|
), status: 403
|
|
|
|
end
|
|
|
|
|
|
|
|
query = SearchLog.where(user_id: current_user.id)
|
|
|
|
|
|
|
|
if current_user.user_option.oldest_search_log_date
|
|
|
|
query = query
|
|
|
|
.where("created_at > ?", current_user.user_option.oldest_search_log_date)
|
|
|
|
end
|
|
|
|
|
|
|
|
results = query.group(:term)
|
|
|
|
.order("max(created_at) DESC")
|
|
|
|
.limit(MAX_RECENT_SEARCHES)
|
|
|
|
.pluck(:term)
|
|
|
|
|
|
|
|
render json: success_json.merge(recent_searches: results)
|
|
|
|
end
|
|
|
|
|
|
|
|
def reset_recent_searches
|
|
|
|
current_user.user_option.update!(oldest_search_log_date: 1.second.ago)
|
|
|
|
render json: success_json
|
|
|
|
end
|
|
|
|
|
2015-01-06 02:49:32 +08:00
|
|
|
def staff_info
|
2015-11-28 03:02:24 +08:00
|
|
|
@user = fetch_user_from_params(include_inactive: true)
|
2015-01-06 02:49:32 +08:00
|
|
|
guardian.ensure_can_see_staff_info!(@user)
|
|
|
|
|
|
|
|
result = {}
|
|
|
|
|
2020-03-16 20:52:08 +08:00
|
|
|
%W{
|
|
|
|
number_of_deleted_posts number_of_flagged_posts number_of_flags_given
|
|
|
|
number_of_suspensions warnings_received_count number_of_rejected_posts
|
|
|
|
}.each do |info|
|
2019-05-07 09:27:05 +08:00
|
|
|
result[info] = @user.public_send(info)
|
2015-01-06 02:49:32 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
render json: result
|
|
|
|
end
|
|
|
|
|
2017-04-05 01:59:22 +08: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
|
|
|
|
|
2019-07-24 01:17:44 +08:00
|
|
|
respond_to do |format|
|
|
|
|
format.json { render json: success_json }
|
|
|
|
format.html { render layout: 'no_ember' }
|
|
|
|
end
|
2017-04-05 01:59:22 +08:00
|
|
|
end
|
|
|
|
|
2019-06-27 07:58:06 +08:00
|
|
|
def list_second_factors
|
2021-02-08 18:04:33 +08:00
|
|
|
raise Discourse::NotFound if SiteSetting.enable_discourse_connect || !SiteSetting.enable_local_logins
|
2018-02-20 14:44:51 +08:00
|
|
|
|
2019-06-27 07:58:06 +08:00
|
|
|
unless params[:password].empty?
|
|
|
|
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
|
2020-01-10 08:45:56 +08:00
|
|
|
confirm_secure_session
|
2018-02-20 14:44:51 +08:00
|
|
|
end
|
|
|
|
|
2020-01-10 08:45:56 +08:00
|
|
|
if secure_session_confirmed?
|
2019-10-02 10:08:41 +08:00
|
|
|
totp_second_factors = current_user.totps
|
|
|
|
.select(:id, :name, :last_used, :created_at, :method)
|
|
|
|
.where(enabled: true).order(:created_at)
|
|
|
|
|
|
|
|
security_keys = current_user.security_keys.where(factor_type: UserSecurityKey.factor_types[:second_factor]).order(:created_at)
|
|
|
|
|
2019-06-27 07:58:06 +08:00
|
|
|
render json: success_json.merge(
|
2019-10-02 10:08:41 +08:00
|
|
|
totps: totp_second_factors,
|
|
|
|
security_keys: security_keys
|
2019-06-27 07:58:06 +08:00
|
|
|
)
|
|
|
|
else
|
|
|
|
render json: success_json.merge(
|
|
|
|
password_required: true
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def create_second_factor_backup
|
|
|
|
backup_codes = current_user.generate_backup_codes
|
|
|
|
|
|
|
|
render json: success_json.merge(
|
|
|
|
backup_codes: backup_codes
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def create_second_factor_totp
|
2019-12-17 08:33:51 +08:00
|
|
|
require 'rotp' if !defined? ROTP
|
2019-12-09 12:29:48 +08:00
|
|
|
totp_data = ROTP::Base32.random
|
2019-06-27 07:58:06 +08:00
|
|
|
secure_session["staged-totp-#{current_user.id}"] = totp_data
|
2022-01-04 12:31:46 +08:00
|
|
|
qrcode_png = RQRCode::QRCode.new(current_user.totp_provisioning_uri(totp_data)).as_png(
|
|
|
|
border_modules: 1,
|
|
|
|
size: 240
|
2018-02-20 14:44:51 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
render json: success_json.merge(
|
2019-06-27 07:58:06 +08:00
|
|
|
key: totp_data.scan(/.{4}/).join(" "),
|
2022-01-04 12:31:46 +08:00
|
|
|
qr: qrcode_png.to_data_url
|
2019-06-27 07:58:06 +08:00
|
|
|
)
|
2018-02-20 14:44:51 +08:00
|
|
|
end
|
|
|
|
|
2019-10-02 10:08:41 +08:00
|
|
|
def create_second_factor_security_key
|
2020-01-10 08:45:56 +08:00
|
|
|
challenge_session = Webauthn.stage_challenge(current_user, secure_session)
|
2019-10-02 10:08:41 +08:00
|
|
|
render json: success_json.merge(
|
2020-01-10 08:45:56 +08:00
|
|
|
challenge: challenge_session.challenge,
|
|
|
|
rp_id: challenge_session.rp_id,
|
|
|
|
rp_name: challenge_session.rp_name,
|
2020-03-09 07:22:08 +08:00
|
|
|
supported_algorithms: ::Webauthn::SUPPORTED_ALGORITHMS,
|
2019-10-02 10:08:41 +08:00
|
|
|
user_secure_id: current_user.create_or_fetch_secure_identifier,
|
|
|
|
existing_active_credential_ids: current_user.second_factor_security_key_credential_ids
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def register_second_factor_security_key
|
|
|
|
params.require(:name)
|
|
|
|
params.require(:attestation)
|
|
|
|
params.require(:clientData)
|
|
|
|
|
|
|
|
::Webauthn::SecurityKeyRegistrationService.new(
|
|
|
|
current_user,
|
|
|
|
params,
|
2020-01-10 08:45:56 +08:00
|
|
|
challenge: Webauthn.challenge(current_user, secure_session),
|
|
|
|
rp_id: Webauthn.rp_id(current_user, secure_session),
|
2019-10-02 10:08:41 +08:00
|
|
|
origin: Discourse.base_url
|
|
|
|
).register_second_factor_security_key
|
|
|
|
render json: success_json
|
|
|
|
rescue ::Webauthn::SecurityKeyError => err
|
2020-01-10 08:45:56 +08:00
|
|
|
render json: failed_json.merge(error: err.message)
|
2019-10-02 10:08:41 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def update_security_key
|
|
|
|
user_security_key = current_user.security_keys.find_by(id: params[:id].to_i)
|
|
|
|
raise Discourse::InvalidParameters unless user_security_key
|
|
|
|
|
|
|
|
if params[:name] && !params[:name].blank?
|
|
|
|
user_security_key.update!(name: params[:name])
|
|
|
|
end
|
|
|
|
if params[:disable] == "true"
|
|
|
|
user_security_key.update!(enabled: false)
|
|
|
|
end
|
|
|
|
|
|
|
|
render json: success_json
|
|
|
|
end
|
|
|
|
|
2019-06-27 07:58:06 +08:00
|
|
|
def enable_second_factor_totp
|
2020-03-06 12:37:40 +08:00
|
|
|
if params[:second_factor_token].blank?
|
|
|
|
return render json: failed_json.merge(error: I18n.t("login.missing_second_factor_code"))
|
|
|
|
end
|
|
|
|
if params[:name].blank?
|
|
|
|
return render json: failed_json.merge(error: I18n.t("login.missing_second_factor_name"))
|
|
|
|
end
|
2019-06-27 07:58:06 +08:00
|
|
|
auth_token = params[:second_factor_token]
|
|
|
|
|
|
|
|
totp_data = secure_session["staged-totp-#{current_user.id}"]
|
|
|
|
totp_object = current_user.get_totp_object(totp_data)
|
|
|
|
|
2021-02-04 07:03:30 +08:00
|
|
|
rate_limit_second_factor!(current_user)
|
2018-06-28 16:12:32 +08:00
|
|
|
|
2019-12-09 12:29:48 +08:00
|
|
|
authenticated = !auth_token.blank? && totp_object.verify(
|
|
|
|
auth_token,
|
|
|
|
drift_ahead: SecondFactorManager::TOTP_ALLOWED_DRIFT_SECONDS,
|
|
|
|
drift_behind: SecondFactorManager::TOTP_ALLOWED_DRIFT_SECONDS
|
|
|
|
)
|
2019-06-27 07:58:06 +08:00
|
|
|
unless authenticated
|
2018-06-28 16:12:32 +08:00
|
|
|
return render json: failed_json.merge(
|
2019-06-27 07:58:06 +08:00
|
|
|
error: I18n.t("login.invalid_second_factor_code")
|
|
|
|
)
|
2018-06-28 16:12:32 +08:00
|
|
|
end
|
2019-06-27 07:58:06 +08:00
|
|
|
current_user.create_totp(data: totp_data, name: params[:name], enabled: true)
|
|
|
|
render json: success_json
|
|
|
|
end
|
2018-06-28 16:12:32 +08:00
|
|
|
|
2019-06-27 07:58:06 +08:00
|
|
|
def disable_second_factor
|
|
|
|
# delete all second factors for a user
|
|
|
|
current_user.user_second_factors.destroy_all
|
2020-09-01 00:56:57 +08:00
|
|
|
current_user.security_keys.destroy_all
|
2018-06-28 16:12:32 +08:00
|
|
|
|
2019-06-27 07:58:06 +08:00
|
|
|
Jobs.enqueue(
|
|
|
|
:critical_user_email,
|
2022-02-05 07:43:53 +08:00
|
|
|
type: "account_second_factor_disabled",
|
2019-06-27 07:58:06 +08:00
|
|
|
user_id: current_user.id
|
2018-06-28 16:12:32 +08:00
|
|
|
)
|
2019-06-27 07:58:06 +08:00
|
|
|
|
|
|
|
render json: success_json
|
2018-06-28 16:12:32 +08:00
|
|
|
end
|
|
|
|
|
2018-02-20 14:44:51 +08:00
|
|
|
def update_second_factor
|
2019-02-27 17:37:33 +08:00
|
|
|
params.require(:second_factor_target)
|
|
|
|
update_second_factor_method = params[:second_factor_target].to_i
|
2018-02-20 14:44:51 +08:00
|
|
|
|
2019-02-27 17:37:33 +08:00
|
|
|
if update_second_factor_method == UserSecondFactor.methods[:totp]
|
2019-06-27 07:58:06 +08:00
|
|
|
params.require(:id)
|
|
|
|
second_factor_id = params[:id].to_i
|
|
|
|
user_second_factor = current_user.user_second_factors.totps.find_by(id: second_factor_id)
|
2019-02-27 17:37:33 +08:00
|
|
|
elsif update_second_factor_method == UserSecondFactor.methods[:backup_codes]
|
2018-06-28 16:12:32 +08:00
|
|
|
user_second_factor = current_user.user_second_factors.backup_codes
|
|
|
|
end
|
|
|
|
|
2018-02-20 14:44:51 +08:00
|
|
|
raise Discourse::InvalidParameters unless user_second_factor
|
|
|
|
|
2019-06-27 07:58:06 +08:00
|
|
|
if params[:name] && !params[:name].blank?
|
|
|
|
user_second_factor.update!(name: params[:name])
|
2018-02-20 14:44:51 +08:00
|
|
|
end
|
2019-06-27 07:58:06 +08:00
|
|
|
if params[:disable] == "true"
|
|
|
|
# Disabling backup codes deletes *all* backup codes
|
|
|
|
if update_second_factor_method == UserSecondFactor.methods[:backup_codes]
|
2018-06-28 16:12:32 +08:00
|
|
|
current_user.user_second_factors.where(method: UserSecondFactor.methods[:backup_codes]).destroy_all
|
2019-06-27 07:58:06 +08:00
|
|
|
else
|
|
|
|
user_second_factor.update!(enabled: false)
|
2018-06-28 16:12:32 +08:00
|
|
|
end
|
2019-06-27 07:58:06 +08:00
|
|
|
|
2018-02-20 14:44:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
render json: success_json
|
|
|
|
end
|
|
|
|
|
2019-06-27 07:58:06 +08:00
|
|
|
def second_factor_check_confirmed_password
|
2021-02-08 18:04:33 +08:00
|
|
|
raise Discourse::NotFound if SiteSetting.enable_discourse_connect || !SiteSetting.enable_local_logins
|
2019-06-27 07:58:06 +08:00
|
|
|
|
2020-01-10 08:45:56 +08:00
|
|
|
raise Discourse::InvalidAccess.new unless current_user && secure_session_confirmed?
|
2019-06-27 07:58:06 +08:00
|
|
|
end
|
|
|
|
|
2018-07-23 23:51:57 +08:00
|
|
|
def revoke_account
|
|
|
|
user = fetch_user_from_params
|
|
|
|
guardian.ensure_can_edit!(user)
|
|
|
|
provider_name = params.require(:provider_name)
|
|
|
|
|
|
|
|
# Using Discourse.authenticators rather than Discourse.enabled_authenticators so users can
|
|
|
|
# revoke permissions even if the admin has temporarily disabled that type of login
|
2018-08-10 12:53:55 +08:00
|
|
|
authenticator = Discourse.authenticators.find { |a| a.name == provider_name }
|
2018-07-31 23:18:50 +08:00
|
|
|
raise Discourse::NotFound if authenticator.nil? || !authenticator.can_revoke?
|
2018-07-23 23:51:57 +08:00
|
|
|
|
|
|
|
skip_remote = params.permit(:skip_remote)
|
|
|
|
|
|
|
|
# We're likely going to contact the remote auth provider, so hijack request
|
|
|
|
hijack do
|
2019-06-11 09:28:42 +08:00
|
|
|
DiscourseEvent.trigger(:before_auth_revoke, authenticator, user)
|
2018-07-23 23:51:57 +08:00
|
|
|
result = authenticator.revoke(user, skip_remote: skip_remote)
|
|
|
|
if result
|
2018-08-10 12:53:55 +08:00
|
|
|
render json: success_json
|
2018-07-23 23:51:57 +08:00
|
|
|
else
|
2018-08-10 12:53:55 +08:00
|
|
|
render json: {
|
2018-07-23 23:51:57 +08:00
|
|
|
success: false,
|
|
|
|
message: I18n.t("associated_accounts.revoke_failed", provider_name: provider_name)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-08-31 16:18:06 +08:00
|
|
|
def revoke_auth_token
|
|
|
|
user = fetch_user_from_params
|
|
|
|
guardian.ensure_can_edit!(user)
|
|
|
|
|
2018-10-12 07:40:48 +08:00
|
|
|
if params[:token_id]
|
|
|
|
token = UserAuthToken.find_by(id: params[:token_id], user_id: user.id)
|
|
|
|
# The user should not be able to revoke the auth token of current session.
|
2020-03-07 21:04:12 +08:00
|
|
|
raise Discourse::InvalidParameters.new(:token_id) if !token || guardian.auth_token == token.auth_token
|
2018-10-09 22:21:41 +08:00
|
|
|
UserAuthToken.where(id: params[:token_id], user_id: user.id).each(&:destroy!)
|
2019-07-19 18:54:58 +08:00
|
|
|
|
|
|
|
MessageBus.publish "/file-change", ["refresh"], user_ids: [user.id]
|
2018-10-09 22:21:41 +08:00
|
|
|
else
|
|
|
|
UserAuthToken.where(user_id: user.id).each(&:destroy!)
|
|
|
|
end
|
2018-08-31 16:18:06 +08:00
|
|
|
|
2018-10-15 11:42:45 +08:00
|
|
|
render json: success_json
|
2018-08-31 16:18:06 +08:00
|
|
|
end
|
|
|
|
|
2019-12-10 03:15:47 +08:00
|
|
|
def feature_topic
|
|
|
|
user = fetch_user_from_params
|
|
|
|
topic = Topic.find(params[:topic_id].to_i)
|
|
|
|
|
2020-03-10 00:41:07 +08:00
|
|
|
if !guardian.can_feature_topic?(user, topic)
|
|
|
|
return render_json_error(I18n.t('activerecord.errors.models.user_profile.attributes.featured_topic_id.invalid'), 403)
|
|
|
|
end
|
|
|
|
|
2019-12-10 03:15:47 +08:00
|
|
|
user.user_profile.update(featured_topic_id: topic.id)
|
|
|
|
render json: success_json
|
|
|
|
end
|
|
|
|
|
|
|
|
def clear_featured_topic
|
|
|
|
user = fetch_user_from_params
|
|
|
|
guardian.ensure_can_edit!(user)
|
|
|
|
user.user_profile.update(featured_topic_id: nil)
|
|
|
|
render json: success_json
|
|
|
|
end
|
|
|
|
|
2020-03-12 13:20:56 +08:00
|
|
|
def bookmarks
|
|
|
|
user = fetch_user_from_params
|
2020-03-19 08:59:32 +08:00
|
|
|
guardian.ensure_can_edit!(user)
|
2020-03-12 13:20:56 +08:00
|
|
|
|
2020-03-18 06:22:41 +08:00
|
|
|
respond_to do |format|
|
|
|
|
format.json do
|
2020-04-01 12:09:07 +08:00
|
|
|
bookmark_list = UserBookmarkList.new(user: user, guardian: guardian, params: params)
|
|
|
|
bookmark_list.load
|
2020-03-18 06:22:41 +08:00
|
|
|
|
2020-04-01 12:09:07 +08:00
|
|
|
if bookmark_list.bookmarks.empty?
|
2020-03-18 06:22:41 +08:00
|
|
|
render json: {
|
2021-12-09 01:46:54 +08:00
|
|
|
bookmarks: []
|
2020-03-18 06:22:41 +08:00
|
|
|
}
|
|
|
|
else
|
2020-04-01 12:09:07 +08:00
|
|
|
page = params[:page].to_i + 1
|
|
|
|
bookmark_list.more_bookmarks_url = "#{Discourse.base_path}/u/#{params[:username]}/bookmarks.json?page=#{page}"
|
|
|
|
render_serialized(bookmark_list, UserBookmarkListSerializer)
|
2020-03-18 06:22:41 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
format.ics do
|
2021-09-15 08:16:54 +08:00
|
|
|
@bookmark_reminders = Bookmark.with_reminders
|
|
|
|
.where(user_id: user.id)
|
|
|
|
.includes(:topic)
|
|
|
|
.order(:reminder_at)
|
2020-03-18 06:22:41 +08:00
|
|
|
end
|
2020-03-12 13:20:56 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-10-16 13:53:31 +08:00
|
|
|
private
|
|
|
|
|
2021-07-31 01:50:47 +08:00
|
|
|
def clean_custom_field_values(field)
|
|
|
|
field_values = params[:user_fields][field.id.to_s]
|
|
|
|
|
|
|
|
return field_values if field_values.nil? || field_values.empty?
|
|
|
|
|
|
|
|
if field.field_type == "dropdown"
|
|
|
|
field.user_field_options.find_by_value(field_values)&.value
|
|
|
|
elsif field.field_type == "multiselect"
|
2021-09-03 21:26:57 +08:00
|
|
|
field_values = Array.wrap(field_values)
|
2021-07-31 01:50:47 +08:00
|
|
|
bad_values = field_values - field.user_field_options.map(&:value)
|
|
|
|
field_values - bad_values
|
|
|
|
else
|
|
|
|
field_values
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-01-15 18:27:12 +08:00
|
|
|
def password_reset_find_user(token, committing_change:)
|
2021-11-25 15:34:39 +08:00
|
|
|
@user = if committing_change
|
|
|
|
EmailToken.confirm(token, scope: EmailToken.scopes[:password_reset])
|
|
|
|
else
|
|
|
|
EmailToken.confirmable(token, scope: EmailToken.scopes[:password_reset])&.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
|
2020-01-15 18:27:12 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
@error = I18n.t('password_reset.no_token') if !@user
|
|
|
|
end
|
|
|
|
|
2013-11-13 06:37:38 +08:00
|
|
|
def respond_to_suspicious_request
|
|
|
|
if suspicious?(params)
|
2014-09-23 07:06:19 +08:00
|
|
|
render json: {
|
2015-05-20 07:39:58 +08:00
|
|
|
success: true,
|
2014-09-23 07:06:19 +08:00
|
|
|
active: false,
|
2013-11-13 06:37:38 +08:00
|
|
|
message: I18n.t("login.activate_email", email: params[:email])
|
2018-06-07 13:28:18 +08:00
|
|
|
}
|
2013-11-13 06:37:38 +08:00
|
|
|
end
|
2018-06-07 13:28:18 +08:00
|
|
|
end
|
2013-11-13 06:37:38 +08:00
|
|
|
|
|
|
|
def suspicious?(params)
|
2014-09-23 07:06:19 +08:00
|
|
|
return false if current_user && is_api? && current_user.admin?
|
2013-11-13 06:37:38 +08:00
|
|
|
honeypot_or_challenge_fails?(params) || SiteSetting.invite_only?
|
|
|
|
end
|
|
|
|
|
|
|
|
def honeypot_or_challenge_fails?(params)
|
2017-08-31 12:06:56 +08:00
|
|
|
return false if is_api?
|
2017-09-29 19:09:48 +08:00
|
|
|
params[:password_confirmation] != honeypot_value ||
|
2017-09-27 02:00:10 +08:00
|
|
|
params[:challenge] != challenge_value.try(:reverse)
|
2018-06-07 13:28:18 +08:00
|
|
|
end
|
2016-08-05 23:57:13 +08:00
|
|
|
|
|
|
|
def user_params
|
2017-09-27 02:00:10 +08:00
|
|
|
permitted = [
|
2018-06-07 13:28:18 +08:00
|
|
|
:name,
|
2016-08-05 23:57:13 +08:00
|
|
|
:email,
|
2017-08-31 12:06:56 +08:00
|
|
|
:password,
|
2016-08-05 23:57:13 +08:00
|
|
|
:username,
|
2018-06-07 13:28:18 +08:00
|
|
|
:title,
|
2017-08-31 12:06:56 +08:00
|
|
|
:date_of_birth,
|
|
|
|
:muted_usernames,
|
2020-07-21 05:23:49 +08:00
|
|
|
:allowed_pm_usernames,
|
2018-07-12 12:18:21 +08:00
|
|
|
:theme_ids,
|
2016-08-05 23:57:13 +08:00
|
|
|
:locale,
|
2017-09-28 20:59:53 +08:00
|
|
|
:bio_raw,
|
|
|
|
:location,
|
|
|
|
:website,
|
2016-08-05 23:57:13 +08:00
|
|
|
:dismissed_banner_key,
|
2019-04-29 11:58:52 +08:00
|
|
|
:profile_background_upload_url,
|
2019-10-29 01:46:27 +08:00
|
|
|
:card_background_upload_url,
|
2019-12-10 03:15:47 +08:00
|
|
|
:primary_group_id,
|
2021-07-08 15:46:21 +08:00
|
|
|
:flair_group_id,
|
2019-12-10 03:15:47 +08:00
|
|
|
:featured_topic_id
|
2018-06-07 13:28:18 +08:00
|
|
|
]
|
|
|
|
|
2019-10-11 16:57:55 +08:00
|
|
|
editable_custom_fields = User.editable_user_custom_fields(by_staff: current_user.try(:staff?))
|
|
|
|
permitted << { custom_fields: editable_custom_fields } unless editable_custom_fields.blank?
|
2016-08-05 23:57:13 +08:00
|
|
|
permitted.concat UserUpdater::OPTION_ATTR
|
2017-09-29 19:09:48 +08:00
|
|
|
permitted.concat UserUpdater::CATEGORY_IDS.keys.map { |k| { k => [] } }
|
2016-08-05 23:57:13 +08:00
|
|
|
permitted.concat UserUpdater::TAG_NAMES.keys
|
2021-01-21 00:31:52 +08:00
|
|
|
permitted << UserUpdater::NOTIFICATION_SCHEDULE_ATTRS
|
2018-06-07 13:28:18 +08:00
|
|
|
|
2016-08-05 23:57:13 +08:00
|
|
|
result = params
|
2018-07-12 12:18:21 +08:00
|
|
|
.permit(permitted, theme_ids: [])
|
2016-08-05 23:57:13 +08:00
|
|
|
.reverse_merge(
|
|
|
|
ip_address: request.remote_ip,
|
2019-06-07 23:51:58 +08:00
|
|
|
registration_ip_address: request.remote_ip
|
2018-06-07 13:28:18 +08:00
|
|
|
)
|
2016-08-05 23:57:13 +08:00
|
|
|
|
2017-08-29 03:36:46 +08:00
|
|
|
if !UsernameCheckerService.is_developer?(result['email']) &&
|
|
|
|
is_api? &&
|
|
|
|
current_user.present? &&
|
2016-08-05 23:57:13 +08:00
|
|
|
current_user.admin?
|
|
|
|
|
2018-02-17 08:11:41 +08:00
|
|
|
result.merge!(params.permit(:active, :staged, :approved))
|
|
|
|
end
|
|
|
|
|
|
|
|
modify_user_params(result)
|
2016-02-07 03:49:39 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
# Plugins can use this to modify user parameters
|
|
|
|
def modify_user_params(attrs)
|
2018-06-07 13:28:18 +08:00
|
|
|
attrs
|
2013-11-13 06:37:38 +08:00
|
|
|
end
|
2014-09-27 02:48:34 +08:00
|
|
|
|
|
|
|
def fail_with(key)
|
|
|
|
render json: { success: false, message: I18n.t(key) }
|
|
|
|
end
|
|
|
|
|
2015-09-14 15:51:17 +08: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
|
2018-06-07 13:28:18 +08:00
|
|
|
end
|
2015-09-14 15:51:17 +08:00
|
|
|
|
2019-09-14 02:11:27 +08:00
|
|
|
def clashing_with_existing_route?(username)
|
|
|
|
normalized_username = User.normalize_username(username)
|
|
|
|
http_verbs = %w[GET POST PUT DELETE PATCH]
|
|
|
|
allowed_actions = %w[show update destroy]
|
|
|
|
|
|
|
|
http_verbs.any? do |verb|
|
|
|
|
begin
|
|
|
|
path = Rails.application.routes.recognize_path("/u/#{normalized_username}", method: verb)
|
|
|
|
allowed_actions.exclude?(path[:action])
|
|
|
|
rescue ActionController::RoutingError
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2019-10-02 10:08:41 +08:00
|
|
|
|
2020-01-10 08:45:56 +08:00
|
|
|
def confirm_secure_session
|
|
|
|
secure_session["confirmed-password-#{current_user.id}"] = "true"
|
2019-10-02 10:08:41 +08:00
|
|
|
end
|
|
|
|
|
2020-01-10 08:45:56 +08:00
|
|
|
def secure_session_confirmed?
|
|
|
|
secure_session["confirmed-password-#{current_user.id}"] == "true"
|
2019-10-02 10:08:41 +08:00
|
|
|
end
|
2020-07-08 05:57:42 +08:00
|
|
|
|
|
|
|
def summary_cache_key(user)
|
|
|
|
"user_summary:#{user.id}:#{current_user ? current_user.id : 0}"
|
|
|
|
end
|
2020-10-03 15:35:26 +08:00
|
|
|
|
|
|
|
def render_invite_error(message)
|
|
|
|
render json: {
|
|
|
|
invites: [],
|
|
|
|
can_see_invite_details: false,
|
|
|
|
error: message
|
|
|
|
}
|
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|