1 class UsersController < ApplicationController
8 skip_before_action :verify_authenticity_token, :only => [:auth_success]
9 before_action :disable_terms_redirect, :only => [:terms, :save]
10 before_action :authorize_web
11 before_action :set_locale
12 before_action :check_database_readable
16 before_action :check_database_writable, :only => [:new, :go_public]
17 before_action :require_cookies, :only => [:new]
18 before_action :lookup_user_by_name, :only => [:set_status, :destroy]
20 allow_thirdparty_images :only => :show
21 allow_social_login :only => :new
24 @user = User.find_by(:display_name => params[:display_name])
27 (@user.visible? || current_user&.administrator?)
28 @title = @user.display_name
30 render_unknown_user params[:display_name]
36 @referer = safe_referer(params[:referer])
38 parse_oauth_referer @referer
41 # The user is logged in already, so don't show them the signup
42 # page, instead send them to the home page
43 redirect_to @referer || { :controller => "site", :action => "index" }
44 elsif params.key?(:auth_provider) && params.key?(:auth_uid)
45 @email_hmac = params[:email_hmac]
47 self.current_user = User.new(:email => params[:email],
48 :display_name => params[:nickname],
49 :auth_provider => params[:auth_provider],
50 :auth_uid => params[:auth_uid])
52 if current_user.valid? || current_user.errors[:email].empty?
53 flash.now[:notice] = render_to_string :partial => "auth_association"
55 flash.now[:warning] = t ".duplicate_social_email"
60 self.current_user = User.new
65 self.current_user = User.new(user_params)
67 if check_signup_allowed(current_user.email)
68 if current_user.auth_uid.present?
69 # We are creating an account with external authentication and
70 # no password was specified so create a random one
71 current_user.pass_crypt = SecureRandom.base64(16)
72 current_user.pass_crypt_confirmation = current_user.pass_crypt
75 if current_user.invalid?
76 # Something is wrong with a new user, so rerender the form
77 render :action => "new"
79 # Save the user record
80 save_new_user params[:email_hmac], params[:referer]
86 # destroy a user, marking them as deleted and removing personal data
89 redirect_to user_path(:display_name => params[:display_name])
93 @legale = params[:legale] || OSM.ip_to_country(request.remote_ip) || Settings.default_legale
94 @text = OSM.legal_text_for_country(@legale)
97 render :partial => "terms"
101 if current_user&.terms_agreed?
102 # Already agreed to terms, so just show settings
103 redirect_to edit_account_path
104 elsif current_user.nil?
105 redirect_to login_path(:referer => request.fullpath)
111 @title = t "users.new.title"
113 if params[:decline] || !(params[:read_tou] && params[:read_ct])
115 current_user.terms_seen = true
117 flash[:notice] = { :partial => "users/terms_declined_flash" } if current_user.save
119 referer = safe_referer(params[:referer]) if params[:referer]
121 redirect_to referer || edit_account_path
122 elsif params[:decline]
123 redirect_to t("users.terms.declined"), :allow_other_host => true
125 redirect_to :action => :terms
128 unless current_user.terms_agreed?
129 current_user.consider_pd = params[:user][:consider_pd]
130 current_user.tou_agreed = Time.now.utc
131 current_user.terms_agreed = Time.now.utc
132 current_user.terms_seen = true
134 flash[:notice] = t "users.new.terms accepted" if current_user.save
137 referer = safe_referer(params[:referer]) if params[:referer]
139 redirect_to referer || edit_account_path
144 current_user.data_public = true
146 flash[:notice] = t ".flash success"
147 redirect_to edit_account_path
151 # sets a user's status
153 @user.activate! if params[:event] == "activate"
154 @user.confirm! if params[:event] == "confirm"
155 @user.unconfirm! if params[:event] == "unconfirm"
156 @user.hide! if params[:event] == "hide"
157 @user.unhide! if params[:event] == "unhide"
158 @user.unsuspend! if params[:event] == "unsuspend"
159 redirect_to user_path(:display_name => params[:display_name])
163 # omniauth success callback
165 referer = request.env["omniauth.params"]["referer"]
166 auth_info = request.env["omniauth.auth"]
168 provider = auth_info[:provider]
169 uid = auth_info[:uid]
170 name = auth_info[:info][:name]
171 email = auth_info[:info][:email]
173 email_verified = case provider
175 uid.match(%r{https://www.google.com/accounts/o8/id?(.*)}) ||
176 uid.match(%r{https://me.yahoo.com/(.*)})
177 when "google", "facebook", "microsoft", "github", "wikipedia"
183 if settings = session.delete(:new_user_settings)
184 current_user.auth_provider = provider
185 current_user.auth_uid = uid
187 update_user(current_user, settings)
191 session[:user_errors] = current_user.errors.as_json
193 redirect_to edit_account_path
195 user = User.find_by(:auth_provider => provider, :auth_uid => uid)
197 if user.nil? && provider == "google"
198 openid_url = auth_info[:extra][:id_info]["openid_id"]
199 user = User.find_by(:auth_provider => "openid", :auth_uid => openid_url) if openid_url
200 user&.update(:auth_provider => provider, :auth_uid => uid)
206 unconfirmed_login(user, referer)
207 when "active", "confirmed"
208 successful_login(user, referer)
210 failed_login({ :partial => "sessions/suspended_flash" }, user.display_name, referer)
212 failed_login(t("sessions.new.auth failure"), user.display_name, referer)
215 email_hmac = UsersController.message_hmac(email) if email_verified && email
216 redirect_to :action => "new", :nickname => name, :email => email, :email_hmac => email_hmac,
217 :auth_provider => provider, :auth_uid => uid, :referer => referer
223 # omniauth failure callback
225 flash[:error] = t(params[:message], :scope => "users.auth_failure", :default => t(".unknown_error"))
227 origin = safe_referer(params[:origin]) if params[:origin]
229 redirect_to origin || login_url
232 def self.message_hmac(text)
233 sha256 = Digest::SHA256.new
234 sha256 << Rails.application.key_generator.generate_key("openstreetmap/email_address")
236 Base64.urlsafe_encode64(sha256.digest)
241 def save_new_user(email_hmac, referer = nil)
242 current_user.data_public = true
243 current_user.description = "" if current_user.description.nil?
244 current_user.creation_address = request.remote_ip
245 current_user.languages = http_accept_language.user_preferred_languages
246 current_user.terms_agreed = Time.now.utc
247 current_user.tou_agreed = Time.now.utc
248 current_user.terms_seen = true
250 if current_user.auth_uid.blank?
251 current_user.auth_provider = nil
252 current_user.auth_uid = nil
253 elsif email_hmac && ActiveSupport::SecurityUtils.secure_compare(email_hmac, UsersController.message_hmac(current_user.email))
254 current_user.activate
258 SIGNUP_IP_LIMITER&.update(request.remote_ip)
259 SIGNUP_EMAIL_LIMITER&.update(canonical_email(current_user.email))
261 flash[:matomo_goal] = Settings.matomo["goals"]["signup"] if defined?(Settings.matomo)
263 referer = welcome_path(welcome_options(referer))
265 if current_user.status == "active"
266 successful_login(current_user, referer)
268 session[:pending_user] = current_user.id
269 UserMailer.signup_confirm(current_user, current_user.generate_token_for(:new_user), referer).deliver_later
270 redirect_to :controller => :confirmations, :action => :confirm, :display_name => current_user.display_name
273 render :action => "new", :referer => params[:referer]
277 def welcome_options(referer = nil)
278 uri = URI(referer) if referer.present?
280 return { "oauth_return_url" => uri&.to_s } if uri&.path == oauth_authorization_path
283 %r{map=(.*)/(.*)/(.*)}.match(uri.fragment) do |m|
284 editor = Rack::Utils.parse_query(uri.query).slice("editor")
285 return { "zoom" => m[1], "lat" => m[2], "lon" => m[3] }.merge(editor)
293 # ensure that there is a "user" instance variable
294 def lookup_user_by_name
295 @user = User.find_by(:display_name => params[:display_name])
296 rescue ActiveRecord::RecordNotFound
297 redirect_to :action => "view", :display_name => params[:display_name] unless @user
301 # return permitted user parameters
303 params.require(:user).permit(:email, :display_name,
304 :auth_provider, :auth_uid,
305 :pass_crypt, :pass_crypt_confirmation,
311 def check_signup_allowed(email = nil)
312 domain = if email.nil?
315 email.split("@").last
318 mx_servers = if domain.nil?
321 domain_mx_servers(domain)
324 return true if Acl.allow_account_creation(request.remote_ip, :domain => domain, :mx => mx_servers)
326 blocked = Acl.no_account_creation(request.remote_ip, :domain => domain, :mx => mx_servers)
328 blocked ||= SIGNUP_IP_LIMITER && !SIGNUP_IP_LIMITER.allow?(request.remote_ip)
330 blocked ||= email && SIGNUP_EMAIL_LIMITER && !SIGNUP_EMAIL_LIMITER.allow?(canonical_email(email))
333 logger.info "Blocked signup from #{request.remote_ip} for #{email}"
335 render :action => "blocked"