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), :status => :temporary_redirect
265 # Save the user record
266 session[:new_user] = current_user
267 redirect_to :action => :terms
273 append_content_security_policy_directives(
274 :form_action => %w[accounts.google.com *.facebook.com login.live.com github.com meta.wikimedia.org]
277 session[:referer] = safe_referer(params[:referer]) if params[:referer]
279 if params[:username].present? && params[:password].present?
280 session[:remember_me] ||= params[:remember_me]
281 password_authentication(params[:username], params[:password])
286 @title = t "users.logout.title"
290 token = UserToken.find_by(:token => session[:token])
292 session.delete(:token)
294 session.delete(:user)
295 session_expires_automatically
297 redirect_to safe_referer(params[:referer])
299 redirect_to :controller => "site", :action => "index"
306 token = UserToken.find_by(:token => params[:confirm_string])
307 if token&.user&.active?
308 flash[:error] = t("users.confirm.already active")
309 redirect_to :action => "login"
310 elsif !token || token.expired?
311 flash[:error] = t("users.confirm.unknown token")
312 redirect_to :action => "confirm"
315 user.status = "active"
316 user.email_valid = true
317 flash[:notice] = gravatar_status_message(user) if gravatar_enable(user)
319 referer = safe_referer(token.referer) if token.referer
323 token = UserToken.find_by(:token => session[:token])
324 session.delete(:token)
329 if token.nil? || token.user != user
330 flash[:notice] = t("users.confirm.success")
331 redirect_to :action => :login, :referer => referer
335 session[:user] = user.id
336 session[:fingerprint] = user.fingerprint
338 redirect_to referer || welcome_path
342 user = User.find_by(:display_name => params[:display_name])
344 redirect_to root_path if user.nil? || user.active?
349 user = User.find_by(:display_name => params[:display_name])
350 token = UserToken.find_by(:token => session[:token])
352 if user.nil? || token.nil? || token.user != user
353 flash[:error] = t "users.confirm_resend.failure", :name => params[:display_name]
355 UserMailer.signup_confirm(user, user.tokens.create).deliver_later
356 flash[:notice] = t "users.confirm_resend.success_html", :email => user.email, :sender => Settings.support_email
359 redirect_to :action => "login"
364 token = UserToken.find_by(:token => params[:confirm_string])
365 if token&.user&.new_email?
366 self.current_user = token.user
367 current_user.email = current_user.new_email
368 current_user.new_email = nil
369 current_user.email_valid = true
370 gravatar_enabled = gravatar_enable(current_user)
372 flash[:notice] = if gravatar_enabled
373 "#{t('users.confirm_email.success')} #{gravatar_status_message(current_user)}"
375 t("users.confirm_email.success")
378 flash[:errors] = current_user.errors
380 current_user.tokens.delete_all
381 session[:user] = current_user.id
382 session[:fingerprint] = current_user.fingerprint
383 redirect_to :action => "account", :display_name => current_user.display_name
385 flash[:error] = t "users.confirm_email.failure"
386 redirect_to :action => "account", :display_name => token.user.display_name
388 flash[:error] = t "users.confirm_email.unknown_token"
394 @user = User.find_by(:display_name => params[:display_name])
397 (@user.visible? || current_user&.administrator?)
398 @title = @user.display_name
400 render_unknown_user params[:display_name]
405 # sets a user's status
407 @user.status = params[:status]
409 redirect_to user_path(:display_name => params[:display_name])
413 # delete a user, marking them as deleted and removing personal data
416 redirect_to user_path(:display_name => params[:display_name])
420 # display a list of users matching specified criteria
423 ids = params[:user].keys.collect(&:to_i)
425 User.where(:id => ids).update_all(:status => "confirmed") if params[:confirm]
426 User.where(:id => ids).update_all(:status => "deleted") if params[:hide]
428 redirect_to url_for(:status => params[:status], :ip => params[:ip], :page => params[:page])
430 @params = params.permit(:status, :ip)
433 conditions[:status] = @params[:status] if @params[:status]
434 conditions[:creation_ip] = @params[:ip] if @params[:ip]
436 @user_pages, @users = paginate(:users,
437 :conditions => conditions,
444 # omniauth success callback
446 auth_info = request.env["omniauth.auth"]
448 provider = auth_info[:provider]
449 uid = auth_info[:uid]
450 name = auth_info[:info][:name]
451 email = auth_info[:info][:email]
455 email_verified = uid.match(%r{https://www.google.com/accounts/o8/id?(.*)}) ||
456 uid.match(%r{https://me.yahoo.com/(.*)})
457 when "google", "facebook"
458 email_verified = true
460 email_verified = false
463 if settings = session.delete(:new_user_settings)
464 current_user.auth_provider = provider
465 current_user.auth_uid = uid
467 update_user(current_user, settings)
471 session[:user_errors] = current_user.errors.as_json
473 redirect_to :action => "account", :display_name => current_user.display_name
474 elsif session[:new_user]
475 session[:new_user].auth_provider = provider
476 session[:new_user].auth_uid = uid
478 session[:new_user].status = "active" if email_verified && email == session[:new_user].email
480 redirect_to :action => "terms"
482 user = User.find_by(:auth_provider => provider, :auth_uid => uid)
484 if user.nil? && provider == "google"
485 openid_url = auth_info[:extra][:id_info]["openid_id"]
486 user = User.find_by(:auth_provider => "openid", :auth_uid => openid_url) if openid_url
487 user&.update(:auth_provider => provider, :auth_uid => uid)
493 unconfirmed_login(user)
494 when "active", "confirmed"
495 successful_login(user, request.env["omniauth.params"]["referer"])
497 failed_login t("users.login.account is suspended", :webmaster => "mailto:#{Settings.support_email}").html_safe
499 failed_login t("users.login.auth failure")
502 redirect_to :action => "new", :nickname => name, :email => email,
503 :auth_provider => provider, :auth_uid => uid
509 # omniauth failure callback
511 flash[:error] = t("users.auth_failure.#{params[:message]}")
512 redirect_to params[:origin] || login_url
518 # handle password authentication
519 def password_authentication(username, password)
520 if user = User.authenticate(:username => username, :password => password)
521 successful_login(user)
522 elsif user = User.authenticate(:username => username, :password => password, :pending => true)
523 unconfirmed_login(user)
524 elsif User.authenticate(:username => username, :password => password, :suspended => true)
525 failed_login t("users.login.account is suspended", :webmaster => "mailto:#{Settings.support_email}").html_safe, username
527 failed_login t("users.login.auth failure"), username
532 # return the URL to use for authentication
533 def auth_url(provider, uid, referer = nil)
534 params = { :provider => provider }
536 params[:openid_url] = openid_expand_url(uid) if provider == "openid"
539 params[:origin] = request.path
541 params[:origin] = "#{request.path}?referer=#{CGI.escape(referer)}"
542 params[:referer] = referer
549 # special case some common OpenID providers by applying heuristics to
550 # try and come up with the correct URL based on what the user entered
551 def openid_expand_url(openid_url)
554 elsif openid_url.match(%r{(.*)gmail.com(/?)$}) || openid_url.match(%r{(.*)googlemail.com(/?)$})
555 # Special case gmail.com as it is potentially a popular OpenID
556 # provider and, unlike yahoo.com, where it works automatically, Google
557 # have hidden their OpenID endpoint somewhere obscure this making it
558 # somewhat less user friendly.
559 "https://www.google.com/accounts/o8/id"
566 # process a successful login
567 def successful_login(user, referer = nil)
568 session[:user] = user.id
569 session[:fingerprint] = user.fingerprint
570 session_expires_after 28.days if session[:remember_me]
572 target = referer || session[:referer] || url_for(:controller => :site, :action => :index)
574 # The user is logged in, so decide where to send them:
576 # - If they haven't seen the contributor terms, send them there.
577 # - If they have a block on them, show them that.
578 # - If they were referred to the login, send them back there.
579 # - Otherwise, send them to the home page.
581 redirect_to :action => :terms, :referer => target
582 elsif user.blocked_on_view
583 redirect_to user.blocked_on_view, :referer => target
588 session.delete(:remember_me)
589 session.delete(:referer)
593 # process a failed login
594 def failed_login(message, username = nil)
595 flash[:error] = message
597 redirect_to :action => "login", :referer => session[:referer],
598 :username => username, :remember_me => session[:remember_me]
600 session.delete(:remember_me)
601 session.delete(:referer)
606 def unconfirmed_login(user)
607 session[:token] = user.tokens.create.token
609 redirect_to :action => "confirm", :display_name => user.display_name
611 session.delete(:remember_me)
612 session.delete(:referer)
616 # update a user's details
617 def update_user(user, params)
618 user.display_name = params[:user][:display_name]
619 user.new_email = params[:user][:new_email]
621 unless params[:user][:pass_crypt].empty? && params[:user][:pass_crypt_confirmation].empty?
622 user.pass_crypt = params[:user][:pass_crypt]
623 user.pass_crypt_confirmation = params[:user][:pass_crypt_confirmation]
626 if params[:user][:description] != user.description
627 user.description = params[:user][:description]
628 user.description_format = "markdown"
631 user.languages = params[:user][:languages].split(",")
633 case params[:avatar_action]
635 user.avatar.attach(params[:user][:avatar])
636 user.image_use_gravatar = false
638 user.avatar.purge_later
639 user.image_use_gravatar = false
641 user.avatar.purge_later
642 user.image_use_gravatar = true
645 user.home_lat = params[:user][:home_lat]
646 user.home_lon = params[:user][:home_lon]
648 user.preferred_editor = if params[:user][:preferred_editor] == "default"
651 params[:user][:preferred_editor]
654 if params[:user][:auth_provider].nil? || params[:user][:auth_provider].blank?
655 user.auth_provider = nil
660 session[:fingerprint] = user.fingerprint
662 set_locale(:reset => true)
664 if user.new_email.blank? || user.new_email == user.email
665 flash[:notice] = t "users.account.flash update success"
667 user.email = user.new_email
670 flash[:notice] = t "users.account.flash update success confirm needed"
673 UserMailer.email_confirm(user, user.tokens.create).deliver_later
675 # Ignore errors sending email
678 current_user.errors.add(:new_email, current_user.errors[:email])
679 current_user.errors.add(:email, [])
688 # require that the user in the URL is the logged in user
690 head :forbidden if params[:display_name] != current_user.display_name
694 # ensure that there is a "user" instance variable
695 def lookup_user_by_name
696 @user = User.find_by(:display_name => params[:display_name])
697 rescue ActiveRecord::RecordNotFound
698 redirect_to :action => "view", :display_name => params[:display_name] unless @user
703 def disable_terms_redirect
704 # this is necessary otherwise going to the user terms page, when
705 # having not agreed already would cause an infinite redirect loop.
706 # it's .now so that this doesn't propagate to other pages.
707 flash.now[:skip_terms] = true
711 # return permitted user parameters
713 params.require(:user).permit(:email, :email_confirmation, :display_name,
714 :auth_provider, :auth_uid,
715 :pass_crypt, :pass_crypt_confirmation)
720 def check_signup_allowed(email = nil)
721 domain = if email.nil?
724 email.split("@").last
727 mx_servers = if domain.nil?
730 domain_mx_servers(domain)
733 if blocked = Acl.no_account_creation(request.remote_ip, :domain => domain, :mx => mx_servers)
734 logger.info "Blocked signup from #{request.remote_ip} for #{email}"
736 render :action => "blocked"
743 # get list of MX servers for a domains
744 def domain_mx_servers(domain)
745 Resolv::DNS.open do |dns|
746 dns.getresources(domain, Resolv::DNS::Resource::IN::MX).collect(&:exchange).collect(&:to_s)
751 # check if this user has a gravatar and set the user pref is true
752 def gravatar_enable(user)
753 # code from example https://en.gravatar.com/site/implement/images/ruby/
754 return false if user.avatar.attached?
756 hash = Digest::MD5.hexdigest(user.email.downcase)
757 url = "https://www.gravatar.com/avatar/#{hash}?d=404" # without d=404 we will always get an image back
758 response = OSM.http_client.get(URI.parse(url))
759 oldsetting = user.image_use_gravatar
760 user.image_use_gravatar = response.success?
761 oldsetting != user.image_use_gravatar
765 # display a message about th current status of the gravatar setting
766 def gravatar_status_message(user)
767 if user.image_use_gravatar
768 t "users.account.gravatar.enabled"
770 t "users.account.gravatar.disabled"