1 class UsersController < ApplicationController
6 skip_before_action :verify_authenticity_token, :only => [:auth_success]
7 before_action :disable_terms_redirect, :only => [:terms, :save]
8 before_action :authorize_web
9 before_action :set_locale
10 before_action :check_database_readable
14 before_action :require_self, :only => [:account]
15 before_action :check_database_writable, :only => [:new, :account, :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, :account]
21 @legale = params[:legale] || OSM.ip_to_country(request.remote_ip) || Settings.default_legale
22 @text = OSM.legal_text_for_country(@legale)
25 render :partial => "terms"
27 @title = t "users.terms.title"
29 if current_user&.terms_agreed?
30 # Already agreed to terms, so just show settings
31 redirect_to user_account_path(current_user)
32 elsif current_user.nil? && session[:new_user].nil?
33 redirect_to login_path(:referer => request.fullpath)
39 @title = t "users.new.title"
41 if params[:decline] || !(params[:read_tou] && params[:read_ct])
43 current_user.terms_seen = true
45 flash[:notice] = { :partial => "users/terms_declined_flash" } if current_user.save
48 redirect_to safe_referer(params[:referer])
50 redirect_to user_account_path(current_user)
52 elsif params[:decline]
53 redirect_to t("users.terms.declined")
55 redirect_to :action => :terms
58 unless current_user.terms_agreed?
59 current_user.consider_pd = params[:user][:consider_pd]
60 current_user.tou_agreed = Time.now.getutc
61 current_user.terms_agreed = Time.now.getutc
62 current_user.terms_seen = true
64 flash[:notice] = t "users.new.terms accepted" if current_user.save
68 redirect_to safe_referer(params[:referer])
70 redirect_to user_account_path(current_user)
73 self.current_user = session.delete(:new_user)
75 if check_signup_allowed(current_user.email)
76 current_user.data_public = true
77 current_user.description = "" if current_user.description.nil?
78 current_user.creation_ip = request.remote_ip
79 current_user.languages = http_accept_language.user_preferred_languages
80 current_user.terms_agreed = Time.now.getutc
81 current_user.tou_agreed = Time.now.getutc
82 current_user.terms_seen = true
84 if current_user.auth_uid.blank?
85 current_user.auth_provider = nil
86 current_user.auth_uid = nil
90 flash[:piwik_goal] = PIWIK["goals"]["signup"] if defined?(PIWIK)
92 referer = welcome_path
95 uri = URI(session[:referer])
96 %r{map=(.*)/(.*)/(.*)}.match(uri.fragment) do |m|
97 editor = Rack::Utils.parse_query(uri.query).slice("editor")
98 referer = welcome_path({ "zoom" => m[1],
100 "lon" => m[3] }.merge(editor))
106 if current_user.status == "active"
107 session[:referer] = referer
108 successful_login(current_user)
110 session[:token] = current_user.tokens.create.token
111 UserMailer.signup_confirm(current_user, current_user.tokens.create(:referer => referer)).deliver_later
112 redirect_to :controller => :confirmations, :action => :confirm, :display_name => current_user.display_name
115 render :action => "new", :referer => params[:referer]
122 @tokens = current_user.oauth_tokens.authorized
124 append_content_security_policy_directives(
125 :form_action => %w[accounts.google.com *.facebook.com login.live.com github.com meta.wikimedia.org]
129 if params[:user][:auth_provider].blank? ||
130 (params[:user][:auth_provider] == current_user.auth_provider &&
131 params[:user][:auth_uid] == current_user.auth_uid)
132 update_user(current_user, params)
133 redirect_to user_account_url(current_user) if current_user.errors.count.zero?
135 session[:new_user_settings] = params
136 redirect_to auth_url(params[:user][:auth_provider], params[:user][:auth_uid]), :status => :temporary_redirect
138 elsif errors = session.delete(:user_errors)
139 errors.each do |attribute, error|
140 current_user.errors.add(attribute, error)
143 @title = t "users.account.title"
147 current_user.data_public = true
149 flash[:notice] = t "users.go_public.flash success"
150 redirect_to user_account_path(current_user)
154 @title = t "users.new.title"
155 @referer = if params[:referer]
156 safe_referer(params[:referer])
161 append_content_security_policy_directives(
162 :form_action => %w[accounts.google.com *.facebook.com login.live.com github.com meta.wikimedia.org]
166 # The user is logged in already, so don't show them the signup
167 # page, instead send them to the home page
171 redirect_to :controller => "site", :action => "index"
173 elsif params.key?(:auth_provider) && params.key?(:auth_uid)
174 self.current_user = User.new(:email => params[:email],
175 :email_confirmation => params[:email],
176 :display_name => params[:nickname],
177 :auth_provider => params[:auth_provider],
178 :auth_uid => params[:auth_uid])
180 flash.now[:notice] = render_to_string :partial => "auth_association"
184 self.current_user = User.new
189 self.current_user = User.new(user_params)
191 if check_signup_allowed(current_user.email)
192 session[:referer] = safe_referer(params[:referer]) if params[:referer]
194 Rails.logger.info "create: #{session[:referer]}"
196 current_user.status = "pending"
198 if current_user.auth_provider.present? && current_user.pass_crypt.empty?
199 # We are creating an account with external authentication and
200 # no password was specified so create a random one
201 current_user.pass_crypt = SecureRandom.base64(16)
202 current_user.pass_crypt_confirmation = current_user.pass_crypt
205 if current_user.invalid?
206 # Something is wrong with a new user, so rerender the form
207 render :action => "new"
208 elsif current_user.auth_provider.present?
209 # Verify external authenticator before moving on
210 session[:new_user] = current_user
211 redirect_to auth_url(current_user.auth_provider, current_user.auth_uid), :status => :temporary_redirect
213 # Save the user record
214 session[:new_user] = current_user
215 redirect_to :action => :terms
221 @user = User.find_by(:display_name => params[:display_name])
224 (@user.visible? || current_user&.administrator?)
225 @title = @user.display_name
227 render_unknown_user params[:display_name]
232 # sets a user's status
234 @user.status = params[:status]
236 redirect_to user_path(:display_name => params[:display_name])
240 # delete a user, marking them as deleted and removing personal data
243 redirect_to user_path(:display_name => params[:display_name])
247 # display a list of users matching specified criteria
250 ids = params[:user].keys.collect(&:to_i)
252 User.where(:id => ids).update_all(:status => "confirmed") if params[:confirm]
253 User.where(:id => ids).update_all(:status => "deleted") if params[:hide]
255 redirect_to url_for(:status => params[:status], :ip => params[:ip], :page => params[:page])
257 @params = params.permit(:status, :ip)
260 conditions[:status] = @params[:status] if @params[:status]
261 conditions[:creation_ip] = @params[:ip] if @params[:ip]
263 @user_pages, @users = paginate(:users,
264 :conditions => conditions,
271 # omniauth success callback
273 auth_info = request.env["omniauth.auth"]
275 provider = auth_info[:provider]
276 uid = auth_info[:uid]
277 name = auth_info[:info][:name]
278 email = auth_info[:info][:email]
280 email_verified = case provider
282 uid.match(%r{https://www.google.com/accounts/o8/id?(.*)}) ||
283 uid.match(%r{https://me.yahoo.com/(.*)})
284 when "google", "facebook"
290 if settings = session.delete(:new_user_settings)
291 current_user.auth_provider = provider
292 current_user.auth_uid = uid
294 update_user(current_user, settings)
298 session[:user_errors] = current_user.errors.as_json
300 redirect_to user_account_path(current_user)
301 elsif session[:new_user]
302 session[:new_user].auth_provider = provider
303 session[:new_user].auth_uid = uid
305 session[:new_user].status = "active" if email_verified && email == session[:new_user].email
307 redirect_to :action => "terms"
309 user = User.find_by(:auth_provider => provider, :auth_uid => uid)
311 if user.nil? && provider == "google"
312 openid_url = auth_info[:extra][:id_info]["openid_id"]
313 user = User.find_by(:auth_provider => "openid", :auth_uid => openid_url) if openid_url
314 user&.update(:auth_provider => provider, :auth_uid => uid)
320 unconfirmed_login(user)
321 when "active", "confirmed"
322 successful_login(user, request.env["omniauth.params"]["referer"])
324 failed_login t("sessions.new.account is suspended", :webmaster => "mailto:#{Settings.support_email}").html_safe
326 failed_login t("sessions.new.auth failure")
329 redirect_to :action => "new", :nickname => name, :email => email,
330 :auth_provider => provider, :auth_uid => uid
336 # omniauth failure callback
338 flash[:error] = t("users.auth_failure.#{params[:message]}")
339 redirect_to params[:origin] || login_url
346 def unconfirmed_login(user)
347 session[:token] = user.tokens.create.token
349 redirect_to :action => "confirm", :display_name => user.display_name
351 session.delete(:remember_me)
352 session.delete(:referer)
356 # update a user's details
357 def update_user(user, params)
358 user.display_name = params[:user][:display_name]
359 user.new_email = params[:user][:new_email]
361 unless params[:user][:pass_crypt].empty? && params[:user][:pass_crypt_confirmation].empty?
362 user.pass_crypt = params[:user][:pass_crypt]
363 user.pass_crypt_confirmation = params[:user][:pass_crypt_confirmation]
366 if params[:user][:auth_provider].nil? || params[:user][:auth_provider].blank?
367 user.auth_provider = nil
372 session[:fingerprint] = user.fingerprint
374 if user.new_email.blank? || user.new_email == user.email
375 flash[:notice] = t "users.account.flash update success"
377 user.email = user.new_email
380 flash[:notice] = t "users.account.flash update success confirm needed"
383 UserMailer.email_confirm(user, user.tokens.create).deliver_later
385 # Ignore errors sending email
388 current_user.errors.add(:new_email, current_user.errors[:email])
389 current_user.errors.add(:email, [])
398 # require that the user in the URL is the logged in user
400 head :forbidden if params[:display_name] != current_user.display_name
404 # ensure that there is a "user" instance variable
405 def lookup_user_by_name
406 @user = User.find_by(:display_name => params[:display_name])
407 rescue ActiveRecord::RecordNotFound
408 redirect_to :action => "view", :display_name => params[:display_name] unless @user
412 # return permitted user parameters
414 params.require(:user).permit(:email, :email_confirmation, :display_name,
415 :auth_provider, :auth_uid,
416 :pass_crypt, :pass_crypt_confirmation)
421 def check_signup_allowed(email = nil)
422 domain = if email.nil?
425 email.split("@").last
428 mx_servers = if domain.nil?
431 domain_mx_servers(domain)
434 if blocked = Acl.no_account_creation(request.remote_ip, :domain => domain, :mx => mx_servers)
435 logger.info "Blocked signup from #{request.remote_ip} for #{email}"
437 render :action => "blocked"
444 # get list of MX servers for a domains
445 def domain_mx_servers(domain)
446 Resolv::DNS.open do |dns|
447 dns.getresources(domain, Resolv::DNS::Resource::IN::MX).collect(&:exchange).collect(&:to_s)