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 Notifier.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 if params[:user] && params[:user][:display_name] && params[:user][:description]
123 if params[:user][:auth_provider].blank? ||
124 (params[:user][:auth_provider] == current_user.auth_provider &&
125 params[:user][:auth_uid] == current_user.auth_uid)
126 update_user(current_user, params)
128 session[:new_user_settings] = params
129 redirect_to auth_url(params[:user][:auth_provider], params[:user][:auth_uid])
131 elsif errors = session.delete(:user_errors)
132 errors.each do |attribute, error|
133 current_user.errors.add(attribute, error)
136 @title = t "users.account.title"
140 current_user.data_public = true
142 flash[:notice] = t "users.go_public.flash success"
143 redirect_to :action => "account", :display_name => current_user.display_name
147 @title = t "users.lost_password.title"
149 if params[:user] && params[:user][:email]
150 user = User.visible.find_by(:email => params[:user][:email])
153 users = User.visible.where("LOWER(email) = LOWER(?)", params[:user][:email])
155 user = users.first if users.count == 1
159 token = user.tokens.create
160 Notifier.lost_password(user, token).deliver_later
161 flash[:notice] = t "users.lost_password.notice email on way"
162 redirect_to :action => "login"
164 flash.now[:error] = t "users.lost_password.notice email cannot find"
170 @title = t "users.reset_password.title"
173 token = UserToken.find_by(:token => params[:token])
176 self.current_user = token.user
179 current_user.pass_crypt = params[:user][:pass_crypt]
180 current_user.pass_crypt_confirmation = params[:user][:pass_crypt_confirmation]
181 current_user.status = "active" if current_user.status == "pending"
182 current_user.email_valid = true
186 flash[:notice] = t "users.reset_password.flash changed"
187 successful_login(current_user)
191 flash[:error] = t "users.reset_password.flash token bad"
192 redirect_to :action => "lost_password"
200 @title = t "users.new.title"
201 @referer = if params[:referer]
202 safe_referer(params[:referer])
207 append_content_security_policy_directives(
208 :form_action => %w[accounts.google.com *.facebook.com login.live.com github.com meta.wikimedia.org]
212 # The user is logged in already, so don't show them the signup
213 # page, instead send them to the home page
217 redirect_to :controller => "site", :action => "index"
219 elsif params.key?(:auth_provider) && params.key?(:auth_uid)
220 self.current_user = User.new(:email => params[:email],
221 :email_confirmation => params[:email],
222 :display_name => params[:nickname],
223 :auth_provider => params[:auth_provider],
224 :auth_uid => params[:auth_uid])
226 flash.now[:notice] = render_to_string :partial => "auth_association"
230 self.current_user = User.new
235 self.current_user = User.new(user_params)
237 if check_signup_allowed(current_user.email)
238 session[:referer] = safe_referer(params[:referer]) if params[:referer]
240 Rails.logger.info "create: #{session[:referer]}"
242 current_user.status = "pending"
244 if current_user.auth_provider.present? && current_user.pass_crypt.empty?
245 # We are creating an account with external authentication and
246 # no password was specified so create a random one
247 current_user.pass_crypt = SecureRandom.base64(16)
248 current_user.pass_crypt_confirmation = current_user.pass_crypt
251 if current_user.invalid?
252 # Something is wrong with a new user, so rerender the form
253 render :action => "new"
254 elsif current_user.auth_provider.present?
255 # Verify external authenticator before moving on
256 session[:new_user] = current_user
257 redirect_to auth_url(current_user.auth_provider, current_user.auth_uid)
259 # Save the user record
260 session[:new_user] = current_user
261 redirect_to :action => :terms
267 session[:referer] = safe_referer(params[:referer]) if params[:referer]
269 if params[:username].present? && params[:password].present?
270 session[:remember_me] ||= params[:remember_me]
271 password_authentication(params[:username], params[:password])
276 @title = t "users.logout.title"
280 token = UserToken.find_by(:token => session[:token])
282 session.delete(:token)
284 session.delete(:user)
285 session_expires_automatically
287 redirect_to safe_referer(params[:referer])
289 redirect_to :controller => "site", :action => "index"
296 token = UserToken.find_by(:token => params[:confirm_string])
297 if token&.user&.active?
298 flash[:error] = t("users.confirm.already active")
299 redirect_to :action => "login"
300 elsif !token || token.expired?
301 flash[:error] = t("users.confirm.unknown token")
302 redirect_to :action => "confirm"
305 user.status = "active"
306 user.email_valid = true
307 flash[:notice] = gravatar_status_message(user) if gravatar_enable(user)
309 referer = safe_referer(token.referer) if token.referer
313 token = UserToken.find_by(:token => session[:token])
314 session.delete(:token)
319 if token.nil? || token.user != user
320 flash[:notice] = t("users.confirm.success")
321 redirect_to :action => :login, :referer => referer
325 session[:user] = user.id
327 redirect_to referer || welcome_path
331 user = User.find_by(:display_name => params[:display_name])
333 redirect_to root_path if user.nil? || user.active?
338 user = User.find_by(:display_name => params[:display_name])
339 token = UserToken.find_by(:token => session[:token])
341 if user.nil? || token.nil? || token.user != user
342 flash[:error] = t "users.confirm_resend.failure", :name => params[:display_name]
344 Notifier.signup_confirm(user, user.tokens.create).deliver_later
345 flash[:notice] = t("users.confirm_resend.success", :email => user.email, :sender => Settings.support_email).html_safe
348 redirect_to :action => "login"
353 token = UserToken.find_by(:token => params[:confirm_string])
354 if token&.user&.new_email?
355 self.current_user = token.user
356 current_user.email = current_user.new_email
357 current_user.new_email = nil
358 current_user.email_valid = true
359 gravatar_enabled = gravatar_enable(current_user)
361 flash[:notice] = if gravatar_enabled
362 "#{t('users.confirm_email.success')} #{gravatar_status_message(current_user)}"
364 t("users.confirm_email.success")
367 flash[:errors] = current_user.errors
370 session[:user] = current_user.id
371 redirect_to :action => "account", :display_name => current_user.display_name
373 flash[:error] = t "users.confirm_email.failure"
374 redirect_to :action => "account", :display_name => token.user.display_name
376 flash[:error] = t "users.confirm_email.unknown_token"
382 @user = User.find_by(:display_name => params[:display_name])
385 (@user.visible? || current_user&.administrator?)
386 @title = @user.display_name
388 render_unknown_user params[:display_name]
393 # sets a user's status
395 @user.status = params[:status]
397 redirect_to user_path(:display_name => params[:display_name])
401 # delete a user, marking them as deleted and removing personal data
404 redirect_to user_path(:display_name => params[:display_name])
408 # display a list of users matching specified criteria
411 ids = params[:user].keys.collect(&:to_i)
413 User.where(:id => ids).update_all(:status => "confirmed") if params[:confirm]
414 User.where(:id => ids).update_all(:status => "deleted") if params[:hide]
416 redirect_to url_for(:status => params[:status], :ip => params[:ip], :page => params[:page])
418 @params = params.permit(:status, :ip)
421 conditions[:status] = @params[:status] if @params[:status]
422 conditions[:creation_ip] = @params[:ip] if @params[:ip]
424 @user_pages, @users = paginate(:users,
425 :conditions => conditions,
432 # omniauth success callback
434 auth_info = request.env["omniauth.auth"]
436 provider = auth_info[:provider]
437 uid = auth_info[:uid]
438 name = auth_info[:info][:name]
439 email = auth_info[:info][:email]
443 email_verified = uid.match(%r{https://www.google.com/accounts/o8/id?(.*)}) ||
444 uid.match(%r{https://me.yahoo.com/(.*)})
445 when "google", "facebook"
446 email_verified = true
448 email_verified = false
451 if settings = session.delete(:new_user_settings)
452 current_user.auth_provider = provider
453 current_user.auth_uid = uid
455 update_user(current_user, settings)
457 session[:user_errors] = current_user.errors.as_json
459 redirect_to :action => "account", :display_name => current_user.display_name
460 elsif session[:new_user]
461 session[:new_user].auth_provider = provider
462 session[:new_user].auth_uid = uid
464 session[:new_user].status = "active" if email_verified && email == session[:new_user].email
466 redirect_to :action => "terms"
468 user = User.find_by(:auth_provider => provider, :auth_uid => uid)
470 if user.nil? && provider == "google"
471 openid_url = auth_info[:extra][:id_info]["openid_id"]
472 user = User.find_by(:auth_provider => "openid", :auth_uid => openid_url) if openid_url
473 user&.update(:auth_provider => provider, :auth_uid => uid)
479 unconfirmed_login(user)
480 when "active", "confirmed"
481 successful_login(user, request.env["omniauth.params"]["referer"])
483 failed_login t("users.login.account is suspended", :webmaster => "mailto:#{Settings.support_email}").html_safe
485 failed_login t("users.login.auth failure")
488 redirect_to :action => "new", :nickname => name, :email => email,
489 :auth_provider => provider, :auth_uid => uid
495 # omniauth failure callback
497 flash[:error] = t("users.auth_failure.#{params[:message]}")
498 redirect_to params[:origin] || login_url
504 # handle password authentication
505 def password_authentication(username, password)
506 if user = User.authenticate(:username => username, :password => password)
507 successful_login(user)
508 elsif user = User.authenticate(:username => username, :password => password, :pending => true)
509 unconfirmed_login(user)
510 elsif User.authenticate(:username => username, :password => password, :suspended => true)
511 failed_login t("users.login.account is suspended", :webmaster => "mailto:#{Settings.support_email}").html_safe, username
513 failed_login t("users.login.auth failure"), username
518 # return the URL to use for authentication
519 def auth_url(provider, uid, referer = nil)
520 params = { :provider => provider }
522 params[:openid_url] = openid_expand_url(uid) if provider == "openid"
525 params[:origin] = request.path
527 params[:origin] = "#{request.path}?referer=#{CGI.escape(referer)}"
528 params[:referer] = referer
535 # special case some common OpenID providers by applying heuristics to
536 # try and come up with the correct URL based on what the user entered
537 def openid_expand_url(openid_url)
540 elsif openid_url.match(%r{(.*)gmail.com(/?)$}) || openid_url.match(%r{(.*)googlemail.com(/?)$})
541 # Special case gmail.com as it is potentially a popular OpenID
542 # provider and, unlike yahoo.com, where it works automatically, Google
543 # have hidden their OpenID endpoint somewhere obscure this making it
544 # somewhat less user friendly.
545 "https://www.google.com/accounts/o8/id"
552 # process a successful login
553 def successful_login(user, referer = nil)
554 session[:user] = user.id
555 session_expires_after 28.days if session[:remember_me]
557 target = referer || session[:referer] || url_for(:controller => :site, :action => :index)
559 # The user is logged in, so decide where to send them:
561 # - If they haven't seen the contributor terms, send them there.
562 # - If they have a block on them, show them that.
563 # - If they were referred to the login, send them back there.
564 # - Otherwise, send them to the home page.
566 redirect_to :action => :terms, :referer => target
567 elsif user.blocked_on_view
568 redirect_to user.blocked_on_view, :referer => target
573 session.delete(:remember_me)
574 session.delete(:referer)
578 # process a failed login
579 def failed_login(message, username = nil)
580 flash[:error] = message
582 redirect_to :action => "login", :referer => session[:referer],
583 :username => username, :remember_me => session[:remember_me]
585 session.delete(:remember_me)
586 session.delete(:referer)
591 def unconfirmed_login(user)
592 session[:token] = user.tokens.create.token
594 redirect_to :action => "confirm", :display_name => user.display_name
596 session.delete(:remember_me)
597 session.delete(:referer)
601 # update a user's details
602 def update_user(user, params)
603 user.display_name = params[:user][:display_name]
604 user.new_email = params[:user][:new_email]
606 unless params[:user][:pass_crypt].empty? && params[:user][:pass_crypt_confirmation].empty?
607 user.pass_crypt = params[:user][:pass_crypt]
608 user.pass_crypt_confirmation = params[:user][:pass_crypt_confirmation]
611 if params[:user][:description] != user.description
612 user.description = params[:user][:description]
613 user.description_format = "markdown"
616 user.languages = params[:user][:languages].split(",")
618 case params[:avatar_action]
620 user.avatar.attach(params[:user][:avatar])
621 user.image_use_gravatar = false
623 user.avatar.purge_later
624 user.image_use_gravatar = false
626 user.avatar.purge_later
627 user.image_use_gravatar = true
630 user.home_lat = params[:user][:home_lat]
631 user.home_lon = params[:user][:home_lon]
633 user.preferred_editor = if params[:user][:preferred_editor] == "default"
636 params[:user][:preferred_editor]
639 if params[:user][:auth_provider].nil? || params[:user][:auth_provider].blank?
640 user.auth_provider = nil
647 if user.new_email.blank? || user.new_email == user.email
648 flash.now[:notice] = t "users.account.flash update success"
650 user.email = user.new_email
653 flash.now[:notice] = t "users.account.flash update success confirm needed"
656 Notifier.email_confirm(user, user.tokens.create).deliver_later
658 # Ignore errors sending email
661 current_user.errors.add(:new_email, current_user.errors[:email])
662 current_user.errors.add(:email, [])
671 # require that the user in the URL is the logged in user
673 head :forbidden if params[:display_name] != current_user.display_name
677 # ensure that there is a "user" instance variable
678 def lookup_user_by_name
679 @user = User.find_by(:display_name => params[:display_name])
680 rescue ActiveRecord::RecordNotFound
681 redirect_to :action => "view", :display_name => params[:display_name] unless @user
686 def disable_terms_redirect
687 # this is necessary otherwise going to the user terms page, when
688 # having not agreed already would cause an infinite redirect loop.
689 # it's .now so that this doesn't propagate to other pages.
690 flash.now[:skip_terms] = true
694 # return permitted user parameters
696 params.require(:user).permit(:email, :email_confirmation, :display_name,
697 :auth_provider, :auth_uid,
698 :pass_crypt, :pass_crypt_confirmation)
703 def check_signup_allowed(email = nil)
704 domain = if email.nil?
707 email.split("@").last
710 mx_servers = if domain.nil?
713 domain_mx_servers(domain)
716 if blocked = Acl.no_account_creation(request.remote_ip, :domain => domain, :mx => mx_servers)
717 logger.info "Blocked signup from #{request.remote_ip} for #{email}"
719 render :action => "blocked"
726 # get list of MX servers for a domains
727 def domain_mx_servers(domain)
728 Resolv::DNS.open do |dns|
729 dns.getresources(domain, Resolv::DNS::Resource::IN::MX).collect(&:exchange).collect(&:to_s)
734 # check if this user has a gravatar and set the user pref is true
735 def gravatar_enable(user)
736 # code from example https://en.gravatar.com/site/implement/images/ruby/
737 return false if user.avatar.attached?
739 hash = Digest::MD5.hexdigest(user.email.downcase)
740 url = "https://www.gravatar.com/avatar/#{hash}?d=404" # without d=404 we will always get an image back
741 response = OSM.http_client.get(URI.parse(url))
742 oldsetting = user.image_use_gravatar
743 user.image_use_gravatar = response.success?
744 oldsetting != user.image_use_gravatar
748 # display a message about th current status of the gravatar setting
749 def gravatar_status_message(user)
750 if user.image_use_gravatar
751 t "users.account.gravatar.enabled"
753 t "users.account.gravatar.disabled"