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]
206 append_content_security_policy_directives(
207 :form_action => %w[accounts.google.com *.facebook.com login.live.com github.com meta.wikimedia.org]
211 # The user is logged in already, so don't show them the signup
212 # page, instead send them to the home page
216 redirect_to :controller => "site", :action => "index"
218 elsif params.key?(:auth_provider) && params.key?(:auth_uid)
219 self.current_user = User.new(:email => params[:email],
220 :email_confirmation => params[:email],
221 :display_name => params[:nickname],
222 :auth_provider => params[:auth_provider],
223 :auth_uid => params[:auth_uid])
225 flash.now[:notice] = render_to_string :partial => "auth_association"
229 self.current_user = User.new
234 self.current_user = User.new(user_params)
236 if check_signup_allowed(current_user.email)
237 session[:referer] = params[:referer]
239 current_user.status = "pending"
241 if current_user.auth_provider.present? && current_user.pass_crypt.empty?
242 # We are creating an account with external authentication and
243 # no password was specified so create a random one
244 current_user.pass_crypt = SecureRandom.base64(16)
245 current_user.pass_crypt_confirmation = current_user.pass_crypt
248 if current_user.invalid?
249 # Something is wrong with a new user, so rerender the form
250 render :action => "new"
251 elsif current_user.auth_provider.present?
252 # Verify external authenticator before moving on
253 session[:new_user] = current_user
254 redirect_to auth_url(current_user.auth_provider, current_user.auth_uid)
256 # Save the user record
257 session[:new_user] = current_user
258 redirect_to :action => :terms
264 session[:referer] = params[:referer] if params[:referer]
266 if params[:username].present? && params[:password].present?
267 session[:remember_me] ||= params[:remember_me]
268 password_authentication(params[:username], params[:password])
269 elsif params[:openid_url].present?
270 session[:remember_me] ||= params[:remember_me_openid]
271 redirect_to auth_url("openid", params[:openid_url], params[:referer])
276 @title = t "user.logout.title"
278 if params[:session] == session.id
280 token = UserToken.find_by(:token => session[:token])
281 token.destroy if token
282 session.delete(:token)
284 session.delete(:user)
285 session_expires_automatically
287 redirect_to params[:referer]
289 redirect_to :controller => "site", :action => "index"
296 token = UserToken.find_by(:token => params[:confirm_string])
297 if token && token.user.active?
298 flash[:error] = t("user.confirm.already active")
299 redirect_to :action => "login"
300 elsif !token || token.expired?
301 flash[:error] = t("user.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 = 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("user.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 "user.confirm_resend.failure", :name => params[:display_name]
344 Notifier.signup_confirm(user, user.tokens.create).deliver_now
345 flash[:notice] = t("user.confirm_resend.success", :email => user.email, :sender => SUPPORT_EMAIL).html_safe
348 redirect_to :action => "login"
353 token = UserToken.find_by(:token => params[:confirm_string])
354 if token && 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("user.confirm_email.success") + " " + gravatar_status_message(current_user)
364 t("user.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 "user.confirm_email.failure"
374 redirect_to :action => "account", :display_name => token.user.display_name
376 flash[:error] = t "user.confirm_email.unknown_token"
382 if @this_user.visible?
383 render :action => :api_read, :content_type => "text/xml"
390 @this_user = current_user
391 render :action => :api_read, :content_type => "text/xml"
395 doc = OSM::API.new.get_xml_doc
396 current_user.traces.reload.each do |trace|
397 doc.root << trace.to_xml_node
399 render :xml => doc.to_s
403 @this_user = User.find_by(:display_name => params[:display_name])
406 (@this_user.visible? || (current_user && current_user.administrator?))
407 @title = @this_user.display_name
409 render_unknown_user params[:display_name]
414 @new_friend = User.find_by(:display_name => params[:display_name])
419 friend.befriender = current_user
420 friend.befriendee = @new_friend
421 if current_user.is_friends_with?(@new_friend)
422 flash[:warning] = t "user.make_friend.already_a_friend", :name => @new_friend.display_name
424 flash[:notice] = t "user.make_friend.success", :name => @new_friend.display_name
425 Notifier.friend_notification(friend).deliver_now
427 friend.add_error(t("user.make_friend.failed", :name => @new_friend.display_name))
431 redirect_to params[:referer]
433 redirect_to :action => "view"
437 render_unknown_user params[:display_name]
442 @friend = User.find_by(:display_name => params[:display_name])
446 if current_user.is_friends_with?(@friend)
447 Friend.where(:user_id => current_user.id, :friend_user_id => @friend.id).delete_all
448 flash[:notice] = t "user.remove_friend.success", :name => @friend.display_name
450 flash[:error] = t "user.remove_friend.not_a_friend", :name => @friend.display_name
454 redirect_to params[:referer]
456 redirect_to :action => "view"
460 render_unknown_user params[:display_name]
465 # sets a user's status
467 @this_user.status = params[:status]
469 redirect_to :action => "view", :display_name => params[:display_name]
473 # delete a user, marking them as deleted and removing personal data
476 redirect_to :action => "view", :display_name => params[:display_name]
480 # display a list of users matching specified criteria
483 ids = params[:user].keys.collect(&:to_i)
485 User.where(:id => ids).update_all(:status => "confirmed") if params[:confirm]
486 User.where(:id => ids).update_all(:status => "deleted") if params[:hide]
488 redirect_to url_for(:status => params[:status], :ip => params[:ip], :page => params[:page])
490 @params = params.permit(:status, :ip)
493 conditions[:status] = @params[:status] if @params[:status]
494 conditions[:creation_ip] = @params[:ip] if @params[:ip]
496 @user_pages, @users = paginate(:users,
497 :conditions => conditions,
504 # omniauth success callback
506 auth_info = request.env["omniauth.auth"]
508 provider = auth_info[:provider]
509 uid = auth_info[:uid]
510 name = auth_info[:info][:name]
511 email = auth_info[:info][:email]
515 email_verified = uid.match(%r{https://www.google.com/accounts/o8/id?(.*)}) ||
516 uid.match(%r{https://me.yahoo.com/(.*)})
517 when "google", "facebook"
518 email_verified = true
520 email_verified = false
523 if settings = session.delete(:new_user_settings)
524 current_user.auth_provider = provider
525 current_user.auth_uid = uid
527 update_user(current_user, settings)
529 session[:user_errors] = current_user.errors.as_json
531 redirect_to :action => "account", :display_name => current_user.display_name
532 elsif session[:new_user]
533 session[:new_user].auth_provider = provider
534 session[:new_user].auth_uid = uid
536 if email_verified && email == session[:new_user].email
537 session[:new_user].status = "active"
540 redirect_to :action => "terms"
542 user = User.find_by(:auth_provider => provider, :auth_uid => uid)
544 if user.nil? && provider == "google"
545 openid_url = auth_info[:extra][:id_info]["openid_id"]
546 user = User.find_by(:auth_provider => "openid", :auth_uid => openid_url) if openid_url
547 user.update(:auth_provider => provider, :auth_uid => uid) if user
553 unconfirmed_login(user)
554 when "active", "confirmed" then
555 successful_login(user, request.env["omniauth.params"]["referer"])
556 when "suspended" then
557 failed_login t("user.login.account is suspended", :webmaster => "mailto:#{SUPPORT_EMAIL}").html_safe
559 failed_login t("user.login.auth failure")
562 redirect_to :action => "new", :nickname => name, :email => email,
563 :auth_provider => provider, :auth_uid => uid
569 # omniauth failure callback
571 flash[:error] = t("user.auth_failure." + params[:message])
572 redirect_to params[:origin] || login_url
578 # handle password authentication
579 def password_authentication(username, password)
580 if user = User.authenticate(:username => username, :password => password)
581 successful_login(user)
582 elsif user = User.authenticate(:username => username, :password => password, :pending => true)
583 unconfirmed_login(user)
584 elsif User.authenticate(:username => username, :password => password, :suspended => true)
585 failed_login t("user.login.account is suspended", :webmaster => "mailto:#{SUPPORT_EMAIL}").html_safe, username
587 failed_login t("user.login.auth failure"), username
592 # return the URL to use for authentication
593 def auth_url(provider, uid, referer = nil)
594 params = { :provider => provider }
596 params[:openid_url] = openid_expand_url(uid) if provider == "openid"
599 params[:origin] = request.path
601 params[:origin] = request.path + "?referer=" + CGI.escape(referer)
602 params[:referer] = referer
609 # special case some common OpenID providers by applying heuristics to
610 # try and come up with the correct URL based on what the user entered
611 def openid_expand_url(openid_url)
614 elsif openid_url.match(%r{(.*)gmail.com(/?)$}) || openid_url.match(%r{(.*)googlemail.com(/?)$})
615 # Special case gmail.com as it is potentially a popular OpenID
616 # provider and, unlike yahoo.com, where it works automatically, Google
617 # have hidden their OpenID endpoint somewhere obscure this making it
618 # somewhat less user friendly.
619 "https://www.google.com/accounts/o8/id"
626 # process a successful login
627 def successful_login(user, referer = nil)
628 session[:user] = user.id
629 session_expires_after 28.days if session[:remember_me]
631 target = referer || session[:referer] || url_for(:controller => :site, :action => :index)
633 # The user is logged in, so decide where to send them:
635 # - If they haven't seen the contributor terms, send them there.
636 # - If they have a block on them, show them that.
637 # - If they were referred to the login, send them back there.
638 # - Otherwise, send them to the home page.
639 if REQUIRE_TERMS_SEEN && !user.terms_seen
640 redirect_to :action => :terms, :referer => target
641 elsif user.blocked_on_view
642 redirect_to user.blocked_on_view, :referer => target
647 session.delete(:remember_me)
648 session.delete(:referer)
652 # process a failed login
653 def failed_login(message, username = nil)
654 flash[:error] = message
656 redirect_to :action => "login", :referer => session[:referer],
657 :username => username, :remember_me => session[:remember_me]
659 session.delete(:remember_me)
660 session.delete(:referer)
665 def unconfirmed_login(user)
666 session[:token] = user.tokens.create.token
668 redirect_to :action => "confirm", :display_name => user.display_name
670 session.delete(:remember_me)
671 session.delete(:referer)
675 # update a user's details
676 def update_user(user, params)
677 user.display_name = params[:user][:display_name]
678 user.new_email = params[:user][:new_email]
680 unless params[:user][:pass_crypt].empty? && params[:user][:pass_crypt_confirmation].empty?
681 user.pass_crypt = params[:user][:pass_crypt]
682 user.pass_crypt_confirmation = params[:user][:pass_crypt_confirmation]
685 if params[:user][:description] != user.description
686 user.description = params[:user][:description]
687 user.description_format = "markdown"
690 user.languages = params[:user][:languages].split(",")
692 case params[:image_action]
694 user.image = params[:user][:image]
695 user.image_use_gravatar = false
698 user.image_use_gravatar = false
701 user.image_use_gravatar = true
704 user.home_lat = params[:user][:home_lat]
705 user.home_lon = params[:user][:home_lon]
707 user.preferred_editor = if params[:user][:preferred_editor] == "default"
710 params[:user][:preferred_editor]
713 if params[:user][:auth_provider].nil? || params[:user][:auth_provider].blank?
714 user.auth_provider = nil
721 if user.new_email.blank? || user.new_email == user.email
722 flash.now[:notice] = t "user.account.flash update success"
724 user.email = user.new_email
727 flash.now[:notice] = t "user.account.flash update success confirm needed"
730 Notifier.email_confirm(user, user.tokens.create).deliver_now
732 # Ignore errors sending email
735 current_user.errors.add(:new_email, current_user.errors[:email])
736 current_user.errors.add(:email, [])
745 # require that the user is a administrator, or fill out a helpful error message
746 # and return them to the user page.
747 def require_administrator
748 if current_user && !current_user.administrator?
749 flash[:error] = t("user.filter.not_an_administrator")
751 if params[:display_name]
752 redirect_to :action => "view", :display_name => params[:display_name]
754 redirect_to :action => "login", :referer => request.fullpath
757 redirect_to :action => "login", :referer => request.fullpath
762 # require that the user in the URL is the logged in user
764 head :forbidden if params[:display_name] != current_user.display_name
768 # ensure that there is a "this_user" instance variable
769 def lookup_user_by_id
770 @this_user = User.find(params[:id])
774 # ensure that there is a "this_user" instance variable
775 def lookup_user_by_name
776 @this_user = User.find_by(:display_name => params[:display_name])
777 rescue ActiveRecord::RecordNotFound
778 redirect_to :action => "view", :display_name => params[:display_name] unless @this_user
783 def disable_terms_redirect
784 # this is necessary otherwise going to the user terms page, when
785 # having not agreed already would cause an infinite redirect loop.
786 # it's .now so that this doesn't propagate to other pages.
787 flash.now[:skip_terms] = true
791 # return permitted user parameters
793 params.require(:user).permit(:email, :email_confirmation, :display_name,
794 :auth_provider, :auth_uid,
795 :pass_crypt, :pass_crypt_confirmation)
800 def check_signup_allowed(email = nil)
801 domain = if email.nil?
804 email.split("@").last
807 if blocked = Acl.no_account_creation(request.remote_ip, domain)
808 logger.info "Blocked signup from #{request.remote_ip} for #{email}"
810 render :action => "blocked"
817 # check if this user has a gravatar and set the user pref is true
818 def gravatar_enable(user)
819 # code from example https://en.gravatar.com/site/implement/images/ruby/
820 return false if user.image.present?
821 hash = Digest::MD5.hexdigest(user.email.downcase)
822 url = "https://www.gravatar.com/avatar/#{hash}?d=404" # without d=404 we will always get an image back
823 response = OSM.http_client.get(URI.parse(url))
824 oldsetting = user.image_use_gravatar
825 user.image_use_gravatar = response.success?
826 oldsetting != user.image_use_gravatar
830 # display a message about th current status of the gravatar setting
831 def gravatar_status_message(user)
832 if user.image_use_gravatar
833 t "user.account.gravatar.enabled"
835 t "user.account.gravatar.disabled"