1 class UsersController < ApplicationController
5 include PaginationMethods
9 skip_before_action :verify_authenticity_token, :only => [:auth_success]
10 before_action :disable_terms_redirect, :only => [:terms, :save]
11 before_action :authorize_web
12 before_action :set_locale
13 before_action :check_database_readable
17 before_action :check_database_writable, :only => [:new, :go_public]
18 before_action :require_cookies, :only => [:new]
19 before_action :lookup_user_by_name, :only => [:set_status, :destroy]
20 before_action :allow_thirdparty_images, :only => [:show]
23 # display a list of users matching specified criteria
26 ids = params[:user].keys.collect(&:to_i)
28 User.where(:id => ids).update_all(:status => "confirmed") if params[:confirm]
29 User.where(:id => ids).update_all(:status => "deleted") if params[:hide]
31 redirect_to url_for(:status => params[:status], :ip => params[:ip], :page => params[:page])
33 @params = params.permit(:status, :ip, :before, :after)
36 users = users.where(:status => @params[:status]) if @params[:status]
37 users = users.where(:creation_ip => @params[:ip]) if @params[:ip]
39 @users_count = users.count
40 @users, @newer_users_id, @older_users_id = get_page_items(users, :limit => 50)
45 @user = User.find_by(:display_name => params[:display_name])
48 (@user.visible? || current_user&.administrator?)
49 @title = @user.display_name
51 render_unknown_user params[:display_name]
57 @referer = safe_referer(params[:referer])
59 parse_oauth_referer @referer
61 append_content_security_policy_directives(
62 :form_action => %w[accounts.google.com *.facebook.com login.live.com github.com meta.wikimedia.org]
66 # The user is logged in already, so don't show them the signup
67 # page, instead send them to the home page
68 redirect_to @referer || { :controller => "site", :action => "index" }
69 elsif params.key?(:auth_provider) && params.key?(:auth_uid)
70 @email_hmac = params[:email_hmac]
72 self.current_user = User.new(:email => params[:email],
73 :display_name => params[:nickname],
74 :auth_provider => params[:auth_provider],
75 :auth_uid => params[:auth_uid])
77 if current_user.valid? || current_user.errors[:email].empty?
78 flash.now[:notice] = render_to_string :partial => "auth_association"
80 flash.now[:warning] = t ".duplicate_social_email"
85 self.current_user = User.new
90 self.current_user = User.new(user_params)
92 if check_signup_allowed(current_user.email)
93 if current_user.auth_uid.present?
94 # We are creating an account with external authentication and
95 # no password was specified so create a random one
96 current_user.pass_crypt = SecureRandom.base64(16)
97 current_user.pass_crypt_confirmation = current_user.pass_crypt
100 if current_user.invalid?
101 # Something is wrong with a new user, so rerender the form
102 render :action => "new"
103 elsif current_user.auth_provider.present?
104 # Verify external authenticator before moving on
105 session[:new_user] = current_user.slice("email", "display_name", "pass_crypt", "pass_crypt_confirmation")
106 redirect_to auth_url(current_user.auth_provider, current_user.auth_uid), :status => :temporary_redirect
108 # Save the user record
109 session[:new_user] = current_user.slice("email", "display_name", "pass_crypt", "pass_crypt_confirmation")
110 save_new_user params[:email_hmac], params[:referer]
116 # destroy a user, marking them as deleted and removing personal data
119 redirect_to user_path(:display_name => params[:display_name])
123 @legale = params[:legale] || OSM.ip_to_country(request.remote_ip) || Settings.default_legale
124 @text = OSM.legal_text_for_country(@legale)
127 render :partial => "terms"
131 if current_user&.terms_agreed?
132 # Already agreed to terms, so just show settings
133 redirect_to edit_account_path
134 elsif current_user.nil?
135 redirect_to login_path(:referer => request.fullpath)
141 @title = t "users.new.title"
143 if params[:decline] || !(params[:read_tou] && params[:read_ct])
145 current_user.terms_seen = true
147 flash[:notice] = { :partial => "users/terms_declined_flash" } if current_user.save
149 referer = safe_referer(params[:referer]) if params[:referer]
151 redirect_to referer || edit_account_path
152 elsif params[:decline]
153 redirect_to t("users.terms.declined"), :allow_other_host => true
155 redirect_to :action => :terms
158 unless current_user.terms_agreed?
159 current_user.consider_pd = params[:user][:consider_pd]
160 current_user.tou_agreed = Time.now.utc
161 current_user.terms_agreed = Time.now.utc
162 current_user.terms_seen = true
164 flash[:notice] = t "users.new.terms accepted" if current_user.save
167 referer = safe_referer(params[:referer]) if params[:referer]
169 redirect_to referer || edit_account_path
174 current_user.data_public = true
176 flash[:notice] = t ".flash success"
177 redirect_to edit_account_path
181 # sets a user's status
183 @user.activate! if params[:event] == "activate"
184 @user.confirm! if params[:event] == "confirm"
185 @user.unconfirm! if params[:event] == "unconfirm"
186 @user.hide! if params[:event] == "hide"
187 @user.unhide! if params[:event] == "unhide"
188 @user.unsuspend! if params[:event] == "unsuspend"
189 redirect_to user_path(:display_name => params[:display_name])
193 # omniauth success callback
195 referer = request.env["omniauth.params"]["referer"]
196 auth_info = request.env["omniauth.auth"]
198 provider = auth_info[:provider]
199 uid = auth_info[:uid]
200 name = auth_info[:info][:name]
201 email = auth_info[:info][:email]
203 email_verified = case provider
205 uid.match(%r{https://www.google.com/accounts/o8/id?(.*)}) ||
206 uid.match(%r{https://me.yahoo.com/(.*)})
207 when "google", "facebook", "microsoft", "github", "wikipedia"
213 if settings = session.delete(:new_user_settings)
214 current_user.auth_provider = provider
215 current_user.auth_uid = uid
217 update_user(current_user, settings)
221 session[:user_errors] = current_user.errors.as_json
223 redirect_to edit_account_path
224 elsif session[:new_user]
225 session[:new_user]["auth_provider"] = provider
226 session[:new_user]["auth_uid"] = uid
228 email_hmac = UsersController.message_hmac(email) if email_verified && email
229 save_new_user email_hmac, referer
231 user = User.find_by(:auth_provider => provider, :auth_uid => uid)
233 if user.nil? && provider == "google"
234 openid_url = auth_info[:extra][:id_info]["openid_id"]
235 user = User.find_by(:auth_provider => "openid", :auth_uid => openid_url) if openid_url
236 user&.update(:auth_provider => provider, :auth_uid => uid)
242 unconfirmed_login(user, referer)
243 when "active", "confirmed"
244 successful_login(user, referer)
246 failed_login({ :partial => "sessions/suspended_flash" }, user.display_name, referer)
248 failed_login(t("sessions.new.auth failure"), user.display_name, referer)
251 email_hmac = UsersController.message_hmac(email) if email_verified && email
252 redirect_to :action => "new", :nickname => name, :email => email, :email_hmac => email_hmac,
253 :auth_provider => provider, :auth_uid => uid
259 # omniauth failure callback
261 flash[:error] = t(params[:message], :scope => "users.auth_failure", :default => t(".unknown_error"))
263 origin = safe_referer(params[:origin]) if params[:origin]
265 redirect_to origin || login_url
268 def self.message_hmac(text)
269 sha256 = Digest::SHA256.new
270 sha256 << Rails.application.key_generator.generate_key("openstreetmap/email_address")
272 Base64.urlsafe_encode64(sha256.digest)
277 def save_new_user(email_hmac, referer = nil)
278 new_user = session.delete(:new_user)
279 self.current_user = User.new(new_user)
280 if check_signup_allowed(current_user.email)
281 current_user.data_public = true
282 current_user.description = "" if current_user.description.nil?
283 current_user.creation_ip = request.remote_ip
284 current_user.languages = http_accept_language.user_preferred_languages
285 current_user.terms_agreed = Time.now.utc
286 current_user.tou_agreed = Time.now.utc
287 current_user.terms_seen = true
289 if current_user.auth_uid.blank?
290 current_user.auth_provider = nil
291 current_user.auth_uid = nil
292 elsif email_hmac && ActiveSupport::SecurityUtils.secure_compare(email_hmac, UsersController.message_hmac(current_user.email))
293 current_user.activate
297 SIGNUP_IP_LIMITER&.update(request.remote_ip)
298 SIGNUP_EMAIL_LIMITER&.update(canonical_email(current_user.email))
300 flash[:matomo_goal] = Settings.matomo["goals"]["signup"] if defined?(Settings.matomo)
302 referer = welcome_path(welcome_options(referer))
304 if current_user.status == "active"
305 successful_login(current_user, referer)
307 session[:pending_user] = current_user.id
308 UserMailer.signup_confirm(current_user, current_user.generate_token_for(:new_user), referer).deliver_later
309 redirect_to :controller => :confirmations, :action => :confirm, :display_name => current_user.display_name
312 render :action => "new", :referer => params[:referer]
317 def welcome_options(referer = nil)
318 uri = URI(referer) if referer.present?
320 return { "oauth_return_url" => uri&.to_s } if uri&.path == oauth_authorization_path
323 %r{map=(.*)/(.*)/(.*)}.match(uri.fragment) do |m|
324 editor = Rack::Utils.parse_query(uri.query).slice("editor")
325 return { "zoom" => m[1], "lat" => m[2], "lon" => m[3] }.merge(editor)
333 # ensure that there is a "user" instance variable
334 def lookup_user_by_name
335 @user = User.find_by(:display_name => params[:display_name])
336 rescue ActiveRecord::RecordNotFound
337 redirect_to :action => "view", :display_name => params[:display_name] unless @user
341 # return permitted user parameters
343 params.require(:user).permit(:email, :display_name,
344 :auth_provider, :auth_uid,
345 :pass_crypt, :pass_crypt_confirmation,
351 def check_signup_allowed(email = nil)
352 domain = if email.nil?
355 email.split("@").last
358 mx_servers = if domain.nil?
361 domain_mx_servers(domain)
364 return true if Acl.allow_account_creation(request.remote_ip, :domain => domain, :mx => mx_servers)
366 blocked = Acl.no_account_creation(request.remote_ip, :domain => domain, :mx => mx_servers)
368 blocked ||= SIGNUP_IP_LIMITER && !SIGNUP_IP_LIMITER.allow?(request.remote_ip)
370 blocked ||= email && SIGNUP_EMAIL_LIMITER && !SIGNUP_EMAIL_LIMITER.allow?(canonical_email(email))
373 logger.info "Blocked signup from #{request.remote_ip} for #{email}"
375 render :action => "blocked"