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]
21 before_action :allow_thirdparty_images, :only => [:view, :account]
24 @legale = params[:legale] || OSM.ip_to_country(request.remote_ip) || DEFAULT_LEGALE
25 @text = OSM.legal_text_for_country(@legale)
28 render :partial => "terms"
30 @title = t "user.terms.title"
32 if current_user && current_user.terms_agreed?
33 # Already agreed to terms, so just show settings
34 redirect_to :action => :account, :display_name => current_user.display_name
35 elsif current_user.nil? && session[:new_user].nil?
36 redirect_to :action => :login, :referer => request.fullpath
42 @title = t "user.new.title"
46 current_user.terms_seen = true
49 flash[:notice] = t("user.new.terms declined", :url => t("user.new.terms declined url")).html_safe
53 redirect_to params[:referer]
55 redirect_to :action => :account, :display_name => current_user.display_name
58 redirect_to t("user.terms.declined")
61 unless current_user.terms_agreed?
62 current_user.consider_pd = params[:user][:consider_pd]
63 current_user.terms_agreed = Time.now.getutc
64 current_user.terms_seen = true
66 flash[:notice] = t "user.new.terms accepted" if current_user.save
70 redirect_to params[:referer]
72 redirect_to :action => :account, :display_name => current_user.display_name
75 self.current_user = session.delete(:new_user)
77 if check_signup_allowed(current_user.email)
78 current_user.data_public = true
79 current_user.description = "" if current_user.description.nil?
80 current_user.creation_ip = request.remote_ip
81 current_user.languages = http_accept_language.user_preferred_languages
82 current_user.terms_agreed = Time.now.getutc
83 current_user.terms_seen = true
85 if current_user.auth_uid.blank?
86 current_user.auth_provider = nil
87 current_user.auth_uid = nil
91 flash[:piwik_goal] = PIWIK["goals"]["signup"] if defined?(PIWIK)
93 referer = welcome_path
96 uri = URI(session[:referer])
97 %r{map=(.*)/(.*)/(.*)}.match(uri.fragment) do |m|
98 editor = Rack::Utils.parse_query(uri.query).slice("editor")
99 referer = welcome_path({ "zoom" => m[1],
101 "lon" => m[3] }.merge(editor))
107 if current_user.status == "active"
108 session[:referer] = referer
109 successful_login(current_user)
111 session[:token] = current_user.tokens.create.token
112 Notifier.signup_confirm(current_user, current_user.tokens.create(:referer => referer)).deliver_now
113 redirect_to :action => "confirm", :display_name => current_user.display_name
116 render :action => "new", :referer => params[:referer]
123 @title = t "user.account.title"
124 @tokens = current_user.oauth_tokens.authorized
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)
143 current_user.data_public = true
145 flash[:notice] = t "user.go_public.flash success"
146 redirect_to :action => "account", :display_name => current_user.display_name
150 @title = t "user.lost_password.title"
152 if params[:user] && params[:user][:email]
153 user = User.visible.find_by(:email => params[:user][:email])
156 users = User.visible.where("LOWER(email) = LOWER(?)", params[:user][:email])
158 user = users.first if users.count == 1
162 token = user.tokens.create
163 Notifier.lost_password(user, token).deliver_now
164 flash[:notice] = t "user.lost_password.notice email on way"
165 redirect_to :action => "login"
167 flash.now[:error] = t "user.lost_password.notice email cannot find"
173 @title = t "user.reset_password.title"
176 token = UserToken.find_by(:token => params[:token])
179 self.current_user = token.user
182 current_user.pass_crypt = params[:user][:pass_crypt]
183 current_user.pass_crypt_confirmation = params[:user][:pass_crypt_confirmation]
184 current_user.status = "active" if current_user.status == "pending"
185 current_user.email_valid = true
189 flash[:notice] = t "user.reset_password.flash changed"
190 successful_login(current_user)
194 flash[:error] = t "user.reset_password.flash token bad"
195 redirect_to :action => "lost_password"
203 @title = t "user.new.title"
204 @referer = params[:referer] || session[:referer]
207 # The user is logged in already, so don't show them the signup
208 # page, instead send them to the home page
212 redirect_to :controller => "site", :action => "index"
214 elsif params.key?(:auth_provider) && params.key?(:auth_uid)
215 self.current_user = User.new(:email => params[:email],
216 :email_confirmation => params[:email],
217 :display_name => params[:nickname],
218 :auth_provider => params[:auth_provider],
219 :auth_uid => params[:auth_uid])
221 flash.now[:notice] = render_to_string :partial => "auth_association"
225 self.current_user = User.new
230 self.current_user = User.new(user_params)
232 if check_signup_allowed(current_user.email)
233 session[:referer] = params[:referer]
235 current_user.status = "pending"
237 if current_user.auth_provider.present? && current_user.pass_crypt.empty?
238 # We are creating an account with external authentication and
239 # no password was specified so create a random one
240 current_user.pass_crypt = SecureRandom.base64(16)
241 current_user.pass_crypt_confirmation = current_user.pass_crypt
244 if current_user.invalid?
245 # Something is wrong with a new user, so rerender the form
246 render :action => "new"
247 elsif current_user.auth_provider.present?
248 # Verify external authenticator before moving on
249 session[:new_user] = current_user
250 redirect_to auth_url(current_user.auth_provider, current_user.auth_uid)
252 # Save the user record
253 session[:new_user] = current_user
254 redirect_to :action => :terms
260 session[:referer] = params[:referer] if params[:referer]
262 if params[:username].present? && params[:password].present?
263 session[:remember_me] ||= params[:remember_me]
264 password_authentication(params[:username], params[:password])
265 elsif params[:openid_url].present?
266 session[:remember_me] ||= params[:remember_me_openid]
267 redirect_to auth_url("openid", params[:openid_url], params[:referer])
272 @title = t "user.logout.title"
274 if params[:session] == session.id
276 token = UserToken.find_by(:token => session[:token])
277 token.destroy if token
278 session.delete(:token)
280 session.delete(:user)
281 session_expires_automatically
283 redirect_to params[:referer]
285 redirect_to :controller => "site", :action => "index"
292 token = UserToken.find_by(:token => params[:confirm_string])
293 if token && token.user.active?
294 flash[:error] = t("user.confirm.already active")
295 redirect_to :action => "login"
296 elsif !token || token.expired?
297 flash[:error] = t("user.confirm.unknown token")
298 redirect_to :action => "confirm"
301 user.status = "active"
302 user.email_valid = true
303 flash[:notice] = gravatar_status_message(user) if gravatar_enable(user)
305 referer = token.referer
309 token = UserToken.find_by(:token => session[:token])
310 session.delete(:token)
315 if token.nil? || token.user != user
316 flash[:notice] = t("user.confirm.success")
317 redirect_to :action => :login, :referer => referer
321 session[:user] = user.id
323 redirect_to referer || welcome_path
327 user = User.find_by(:display_name => params[:display_name])
329 redirect_to root_path if user.nil? || user.active?
334 user = User.find_by(:display_name => params[:display_name])
335 token = UserToken.find_by(:token => session[:token])
337 if user.nil? || token.nil? || token.user != user
338 flash[:error] = t "user.confirm_resend.failure", :name => params[:display_name]
340 Notifier.signup_confirm(user, user.tokens.create).deliver_now
341 flash[:notice] = t("user.confirm_resend.success", :email => user.email, :sender => SUPPORT_EMAIL).html_safe
344 redirect_to :action => "login"
349 token = UserToken.find_by(:token => params[:confirm_string])
350 if token && token.user.new_email?
351 self.current_user = token.user
352 current_user.email = current_user.new_email
353 current_user.new_email = nil
354 current_user.email_valid = true
355 gravatar_enabled = gravatar_enable(current_user)
357 flash[:notice] = if gravatar_enabled
358 t("user.confirm_email.success") + " " + gravatar_status_message(current_user)
360 t("user.confirm_email.success")
363 flash[:errors] = current_user.errors
366 session[:user] = current_user.id
367 redirect_to :action => "account", :display_name => current_user.display_name
369 flash[:error] = t "user.confirm_email.failure"
370 redirect_to :action => "account", :display_name => token.user.display_name
372 flash[:error] = t "user.confirm_email.unknown_token"
378 if @this_user.visible?
379 render :action => :api_read, :content_type => "text/xml"
386 @this_user = current_user
387 render :action => :api_read, :content_type => "text/xml"
391 doc = OSM::API.new.get_xml_doc
392 current_user.traces.reload.each do |trace|
393 doc.root << trace.to_xml_node
395 render :xml => doc.to_s
399 @this_user = User.find_by(:display_name => params[:display_name])
402 (@this_user.visible? || (current_user && current_user.administrator?))
403 @title = @this_user.display_name
405 render_unknown_user params[:display_name]
410 @new_friend = User.find_by(:display_name => params[:display_name])
415 friend.befriender = current_user
416 friend.befriendee = @new_friend
417 if current_user.is_friends_with?(@new_friend)
418 flash[:warning] = t "user.make_friend.already_a_friend", :name => @new_friend.display_name
420 flash[:notice] = t "user.make_friend.success", :name => @new_friend.display_name
421 Notifier.friend_notification(friend).deliver_now
423 friend.add_error(t("user.make_friend.failed", :name => @new_friend.display_name))
427 redirect_to params[:referer]
429 redirect_to :action => "view"
433 render_unknown_user params[:display_name]
438 @friend = User.find_by(:display_name => params[:display_name])
442 if current_user.is_friends_with?(@friend)
443 Friend.where(:user_id => current_user.id, :friend_user_id => @friend.id).delete_all
444 flash[:notice] = t "user.remove_friend.success", :name => @friend.display_name
446 flash[:error] = t "user.remove_friend.not_a_friend", :name => @friend.display_name
450 redirect_to params[:referer]
452 redirect_to :action => "view"
456 render_unknown_user params[:display_name]
461 # sets a user's status
463 @this_user.status = params[:status]
465 redirect_to :action => "view", :display_name => params[:display_name]
469 # delete a user, marking them as deleted and removing personal data
472 redirect_to :action => "view", :display_name => params[:display_name]
476 # display a list of users matching specified criteria
479 ids = params[:user].keys.collect(&:to_i)
481 User.where(:id => ids).update_all(:status => "confirmed") if params[:confirm]
482 User.where(:id => ids).update_all(:status => "deleted") if params[:hide]
484 redirect_to url_for(:status => params[:status], :ip => params[:ip], :page => params[:page])
486 @params = params.permit(:status, :ip)
489 conditions[:status] = @params[:status] if @params[:status]
490 conditions[:creation_ip] = @params[:ip] if @params[:ip]
492 @user_pages, @users = paginate(:users,
493 :conditions => conditions,
500 # omniauth success callback
502 auth_info = request.env["omniauth.auth"]
504 provider = auth_info[:provider]
505 uid = auth_info[:uid]
506 name = auth_info[:info][:name]
507 email = auth_info[:info][:email]
511 email_verified = uid.match(%r{https://www.google.com/accounts/o8/id?(.*)}) ||
512 uid.match(%r{https://me.yahoo.com/(.*)})
513 when "google", "facebook"
514 email_verified = true
516 email_verified = false
519 if settings = session.delete(:new_user_settings)
520 current_user.auth_provider = provider
521 current_user.auth_uid = uid
523 update_user(current_user, settings)
525 session[:user_errors] = current_user.errors.as_json
527 redirect_to :action => "account", :display_name => current_user.display_name
528 elsif session[:new_user]
529 session[:new_user].auth_provider = provider
530 session[:new_user].auth_uid = uid
532 if email_verified && email == session[:new_user].email
533 session[:new_user].status = "active"
536 redirect_to :action => "terms"
538 user = User.find_by(:auth_provider => provider, :auth_uid => uid)
540 if user.nil? && provider == "google"
541 openid_url = auth_info[:extra][:id_info]["openid_id"]
542 user = User.find_by(:auth_provider => "openid", :auth_uid => openid_url) if openid_url
543 user.update(:auth_provider => provider, :auth_uid => uid) if user
549 unconfirmed_login(user)
550 when "active", "confirmed" then
551 successful_login(user, request.env["omniauth.params"]["referer"])
552 when "suspended" then
553 failed_login t("user.login.account is suspended", :webmaster => "mailto:#{SUPPORT_EMAIL}").html_safe
555 failed_login t("user.login.auth failure")
558 redirect_to :action => "new", :nickname => name, :email => email,
559 :auth_provider => provider, :auth_uid => uid
565 # omniauth failure callback
567 flash[:error] = t("user.auth_failure." + params[:message])
568 redirect_to params[:origin] || login_url
574 # handle password authentication
575 def password_authentication(username, password)
576 if user = User.authenticate(:username => username, :password => password)
577 successful_login(user)
578 elsif user = User.authenticate(:username => username, :password => password, :pending => true)
579 unconfirmed_login(user)
580 elsif User.authenticate(:username => username, :password => password, :suspended => true)
581 failed_login t("user.login.account is suspended", :webmaster => "mailto:#{SUPPORT_EMAIL}").html_safe, username
583 failed_login t("user.login.auth failure"), username
588 # return the URL to use for authentication
589 def auth_url(provider, uid, referer = nil)
590 params = { :provider => provider }
592 params[:openid_url] = openid_expand_url(uid) if provider == "openid"
595 params[:origin] = request.path
597 params[:origin] = request.path + "?referer=" + CGI.escape(referer)
598 params[:referer] = referer
605 # special case some common OpenID providers by applying heuristics to
606 # try and come up with the correct URL based on what the user entered
607 def openid_expand_url(openid_url)
610 elsif openid_url.match(%r{(.*)gmail.com(/?)$}) || openid_url.match(%r{(.*)googlemail.com(/?)$})
611 # Special case gmail.com as it is potentially a popular OpenID
612 # provider and, unlike yahoo.com, where it works automatically, Google
613 # have hidden their OpenID endpoint somewhere obscure this making it
614 # somewhat less user friendly.
615 "https://www.google.com/accounts/o8/id"
622 # process a successful login
623 def successful_login(user, referer = nil)
624 session[:user] = user.id
625 session_expires_after 28.days if session[:remember_me]
627 target = referer || session[:referer] || url_for(:controller => :site, :action => :index)
629 # The user is logged in, so decide where to send them:
631 # - If they haven't seen the contributor terms, send them there.
632 # - If they have a block on them, show them that.
633 # - If they were referred to the login, send them back there.
634 # - Otherwise, send them to the home page.
635 if REQUIRE_TERMS_SEEN && !user.terms_seen
636 redirect_to :action => :terms, :referer => target
637 elsif user.blocked_on_view
638 redirect_to user.blocked_on_view, :referer => target
643 session.delete(:remember_me)
644 session.delete(:referer)
648 # process a failed login
649 def failed_login(message, username = nil)
650 flash[:error] = message
652 redirect_to :action => "login", :referer => session[:referer],
653 :username => username, :remember_me => session[:remember_me]
655 session.delete(:remember_me)
656 session.delete(:referer)
661 def unconfirmed_login(user)
662 session[:token] = user.tokens.create.token
664 redirect_to :action => "confirm", :display_name => user.display_name
666 session.delete(:remember_me)
667 session.delete(:referer)
671 # update a user's details
672 def update_user(user, params)
673 user.display_name = params[:user][:display_name]
674 user.new_email = params[:user][:new_email]
676 unless params[:user][:pass_crypt].empty? && params[:user][:pass_crypt_confirmation].empty?
677 user.pass_crypt = params[:user][:pass_crypt]
678 user.pass_crypt_confirmation = params[:user][:pass_crypt_confirmation]
681 if params[:user][:description] != user.description
682 user.description = params[:user][:description]
683 user.description_format = "markdown"
686 user.languages = params[:user][:languages].split(",")
688 case params[:image_action]
690 user.image = params[:user][:image]
691 user.image_use_gravatar = false
694 user.image_use_gravatar = false
697 user.image_use_gravatar = true
700 user.home_lat = params[:user][:home_lat]
701 user.home_lon = params[:user][:home_lon]
703 user.preferred_editor = if params[:user][:preferred_editor] == "default"
706 params[:user][:preferred_editor]
709 if params[:user][:auth_provider].nil? || params[:user][:auth_provider].blank?
710 user.auth_provider = nil
717 if user.new_email.blank? || user.new_email == user.email
718 flash.now[:notice] = t "user.account.flash update success"
720 user.email = user.new_email
723 flash.now[:notice] = t "user.account.flash update success confirm needed"
726 Notifier.email_confirm(user, user.tokens.create).deliver_now
728 # Ignore errors sending email
731 current_user.errors.add(:new_email, current_user.errors[:email])
732 current_user.errors.add(:email, [])
741 # require that the user is a administrator, or fill out a helpful error message
742 # and return them to the user page.
743 def require_administrator
744 if current_user && !current_user.administrator?
745 flash[:error] = t("user.filter.not_an_administrator")
747 if params[:display_name]
748 redirect_to :action => "view", :display_name => params[:display_name]
750 redirect_to :action => "login", :referer => request.fullpath
753 redirect_to :action => "login", :referer => request.fullpath
758 # require that the user in the URL is the logged in user
760 head :forbidden if params[:display_name] != current_user.display_name
764 # ensure that there is a "this_user" instance variable
765 def lookup_user_by_id
766 @this_user = User.find(params[:id])
770 # ensure that there is a "this_user" instance variable
771 def lookup_user_by_name
772 @this_user = User.find_by(:display_name => params[:display_name])
773 rescue ActiveRecord::RecordNotFound
774 redirect_to :action => "view", :display_name => params[:display_name] unless @this_user
779 def disable_terms_redirect
780 # this is necessary otherwise going to the user terms page, when
781 # having not agreed already would cause an infinite redirect loop.
782 # it's .now so that this doesn't propagate to other pages.
783 flash.now[:skip_terms] = true
787 # return permitted user parameters
789 params.require(:user).permit(:email, :email_confirmation, :display_name,
790 :auth_provider, :auth_uid,
791 :pass_crypt, :pass_crypt_confirmation)
796 def check_signup_allowed(email = nil)
797 domain = if email.nil?
800 email.split("@").last
803 if blocked = Acl.no_account_creation(request.remote_ip, domain)
804 logger.info "Blocked signup from #{request.remote_ip} for #{email}"
806 render :action => "blocked"
813 # check if this user has a gravatar and set the user pref is true
814 def gravatar_enable(user)
815 # code from example https://en.gravatar.com/site/implement/images/ruby/
816 return false if user.image.present?
817 hash = Digest::MD5.hexdigest(user.email.downcase)
818 url = "https://www.gravatar.com/avatar/#{hash}?d=404" # without d=404 we will always get an image back
819 response = OSM.http_client.get(URI.parse(url))
820 oldsetting = user.image_use_gravatar
821 user.image_use_gravatar = response.success?
822 oldsetting != user.image_use_gravatar
826 # display a message about th current status of the gravatar setting
827 def gravatar_status_message(user)
828 if user.image_use_gravatar
829 t "user.account.gravatar.enabled"
831 t "user.account.gravatar.disabled"