1 class UsersController < ApplicationController
7 skip_before_action :verify_authenticity_token, :only => [:auth_success]
8 before_action :disable_terms_redirect, :only => [:terms, :save]
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]
17 before_action :lookup_user_by_name, :only => [:set_status, :destroy]
18 before_action :allow_thirdparty_images, :only => [:show]
21 # display a list of users matching specified criteria
24 ids = params[:user].keys.collect(&:to_i)
26 User.where(:id => ids).update_all(:status => "confirmed") if params[:confirm]
27 User.where(:id => ids).update_all(:status => "deleted") if params[:hide]
29 redirect_to url_for(:status => params[:status], :ip => params[:ip], :page => params[:page])
31 @params = params.permit(:status, :ip)
34 conditions[:status] = @params[:status] if @params[:status]
35 conditions[:creation_ip] = @params[:ip] if @params[:ip]
37 @user_pages, @users = paginate(:users,
38 :conditions => conditions,
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 = if params[:referer]
58 safe_referer(params[:referer])
63 append_content_security_policy_directives(
64 :form_action => %w[accounts.google.com *.facebook.com login.live.com github.com meta.wikimedia.org]
68 # The user is logged in already, so don't show them the signup
69 # page, instead send them to the home page
70 redirect_to @referer || { :controller => "site", :action => "index" }
71 elsif params.key?(:auth_provider) && params.key?(:auth_uid)
72 self.current_user = User.new(:email => params[:email],
73 :email_confirmation => params[:email],
74 :display_name => params[:nickname],
75 :auth_provider => params[:auth_provider],
76 :auth_uid => params[:auth_uid])
78 flash.now[:notice] = render_to_string :partial => "auth_association"
82 self.current_user = User.new
87 self.current_user = User.new(user_params)
89 if check_signup_allowed(current_user.email)
90 session[:referer] = safe_referer(params[:referer]) if params[:referer]
92 Rails.logger.info "create: #{session[:referer]}"
94 if current_user.auth_provider.present? && current_user.pass_crypt.empty?
95 # We are creating an account with external authentication and
96 # no password was specified so create a random one
97 current_user.pass_crypt = SecureRandom.base64(16)
98 current_user.pass_crypt_confirmation = current_user.pass_crypt
101 if current_user.invalid?
102 # Something is wrong with a new user, so rerender the form
103 render :action => "new"
104 elsif current_user.auth_provider.present?
105 # Verify external authenticator before moving on
106 session[:new_user] = current_user
107 redirect_to auth_url(current_user.auth_provider, current_user.auth_uid), :status => :temporary_redirect
109 # Save the user record
110 session[:new_user] = current_user
111 redirect_to :action => :terms
117 # destroy a user, marking them as deleted and removing personal data
120 redirect_to user_path(:display_name => params[:display_name])
124 @legale = params[:legale] || OSM.ip_to_country(request.remote_ip) || Settings.default_legale
125 @text = OSM.legal_text_for_country(@legale)
128 render :partial => "terms"
132 if current_user&.terms_agreed?
133 # Already agreed to terms, so just show settings
134 redirect_to edit_account_path
135 elsif current_user.nil? && session[:new_user].nil?
136 redirect_to login_path(:referer => request.fullpath)
142 @title = t "users.new.title"
144 if params[:decline] || !(params[:read_tou] && params[:read_ct])
146 current_user.terms_seen = true
148 flash[:notice] = { :partial => "users/terms_declined_flash" } if current_user.save
150 referer = safe_referer(params[:referer]) if params[:referer]
152 redirect_to referer || edit_account_path
153 elsif params[:decline]
154 redirect_to t("users.terms.declined"), :allow_other_host => true
156 redirect_to :action => :terms
159 unless current_user.terms_agreed?
160 current_user.consider_pd = params[:user][:consider_pd]
161 current_user.tou_agreed = Time.now.utc
162 current_user.terms_agreed = Time.now.utc
163 current_user.terms_seen = true
165 flash[:notice] = t "users.new.terms accepted" if current_user.save
168 referer = safe_referer(params[:referer]) if params[:referer]
170 redirect_to referer || edit_account_path
172 self.current_user = session.delete(:new_user)
174 if check_signup_allowed(current_user.email)
175 current_user.data_public = true
176 current_user.description = "" if current_user.description.nil?
177 current_user.creation_ip = request.remote_ip
178 current_user.languages = http_accept_language.user_preferred_languages
179 current_user.terms_agreed = Time.now.utc
180 current_user.tou_agreed = Time.now.utc
181 current_user.terms_seen = true
183 if current_user.auth_uid.blank?
184 current_user.auth_provider = nil
185 current_user.auth_uid = nil
189 flash[:matomo_goal] = Settings.matomo["goals"]["signup"] if defined?(Settings.matomo)
191 referer = welcome_path
194 uri = URI(session[:referer])
195 %r{map=(.*)/(.*)/(.*)}.match(uri.fragment) do |m|
196 editor = Rack::Utils.parse_query(uri.query).slice("editor")
197 referer = welcome_path({ "zoom" => m[1],
199 "lon" => m[3] }.merge(editor))
205 if current_user.status == "active"
206 session[:referer] = referer
207 successful_login(current_user)
209 session[:token] = current_user.tokens.create.token
210 UserMailer.signup_confirm(current_user, current_user.tokens.create(:referer => referer)).deliver_later
211 redirect_to :controller => :confirmations, :action => :confirm, :display_name => current_user.display_name
214 render :action => "new", :referer => params[:referer]
221 current_user.data_public = true
223 flash[:notice] = t ".flash success"
224 redirect_to edit_account_path
228 # sets a user's status
230 @user.activate! if params[:event] == "activate"
231 @user.confirm! if params[:event] == "confirm"
232 @user.unconfirm! if params[:event] == "unconfirm"
233 @user.hide! if params[:event] == "hide"
234 @user.unhide! if params[:event] == "unhide"
235 @user.unsuspend! if params[:event] == "unsuspend"
236 redirect_to user_path(:display_name => params[:display_name])
240 # omniauth success callback
242 auth_info = request.env["omniauth.auth"]
244 provider = auth_info[:provider]
245 uid = auth_info[:uid]
246 name = auth_info[:info][:name]
247 email = auth_info[:info][:email]
249 email_verified = case provider
251 uid.match(%r{https://www.google.com/accounts/o8/id?(.*)}) ||
252 uid.match(%r{https://me.yahoo.com/(.*)})
253 when "google", "facebook", "windowslive"
259 if settings = session.delete(:new_user_settings)
260 current_user.auth_provider = provider
261 current_user.auth_uid = uid
263 update_user(current_user, settings)
267 session[:user_errors] = current_user.errors.as_json
269 redirect_to edit_account_path
270 elsif session[:new_user]
271 session[:new_user].auth_provider = provider
272 session[:new_user].auth_uid = uid
274 session[:new_user].activate if email_verified && email == session[:new_user].email
276 redirect_to :action => "terms"
278 user = User.find_by(:auth_provider => provider, :auth_uid => uid)
280 if user.nil? && provider == "google"
281 openid_url = auth_info[:extra][:id_info]["openid_id"]
282 user = User.find_by(:auth_provider => "openid", :auth_uid => openid_url) if openid_url
283 user&.update(:auth_provider => provider, :auth_uid => uid)
289 unconfirmed_login(user)
290 when "active", "confirmed"
291 successful_login(user, request.env["omniauth.params"]["referer"])
293 failed_login({ :partial => "sessions/suspended_flash" })
295 failed_login t("sessions.new.auth failure")
298 redirect_to :action => "new", :nickname => name, :email => email,
299 :auth_provider => provider, :auth_uid => uid
305 # omniauth failure callback
307 flash[:error] = t(params[:message], :scope => "users.auth_failure", :default => t(".unknown_error"))
309 origin = safe_referer(params[:origin]) if params[:origin]
311 redirect_to origin || login_url
317 # ensure that there is a "user" instance variable
318 def lookup_user_by_name
319 @user = User.find_by(:display_name => params[:display_name])
320 rescue ActiveRecord::RecordNotFound
321 redirect_to :action => "view", :display_name => params[:display_name] unless @user
325 # return permitted user parameters
327 params.require(:user).permit(:email, :email_confirmation, :display_name,
328 :auth_provider, :auth_uid,
329 :pass_crypt, :pass_crypt_confirmation)
334 def check_signup_allowed(email = nil)
335 domain = if email.nil?
338 email.split("@").last
341 mx_servers = if domain.nil?
344 domain_mx_servers(domain)
347 if blocked = Acl.no_account_creation(request.remote_ip, :domain => domain, :mx => mx_servers)
348 logger.info "Blocked signup from #{request.remote_ip} for #{email}"
350 render :action => "blocked"
357 # get list of MX servers for a domains
358 def domain_mx_servers(domain)
359 Resolv::DNS.open do |dns|
360 dns.getresources(domain, Resolv::DNS::Resource::IN::MX).collect { |mx| mx.exchange.to_s }