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])
24 if @user && (@user.visible? || current_user&.administrator?)
25 @title = @user.display_name
27 @heatmap_data = Rails.cache.fetch("heatmap_data_with_ids_user_#{@user.id}", :expires_in => 1.day) do
28 one_year_ago = 1.year.ago.beginning_of_day
29 today = Time.zone.now.end_of_day
32 .where(:user_id => @user.id)
33 .where(:created_at => one_year_ago..today)
34 .where(:num_changes => 1..)
35 .group("date_trunc('day', created_at)")
36 .select("date_trunc('day', created_at) AS date, SUM(num_changes) AS total_changes, MAX(id) AS max_id")
40 :date => changeset.date.to_date.to_s,
41 :total_changes => changeset.total_changes.to_i,
42 :max_id => changeset.max_id
47 @changes_count = @heatmap_data.sum { |entry| entry[:total_changes] }
49 render_unknown_user params[:display_name]
55 @referer = safe_referer(params[:referer])
57 parse_oauth_referer @referer
60 # The user is logged in already, so don't show them the signup
61 # page, instead send them to the home page
62 redirect_to @referer || { :controller => "site", :action => "index" }
63 elsif params.key?(:auth_provider) && params.key?(:auth_uid)
64 @email_hmac = params[:email_hmac]
66 self.current_user = User.new(:email => params[:email],
67 :display_name => params[:nickname],
68 :auth_provider => params[:auth_provider],
69 :auth_uid => params[:auth_uid])
71 if current_user.valid? || current_user.errors[:email].empty?
72 flash.now[:notice] = render_to_string :partial => "auth_association"
74 flash.now[:warning] = t ".duplicate_social_email"
79 self.current_user = User.new
84 self.current_user = User.new(user_params)
86 if check_signup_allowed(current_user.email)
87 if current_user.auth_uid.present?
88 # We are creating an account with external authentication and
89 # no password was specified so create a random one
90 current_user.pass_crypt = SecureRandom.base64(16)
91 current_user.pass_crypt_confirmation = current_user.pass_crypt
94 if current_user.invalid?
95 # Something is wrong with a new user, so rerender the form
96 render :action => "new"
98 # Save the user record
99 if save_new_user params[:email_hmac]
100 SIGNUP_IP_LIMITER&.update(request.remote_ip)
101 SIGNUP_EMAIL_LIMITER&.update(canonical_email(current_user.email))
103 flash[:matomo_goal] = Settings.matomo["goals"]["signup"] if defined?(Settings.matomo)
105 referer = welcome_path(welcome_options(params[:referer]))
107 if current_user.status == "active"
108 successful_login(current_user, referer)
110 session[:pending_user] = current_user.id
111 UserMailer.signup_confirm(current_user, current_user.generate_token_for(:new_user), referer).deliver_later
112 redirect_to :controller => :confirmations, :action => :confirm, :display_name => current_user.display_name
115 render :action => "new", :referer => params[:referer]
122 current_user.data_public = true
124 flash[:notice] = t ".flash success"
125 redirect_to account_path
129 # omniauth success callback
131 referer = request.env["omniauth.params"]["referer"]
132 auth_info = request.env["omniauth.auth"]
134 provider = auth_info[:provider]
135 uid = auth_info[:uid]
136 name = auth_info[:info][:name]
137 email = auth_info[:info][:email]
139 email_verified = case provider
141 uid.match(%r{https://www.google.com/accounts/o8/id?(.*)}) ||
142 uid.match(%r{https://me.yahoo.com/(.*)})
143 when "google", "facebook", "microsoft", "github", "wikipedia"
149 if settings = session.delete(:new_user_settings)
150 current_user.auth_provider = provider
151 current_user.auth_uid = uid
153 update_user(current_user, settings)
157 session[:user_errors] = current_user.errors.as_json
159 redirect_to account_path
161 user = User.find_by(:auth_provider => provider, :auth_uid => uid)
163 if user.nil? && provider == "google"
164 openid_url = auth_info[:extra][:id_info]["openid_id"]
165 user = User.find_by(:auth_provider => "openid", :auth_uid => openid_url) if openid_url
166 user&.update(:auth_provider => provider, :auth_uid => uid)
172 unconfirmed_login(user, referer)
173 when "active", "confirmed"
174 successful_login(user, referer)
176 failed_login({ :partial => "sessions/suspended_flash" }, user.display_name, referer)
178 failed_login(t("sessions.new.auth failure"), user.display_name, referer)
181 email_hmac = UsersController.message_hmac(email) if email_verified && email
182 redirect_to :action => "new", :nickname => name, :email => email, :email_hmac => email_hmac,
183 :auth_provider => provider, :auth_uid => uid, :referer => referer
189 # omniauth failure callback
191 flash[:error] = t(params[:message], :scope => "users.auth_failure", :default => t(".unknown_error"))
193 origin = safe_referer(params[:origin]) if params[:origin]
195 redirect_to origin || login_url
198 def self.message_hmac(text)
199 sha256 = Digest::SHA256.new
200 sha256 << Rails.application.key_generator.generate_key("openstreetmap/email_address")
202 Base64.urlsafe_encode64(sha256.digest)
207 def save_new_user(email_hmac)
208 current_user.data_public = true
209 current_user.description = "" if current_user.description.nil?
210 current_user.creation_address = request.remote_ip
211 current_user.languages = http_accept_language.user_preferred_languages
212 current_user.terms_agreed = Time.now.utc
213 current_user.tou_agreed = Time.now.utc
214 current_user.terms_seen = true
216 if current_user.auth_uid.blank?
217 current_user.auth_provider = nil
218 current_user.auth_uid = nil
219 elsif email_hmac && ActiveSupport::SecurityUtils.secure_compare(email_hmac, UsersController.message_hmac(current_user.email))
220 current_user.activate
226 def welcome_options(referer = nil)
227 uri = URI(referer) if referer.present?
229 return { "oauth_return_url" => uri&.to_s } if uri&.path == oauth_authorization_path
232 %r{map=(.*)/(.*)/(.*)}.match(uri.fragment) do |m|
233 editor = Rack::Utils.parse_query(uri.query).slice("editor")
234 return { "zoom" => m[1], "lat" => m[2], "lon" => m[3] }.merge(editor)
242 # return permitted user parameters
244 params.expect(:user => [:email, :display_name,
245 :auth_provider, :auth_uid,
246 :pass_crypt, :pass_crypt_confirmation])
251 def check_signup_allowed(email = nil)
252 domain = if email.nil?
255 email.split("@").last
258 mx_servers = if domain.nil?
261 domain_mx_servers(domain)
264 return true if Acl.allow_account_creation(request.remote_ip, :domain => domain, :mx => mx_servers)
266 blocked = Acl.no_account_creation(request.remote_ip, :domain => domain, :mx => mx_servers)
268 blocked ||= SIGNUP_IP_LIMITER && !SIGNUP_IP_LIMITER.allow?(request.remote_ip)
270 blocked ||= email && SIGNUP_EMAIL_LIMITER && !SIGNUP_EMAIL_LIMITER.allow?(canonical_email(email))
273 logger.info "Blocked signup from #{request.remote_ip} for #{email}"
275 render :action => "blocked"