1 class UserController < ApplicationController
2 layout "site", :except => [:api_details]
4 skip_before_action :verify_authenticity_token, :only => [:api_read, :api_details, :api_gpx_files, :auth_success]
5 before_action :disable_terms_redirect, :only => [:terms, :save, :logout, :api_details]
6 before_action :authorize, :only => [:api_details, :api_gpx_files]
7 before_action :authorize_web, :except => [:api_read, :api_details, :api_gpx_files]
8 before_action :set_locale, :except => [:api_read, :api_details, :api_gpx_files]
9 before_action :require_user, :only => [:account, :go_public, :make_friend, :remove_friend]
10 before_action :require_self, :only => [:account]
11 before_action :check_database_readable, :except => [:login, :api_read, :api_details, :api_gpx_files]
12 before_action :check_database_writable, :only => [:new, :account, :confirm, :confirm_email, :lost_password, :reset_password, :go_public, :make_friend, :remove_friend]
13 before_action :check_api_readable, :only => [:api_read, :api_details, :api_gpx_files]
14 before_action :require_allow_read_prefs, :only => [:api_details]
15 before_action :require_allow_read_gpx, :only => [:api_gpx_files]
16 before_action :require_cookies, :only => [:new, :login, :confirm]
17 before_action :require_administrator, :only => [:set_status, :delete, :list]
18 around_action :api_call_handle_error, :only => [:api_read, :api_details, :api_gpx_files]
19 before_action :lookup_user_by_id, :only => [:api_read]
20 before_action :lookup_user_by_name, :only => [:set_status, :delete]
23 @legale = params[:legale] || OSM.ip_to_country(request.remote_ip) || DEFAULT_LEGALE
24 @text = OSM.legal_text_for_country(@legale)
27 render :partial => "terms"
29 @title = t "user.terms.title"
31 if @user && @user.terms_agreed?
32 # Already agreed to terms, so just show settings
33 redirect_to :action => :account, :display_name => @user.display_name
34 elsif @user.nil? && session[:new_user].nil?
35 redirect_to :action => :login, :referer => request.fullpath
41 @title = t "user.new.title"
45 @user.terms_seen = true
48 flash[:notice] = t("user.new.terms declined", :url => t("user.new.terms declined url")).html_safe
52 redirect_to params[:referer]
54 redirect_to :action => :account, :display_name => @user.display_name
57 redirect_to t("user.terms.declined")
60 unless @user.terms_agreed?
61 @user.consider_pd = params[:user][:consider_pd]
62 @user.terms_agreed = Time.now.getutc
63 @user.terms_seen = true
65 flash[:notice] = t "user.new.terms accepted" if @user.save
69 redirect_to params[:referer]
71 redirect_to :action => :account, :display_name => @user.display_name
74 @user = session.delete(:new_user)
76 if check_signup_allowed(@user.email)
77 @user.data_public = true
78 @user.description = "" if @user.description.nil?
79 @user.creation_ip = request.remote_ip
80 @user.languages = http_accept_language.user_preferred_languages
81 @user.terms_agreed = Time.now.getutc
82 @user.terms_seen = true
84 if @user.auth_uid.nil? || @user.auth_uid.empty?
85 @user.auth_provider = 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 @user.status == "active"
107 session[:referer] = referer
108 successful_login(@user)
110 session[:token] = @user.tokens.create.token
111 Notifier.signup_confirm(@user, @user.tokens.create(:referer => referer)).deliver_now
112 redirect_to :action => "confirm", :display_name => @user.display_name
115 render :action => "new", :referer => params[:referer]
122 @title = t "user.account.title"
123 @tokens = @user.oauth_tokens.authorized
125 if params[:user] && params[:user][:display_name] && params[:user][:description]
126 if params[:user][:auth_provider].blank? ||
127 (params[:user][:auth_provider] == @user.auth_provider &&
128 params[:user][:auth_uid] == @user.auth_uid)
129 update_user(@user, params)
131 session[:new_user_settings] = params
132 redirect_to auth_url(params[:user][:auth_provider], params[:user][:auth_uid])
134 elsif errors = session.delete(:user_errors)
135 errors.each do |attribute, error|
136 @user.errors.add(attribute, error)
142 @user.data_public = true
144 flash[:notice] = t "user.go_public.flash success"
145 redirect_to :action => "account", :display_name => @user.display_name
149 @title = t "user.lost_password.title"
151 if params[:user] && params[:user][:email]
152 user = User.visible.find_by(:email => params[:user][:email])
155 users = User.visible.where("LOWER(email) = LOWER(?)", params[:user][:email])
157 user = users.first if users.count == 1
161 token = user.tokens.create
162 Notifier.lost_password(user, token).deliver_now
163 flash[:notice] = t "user.lost_password.notice email on way"
164 redirect_to :action => "login"
166 flash.now[:error] = t "user.lost_password.notice email cannot find"
172 @title = t "user.reset_password.title"
175 token = UserToken.find_by(:token => params[:token])
181 @user.pass_crypt = params[:user][:pass_crypt]
182 @user.pass_crypt_confirmation = params[:user][:pass_crypt_confirmation]
183 @user.status = "active" if @user.status == "pending"
184 @user.email_valid = true
188 flash[:notice] = t "user.reset_password.flash changed"
189 successful_login(@user)
193 flash[:error] = t "user.reset_password.flash token bad"
194 redirect_to :action => "lost_password"
197 render :text => "", :status => :bad_request
202 @title = t "user.new.title"
203 @referer = params[:referer] || session[:referer]
206 # The user is logged in already, so don't show them the signup
207 # page, instead send them to the home page
211 redirect_to :controller => "site", :action => "index"
213 elsif params.key?(:auth_provider) && params.key?(:auth_uid)
214 @user = User.new(:email => params[:email],
215 :email_confirmation => params[:email],
216 :display_name => params[:nickname],
217 :auth_provider => params[:auth_provider],
218 :auth_uid => params[:auth_uid])
220 flash.now[:notice] = render_to_string :partial => "auth_association"
227 @user = User.new(user_params)
229 if check_signup_allowed(@user.email)
230 session[:referer] = params[:referer]
232 @user.status = "pending"
234 if @user.auth_provider.present? && @user.pass_crypt.empty?
235 # We are creating an account with external authentication and
236 # no password was specified so create a random one
237 @user.pass_crypt = SecureRandom.base64(16)
238 @user.pass_crypt_confirmation = @user.pass_crypt
242 # Something is wrong with a new user, so rerender the form
243 render :action => "new"
244 elsif @user.auth_provider.present?
245 # Verify external authenticator before moving on
246 session[:new_user] = @user
247 redirect_to auth_url(@user.auth_provider, @user.auth_uid)
249 # Save the user record
250 session[:new_user] = @user
251 redirect_to :action => :terms
257 session[:referer] = params[:referer] if params[:referer]
259 if params[:username].present? && params[:password].present?
260 session[:remember_me] ||= params[:remember_me]
261 password_authentication(params[:username], params[:password])
262 elsif params[:openid_url].present?
263 session[:remember_me] ||= params[:remember_me_openid]
264 redirect_to auth_url("openid", params[:openid_url], params[:referer])
269 @title = t "user.logout.title"
271 if params[:session] == request.session_options[:id]
273 token = UserToken.find_by(:token => session[:token])
274 token.destroy if token
275 session.delete(:token)
277 session.delete(:user)
278 session_expires_automatically
280 redirect_to params[:referer]
282 redirect_to :controller => "site", :action => "index"
289 token = UserToken.find_by(:token => params[:confirm_string])
290 if token && token.user.active?
291 flash[:error] = t("user.confirm.already active")
292 redirect_to :action => "login"
293 elsif !token || token.expired?
294 flash[:error] = t("user.confirm.unknown token")
295 redirect_to :action => "confirm"
298 user.status = "active"
299 user.email_valid = true
300 flash[:notice] = gravatar_status_message(user) if gravatar_enable(user)
302 referer = token.referer
306 token = UserToken.find_by(:token => session[:token])
307 session.delete(:token)
312 if token.nil? || token.user != user
313 flash[:notice] = t("user.confirm.success")
314 redirect_to :action => :login, :referer => referer
318 session[:user] = user.id
320 redirect_to referer || welcome_path
324 user = User.find_by(:display_name => params[:display_name])
326 redirect_to root_path if user.nil? || user.active?
331 user = User.find_by(:display_name => params[:display_name])
332 token = UserToken.find_by(:token => session[:token])
334 if user.nil? || token.nil? || token.user != user
335 flash[:error] = t "user.confirm_resend.failure", :name => params[:display_name]
337 Notifier.signup_confirm(user, user.tokens.create).deliver_now
338 flash[:notice] = t("user.confirm_resend.success", :email => user.email, :sender => SUPPORT_EMAIL).html_safe
341 redirect_to :action => "login"
346 token = UserToken.find_by(:token => params[:confirm_string])
347 if token && token.user.new_email?
349 @user.email = @user.new_email
350 @user.new_email = nil
351 @user.email_valid = true
352 gravatar_enabled = gravatar_enable(@user)
354 flash[:notice] = if gravatar_enabled
355 t("user.confirm_email.success") + " " + gravatar_status_message(@user)
357 t("user.confirm_email.success")
360 flash[:errors] = @user.errors
363 session[:user] = @user.id
364 redirect_to :action => "account", :display_name => @user.display_name
366 flash[:error] = t "user.confirm_email.failure"
367 redirect_to :action => "account", :display_name => token.user.display_name
369 flash[:error] = t "user.confirm_email.unknown_token"
375 if @this_user.visible?
376 render :action => :api_read, :content_type => "text/xml"
378 render :text => "", :status => :gone
384 render :action => :api_read, :content_type => "text/xml"
388 doc = OSM::API.new.get_xml_doc
389 @user.traces.reload.each do |trace|
390 doc.root << trace.to_xml_node
392 render :text => doc.to_s, :content_type => "text/xml"
396 @this_user = User.find_by(:display_name => params[:display_name])
399 (@this_user.visible? || (@user && @user.administrator?))
400 @title = @this_user.display_name
402 render_unknown_user params[:display_name]
407 @new_friend = User.find_by(:display_name => params[:display_name])
412 friend.user_id = @user.id
413 friend.friend_user_id = @new_friend.id
414 if @user.is_friends_with?(@new_friend)
415 flash[:warning] = t "user.make_friend.already_a_friend", :name => @new_friend.display_name
417 flash[:notice] = t "user.make_friend.success", :name => @new_friend.display_name
418 Notifier.friend_notification(friend).deliver_now
420 friend.add_error(t("user.make_friend.failed", :name => @new_friend.display_name))
424 redirect_to params[:referer]
426 redirect_to :action => "view"
430 render_unknown_user params[:display_name]
435 @friend = User.find_by(:display_name => params[:display_name])
439 if @user.is_friends_with?(@friend)
440 Friend.delete_all "user_id = #{@user.id} AND friend_user_id = #{@friend.id}"
441 flash[:notice] = t "user.remove_friend.success", :name => @friend.display_name
443 flash[:error] = t "user.remove_friend.not_a_friend", :name => @friend.display_name
447 redirect_to params[:referer]
449 redirect_to :action => "view"
453 render_unknown_user params[:display_name]
458 # sets a user's status
460 @this_user.status = params[:status]
462 redirect_to :action => "view", :display_name => params[:display_name]
466 # delete a user, marking them as deleted and removing personal data
469 redirect_to :action => "view", :display_name => params[:display_name]
473 # display a list of users matching specified criteria
476 ids = params[:user].keys.collect(&:to_i)
478 User.where(:id => ids).update_all(:status => "confirmed") if params[:confirm]
479 User.where(:id => ids).update_all(:status => "deleted") if params[:hide]
481 redirect_to url_for(:status => params[:status], :ip => params[:ip], :page => params[:page])
484 conditions[:status] = params[:status] if params[:status]
485 conditions[:creation_ip] = params[:ip] if params[:ip]
487 @user_pages, @users = paginate(:users,
488 :conditions => conditions,
495 # omniauth success callback
497 auth_info = env["omniauth.auth"]
499 provider = auth_info[:provider]
500 uid = auth_info[:uid]
501 name = auth_info[:info][:name]
502 email = auth_info[:info][:email]
506 email_verified = uid.match(%r{https://www.google.com/accounts/o8/id?(.*)}) ||
507 uid.match(%r{https://me.yahoo.com/(.*)})
508 when "google", "facebook"
509 email_verified = true
511 email_verified = false
514 if settings = session.delete(:new_user_settings)
515 @user.auth_provider = provider
518 update_user(@user, settings)
520 session[:user_errors] = @user.errors.as_json
522 redirect_to :action => "account", :display_name => @user.display_name
523 elsif session[:new_user]
524 session[:new_user].auth_provider = provider
525 session[:new_user].auth_uid = uid
527 if email_verified && email == session[:new_user].email
528 session[:new_user].status = "active"
531 redirect_to :action => "terms"
533 user = User.find_by(:auth_provider => provider, :auth_uid => uid)
535 if user.nil? && provider == "google"
536 openid_url = auth_info[:extra][:id_info]["openid_id"]
537 user = User.find_by(:auth_provider => "openid", :auth_uid => openid_url) if openid_url
538 user.update(:auth_provider => provider, :auth_uid => uid) if user
544 unconfirmed_login(user)
545 when "active", "confirmed" then
546 successful_login(user, env["omniauth.params"]["referer"])
547 when "suspended" then
548 failed_login t("user.login.account is suspended", :webmaster => "mailto:#{SUPPORT_EMAIL}")
550 failed_login t("user.login.auth failure")
553 redirect_to :action => "new", :nickname => name, :email => email,
554 :auth_provider => provider, :auth_uid => uid
560 # omniauth failure callback
562 flash[:error] = t("user.auth_failure." + params[:message])
563 redirect_to params[:origin] || login_url
569 # handle password authentication
570 def password_authentication(username, password)
571 if user = User.authenticate(:username => username, :password => password)
572 successful_login(user)
573 elsif user = User.authenticate(:username => username, :password => password, :pending => true)
574 unconfirmed_login(user)
575 elsif User.authenticate(:username => username, :password => password, :suspended => true)
576 failed_login t("user.login.account is suspended", :webmaster => "mailto:#{SUPPORT_EMAIL}"), username
578 failed_login t("user.login.auth failure"), username
583 # return the URL to use for authentication
584 def auth_url(provider, uid, referer = nil)
585 params = { :provider => provider }
587 params[:openid_url] = openid_expand_url(uid) if provider == "openid"
590 params[:origin] = request.path
592 params[:origin] = request.path + "?referer=" + CGI.escape(referer)
593 params[:referer] = referer
600 # special case some common OpenID providers by applying heuristics to
601 # try and come up with the correct URL based on what the user entered
602 def openid_expand_url(openid_url)
605 elsif openid_url.match(%r{(.*)gmail.com(/?)$}) || openid_url.match(%r{(.*)googlemail.com(/?)$})
606 # Special case gmail.com as it is potentially a popular OpenID
607 # provider and, unlike yahoo.com, where it works automatically, Google
608 # have hidden their OpenID endpoint somewhere obscure this making it
609 # somewhat less user friendly.
610 "https://www.google.com/accounts/o8/id"
617 # process a successful login
618 def successful_login(user, referer = nil)
619 session[:user] = user.id
620 session_expires_after 28.days if session[:remember_me]
622 target = referer || session[:referer] || url_for(:controller => :site, :action => :index)
624 # The user is logged in, so decide where to send them:
626 # - If they haven't seen the contributor terms, send them there.
627 # - If they have a block on them, show them that.
628 # - If they were referred to the login, send them back there.
629 # - Otherwise, send them to the home page.
630 if REQUIRE_TERMS_SEEN && !user.terms_seen
631 redirect_to :action => :terms, :referer => target
632 elsif user.blocked_on_view
633 redirect_to user.blocked_on_view, :referer => target
638 session.delete(:remember_me)
639 session.delete(:referer)
643 # process a failed login
644 def failed_login(message, username = nil)
645 flash[:error] = message
647 redirect_to :action => "login", :referer => session[:referer],
648 :username => username, :remember_me => session[:remember_me]
650 session.delete(:remember_me)
651 session.delete(:referer)
656 def unconfirmed_login(user)
657 session[:token] = user.tokens.create.token
659 redirect_to :action => "confirm", :display_name => user.display_name
661 session.delete(:remember_me)
662 session.delete(:referer)
666 # update a user's details
667 def update_user(user, params)
668 user.display_name = params[:user][:display_name]
669 user.new_email = params[:user][:new_email]
671 unless params[:user][:pass_crypt].empty? && params[:user][:pass_crypt_confirmation].empty?
672 user.pass_crypt = params[:user][:pass_crypt]
673 user.pass_crypt_confirmation = params[:user][:pass_crypt_confirmation]
676 if params[:user][:description] != user.description
677 user.description = params[:user][:description]
678 user.description_format = "markdown"
681 user.languages = params[:user][:languages].split(",")
683 case params[:image_action]
685 user.image = params[:user][:image]
686 user.image_use_gravatar = false
689 user.image_use_gravatar = false
692 user.image_use_gravatar = true
695 user.home_lat = params[:user][:home_lat]
696 user.home_lon = params[:user][:home_lon]
698 user.preferred_editor = if params[:user][:preferred_editor] == "default"
701 params[:user][:preferred_editor]
704 if params[:user][:auth_provider].nil? || params[:user][:auth_provider].blank?
705 user.auth_provider = nil
712 if user.new_email.blank? || user.new_email == user.email
713 flash.now[:notice] = t "user.account.flash update success"
715 user.email = user.new_email
718 flash.now[:notice] = t "user.account.flash update success confirm needed"
721 Notifier.email_confirm(user, user.tokens.create).deliver_now
723 # Ignore errors sending email
726 @user.errors.set(:new_email, @user.errors.get(:email))
727 @user.errors.set(:email, [])
736 # require that the user is a administrator, or fill out a helpful error message
737 # and return them to the user page.
738 def require_administrator
739 if @user && !@user.administrator?
740 flash[:error] = t("user.filter.not_an_administrator")
742 if params[:display_name]
743 redirect_to :action => "view", :display_name => params[:display_name]
745 redirect_to :action => "login", :referer => request.fullpath
748 redirect_to :action => "login", :referer => request.fullpath
753 # require that the user in the URL is the logged in user
755 if params[:display_name] != @user.display_name
756 render :text => "", :status => :forbidden
761 # ensure that there is a "this_user" instance variable
762 def lookup_user_by_id
763 @this_user = User.find(params[:id])
767 # ensure that there is a "this_user" instance variable
768 def lookup_user_by_name
769 @this_user = User.find_by(:display_name => params[:display_name])
770 rescue ActiveRecord::RecordNotFound
771 redirect_to :action => "view", :display_name => params[:display_name] unless @this_user
776 def disable_terms_redirect
777 # this is necessary otherwise going to the user terms page, when
778 # having not agreed already would cause an infinite redirect loop.
779 # it's .now so that this doesn't propagate to other pages.
780 flash.now[:skip_terms] = true
784 # return permitted user parameters
786 params.require(:user).permit(:email, :email_confirmation, :display_name,
787 :auth_provider, :auth_uid,
788 :pass_crypt, :pass_crypt_confirmation)
793 def check_signup_allowed(email = nil)
794 domain = if email.nil?
797 email.split("@").last
800 if blocked = Acl.no_account_creation(request.remote_ip, domain)
801 logger.info "Blocked signup from #{request.remote_ip} for #{email}"
803 render :action => "blocked"
810 # check if this user has a gravatar and set the user pref is true
811 def gravatar_enable(user)
812 # code from example https://en.gravatar.com/site/implement/images/ruby/
813 return false if user.image.present?
814 hash = Digest::MD5.hexdigest(user.email.downcase)
815 url = "https://www.gravatar.com/avatar/#{hash}?d=404" # without d=404 we will always get an image back
816 response = OSM.http_client.get(URI.parse(url))
817 oldsetting = user.image_use_gravatar
818 user.image_use_gravatar = response.success?
819 oldsetting != user.image_use_gravatar
823 # display a message about th current status of the gravatar setting
824 def gravatar_status_message(user)
825 if user.image_use_gravatar
826 t "user.account.gravatar.enabled"
828 t "user.account.gravatar.disabled"