1 class UsersController < ApplicationController
2 layout "site", :except => [:api_details]
4 skip_before_action :verify_authenticity_token, :only => [:api_read, :api_users, :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_users, :api_details, :api_gpx_files]
8 before_action :set_locale, :except => [:api_read, :api_users, :api_details, :api_gpx_files]
9 before_action :api_deny_access_handler, :only => [:api_read, :api_users, :api_details, :api_gpx_files]
13 before_action :require_self, :only => [:account]
14 before_action :check_database_readable, :except => [:login, :api_read, :api_users, :api_details, :api_gpx_files]
15 before_action :check_database_writable, :only => [:new, :account, :confirm, :confirm_email, :lost_password, :reset_password, :go_public, :make_friend, :remove_friend]
16 before_action :check_api_readable, :only => [:api_read, :api_users, :api_details, :api_gpx_files]
17 before_action :require_cookies, :only => [:new, :login, :confirm]
18 around_action :api_call_handle_error, :only => [:api_read, :api_users, :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 => [:show, :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 "users.terms.title"
32 if 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 "users.new.title"
46 current_user.terms_seen = true
48 flash[:notice] = t("users.new.terms declined", :url => t("users.new.terms declined url")).html_safe if current_user.save
51 redirect_to params[:referer]
53 redirect_to :action => :account, :display_name => current_user.display_name
56 redirect_to t("users.terms.declined")
59 unless current_user.terms_agreed?
60 current_user.consider_pd = params[:user][:consider_pd]
61 current_user.terms_agreed = Time.now.getutc
62 current_user.terms_seen = true
64 flash[:notice] = t "users.new.terms accepted" if current_user.save
68 redirect_to params[:referer]
70 redirect_to :action => :account, :display_name => current_user.display_name
73 self.current_user = session.delete(:new_user)
75 if check_signup_allowed(current_user.email)
76 current_user.data_public = true
77 current_user.description = "" if current_user.description.nil?
78 current_user.creation_ip = request.remote_ip
79 current_user.languages = http_accept_language.user_preferred_languages
80 current_user.terms_agreed = Time.now.getutc
81 current_user.terms_seen = true
83 if current_user.auth_uid.blank?
84 current_user.auth_provider = nil
85 current_user.auth_uid = nil
89 flash[:piwik_goal] = PIWIK["goals"]["signup"] if defined?(PIWIK)
91 referer = welcome_path
94 uri = URI(session[:referer])
95 %r{map=(.*)/(.*)/(.*)}.match(uri.fragment) do |m|
96 editor = Rack::Utils.parse_query(uri.query).slice("editor")
97 referer = welcome_path({ "zoom" => m[1],
99 "lon" => m[3] }.merge(editor))
105 if current_user.status == "active"
106 session[:referer] = referer
107 successful_login(current_user)
109 session[:token] = current_user.tokens.create.token
110 Notifier.signup_confirm(current_user, current_user.tokens.create(:referer => referer)).deliver_later
111 redirect_to :action => "confirm", :display_name => current_user.display_name
114 render :action => "new", :referer => params[:referer]
121 @tokens = current_user.oauth_tokens.authorized
123 if params[:user] && params[:user][:display_name] && params[:user][:description]
124 if params[:user][:auth_provider].blank? ||
125 (params[:user][:auth_provider] == current_user.auth_provider &&
126 params[:user][:auth_uid] == current_user.auth_uid)
127 update_user(current_user, params)
129 session[:new_user_settings] = params
130 redirect_to auth_url(params[:user][:auth_provider], params[:user][:auth_uid])
132 elsif errors = session.delete(:user_errors)
133 errors.each do |attribute, error|
134 current_user.errors.add(attribute, error)
137 @title = t "users.account.title"
141 current_user.data_public = true
143 flash[:notice] = t "users.go_public.flash success"
144 redirect_to :action => "account", :display_name => current_user.display_name
148 @title = t "users.lost_password.title"
150 if params[:user] && params[:user][:email]
151 user = User.visible.find_by(:email => params[:user][:email])
154 users = User.visible.where("LOWER(email) = LOWER(?)", params[:user][:email])
156 user = users.first if users.count == 1
160 token = user.tokens.create
161 Notifier.lost_password(user, token).deliver_later
162 flash[:notice] = t "users.lost_password.notice email on way"
163 redirect_to :action => "login"
165 flash.now[:error] = t "users.lost_password.notice email cannot find"
171 @title = t "users.reset_password.title"
174 token = UserToken.find_by(:token => params[:token])
177 self.current_user = token.user
180 current_user.pass_crypt = params[:user][:pass_crypt]
181 current_user.pass_crypt_confirmation = params[:user][:pass_crypt_confirmation]
182 current_user.status = "active" if current_user.status == "pending"
183 current_user.email_valid = true
187 flash[:notice] = t "users.reset_password.flash changed"
188 successful_login(current_user)
192 flash[:error] = t "users.reset_password.flash token bad"
193 redirect_to :action => "lost_password"
201 @title = t "users.new.title"
202 @referer = params[:referer] || session[:referer]
204 append_content_security_policy_directives(
205 :form_action => %w[accounts.google.com *.facebook.com login.live.com github.com meta.wikimedia.org]
209 # The user is logged in already, so don't show them the signup
210 # page, instead send them to the home page
214 redirect_to :controller => "site", :action => "index"
216 elsif params.key?(:auth_provider) && params.key?(:auth_uid)
217 self.current_user = User.new(:email => params[:email],
218 :email_confirmation => params[:email],
219 :display_name => params[:nickname],
220 :auth_provider => params[:auth_provider],
221 :auth_uid => params[:auth_uid])
223 flash.now[:notice] = render_to_string :partial => "auth_association"
227 self.current_user = User.new
232 self.current_user = User.new(user_params)
234 if check_signup_allowed(current_user.email)
235 session[:referer] = params[:referer]
237 current_user.status = "pending"
239 if current_user.auth_provider.present? && current_user.pass_crypt.empty?
240 # We are creating an account with external authentication and
241 # no password was specified so create a random one
242 current_user.pass_crypt = SecureRandom.base64(16)
243 current_user.pass_crypt_confirmation = current_user.pass_crypt
246 if current_user.invalid?
247 # Something is wrong with a new user, so rerender the form
248 render :action => "new"
249 elsif current_user.auth_provider.present?
250 # Verify external authenticator before moving on
251 session[:new_user] = current_user
252 redirect_to auth_url(current_user.auth_provider, current_user.auth_uid)
254 # Save the user record
255 session[:new_user] = current_user
256 redirect_to :action => :terms
262 session[:referer] = params[:referer] if params[:referer]
264 if params[:username].present? && params[:password].present?
265 session[:remember_me] ||= params[:remember_me]
266 password_authentication(params[:username], params[:password])
267 elsif params[:openid_url].present?
268 session[:remember_me] ||= params[:remember_me_openid]
269 redirect_to auth_url("openid", params[:openid_url], params[:referer])
274 @title = t "users.logout.title"
276 if params[:session] == session.id
278 token = UserToken.find_by(:token => session[:token])
280 session.delete(:token)
282 session.delete(:user)
283 session_expires_automatically
285 redirect_to params[:referer]
287 redirect_to :controller => "site", :action => "index"
294 token = UserToken.find_by(:token => params[:confirm_string])
295 if token&.user&.active?
296 flash[:error] = t("users.confirm.already active")
297 redirect_to :action => "login"
298 elsif !token || token.expired?
299 flash[:error] = t("users.confirm.unknown token")
300 redirect_to :action => "confirm"
303 user.status = "active"
304 user.email_valid = true
305 flash[:notice] = gravatar_status_message(user) if gravatar_enable(user)
307 referer = token.referer
311 token = UserToken.find_by(:token => session[:token])
312 session.delete(:token)
317 if token.nil? || token.user != user
318 flash[:notice] = t("users.confirm.success")
319 redirect_to :action => :login, :referer => referer
323 session[:user] = user.id
325 redirect_to referer || welcome_path
329 user = User.find_by(:display_name => params[:display_name])
331 redirect_to root_path if user.nil? || user.active?
336 user = User.find_by(:display_name => params[:display_name])
337 token = UserToken.find_by(:token => session[:token])
339 if user.nil? || token.nil? || token.user != user
340 flash[:error] = t "users.confirm_resend.failure", :name => params[:display_name]
342 Notifier.signup_confirm(user, user.tokens.create).deliver_later
343 flash[:notice] = t("users.confirm_resend.success", :email => user.email, :sender => SUPPORT_EMAIL).html_safe
346 redirect_to :action => "login"
351 token = UserToken.find_by(:token => params[:confirm_string])
352 if token&.user&.new_email?
353 self.current_user = token.user
354 current_user.email = current_user.new_email
355 current_user.new_email = nil
356 current_user.email_valid = true
357 gravatar_enabled = gravatar_enable(current_user)
359 flash[:notice] = if gravatar_enabled
360 t("users.confirm_email.success") + " " + gravatar_status_message(current_user)
362 t("users.confirm_email.success")
365 flash[:errors] = current_user.errors
368 session[:user] = current_user.id
369 redirect_to :action => "account", :display_name => current_user.display_name
371 flash[:error] = t "users.confirm_email.failure"
372 redirect_to :action => "account", :display_name => token.user.display_name
374 flash[:error] = t "users.confirm_email.unknown_token"
381 render :action => :api_read, :content_type => "text/xml"
389 render :action => :api_read, :content_type => "text/xml"
393 raise OSM::APIBadUserInput, "The parameter users is required, and must be of the form users=id[,id[,id...]]" unless params["users"]
395 ids = params["users"].split(",").collect(&:to_i)
397 raise OSM::APIBadUserInput, "No users were given to search for" if ids.empty?
399 @users = User.visible.find(ids)
401 render :action => :api_users, :content_type => "text/xml"
405 doc = OSM::API.new.get_xml_doc
406 current_user.traces.reload.each do |trace|
407 doc.root << trace.to_xml_node
409 render :xml => doc.to_s
413 @user = User.find_by(:display_name => params[:display_name])
416 (@user.visible? || (current_user&.administrator?))
417 @title = @user.display_name
419 render_unknown_user params[:display_name]
424 @new_friend = User.find_by(:display_name => params[:display_name])
429 friend.befriender = current_user
430 friend.befriendee = @new_friend
431 if current_user.is_friends_with?(@new_friend)
432 flash[:warning] = t "users.make_friend.already_a_friend", :name => @new_friend.display_name
434 flash[:notice] = t "users.make_friend.success", :name => @new_friend.display_name
435 Notifier.friend_notification(friend).deliver_later
437 friend.add_error(t("users.make_friend.failed", :name => @new_friend.display_name))
441 redirect_to params[:referer]
443 redirect_to :action => "show"
447 render_unknown_user params[:display_name]
452 @friend = User.find_by(:display_name => params[:display_name])
456 if current_user.is_friends_with?(@friend)
457 Friend.where(:user_id => current_user.id, :friend_user_id => @friend.id).delete_all
458 flash[:notice] = t "users.remove_friend.success", :name => @friend.display_name
460 flash[:error] = t "users.remove_friend.not_a_friend", :name => @friend.display_name
464 redirect_to params[:referer]
466 redirect_to :action => "show"
470 render_unknown_user params[:display_name]
475 # sets a user's status
477 @user.status = params[:status]
479 redirect_to user_path(:display_name => params[:display_name])
483 # delete a user, marking them as deleted and removing personal data
486 redirect_to user_path(:display_name => params[:display_name])
490 # display a list of users matching specified criteria
493 ids = params[:user].keys.collect(&:to_i)
495 User.where(:id => ids).update_all(:status => "confirmed") if params[:confirm]
496 User.where(:id => ids).update_all(:status => "deleted") if params[:hide]
498 redirect_to url_for(:status => params[:status], :ip => params[:ip], :page => params[:page])
500 @params = params.permit(:status, :ip)
503 conditions[:status] = @params[:status] if @params[:status]
504 conditions[:creation_ip] = @params[:ip] if @params[:ip]
506 @user_pages, @users = paginate(:users,
507 :conditions => conditions,
514 # omniauth success callback
516 auth_info = request.env["omniauth.auth"]
518 provider = auth_info[:provider]
519 uid = auth_info[:uid]
520 name = auth_info[:info][:name]
521 email = auth_info[:info][:email]
525 email_verified = uid.match(%r{https://www.google.com/accounts/o8/id?(.*)}) ||
526 uid.match(%r{https://me.yahoo.com/(.*)})
527 when "google", "facebook"
528 email_verified = true
530 email_verified = false
533 if settings = session.delete(:new_user_settings)
534 current_user.auth_provider = provider
535 current_user.auth_uid = uid
537 update_user(current_user, settings)
539 session[:user_errors] = current_user.errors.as_json
541 redirect_to :action => "account", :display_name => current_user.display_name
542 elsif session[:new_user]
543 session[:new_user].auth_provider = provider
544 session[:new_user].auth_uid = uid
546 session[:new_user].status = "active" if email_verified && email == session[:new_user].email
548 redirect_to :action => "terms"
550 user = User.find_by(:auth_provider => provider, :auth_uid => uid)
552 if user.nil? && provider == "google"
553 openid_url = auth_info[:extra][:id_info]["openid_id"]
554 user = User.find_by(:auth_provider => "openid", :auth_uid => openid_url) if openid_url
555 user&.update(:auth_provider => provider, :auth_uid => uid)
561 unconfirmed_login(user)
562 when "active", "confirmed" then
563 successful_login(user, request.env["omniauth.params"]["referer"])
564 when "suspended" then
565 failed_login t("users.login.account is suspended", :webmaster => "mailto:#{SUPPORT_EMAIL}").html_safe
567 failed_login t("users.login.auth failure")
570 redirect_to :action => "new", :nickname => name, :email => email,
571 :auth_provider => provider, :auth_uid => uid
577 # omniauth failure callback
579 flash[:error] = t("users.auth_failure." + params[:message])
580 redirect_to params[:origin] || login_url
586 # handle password authentication
587 def password_authentication(username, password)
588 if user = User.authenticate(:username => username, :password => password)
589 successful_login(user)
590 elsif user = User.authenticate(:username => username, :password => password, :pending => true)
591 unconfirmed_login(user)
592 elsif User.authenticate(:username => username, :password => password, :suspended => true)
593 failed_login t("users.login.account is suspended", :webmaster => "mailto:#{SUPPORT_EMAIL}").html_safe, username
595 failed_login t("users.login.auth failure"), username
600 # return the URL to use for authentication
601 def auth_url(provider, uid, referer = nil)
602 params = { :provider => provider }
604 params[:openid_url] = openid_expand_url(uid) if provider == "openid"
607 params[:origin] = request.path
609 params[:origin] = request.path + "?referer=" + CGI.escape(referer)
610 params[:referer] = referer
617 # special case some common OpenID providers by applying heuristics to
618 # try and come up with the correct URL based on what the user entered
619 def openid_expand_url(openid_url)
622 elsif openid_url.match(%r{(.*)gmail.com(/?)$}) || openid_url.match(%r{(.*)googlemail.com(/?)$})
623 # Special case gmail.com as it is potentially a popular OpenID
624 # provider and, unlike yahoo.com, where it works automatically, Google
625 # have hidden their OpenID endpoint somewhere obscure this making it
626 # somewhat less user friendly.
627 "https://www.google.com/accounts/o8/id"
634 # process a successful login
635 def successful_login(user, referer = nil)
636 session[:user] = user.id
637 session_expires_after 28.days if session[:remember_me]
639 target = referer || session[:referer] || url_for(:controller => :site, :action => :index)
641 # The user is logged in, so decide where to send them:
643 # - If they haven't seen the contributor terms, send them there.
644 # - If they have a block on them, show them that.
645 # - If they were referred to the login, send them back there.
646 # - Otherwise, send them to the home page.
647 if REQUIRE_TERMS_SEEN && !user.terms_seen
648 redirect_to :action => :terms, :referer => target
649 elsif user.blocked_on_view
650 redirect_to user.blocked_on_view, :referer => target
655 session.delete(:remember_me)
656 session.delete(:referer)
660 # process a failed login
661 def failed_login(message, username = nil)
662 flash[:error] = message
664 redirect_to :action => "login", :referer => session[:referer],
665 :username => username, :remember_me => session[:remember_me]
667 session.delete(:remember_me)
668 session.delete(:referer)
673 def unconfirmed_login(user)
674 session[:token] = user.tokens.create.token
676 redirect_to :action => "confirm", :display_name => user.display_name
678 session.delete(:remember_me)
679 session.delete(:referer)
683 # update a user's details
684 def update_user(user, params)
685 user.display_name = params[:user][:display_name]
686 user.new_email = params[:user][:new_email]
688 unless params[:user][:pass_crypt].empty? && params[:user][:pass_crypt_confirmation].empty?
689 user.pass_crypt = params[:user][:pass_crypt]
690 user.pass_crypt_confirmation = params[:user][:pass_crypt_confirmation]
693 if params[:user][:description] != user.description
694 user.description = params[:user][:description]
695 user.description_format = "markdown"
698 user.languages = params[:user][:languages].split(",")
700 case params[:image_action]
702 user.image = params[:user][:image]
703 user.image_use_gravatar = false
706 user.image_use_gravatar = false
709 user.image_use_gravatar = true
712 user.home_lat = params[:user][:home_lat]
713 user.home_lon = params[:user][:home_lon]
715 user.preferred_editor = if params[:user][:preferred_editor] == "default"
718 params[:user][:preferred_editor]
721 if params[:user][:auth_provider].nil? || params[:user][:auth_provider].blank?
722 user.auth_provider = nil
729 if user.new_email.blank? || user.new_email == user.email
730 flash.now[:notice] = t "users.account.flash update success"
732 user.email = user.new_email
735 flash.now[:notice] = t "users.account.flash update success confirm needed"
738 Notifier.email_confirm(user, user.tokens.create).deliver_later
740 # Ignore errors sending email
743 current_user.errors.add(:new_email, current_user.errors[:email])
744 current_user.errors.add(:email, [])
753 # require that the user in the URL is the logged in user
755 head :forbidden if params[:display_name] != current_user.display_name
759 # ensure that there is a "user" instance variable
760 def lookup_user_by_id
761 @user = User.find(params[:id])
765 # ensure that there is a "user" instance variable
766 def lookup_user_by_name
767 @user = User.find_by(:display_name => params[:display_name])
768 rescue ActiveRecord::RecordNotFound
769 redirect_to :action => "view", :display_name => params[:display_name] unless @user
774 def disable_terms_redirect
775 # this is necessary otherwise going to the user terms page, when
776 # having not agreed already would cause an infinite redirect loop.
777 # it's .now so that this doesn't propagate to other pages.
778 flash.now[:skip_terms] = true
782 # return permitted user parameters
784 params.require(:user).permit(:email, :email_confirmation, :display_name,
785 :auth_provider, :auth_uid,
786 :pass_crypt, :pass_crypt_confirmation)
791 def check_signup_allowed(email = nil)
792 domain = if email.nil?
795 email.split("@").last
798 if blocked = Acl.no_account_creation(request.remote_ip, domain)
799 logger.info "Blocked signup from #{request.remote_ip} for #{email}"
801 render :action => "blocked"
808 # check if this user has a gravatar and set the user pref is true
809 def gravatar_enable(user)
810 # code from example https://en.gravatar.com/site/implement/images/ruby/
811 return false if user.image.present?
813 hash = Digest::MD5.hexdigest(user.email.downcase)
814 url = "https://www.gravatar.com/avatar/#{hash}?d=404" # without d=404 we will always get an image back
815 response = OSM.http_client.get(URI.parse(url))
816 oldsetting = user.image_use_gravatar
817 user.image_use_gravatar = response.success?
818 oldsetting != user.image_use_gravatar
822 # display a message about th current status of the gravatar setting
823 def gravatar_status_message(user)
824 if user.image_use_gravatar
825 t "users.account.gravatar.enabled"
827 t "users.account.gravatar.disabled"