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)
131 redirect_to user_account_url(current_user) if current_user.errors.count.zero?
133 session[:new_user_settings] = params
134 redirect_to auth_url(params[:user][:auth_provider], params[:user][:auth_uid])
136 elsif errors = session.delete(:user_errors)
137 errors.each do |attribute, error|
138 current_user.errors.add(attribute, error)
141 @title = t "users.account.title"
145 current_user.data_public = true
147 flash[:notice] = t "users.go_public.flash success"
148 redirect_to :action => "account", :display_name => current_user.display_name
152 @title = t "users.lost_password.title"
155 user = User.visible.find_by(:email => params[:email])
158 users = User.visible.where("LOWER(email) = LOWER(?)", params[:email])
160 user = users.first if users.count == 1
164 token = user.tokens.create
165 UserMailer.lost_password(user, token).deliver_later
166 flash[:notice] = t "users.lost_password.notice email on way"
167 redirect_to :action => "login"
169 flash.now[:error] = t "users.lost_password.notice email cannot find"
175 @title = t "users.reset_password.title"
178 token = UserToken.find_by(:token => params[:token])
181 self.current_user = token.user
184 current_user.pass_crypt = params[:user][:pass_crypt]
185 current_user.pass_crypt_confirmation = params[:user][:pass_crypt_confirmation]
186 current_user.status = "active" if current_user.status == "pending"
187 current_user.email_valid = true
191 session[:fingerprint] = current_user.fingerprint
192 flash[:notice] = t "users.reset_password.flash changed"
193 successful_login(current_user)
197 flash[:error] = t "users.reset_password.flash token bad"
198 redirect_to :action => "lost_password"
206 @title = t "users.new.title"
207 @referer = if params[:referer]
208 safe_referer(params[:referer])
213 append_content_security_policy_directives(
214 :form_action => %w[accounts.google.com *.facebook.com login.live.com github.com meta.wikimedia.org]
218 # The user is logged in already, so don't show them the signup
219 # page, instead send them to the home page
223 redirect_to :controller => "site", :action => "index"
225 elsif params.key?(:auth_provider) && params.key?(:auth_uid)
226 self.current_user = User.new(:email => params[:email],
227 :email_confirmation => params[:email],
228 :display_name => params[:nickname],
229 :auth_provider => params[:auth_provider],
230 :auth_uid => params[:auth_uid])
232 flash.now[:notice] = render_to_string :partial => "auth_association"
236 self.current_user = User.new
241 self.current_user = User.new(user_params)
243 if check_signup_allowed(current_user.email)
244 session[:referer] = safe_referer(params[:referer]) if params[:referer]
246 Rails.logger.info "create: #{session[:referer]}"
248 current_user.status = "pending"
250 if current_user.auth_provider.present? && current_user.pass_crypt.empty?
251 # We are creating an account with external authentication and
252 # no password was specified so create a random one
253 current_user.pass_crypt = SecureRandom.base64(16)
254 current_user.pass_crypt_confirmation = current_user.pass_crypt
257 if current_user.invalid?
258 # Something is wrong with a new user, so rerender the form
259 render :action => "new"
260 elsif current_user.auth_provider.present?
261 # Verify external authenticator before moving on
262 session[:new_user] = current_user
263 redirect_to auth_url(current_user.auth_provider, current_user.auth_uid)
265 # Save the user record
266 session[:new_user] = current_user
267 redirect_to :action => :terms
273 session[:referer] = safe_referer(params[:referer]) if params[:referer]
275 if params[:username].present? && params[:password].present?
276 session[:remember_me] ||= params[:remember_me]
277 password_authentication(params[:username], params[:password])
282 @title = t "users.logout.title"
286 token = UserToken.find_by(:token => session[:token])
288 session.delete(:token)
290 session.delete(:user)
291 session_expires_automatically
293 redirect_to safe_referer(params[:referer])
295 redirect_to :controller => "site", :action => "index"
302 token = UserToken.find_by(:token => params[:confirm_string])
303 if token&.user&.active?
304 flash[:error] = t("users.confirm.already active")
305 redirect_to :action => "login"
306 elsif !token || token.expired?
307 flash[:error] = t("users.confirm.unknown token")
308 redirect_to :action => "confirm"
311 user.status = "active"
312 user.email_valid = true
313 flash[:notice] = gravatar_status_message(user) if gravatar_enable(user)
315 referer = safe_referer(token.referer) if token.referer
319 token = UserToken.find_by(:token => session[:token])
320 session.delete(:token)
325 if token.nil? || token.user != user
326 flash[:notice] = t("users.confirm.success")
327 redirect_to :action => :login, :referer => referer
331 session[:user] = user.id
332 session[:fingerprint] = user.fingerprint
334 redirect_to referer || welcome_path
338 user = User.find_by(:display_name => params[:display_name])
340 redirect_to root_path if user.nil? || user.active?
345 user = User.find_by(:display_name => params[:display_name])
346 token = UserToken.find_by(:token => session[:token])
348 if user.nil? || token.nil? || token.user != user
349 flash[:error] = t "users.confirm_resend.failure", :name => params[:display_name]
351 UserMailer.signup_confirm(user, user.tokens.create).deliver_later
352 flash[:notice] = t "users.confirm_resend.success_html", :email => user.email, :sender => Settings.support_email
355 redirect_to :action => "login"
360 token = UserToken.find_by(:token => params[:confirm_string])
361 if token&.user&.new_email?
362 self.current_user = token.user
363 current_user.email = current_user.new_email
364 current_user.new_email = nil
365 current_user.email_valid = true
366 gravatar_enabled = gravatar_enable(current_user)
368 flash[:notice] = if gravatar_enabled
369 "#{t('users.confirm_email.success')} #{gravatar_status_message(current_user)}"
371 t("users.confirm_email.success")
374 flash[:errors] = current_user.errors
376 current_user.tokens.delete_all
377 session[:user] = current_user.id
378 session[:fingerprint] = current_user.fingerprint
379 redirect_to :action => "account", :display_name => current_user.display_name
381 flash[:error] = t "users.confirm_email.failure"
382 redirect_to :action => "account", :display_name => token.user.display_name
384 flash[:error] = t "users.confirm_email.unknown_token"
390 @user = User.find_by(:display_name => params[:display_name])
393 (@user.visible? || current_user&.administrator?)
394 @title = @user.display_name
396 render_unknown_user params[:display_name]
401 # sets a user's status
403 @user.status = params[:status]
405 redirect_to user_path(:display_name => params[:display_name])
409 # delete a user, marking them as deleted and removing personal data
412 redirect_to user_path(:display_name => params[:display_name])
416 # display a list of users matching specified criteria
419 ids = params[:user].keys.collect(&:to_i)
421 User.where(:id => ids).update_all(:status => "confirmed") if params[:confirm]
422 User.where(:id => ids).update_all(:status => "deleted") if params[:hide]
424 redirect_to url_for(:status => params[:status], :ip => params[:ip], :page => params[:page])
426 @params = params.permit(:status, :ip)
429 conditions[:status] = @params[:status] if @params[:status]
430 conditions[:creation_ip] = @params[:ip] if @params[:ip]
432 @user_pages, @users = paginate(:users,
433 :conditions => conditions,
440 # omniauth success callback
442 auth_info = request.env["omniauth.auth"]
444 provider = auth_info[:provider]
445 uid = auth_info[:uid]
446 name = auth_info[:info][:name]
447 email = auth_info[:info][:email]
451 email_verified = uid.match(%r{https://www.google.com/accounts/o8/id?(.*)}) ||
452 uid.match(%r{https://me.yahoo.com/(.*)})
453 when "google", "facebook"
454 email_verified = true
456 email_verified = false
459 if settings = session.delete(:new_user_settings)
460 current_user.auth_provider = provider
461 current_user.auth_uid = uid
463 update_user(current_user, settings)
467 session[:user_errors] = current_user.errors.as_json
469 redirect_to :action => "account", :display_name => current_user.display_name
470 elsif session[:new_user]
471 session[:new_user].auth_provider = provider
472 session[:new_user].auth_uid = uid
474 session[:new_user].status = "active" if email_verified && email == session[:new_user].email
476 redirect_to :action => "terms"
478 user = User.find_by(:auth_provider => provider, :auth_uid => uid)
480 if user.nil? && provider == "google"
481 openid_url = auth_info[:extra][:id_info]["openid_id"]
482 user = User.find_by(:auth_provider => "openid", :auth_uid => openid_url) if openid_url
483 user&.update(:auth_provider => provider, :auth_uid => uid)
489 unconfirmed_login(user)
490 when "active", "confirmed"
491 successful_login(user, request.env["omniauth.params"]["referer"])
493 failed_login t("users.login.account is suspended", :webmaster => "mailto:#{Settings.support_email}").html_safe
495 failed_login t("users.login.auth failure")
498 redirect_to :action => "new", :nickname => name, :email => email,
499 :auth_provider => provider, :auth_uid => uid
505 # omniauth failure callback
507 flash[:error] = t("users.auth_failure.#{params[:message]}")
508 redirect_to params[:origin] || login_url
514 # handle password authentication
515 def password_authentication(username, password)
516 if user = User.authenticate(:username => username, :password => password)
517 successful_login(user)
518 elsif user = User.authenticate(:username => username, :password => password, :pending => true)
519 unconfirmed_login(user)
520 elsif User.authenticate(:username => username, :password => password, :suspended => true)
521 failed_login t("users.login.account is suspended", :webmaster => "mailto:#{Settings.support_email}").html_safe, username
523 failed_login t("users.login.auth failure"), username
528 # return the URL to use for authentication
529 def auth_url(provider, uid, referer = nil)
530 params = { :provider => provider }
532 params[:openid_url] = openid_expand_url(uid) if provider == "openid"
535 params[:origin] = request.path
537 params[:origin] = "#{request.path}?referer=#{CGI.escape(referer)}"
538 params[:referer] = referer
545 # special case some common OpenID providers by applying heuristics to
546 # try and come up with the correct URL based on what the user entered
547 def openid_expand_url(openid_url)
550 elsif openid_url.match(%r{(.*)gmail.com(/?)$}) || openid_url.match(%r{(.*)googlemail.com(/?)$})
551 # Special case gmail.com as it is potentially a popular OpenID
552 # provider and, unlike yahoo.com, where it works automatically, Google
553 # have hidden their OpenID endpoint somewhere obscure this making it
554 # somewhat less user friendly.
555 "https://www.google.com/accounts/o8/id"
562 # process a successful login
563 def successful_login(user, referer = nil)
564 session[:user] = user.id
565 session[:fingerprint] = user.fingerprint
566 session_expires_after 28.days if session[:remember_me]
568 target = referer || session[:referer] || url_for(:controller => :site, :action => :index)
570 # The user is logged in, so decide where to send them:
572 # - If they haven't seen the contributor terms, send them there.
573 # - If they have a block on them, show them that.
574 # - If they were referred to the login, send them back there.
575 # - Otherwise, send them to the home page.
577 redirect_to :action => :terms, :referer => target
578 elsif user.blocked_on_view
579 redirect_to user.blocked_on_view, :referer => target
584 session.delete(:remember_me)
585 session.delete(:referer)
589 # process a failed login
590 def failed_login(message, username = nil)
591 flash[:error] = message
593 redirect_to :action => "login", :referer => session[:referer],
594 :username => username, :remember_me => session[:remember_me]
596 session.delete(:remember_me)
597 session.delete(:referer)
602 def unconfirmed_login(user)
603 session[:token] = user.tokens.create.token
605 redirect_to :action => "confirm", :display_name => user.display_name
607 session.delete(:remember_me)
608 session.delete(:referer)
612 # update a user's details
613 def update_user(user, params)
614 user.display_name = params[:user][:display_name]
615 user.new_email = params[:user][:new_email]
617 unless params[:user][:pass_crypt].empty? && params[:user][:pass_crypt_confirmation].empty?
618 user.pass_crypt = params[:user][:pass_crypt]
619 user.pass_crypt_confirmation = params[:user][:pass_crypt_confirmation]
622 if params[:user][:description] != user.description
623 user.description = params[:user][:description]
624 user.description_format = "markdown"
627 user.languages = params[:user][:languages].split(",")
629 case params[:avatar_action]
631 user.avatar.attach(params[:user][:avatar])
632 user.image_use_gravatar = false
634 user.avatar.purge_later
635 user.image_use_gravatar = false
637 user.avatar.purge_later
638 user.image_use_gravatar = true
641 user.home_lat = params[:user][:home_lat]
642 user.home_lon = params[:user][:home_lon]
644 user.preferred_editor = if params[:user][:preferred_editor] == "default"
647 params[:user][:preferred_editor]
650 if params[:user][:auth_provider].nil? || params[:user][:auth_provider].blank?
651 user.auth_provider = nil
656 session[:fingerprint] = user.fingerprint
658 set_locale(:reset => true)
660 if user.new_email.blank? || user.new_email == user.email
661 flash[:notice] = t "users.account.flash update success"
663 user.email = user.new_email
666 flash[:notice] = t "users.account.flash update success confirm needed"
669 UserMailer.email_confirm(user, user.tokens.create).deliver_later
671 # Ignore errors sending email
674 current_user.errors.add(:new_email, current_user.errors[:email])
675 current_user.errors.add(:email, [])
684 # require that the user in the URL is the logged in user
686 head :forbidden if params[:display_name] != current_user.display_name
690 # ensure that there is a "user" instance variable
691 def lookup_user_by_name
692 @user = User.find_by(:display_name => params[:display_name])
693 rescue ActiveRecord::RecordNotFound
694 redirect_to :action => "view", :display_name => params[:display_name] unless @user
699 def disable_terms_redirect
700 # this is necessary otherwise going to the user terms page, when
701 # having not agreed already would cause an infinite redirect loop.
702 # it's .now so that this doesn't propagate to other pages.
703 flash.now[:skip_terms] = true
707 # return permitted user parameters
709 params.require(:user).permit(:email, :email_confirmation, :display_name,
710 :auth_provider, :auth_uid,
711 :pass_crypt, :pass_crypt_confirmation)
716 def check_signup_allowed(email = nil)
717 domain = if email.nil?
720 email.split("@").last
723 mx_servers = if domain.nil?
726 domain_mx_servers(domain)
729 if blocked = Acl.no_account_creation(request.remote_ip, :domain => domain, :mx => mx_servers)
730 logger.info "Blocked signup from #{request.remote_ip} for #{email}"
732 render :action => "blocked"
739 # get list of MX servers for a domains
740 def domain_mx_servers(domain)
741 Resolv::DNS.open do |dns|
742 dns.getresources(domain, Resolv::DNS::Resource::IN::MX).collect(&:exchange).collect(&:to_s)
747 # check if this user has a gravatar and set the user pref is true
748 def gravatar_enable(user)
749 # code from example https://en.gravatar.com/site/implement/images/ruby/
750 return false if user.avatar.attached?
752 hash = Digest::MD5.hexdigest(user.email.downcase)
753 url = "https://www.gravatar.com/avatar/#{hash}?d=404" # without d=404 we will always get an image back
754 response = OSM.http_client.get(URI.parse(url))
755 oldsetting = user.image_use_gravatar
756 user.image_use_gravatar = response.success?
757 oldsetting != user.image_use_gravatar
761 # display a message about th current status of the gravatar setting
762 def gravatar_status_message(user)
763 if user.image_use_gravatar
764 t "users.account.gravatar.enabled"
766 t "users.account.gravatar.disabled"