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]
21 allow_thirdparty_images :only => :show
22 allow_social_login :only => :new
25 # display a list of users matching specified criteria
28 ids = params[:user].keys.collect(&:to_i)
30 User.where(:id => ids).update_all(:status => "confirmed") if params[:confirm]
31 User.where(:id => ids).update_all(:status => "deleted") if params[:hide]
33 redirect_to url_for(:status => params[:status], :ip => params[:ip], :page => params[:page])
35 @params = params.permit(:status, :ip, :before, :after)
38 users = users.where(:status => @params[:status]) if @params[:status]
39 users = users.where(:creation_ip => @params[:ip]) if @params[:ip]
41 @users_count = users.count
42 @users, @newer_users_id, @older_users_id = get_page_items(users, :limit => 50)
47 @user = User.find_by(:display_name => params[:display_name])
50 (@user.visible? || current_user&.administrator?)
51 @title = @user.display_name
53 render_unknown_user params[:display_name]
59 @referer = safe_referer(params[:referer])
61 parse_oauth_referer @referer
64 # The user is logged in already, so don't show them the signup
65 # page, instead send them to the home page
66 redirect_to @referer || { :controller => "site", :action => "index" }
67 elsif params.key?(:auth_provider) && params.key?(:auth_uid)
68 @email_hmac = params[:email_hmac]
70 self.current_user = User.new(:email => params[:email],
71 :display_name => params[:nickname],
72 :auth_provider => params[:auth_provider],
73 :auth_uid => params[:auth_uid])
75 if current_user.valid? || current_user.errors[:email].empty?
76 flash.now[:notice] = render_to_string :partial => "auth_association"
78 flash.now[:warning] = t ".duplicate_social_email"
83 self.current_user = User.new
88 self.current_user = User.new(user_params)
90 if check_signup_allowed(current_user.email)
91 if current_user.auth_uid.present?
92 # We are creating an account with external authentication and
93 # no password was specified so create a random one
94 current_user.pass_crypt = SecureRandom.base64(16)
95 current_user.pass_crypt_confirmation = current_user.pass_crypt
98 if current_user.invalid?
99 # Something is wrong with a new user, so rerender the form
100 render :action => "new"
102 # Save the user record
103 save_new_user params[:email_hmac], params[:referer]
109 # destroy a user, marking them as deleted and removing personal data
112 redirect_to user_path(:display_name => params[:display_name])
116 @legale = params[:legale] || OSM.ip_to_country(request.remote_ip) || Settings.default_legale
117 @text = OSM.legal_text_for_country(@legale)
120 render :partial => "terms"
124 if current_user&.terms_agreed?
125 # Already agreed to terms, so just show settings
126 redirect_to edit_account_path
127 elsif current_user.nil?
128 redirect_to login_path(:referer => request.fullpath)
134 @title = t "users.new.title"
136 if params[:decline] || !(params[:read_tou] && params[:read_ct])
138 current_user.terms_seen = true
140 flash[:notice] = { :partial => "users/terms_declined_flash" } if current_user.save
142 referer = safe_referer(params[:referer]) if params[:referer]
144 redirect_to referer || edit_account_path
145 elsif params[:decline]
146 redirect_to t("users.terms.declined"), :allow_other_host => true
148 redirect_to :action => :terms
151 unless current_user.terms_agreed?
152 current_user.consider_pd = params[:user][:consider_pd]
153 current_user.tou_agreed = Time.now.utc
154 current_user.terms_agreed = Time.now.utc
155 current_user.terms_seen = true
157 flash[:notice] = t "users.new.terms accepted" if current_user.save
160 referer = safe_referer(params[:referer]) if params[:referer]
162 redirect_to referer || edit_account_path
167 current_user.data_public = true
169 flash[:notice] = t ".flash success"
170 redirect_to edit_account_path
174 # sets a user's status
176 @user.activate! if params[:event] == "activate"
177 @user.confirm! if params[:event] == "confirm"
178 @user.unconfirm! if params[:event] == "unconfirm"
179 @user.hide! if params[:event] == "hide"
180 @user.unhide! if params[:event] == "unhide"
181 @user.unsuspend! if params[:event] == "unsuspend"
182 redirect_to user_path(:display_name => params[:display_name])
186 # omniauth success callback
188 referer = request.env["omniauth.params"]["referer"]
189 auth_info = request.env["omniauth.auth"]
191 provider = auth_info[:provider]
192 uid = auth_info[:uid]
193 name = auth_info[:info][:name]
194 email = auth_info[:info][:email]
196 email_verified = case provider
198 uid.match(%r{https://www.google.com/accounts/o8/id?(.*)}) ||
199 uid.match(%r{https://me.yahoo.com/(.*)})
200 when "google", "facebook", "microsoft", "github", "wikipedia"
206 if settings = session.delete(:new_user_settings)
207 current_user.auth_provider = provider
208 current_user.auth_uid = uid
210 update_user(current_user, settings)
214 session[:user_errors] = current_user.errors.as_json
216 redirect_to edit_account_path
218 user = User.find_by(:auth_provider => provider, :auth_uid => uid)
220 if user.nil? && provider == "google"
221 openid_url = auth_info[:extra][:id_info]["openid_id"]
222 user = User.find_by(:auth_provider => "openid", :auth_uid => openid_url) if openid_url
223 user&.update(:auth_provider => provider, :auth_uid => uid)
229 unconfirmed_login(user, referer)
230 when "active", "confirmed"
231 successful_login(user, referer)
233 failed_login({ :partial => "sessions/suspended_flash" }, user.display_name, referer)
235 failed_login(t("sessions.new.auth failure"), user.display_name, referer)
238 email_hmac = UsersController.message_hmac(email) if email_verified && email
239 redirect_to :action => "new", :nickname => name, :email => email, :email_hmac => email_hmac,
240 :auth_provider => provider, :auth_uid => uid, :referer => referer
246 # omniauth failure callback
248 flash[:error] = t(params[:message], :scope => "users.auth_failure", :default => t(".unknown_error"))
250 origin = safe_referer(params[:origin]) if params[:origin]
252 redirect_to origin || login_url
255 def self.message_hmac(text)
256 sha256 = Digest::SHA256.new
257 sha256 << Rails.application.key_generator.generate_key("openstreetmap/email_address")
259 Base64.urlsafe_encode64(sha256.digest)
264 def save_new_user(email_hmac, referer = nil)
265 if check_signup_allowed(current_user.email)
266 current_user.data_public = true
267 current_user.description = "" if current_user.description.nil?
268 current_user.creation_ip = request.remote_ip
269 current_user.languages = http_accept_language.user_preferred_languages
270 current_user.terms_agreed = Time.now.utc
271 current_user.tou_agreed = Time.now.utc
272 current_user.terms_seen = true
274 if current_user.auth_uid.blank?
275 current_user.auth_provider = nil
276 current_user.auth_uid = nil
277 elsif email_hmac && ActiveSupport::SecurityUtils.secure_compare(email_hmac, UsersController.message_hmac(current_user.email))
278 current_user.activate
282 SIGNUP_IP_LIMITER&.update(request.remote_ip)
283 SIGNUP_EMAIL_LIMITER&.update(canonical_email(current_user.email))
285 flash[:matomo_goal] = Settings.matomo["goals"]["signup"] if defined?(Settings.matomo)
287 referer = welcome_path(welcome_options(referer))
289 if current_user.status == "active"
290 successful_login(current_user, referer)
292 session[:pending_user] = current_user.id
293 UserMailer.signup_confirm(current_user, current_user.generate_token_for(:new_user), referer).deliver_later
294 redirect_to :controller => :confirmations, :action => :confirm, :display_name => current_user.display_name
297 render :action => "new", :referer => params[:referer]
302 def welcome_options(referer = nil)
303 uri = URI(referer) if referer.present?
305 return { "oauth_return_url" => uri&.to_s } if uri&.path == oauth_authorization_path
308 %r{map=(.*)/(.*)/(.*)}.match(uri.fragment) do |m|
309 editor = Rack::Utils.parse_query(uri.query).slice("editor")
310 return { "zoom" => m[1], "lat" => m[2], "lon" => m[3] }.merge(editor)
318 # ensure that there is a "user" instance variable
319 def lookup_user_by_name
320 @user = User.find_by(:display_name => params[:display_name])
321 rescue ActiveRecord::RecordNotFound
322 redirect_to :action => "view", :display_name => params[:display_name] unless @user
326 # return permitted user parameters
328 params.require(:user).permit(:email, :display_name,
329 :auth_provider, :auth_uid,
330 :pass_crypt, :pass_crypt_confirmation,
336 def check_signup_allowed(email = nil)
337 domain = if email.nil?
340 email.split("@").last
343 mx_servers = if domain.nil?
346 domain_mx_servers(domain)
349 return true if Acl.allow_account_creation(request.remote_ip, :domain => domain, :mx => mx_servers)
351 blocked = Acl.no_account_creation(request.remote_ip, :domain => domain, :mx => mx_servers)
353 blocked ||= SIGNUP_IP_LIMITER && !SIGNUP_IP_LIMITER.allow?(request.remote_ip)
355 blocked ||= email && SIGNUP_EMAIL_LIMITER && !SIGNUP_EMAIL_LIMITER.allow?(canonical_email(email))
358 logger.info "Blocked signup from #{request.remote_ip} for #{email}"
360 render :action => "blocked"