1 class UsersController < ApplicationController
8 skip_before_action :verify_authenticity_token, :only => [:auth_success]
9 before_action :authorize_web
10 before_action :set_locale
11 before_action :check_database_readable
15 before_action :check_database_writable, :only => [:new, :go_public]
16 before_action :require_cookies, :only => [:new]
18 allow_thirdparty_images :only => :show
19 allow_social_login :only => :new
22 @user = User.find_by(:display_name => params[:display_name])
25 (@user.visible? || current_user&.administrator?)
26 @title = @user.display_name
28 render_unknown_user params[:display_name]
34 @referer = safe_referer(params[:referer])
36 parse_oauth_referer @referer
39 # The user is logged in already, so don't show them the signup
40 # page, instead send them to the home page
41 redirect_to @referer || { :controller => "site", :action => "index" }
42 elsif params.key?(:auth_provider) && params.key?(:auth_uid)
43 @email_hmac = params[:email_hmac]
45 self.current_user = User.new(:email => params[:email],
46 :display_name => params[:nickname],
47 :auth_provider => params[:auth_provider],
48 :auth_uid => params[:auth_uid])
50 if current_user.valid? || current_user.errors[:email].empty?
51 flash.now[:notice] = render_to_string :partial => "auth_association"
53 flash.now[:warning] = t ".duplicate_social_email"
58 self.current_user = User.new
63 self.current_user = User.new(user_params)
65 if check_signup_allowed(current_user.email)
66 if current_user.auth_uid.present?
67 # We are creating an account with external authentication and
68 # no password was specified so create a random one
69 current_user.pass_crypt = SecureRandom.base64(16)
70 current_user.pass_crypt_confirmation = current_user.pass_crypt
73 if current_user.invalid?
74 # Something is wrong with a new user, so rerender the form
75 render :action => "new"
77 # Save the user record
78 if save_new_user params[:email_hmac]
79 SIGNUP_IP_LIMITER&.update(request.remote_ip)
80 SIGNUP_EMAIL_LIMITER&.update(canonical_email(current_user.email))
82 flash[:matomo_goal] = Settings.matomo["goals"]["signup"] if defined?(Settings.matomo)
84 referer = welcome_path(welcome_options(params[:referer]))
86 if current_user.status == "active"
87 successful_login(current_user, referer)
89 session[:pending_user] = current_user.id
90 UserMailer.signup_confirm(current_user, current_user.generate_token_for(:new_user), referer).deliver_later
91 redirect_to :controller => :confirmations, :action => :confirm, :display_name => current_user.display_name
94 render :action => "new", :referer => params[:referer]
101 current_user.data_public = true
103 flash[:notice] = t ".flash success"
104 redirect_to edit_account_path
108 # omniauth success callback
110 referer = request.env["omniauth.params"]["referer"]
111 auth_info = request.env["omniauth.auth"]
113 provider = auth_info[:provider]
114 uid = auth_info[:uid]
115 name = auth_info[:info][:name]
116 email = auth_info[:info][:email]
118 email_verified = case provider
120 uid.match(%r{https://www.google.com/accounts/o8/id?(.*)}) ||
121 uid.match(%r{https://me.yahoo.com/(.*)})
122 when "google", "facebook", "microsoft", "github", "wikipedia"
128 if settings = session.delete(:new_user_settings)
129 current_user.auth_provider = provider
130 current_user.auth_uid = uid
132 update_user(current_user, settings)
136 session[:user_errors] = current_user.errors.as_json
138 redirect_to edit_account_path
140 user = User.find_by(:auth_provider => provider, :auth_uid => uid)
142 if user.nil? && provider == "google"
143 openid_url = auth_info[:extra][:id_info]["openid_id"]
144 user = User.find_by(:auth_provider => "openid", :auth_uid => openid_url) if openid_url
145 user&.update(:auth_provider => provider, :auth_uid => uid)
151 unconfirmed_login(user, referer)
152 when "active", "confirmed"
153 successful_login(user, referer)
155 failed_login({ :partial => "sessions/suspended_flash" }, user.display_name, referer)
157 failed_login(t("sessions.new.auth failure"), user.display_name, referer)
160 email_hmac = UsersController.message_hmac(email) if email_verified && email
161 redirect_to :action => "new", :nickname => name, :email => email, :email_hmac => email_hmac,
162 :auth_provider => provider, :auth_uid => uid, :referer => referer
168 # omniauth failure callback
170 flash[:error] = t(params[:message], :scope => "users.auth_failure", :default => t(".unknown_error"))
172 origin = safe_referer(params[:origin]) if params[:origin]
174 redirect_to origin || login_url
177 def self.message_hmac(text)
178 sha256 = Digest::SHA256.new
179 sha256 << Rails.application.key_generator.generate_key("openstreetmap/email_address")
181 Base64.urlsafe_encode64(sha256.digest)
186 def save_new_user(email_hmac)
187 current_user.data_public = true
188 current_user.description = "" if current_user.description.nil?
189 current_user.creation_address = request.remote_ip
190 current_user.languages = http_accept_language.user_preferred_languages
191 current_user.terms_agreed = Time.now.utc
192 current_user.tou_agreed = Time.now.utc
193 current_user.terms_seen = true
195 if current_user.auth_uid.blank?
196 current_user.auth_provider = nil
197 current_user.auth_uid = nil
198 elsif email_hmac && ActiveSupport::SecurityUtils.secure_compare(email_hmac, UsersController.message_hmac(current_user.email))
199 current_user.activate
205 def welcome_options(referer = nil)
206 uri = URI(referer) if referer.present?
208 return { "oauth_return_url" => uri&.to_s } if uri&.path == oauth_authorization_path
211 %r{map=(.*)/(.*)/(.*)}.match(uri.fragment) do |m|
212 editor = Rack::Utils.parse_query(uri.query).slice("editor")
213 return { "zoom" => m[1], "lat" => m[2], "lon" => m[3] }.merge(editor)
221 # return permitted user parameters
223 params.require(:user).permit(:email, :display_name,
224 :auth_provider, :auth_uid,
225 :pass_crypt, :pass_crypt_confirmation,
231 def check_signup_allowed(email = nil)
232 domain = if email.nil?
235 email.split("@").last
238 mx_servers = if domain.nil?
241 domain_mx_servers(domain)
244 return true if Acl.allow_account_creation(request.remote_ip, :domain => domain, :mx => mx_servers)
246 blocked = Acl.no_account_creation(request.remote_ip, :domain => domain, :mx => mx_servers)
248 blocked ||= SIGNUP_IP_LIMITER && !SIGNUP_IP_LIMITER.allow?(request.remote_ip)
250 blocked ||= email && SIGNUP_EMAIL_LIMITER && !SIGNUP_EMAIL_LIMITER.allow?(canonical_email(email))
253 logger.info "Blocked signup from #{request.remote_ip} for #{email}"
255 render :action => "blocked"