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_of_user_#{@user.id}", :expires_at => Time.zone.now.end_of_day) do
28 from = 1.year.ago.beginning_of_day
29 to = Time.zone.now.end_of_day
32 .where(:user_id => @user.id)
33 .where(:created_at => from..to)
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,
41 :total_changes => changeset.total_changes.to_i,
42 :max_id => changeset.max_id
47 :count => mapped.sum { |entry| entry[:total_changes] },
48 :data => mapped.index_by { |entry| entry[:date] },
54 render_unknown_user params[:display_name]
60 @referer = safe_referer(params[:referer])
62 parse_oauth_referer @referer
65 # The user is logged in already, so don't show them the signup
66 # page, instead send them to the home page
67 redirect_to @referer || { :controller => "site", :action => "index" }
68 elsif params.key?(:auth_provider) && params.key?(:auth_uid)
69 @email_hmac = params[:email_hmac]
71 self.current_user = User.new(:email => params[:email],
72 :display_name => params[:nickname],
73 :auth_provider => params[:auth_provider],
74 :auth_uid => params[:auth_uid])
76 if current_user.valid? || current_user.errors[:email].empty?
77 flash.now[:notice] = render_to_string :partial => "auth_association"
79 flash.now[:warning] = t ".duplicate_social_email"
84 self.current_user = User.new
89 self.current_user = User.new(user_params)
91 if check_signup_allowed(current_user.email)
92 if current_user.auth_uid.present?
93 # We are creating an account with external authentication and
94 # no password was specified so create a random one
95 current_user.pass_crypt = SecureRandom.base64(16)
96 current_user.pass_crypt_confirmation = current_user.pass_crypt
99 if current_user.invalid?
100 # Something is wrong with a new user, so rerender the form
101 render :action => "new"
103 # Save the user record
104 if save_new_user params[:email_hmac]
105 SIGNUP_IP_LIMITER&.update(request.remote_ip)
106 SIGNUP_EMAIL_LIMITER&.update(canonical_email(current_user.email))
108 flash[:matomo_goal] = Settings.matomo["goals"]["signup"] if defined?(Settings.matomo)
110 referer = welcome_path(welcome_options(params[:referer]))
112 if current_user.status == "active"
113 successful_login(current_user, referer)
115 session[:pending_user] = current_user.id
116 UserMailer.signup_confirm(current_user, current_user.generate_token_for(:new_user), referer).deliver_later
117 redirect_to :controller => :confirmations, :action => :confirm, :display_name => current_user.display_name
120 render :action => "new", :referer => params[:referer]
127 current_user.data_public = true
129 flash[:notice] = t ".flash success"
130 redirect_to account_path
134 # omniauth success callback
136 referer = request.env["omniauth.params"]["referer"]
137 auth_info = request.env["omniauth.auth"]
139 provider = auth_info[:provider]
140 uid = auth_info[:uid]
141 name = auth_info[:info][:name]
142 email = auth_info[:info][:email]
144 email_verified = case provider
146 uid.match(%r{https://www.google.com/accounts/o8/id?(.*)}) ||
147 uid.match(%r{https://me.yahoo.com/(.*)})
148 when "google", "facebook", "microsoft", "github", "wikipedia"
154 if settings = session.delete(:new_user_settings)
155 current_user.auth_provider = provider
156 current_user.auth_uid = uid
158 update_user(current_user, settings)
162 session[:user_errors] = current_user.errors.as_json
164 redirect_to account_path
166 user = User.find_by(:auth_provider => provider, :auth_uid => uid)
168 if user.nil? && provider == "google"
169 openid_url = auth_info[:extra][:id_info]["openid_id"]
170 user = User.find_by(:auth_provider => "openid", :auth_uid => openid_url) if openid_url
171 user&.update(:auth_provider => provider, :auth_uid => uid)
177 unconfirmed_login(user, referer)
178 when "active", "confirmed"
179 successful_login(user, referer)
181 failed_login({ :partial => "sessions/suspended_flash" }, user.display_name, referer)
183 failed_login(t("sessions.new.auth failure"), user.display_name, referer)
186 email_hmac = UsersController.message_hmac(email) if email_verified && email
187 redirect_to :action => "new", :nickname => name, :email => email, :email_hmac => email_hmac,
188 :auth_provider => provider, :auth_uid => uid, :referer => referer
194 # omniauth failure callback
196 flash[:error] = t(params[:message], :scope => "users.auth_failure", :default => t(".unknown_error"))
198 origin = safe_referer(params[:origin]) if params[:origin]
200 redirect_to origin || login_url
203 def self.message_hmac(text)
204 sha256 = Digest::SHA256.new
205 sha256 << Rails.application.key_generator.generate_key("openstreetmap/email_address")
207 Base64.urlsafe_encode64(sha256.digest)
212 def save_new_user(email_hmac)
213 current_user.data_public = true
214 current_user.description = "" if current_user.description.nil?
215 current_user.creation_address = request.remote_ip
216 current_user.languages = http_accept_language.user_preferred_languages
217 current_user.terms_agreed = Time.now.utc
218 current_user.tou_agreed = Time.now.utc
219 current_user.terms_seen = true
221 if current_user.auth_uid.blank?
222 current_user.auth_provider = nil
223 current_user.auth_uid = nil
224 elsif email_hmac && ActiveSupport::SecurityUtils.secure_compare(email_hmac, UsersController.message_hmac(current_user.email))
225 current_user.activate
231 def welcome_options(referer = nil)
232 uri = URI(referer) if referer.present?
234 return { "oauth_return_url" => uri&.to_s } if uri&.path == oauth_authorization_path
237 %r{map=(.*)/(.*)/(.*)}.match(uri.fragment) do |m|
238 editor = Rack::Utils.parse_query(uri.query).slice("editor")
239 return { "zoom" => m[1], "lat" => m[2], "lon" => m[3] }.merge(editor)
247 # return permitted user parameters
249 params.expect(:user => [:email, :display_name,
250 :auth_provider, :auth_uid,
251 :pass_crypt, :pass_crypt_confirmation])
256 def check_signup_allowed(email = nil)
257 domain = if email.nil?
260 email.split("@").last
263 mx_servers = if domain.nil?
266 domain_mx_servers(domain)
269 return true if Acl.allow_account_creation(request.remote_ip, :domain => domain, :mx => mx_servers)
271 blocked = Acl.no_account_creation(request.remote_ip, :domain => domain, :mx => mx_servers)
273 blocked ||= SIGNUP_IP_LIMITER && !SIGNUP_IP_LIMITER.allow?(request.remote_ip)
275 blocked ||= email && SIGNUP_EMAIL_LIMITER && !SIGNUP_EMAIL_LIMITER.allow?(canonical_email(email))
278 logger.info "Blocked signup from #{request.remote_ip} for #{email}"
280 render :action => "blocked"