1 class UsersController < ApplicationController
4 skip_before_action :verify_authenticity_token, :only => [:auth_success]
5 before_action :disable_terms_redirect, :only => [:terms, :save, :logout]
6 before_action :authorize_web
7 before_action :set_locale
8 before_action :check_database_readable
12 before_action :require_self, :only => [:account]
13 before_action :check_database_writable, :only => [:new, :account, :confirm, :confirm_email, :lost_password, :reset_password, :go_public]
14 before_action :require_cookies, :only => [:new, :login, :confirm]
15 before_action :lookup_user_by_name, :only => [:set_status, :destroy]
16 before_action :allow_thirdparty_images, :only => [:show, :account]
19 @legale = params[:legale] || OSM.ip_to_country(request.remote_ip) || Settings.default_legale
20 @text = OSM.legal_text_for_country(@legale)
23 render :partial => "terms"
25 @title = t "users.terms.title"
27 if current_user&.terms_agreed?
28 # Already agreed to terms, so just show settings
29 redirect_to :action => :account, :display_name => current_user.display_name
30 elsif current_user.nil? && session[:new_user].nil?
31 redirect_to :action => :login, :referer => request.fullpath
37 @title = t "users.new.title"
39 if params[:decline] || !(params[:read_tou] && params[:read_ct])
41 current_user.terms_seen = true
43 flash[:notice] = t("users.new.terms declined", :url => t("users.new.terms declined url")).html_safe if current_user.save
46 redirect_to safe_referer(params[:referer])
48 redirect_to :action => :account, :display_name => current_user.display_name
50 elsif params[:decline]
51 redirect_to t("users.terms.declined")
53 redirect_to :action => :terms
56 unless current_user.terms_agreed?
57 current_user.consider_pd = params[:user][:consider_pd]
58 current_user.tou_agreed = Time.now.getutc
59 current_user.terms_agreed = Time.now.getutc
60 current_user.terms_seen = true
62 flash[:notice] = t "users.new.terms accepted" if current_user.save
66 redirect_to safe_referer(params[:referer])
68 redirect_to :action => :account, :display_name => current_user.display_name
71 self.current_user = session.delete(:new_user)
73 if check_signup_allowed(current_user.email)
74 current_user.data_public = true
75 current_user.description = "" if current_user.description.nil?
76 current_user.creation_ip = request.remote_ip
77 current_user.languages = http_accept_language.user_preferred_languages
78 current_user.terms_agreed = Time.now.getutc
79 current_user.tou_agreed = Time.now.getutc
80 current_user.terms_seen = true
82 if current_user.auth_uid.blank?
83 current_user.auth_provider = nil
84 current_user.auth_uid = nil
88 flash[:piwik_goal] = PIWIK["goals"]["signup"] if defined?(PIWIK)
90 referer = welcome_path
93 uri = URI(session[:referer])
94 %r{map=(.*)/(.*)/(.*)}.match(uri.fragment) do |m|
95 editor = Rack::Utils.parse_query(uri.query).slice("editor")
96 referer = welcome_path({ "zoom" => m[1],
98 "lon" => m[3] }.merge(editor))
104 if current_user.status == "active"
105 session[:referer] = referer
106 successful_login(current_user)
108 session[:token] = current_user.tokens.create.token
109 UserMailer.signup_confirm(current_user, current_user.tokens.create(:referer => referer)).deliver_later
110 redirect_to :action => "confirm", :display_name => current_user.display_name
113 render :action => "new", :referer => params[:referer]
120 @tokens = current_user.oauth_tokens.authorized
122 append_content_security_policy_directives(
123 :form_action => %w[accounts.google.com *.facebook.com login.live.com github.com meta.wikimedia.org]
126 if params[:user] && params[:user][:display_name] && params[:user][:description]
127 if params[:user][:auth_provider].blank? ||
128 (params[:user][:auth_provider] == current_user.auth_provider &&
129 params[:user][:auth_uid] == current_user.auth_uid)
130 update_user(current_user, params)
132 session[:new_user_settings] = params
133 redirect_to auth_url(params[:user][:auth_provider], params[:user][:auth_uid])
135 elsif errors = session.delete(:user_errors)
136 errors.each do |attribute, error|
137 current_user.errors.add(attribute, error)
140 @title = t "users.account.title"
144 current_user.data_public = true
146 flash[:notice] = t "users.go_public.flash success"
147 redirect_to :action => "account", :display_name => current_user.display_name
151 @title = t "users.lost_password.title"
154 user = User.visible.find_by(:email => params[:email])
157 users = User.visible.where("LOWER(email) = LOWER(?)", params[:email])
159 user = users.first if users.count == 1
163 token = user.tokens.create
164 UserMailer.lost_password(user, token).deliver_later
165 flash[:notice] = t "users.lost_password.notice email on way"
166 redirect_to :action => "login"
168 flash.now[:error] = t "users.lost_password.notice email cannot find"
174 @title = t "users.reset_password.title"
177 token = UserToken.find_by(:token => params[:token])
180 self.current_user = token.user
183 current_user.pass_crypt = params[:user][:pass_crypt]
184 current_user.pass_crypt_confirmation = params[:user][:pass_crypt_confirmation]
185 current_user.status = "active" if current_user.status == "pending"
186 current_user.email_valid = true
190 session[:fingerprint] = current_user.fingerprint
191 flash[:notice] = t "users.reset_password.flash changed"
192 successful_login(current_user)
196 flash[:error] = t "users.reset_password.flash token bad"
197 redirect_to :action => "lost_password"
205 @title = t "users.new.title"
206 @referer = if params[:referer]
207 safe_referer(params[:referer])
212 append_content_security_policy_directives(
213 :form_action => %w[accounts.google.com *.facebook.com login.live.com github.com meta.wikimedia.org]
217 # The user is logged in already, so don't show them the signup
218 # page, instead send them to the home page
222 redirect_to :controller => "site", :action => "index"
224 elsif params.key?(:auth_provider) && params.key?(:auth_uid)
225 self.current_user = User.new(:email => params[:email],
226 :email_confirmation => params[:email],
227 :display_name => params[:nickname],
228 :auth_provider => params[:auth_provider],
229 :auth_uid => params[:auth_uid])
231 flash.now[:notice] = render_to_string :partial => "auth_association"
235 self.current_user = User.new
240 self.current_user = User.new(user_params)
242 if check_signup_allowed(current_user.email)
243 session[:referer] = safe_referer(params[:referer]) if params[:referer]
245 Rails.logger.info "create: #{session[:referer]}"
247 current_user.status = "pending"
249 if current_user.auth_provider.present? && current_user.pass_crypt.empty?
250 # We are creating an account with external authentication and
251 # no password was specified so create a random one
252 current_user.pass_crypt = SecureRandom.base64(16)
253 current_user.pass_crypt_confirmation = current_user.pass_crypt
256 if current_user.invalid?
257 # Something is wrong with a new user, so rerender the form
258 render :action => "new"
259 elsif current_user.auth_provider.present?
260 # Verify external authenticator before moving on
261 session[:new_user] = current_user
262 redirect_to auth_url(current_user.auth_provider, current_user.auth_uid)
264 # Save the user record
265 session[:new_user] = current_user
266 redirect_to :action => :terms
272 session[:referer] = safe_referer(params[:referer]) if params[:referer]
274 if params[:username].present? && params[:password].present?
275 session[:remember_me] ||= params[:remember_me]
276 password_authentication(params[:username], params[:password])
281 @title = t "users.logout.title"
285 token = UserToken.find_by(:token => session[:token])
287 session.delete(:token)
289 session.delete(:user)
290 session_expires_automatically
292 redirect_to safe_referer(params[:referer])
294 redirect_to :controller => "site", :action => "index"
301 token = UserToken.find_by(:token => params[:confirm_string])
302 if token&.user&.active?
303 flash[:error] = t("users.confirm.already active")
304 redirect_to :action => "login"
305 elsif !token || token.expired?
306 flash[:error] = t("users.confirm.unknown token")
307 redirect_to :action => "confirm"
310 user.status = "active"
311 user.email_valid = true
312 flash[:notice] = gravatar_status_message(user) if gravatar_enable(user)
314 referer = safe_referer(token.referer) if token.referer
318 token = UserToken.find_by(:token => session[:token])
319 session.delete(:token)
324 if token.nil? || token.user != user
325 flash[:notice] = t("users.confirm.success")
326 redirect_to :action => :login, :referer => referer
330 session[:user] = user.id
331 session[:fingerprint] = user.fingerprint
333 redirect_to referer || welcome_path
337 user = User.find_by(:display_name => params[:display_name])
339 redirect_to root_path if user.nil? || user.active?
344 user = User.find_by(:display_name => params[:display_name])
345 token = UserToken.find_by(:token => session[:token])
347 if user.nil? || token.nil? || token.user != user
348 flash[:error] = t "users.confirm_resend.failure", :name => params[:display_name]
350 UserMailer.signup_confirm(user, user.tokens.create).deliver_later
351 flash[:notice] = t "users.confirm_resend.success_html", :email => user.email, :sender => Settings.support_email
354 redirect_to :action => "login"
359 token = UserToken.find_by(:token => params[:confirm_string])
360 if token&.user&.new_email?
361 self.current_user = token.user
362 current_user.email = current_user.new_email
363 current_user.new_email = nil
364 current_user.email_valid = true
365 gravatar_enabled = gravatar_enable(current_user)
367 flash[:notice] = if gravatar_enabled
368 "#{t('users.confirm_email.success')} #{gravatar_status_message(current_user)}"
370 t("users.confirm_email.success")
373 flash[:errors] = current_user.errors
375 current_user.tokens.delete_all
376 session[:user] = current_user.id
377 session[:fingerprint] = current_user.fingerprint
378 redirect_to :action => "account", :display_name => current_user.display_name
380 flash[:error] = t "users.confirm_email.failure"
381 redirect_to :action => "account", :display_name => token.user.display_name
383 flash[:error] = t "users.confirm_email.unknown_token"
389 @user = User.find_by(:display_name => params[:display_name])
392 (@user.visible? || current_user&.administrator?)
393 @title = @user.display_name
395 render_unknown_user params[:display_name]
400 # sets a user's status
402 @user.status = params[:status]
404 redirect_to user_path(:display_name => params[:display_name])
408 # delete a user, marking them as deleted and removing personal data
411 redirect_to user_path(:display_name => params[:display_name])
415 # display a list of users matching specified criteria
418 ids = params[:user].keys.collect(&:to_i)
420 User.where(:id => ids).update_all(:status => "confirmed") if params[:confirm]
421 User.where(:id => ids).update_all(:status => "deleted") if params[:hide]
423 redirect_to url_for(:status => params[:status], :ip => params[:ip], :page => params[:page])
425 @params = params.permit(:status, :ip)
428 conditions[:status] = @params[:status] if @params[:status]
429 conditions[:creation_ip] = @params[:ip] if @params[:ip]
431 @user_pages, @users = paginate(:users,
432 :conditions => conditions,
439 # omniauth success callback
441 auth_info = request.env["omniauth.auth"]
443 provider = auth_info[:provider]
444 uid = auth_info[:uid]
445 name = auth_info[:info][:name]
446 email = auth_info[:info][:email]
450 email_verified = uid.match(%r{https://www.google.com/accounts/o8/id?(.*)}) ||
451 uid.match(%r{https://me.yahoo.com/(.*)})
452 when "google", "facebook"
453 email_verified = true
455 email_verified = false
458 if settings = session.delete(:new_user_settings)
459 current_user.auth_provider = provider
460 current_user.auth_uid = uid
462 update_user(current_user, settings)
464 session[:user_errors] = current_user.errors.as_json
466 redirect_to :action => "account", :display_name => current_user.display_name
467 elsif session[:new_user]
468 session[:new_user].auth_provider = provider
469 session[:new_user].auth_uid = uid
471 session[:new_user].status = "active" if email_verified && email == session[:new_user].email
473 redirect_to :action => "terms"
475 user = User.find_by(:auth_provider => provider, :auth_uid => uid)
477 if user.nil? && provider == "google"
478 openid_url = auth_info[:extra][:id_info]["openid_id"]
479 user = User.find_by(:auth_provider => "openid", :auth_uid => openid_url) if openid_url
480 user&.update(:auth_provider => provider, :auth_uid => uid)
486 unconfirmed_login(user)
487 when "active", "confirmed"
488 successful_login(user, request.env["omniauth.params"]["referer"])
490 failed_login t("users.login.account is suspended", :webmaster => "mailto:#{Settings.support_email}").html_safe
492 failed_login t("users.login.auth failure")
495 redirect_to :action => "new", :nickname => name, :email => email,
496 :auth_provider => provider, :auth_uid => uid
502 # omniauth failure callback
504 flash[:error] = t("users.auth_failure.#{params[:message]}")
505 redirect_to params[:origin] || login_url
511 # handle password authentication
512 def password_authentication(username, password)
513 if user = User.authenticate(:username => username, :password => password)
514 successful_login(user)
515 elsif user = User.authenticate(:username => username, :password => password, :pending => true)
516 unconfirmed_login(user)
517 elsif User.authenticate(:username => username, :password => password, :suspended => true)
518 failed_login t("users.login.account is suspended", :webmaster => "mailto:#{Settings.support_email}").html_safe, username
520 failed_login t("users.login.auth failure"), username
525 # return the URL to use for authentication
526 def auth_url(provider, uid, referer = nil)
527 params = { :provider => provider }
529 params[:openid_url] = openid_expand_url(uid) if provider == "openid"
532 params[:origin] = request.path
534 params[:origin] = "#{request.path}?referer=#{CGI.escape(referer)}"
535 params[:referer] = referer
542 # special case some common OpenID providers by applying heuristics to
543 # try and come up with the correct URL based on what the user entered
544 def openid_expand_url(openid_url)
547 elsif openid_url.match(%r{(.*)gmail.com(/?)$}) || openid_url.match(%r{(.*)googlemail.com(/?)$})
548 # Special case gmail.com as it is potentially a popular OpenID
549 # provider and, unlike yahoo.com, where it works automatically, Google
550 # have hidden their OpenID endpoint somewhere obscure this making it
551 # somewhat less user friendly.
552 "https://www.google.com/accounts/o8/id"
559 # process a successful login
560 def successful_login(user, referer = nil)
561 session[:user] = user.id
562 session[:fingerprint] = user.fingerprint
563 session_expires_after 28.days if session[:remember_me]
565 target = referer || session[:referer] || url_for(:controller => :site, :action => :index)
567 # The user is logged in, so decide where to send them:
569 # - If they haven't seen the contributor terms, send them there.
570 # - If they have a block on them, show them that.
571 # - If they were referred to the login, send them back there.
572 # - Otherwise, send them to the home page.
574 redirect_to :action => :terms, :referer => target
575 elsif user.blocked_on_view
576 redirect_to user.blocked_on_view, :referer => target
581 session.delete(:remember_me)
582 session.delete(:referer)
586 # process a failed login
587 def failed_login(message, username = nil)
588 flash[:error] = message
590 redirect_to :action => "login", :referer => session[:referer],
591 :username => username, :remember_me => session[:remember_me]
593 session.delete(:remember_me)
594 session.delete(:referer)
599 def unconfirmed_login(user)
600 session[:token] = user.tokens.create.token
602 redirect_to :action => "confirm", :display_name => user.display_name
604 session.delete(:remember_me)
605 session.delete(:referer)
609 # update a user's details
610 def update_user(user, params)
611 user.display_name = params[:user][:display_name]
612 user.new_email = params[:user][:new_email]
614 unless params[:user][:pass_crypt].empty? && params[:user][:pass_crypt_confirmation].empty?
615 user.pass_crypt = params[:user][:pass_crypt]
616 user.pass_crypt_confirmation = params[:user][:pass_crypt_confirmation]
619 if params[:user][:description] != user.description
620 user.description = params[:user][:description]
621 user.description_format = "markdown"
624 user.languages = params[:user][:languages].split(",")
626 case params[:avatar_action]
628 user.avatar.attach(params[:user][:avatar])
629 user.image_use_gravatar = false
631 user.avatar.purge_later
632 user.image_use_gravatar = false
634 user.avatar.purge_later
635 user.image_use_gravatar = true
638 user.home_lat = params[:user][:home_lat]
639 user.home_lon = params[:user][:home_lon]
641 user.preferred_editor = if params[:user][:preferred_editor] == "default"
644 params[:user][:preferred_editor]
647 if params[:user][:auth_provider].nil? || params[:user][:auth_provider].blank?
648 user.auth_provider = nil
653 session[:fingerprint] = user.fingerprint
655 set_locale(:reset => true)
657 if user.new_email.blank? || user.new_email == user.email
658 flash.now[:notice] = t "users.account.flash update success"
660 user.email = user.new_email
663 flash.now[:notice] = t "users.account.flash update success confirm needed"
666 UserMailer.email_confirm(user, user.tokens.create).deliver_later
668 # Ignore errors sending email
671 current_user.errors.add(:new_email, current_user.errors[:email])
672 current_user.errors.add(:email, [])
681 # require that the user in the URL is the logged in user
683 head :forbidden if params[:display_name] != current_user.display_name
687 # ensure that there is a "user" instance variable
688 def lookup_user_by_name
689 @user = User.find_by(:display_name => params[:display_name])
690 rescue ActiveRecord::RecordNotFound
691 redirect_to :action => "view", :display_name => params[:display_name] unless @user
696 def disable_terms_redirect
697 # this is necessary otherwise going to the user terms page, when
698 # having not agreed already would cause an infinite redirect loop.
699 # it's .now so that this doesn't propagate to other pages.
700 flash.now[:skip_terms] = true
704 # return permitted user parameters
706 params.require(:user).permit(:email, :email_confirmation, :display_name,
707 :auth_provider, :auth_uid,
708 :pass_crypt, :pass_crypt_confirmation)
713 def check_signup_allowed(email = nil)
714 domain = if email.nil?
717 email.split("@").last
720 mx_servers = if domain.nil?
723 domain_mx_servers(domain)
726 if blocked = Acl.no_account_creation(request.remote_ip, :domain => domain, :mx => mx_servers)
727 logger.info "Blocked signup from #{request.remote_ip} for #{email}"
729 render :action => "blocked"
736 # get list of MX servers for a domains
737 def domain_mx_servers(domain)
738 Resolv::DNS.open do |dns|
739 dns.getresources(domain, Resolv::DNS::Resource::IN::MX).collect(&:exchange).collect(&:to_s)
744 # check if this user has a gravatar and set the user pref is true
745 def gravatar_enable(user)
746 # code from example https://en.gravatar.com/site/implement/images/ruby/
747 return false if user.avatar.attached?
749 hash = Digest::MD5.hexdigest(user.email.downcase)
750 url = "https://www.gravatar.com/avatar/#{hash}?d=404" # without d=404 we will always get an image back
751 response = OSM.http_client.get(URI.parse(url))
752 oldsetting = user.image_use_gravatar
753 user.image_use_gravatar = response.success?
754 oldsetting != user.image_use_gravatar
758 # display a message about th current status of the gravatar setting
759 def gravatar_status_message(user)
760 if user.image_use_gravatar
761 t "users.account.gravatar.enabled"
763 t "users.account.gravatar.disabled"