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]
17 before_action :lookup_user_by_name, :only => [:set_status, :destroy]
19 allow_thirdparty_images :only => :show
20 allow_social_login :only => :new
23 @user = User.find_by(:display_name => params[:display_name])
26 (@user.visible? || current_user&.administrator?)
27 @title = @user.display_name
29 render_unknown_user params[:display_name]
35 @referer = safe_referer(params[:referer])
37 parse_oauth_referer @referer
40 # The user is logged in already, so don't show them the signup
41 # page, instead send them to the home page
42 redirect_to @referer || { :controller => "site", :action => "index" }
43 elsif params.key?(:auth_provider) && params.key?(:auth_uid)
44 @email_hmac = params[:email_hmac]
46 self.current_user = User.new(:email => params[:email],
47 :display_name => params[:nickname],
48 :auth_provider => params[:auth_provider],
49 :auth_uid => params[:auth_uid])
51 if current_user.valid? || current_user.errors[:email].empty?
52 flash.now[:notice] = render_to_string :partial => "auth_association"
54 flash.now[:warning] = t ".duplicate_social_email"
59 self.current_user = User.new
64 self.current_user = User.new(user_params)
66 if check_signup_allowed(current_user.email)
67 if current_user.auth_uid.present?
68 # We are creating an account with external authentication and
69 # no password was specified so create a random one
70 current_user.pass_crypt = SecureRandom.base64(16)
71 current_user.pass_crypt_confirmation = current_user.pass_crypt
74 if current_user.invalid?
75 # Something is wrong with a new user, so rerender the form
76 render :action => "new"
78 # Save the user record
79 if save_new_user params[:email_hmac]
80 SIGNUP_IP_LIMITER&.update(request.remote_ip)
81 SIGNUP_EMAIL_LIMITER&.update(canonical_email(current_user.email))
83 flash[:matomo_goal] = Settings.matomo["goals"]["signup"] if defined?(Settings.matomo)
85 referer = welcome_path(welcome_options(params[:referer]))
87 if current_user.status == "active"
88 successful_login(current_user, referer)
90 session[:pending_user] = current_user.id
91 UserMailer.signup_confirm(current_user, current_user.generate_token_for(:new_user), referer).deliver_later
92 redirect_to :controller => :confirmations, :action => :confirm, :display_name => current_user.display_name
95 render :action => "new", :referer => params[:referer]
102 # destroy a user, marking them as deleted and removing personal data
105 redirect_to user_path(:display_name => params[:display_name])
109 current_user.data_public = true
111 flash[:notice] = t ".flash success"
112 redirect_to edit_account_path
116 # sets a user's status
118 @user.activate! if params[:event] == "activate"
119 @user.confirm! if params[:event] == "confirm"
120 @user.unconfirm! if params[:event] == "unconfirm"
121 @user.hide! if params[:event] == "hide"
122 @user.unhide! if params[:event] == "unhide"
123 @user.unsuspend! if params[:event] == "unsuspend"
124 redirect_to user_path(:display_name => params[:display_name])
128 # omniauth success callback
130 referer = request.env["omniauth.params"]["referer"]
131 auth_info = request.env["omniauth.auth"]
133 provider = auth_info[:provider]
134 uid = auth_info[:uid]
135 name = auth_info[:info][:name]
136 email = auth_info[:info][:email]
138 email_verified = case provider
140 uid.match(%r{https://www.google.com/accounts/o8/id?(.*)}) ||
141 uid.match(%r{https://me.yahoo.com/(.*)})
142 when "google", "facebook", "microsoft", "github", "wikipedia"
148 if settings = session.delete(:new_user_settings)
149 current_user.auth_provider = provider
150 current_user.auth_uid = uid
152 update_user(current_user, settings)
156 session[:user_errors] = current_user.errors.as_json
158 redirect_to edit_account_path
160 user = User.find_by(:auth_provider => provider, :auth_uid => uid)
162 if user.nil? && provider == "google"
163 openid_url = auth_info[:extra][:id_info]["openid_id"]
164 user = User.find_by(:auth_provider => "openid", :auth_uid => openid_url) if openid_url
165 user&.update(:auth_provider => provider, :auth_uid => uid)
171 unconfirmed_login(user, referer)
172 when "active", "confirmed"
173 successful_login(user, referer)
175 failed_login({ :partial => "sessions/suspended_flash" }, user.display_name, referer)
177 failed_login(t("sessions.new.auth failure"), user.display_name, referer)
180 email_hmac = UsersController.message_hmac(email) if email_verified && email
181 redirect_to :action => "new", :nickname => name, :email => email, :email_hmac => email_hmac,
182 :auth_provider => provider, :auth_uid => uid, :referer => referer
188 # omniauth failure callback
190 flash[:error] = t(params[:message], :scope => "users.auth_failure", :default => t(".unknown_error"))
192 origin = safe_referer(params[:origin]) if params[:origin]
194 redirect_to origin || login_url
197 def self.message_hmac(text)
198 sha256 = Digest::SHA256.new
199 sha256 << Rails.application.key_generator.generate_key("openstreetmap/email_address")
201 Base64.urlsafe_encode64(sha256.digest)
206 def save_new_user(email_hmac)
207 current_user.data_public = true
208 current_user.description = "" if current_user.description.nil?
209 current_user.creation_address = request.remote_ip
210 current_user.languages = http_accept_language.user_preferred_languages
211 current_user.terms_agreed = Time.now.utc
212 current_user.tou_agreed = Time.now.utc
213 current_user.terms_seen = true
215 if current_user.auth_uid.blank?
216 current_user.auth_provider = nil
217 current_user.auth_uid = nil
218 elsif email_hmac && ActiveSupport::SecurityUtils.secure_compare(email_hmac, UsersController.message_hmac(current_user.email))
219 current_user.activate
225 def welcome_options(referer = nil)
226 uri = URI(referer) if referer.present?
228 return { "oauth_return_url" => uri&.to_s } if uri&.path == oauth_authorization_path
231 %r{map=(.*)/(.*)/(.*)}.match(uri.fragment) do |m|
232 editor = Rack::Utils.parse_query(uri.query).slice("editor")
233 return { "zoom" => m[1], "lat" => m[2], "lon" => m[3] }.merge(editor)
241 # ensure that there is a "user" instance variable
242 def lookup_user_by_name
243 @user = User.find_by(:display_name => params[:display_name])
244 rescue ActiveRecord::RecordNotFound
245 redirect_to :action => "view", :display_name => params[:display_name] unless @user
249 # return permitted user parameters
251 params.require(:user).permit(:email, :display_name,
252 :auth_provider, :auth_uid,
253 :pass_crypt, :pass_crypt_confirmation,
259 def check_signup_allowed(email = nil)
260 domain = if email.nil?
263 email.split("@").last
266 mx_servers = if domain.nil?
269 domain_mx_servers(domain)
272 return true if Acl.allow_account_creation(request.remote_ip, :domain => domain, :mx => mx_servers)
274 blocked = Acl.no_account_creation(request.remote_ip, :domain => domain, :mx => mx_servers)
276 blocked ||= SIGNUP_IP_LIMITER && !SIGNUP_IP_LIMITER.allow?(request.remote_ip)
278 blocked ||= email && SIGNUP_EMAIL_LIMITER && !SIGNUP_EMAIL_LIMITER.allow?(canonical_email(email))
281 logger.info "Blocked signup from #{request.remote_ip} for #{email}"
283 render :action => "blocked"