1 class UserController < ApplicationController
4 skip_before_filter :verify_authenticity_token, :only => [:api_read, :api_details, :api_gpx_files]
5 before_filter :disable_terms_redirect, :only => [:terms, :save, :logout, :api_details]
6 before_filter :authorize, :only => [:api_details, :api_gpx_files]
7 before_filter :authorize_web, :except => [:api_read, :api_details, :api_gpx_files]
8 before_filter :set_locale, :except => [:api_read, :api_details, :api_gpx_files]
9 before_filter :require_user, :only => [:account, :go_public, :make_friend, :remove_friend]
10 before_filter :check_database_readable, :except => [:login, :api_read, :api_details, :api_gpx_files]
11 before_filter :check_database_writable, :only => [:new, :account, :confirm, :confirm_email, :lost_password, :reset_password, :go_public, :make_friend, :remove_friend]
12 before_filter :check_api_readable, :only => [:api_read, :api_details, :api_gpx_files]
13 before_filter :require_allow_read_prefs, :only => [:api_details]
14 before_filter :require_allow_read_gpx, :only => [:api_gpx_files]
15 before_filter :require_cookies, :only => [:new, :login, :confirm]
16 before_filter :require_administrator, :only => [:set_status, :delete, :list]
17 around_filter :api_call_handle_error, :only => [:api_read, :api_details, :api_gpx_files]
18 before_filter :lookup_user_by_id, :only => [:api_read]
19 before_filter :lookup_user_by_name, :only => [:set_status, :delete]
22 @legale = params[:legale] || OSM.IPToCountry(request.remote_ip) || DEFAULT_LEGALE
23 @text = OSM.legal_text_for_country(@legale)
26 render :partial => "terms"
28 @title = t 'user.terms.title'
29 @user ||= session[:new_user]
32 redirect_to :action => :login, :referer => request.fullpath
33 elsif @user.terms_agreed?
34 # Already agreed to terms, so just show settings
35 redirect_to :action => :account, :display_name => @user.display_name
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')
52 redirect_to params[:referer]
54 redirect_to :action => :account, :display_name => @user.display_name
57 redirect_to t('user.terms.declined')
60 if !@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'
70 redirect_to params[:referer]
72 redirect_to :action => :account, :display_name => @user.display_name
75 @user = session.delete(:new_user)
77 if Acl.no_account_creation(request.remote_ip, @user.email.split("@").last)
78 render :action => 'blocked'
80 @user.data_public = true
81 @user.description = "" if @user.description.nil?
82 @user.creation_ip = request.remote_ip
83 @user.languages = request.user_preferred_languages
84 @user.terms_agreed = Time.now.getutc
85 @user.terms_seen = true
86 @user.openid_url = nil if @user.openid_url and @user.openid_url.empty?
89 flash[:piwik_goal] = PIWIK_SIGNUP_GOAL if defined?(PIWIK_SIGNUP_GOAL)
92 referer_params = Rack::Utils.parse_query(URI(session[:referer]).query)
93 referer = welcome_path(referer_params.slice(:lat, :lon, :zoom, :editor))
95 referer = welcome_path
98 if @user.status == "active"
99 session[:referer] = referer
100 successful_login(@user)
102 session[:token] = @user.tokens.create.token
103 Notifier.signup_confirm(@user, @user.tokens.create(:referer => referer)).deliver
104 redirect_to :action => 'confirm', :display_name => @user.display_name
107 render :action => 'new', :referer => params[:referer]
114 @title = t 'user.account.title'
115 @tokens = @user.oauth_tokens.authorized
117 if params[:user] and params[:user][:display_name] and params[:user][:description]
118 if params[:user][:openid_url] and
119 params[:user][:openid_url].length > 0 and
120 params[:user][:openid_url] != @user.openid_url
121 # If the OpenID has changed, we want to check that it is a
122 # valid OpenID and one the user has control over before saving
123 # it as a password equivalent for the user.
124 session[:new_user_settings] = params
125 openid_verify(params[:user][:openid_url], @user)
127 update_user(@user, params)
130 # The redirect from the OpenID provider reenters here
131 # again and we need to pass the parameters through to
132 # the open_id_authentication function
133 settings = session.delete(:new_user_settings)
134 openid_verify(nil, @user) do |user|
135 update_user(user, settings)
141 @user.data_public = true
143 flash[:notice] = t 'user.go_public.flash success'
144 redirect_to :controller => 'user', :action => 'account', :display_name => @user.display_name
148 @title = t 'user.lost_password.title'
150 if params[:user] and params[:user][:email]
151 user = User.visible.find_by_email(params[:user][:email])
154 users = User.visible.where("LOWER(email) = LOWER(?)", params[:user][:email])
162 token = user.tokens.create
163 Notifier.lost_password(user, token).deliver
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])
182 @user.pass_crypt = params[:user][:pass_crypt]
183 @user.pass_crypt_confirmation = params[:user][:pass_crypt_confirmation]
184 @user.status = "active" if @user.status == "pending"
185 @user.email_valid = true
189 flash[:notice] = t 'user.reset_password.flash changed'
190 redirect_to :action => 'login'
194 flash[:error] = t 'user.reset_password.flash token bad'
195 redirect_to :action => 'lost_password'
201 @title = t 'user.new.title'
202 @referer = params[:referer] || session[:referer]
205 # The redirect from the OpenID provider reenters here
206 # again and we need to pass the parameters through to
207 # the open_id_authentication function
208 @user = session.delete(:new_user)
210 openid_verify(nil, @user) do |user, verified_email|
211 user.status = "active" if user.email == verified_email
214 if @user.openid_url.nil? or @user.invalid?
215 render :action => 'new'
217 session[:new_user] = @user
218 redirect_to :action => 'terms'
221 # The user is logged in already, so don't show them the signup
222 # page, instead send them to the home page
226 redirect_to :controller => 'site', :action => 'index'
228 elsif params.key?(:openid)
229 @user = User.new(:email => params[:email],
230 :email_confirmation => params[:email],
231 :display_name => params[:nickname],
232 :openid_url => params[:openid])
234 flash.now[:notice] = t 'user.new.openid association'
235 elsif Acl.no_account_creation(request.remote_ip)
236 render :action => 'blocked'
241 if params[:user] and Acl.no_account_creation(request.remote_ip, params[:user][:email].split("@").last)
242 render :action => 'blocked'
245 session[:referer] = params[:referer]
247 @user = User.new(params[:user])
248 @user.status = "pending"
250 if @user.openid_url.present? && @user.pass_crypt.empty?
251 # We are creating an account with OpenID and no password
252 # was specified so create a random one
253 @user.pass_crypt = SecureRandom.base64(16)
254 @user.pass_crypt_confirmation = @user.pass_crypt
258 # Something is wrong with a new user, so rerender the form
259 render :action => "new"
260 elsif @user.openid_url.present?
261 # Verify OpenID before moving on
262 session[:new_user] = @user
263 openid_verify(@user.openid_url, @user)
265 # Save the user record
266 session[:new_user] = @user
267 redirect_to :action => :terms
273 if params[:username] or using_open_id?
274 session[:remember_me] ||= params[:remember_me]
275 session[:referer] ||= params[:referer]
278 openid_authentication(params[:openid_url])
280 password_authentication(params[:username], params[:password])
286 @title = t 'user.logout.title'
288 if params[:session] == request.session_options[:id]
290 token = UserToken.find_by_token(session[:token])
294 session.delete(:token)
296 session.delete(:user)
297 session_expires_automatically
299 redirect_to params[:referer]
301 redirect_to :controller => 'site', :action => 'index'
307 if request.post? && (token = UserToken.find_by_token(params[:confirm_string]))
308 if token.user.active?
309 flash[:error] = t('user.confirm.already active')
310 redirect_to :action => 'login'
313 user.status = "active"
314 user.email_valid = true
316 referer = token.referer
320 token = UserToken.find_by_token(session[:token])
321 session.delete(:token)
326 if token.nil? or token.user != user
327 flash[:notice] = t('user.confirm.success')
328 redirect_to :action => :login, :referer => referer
332 session[:user] = user.id
333 cookies.permanent["_osm_username"] = user.display_name
335 redirect_to referer || welcome_path
339 user = User.find_by_display_name(params[:display_name])
340 if !user || user.active?
341 redirect_to root_path
347 if user = User.find_by_display_name(params[:display_name])
348 Notifier.signup_confirm(user, user.tokens.create).deliver
349 flash[:notice] = t 'user.confirm_resend.success', :email => user.email
351 flash[:notice] = t 'user.confirm_resend.failure', :name => params[:display_name]
354 redirect_to :action => 'login'
359 token = UserToken.find_by_token(params[:confirm_string])
360 if token and token.user.new_email?
362 @user.email = @user.new_email
363 @user.new_email = nil
364 @user.email_valid = true
366 flash[:notice] = t 'user.confirm_email.success'
368 flash[:errors] = @user.errors
371 session[:user] = @user.id
372 cookies.permanent["_osm_username"] = @user.display_name
373 redirect_to :action => 'account', :display_name => @user.display_name
375 flash[:error] = t 'user.confirm_email.failure'
376 redirect_to :action => 'account', :display_name => @user.display_name
382 render :text => "", :status => :gone unless @this_user.visible?
387 render :action => :api_read
391 doc = OSM::API.new.get_xml_doc
392 @user.traces.each do |trace|
393 doc.root << trace.to_xml_node() if trace.public? or trace.user == @user
395 render :text => doc.to_s, :content_type => "text/xml"
399 @this_user = User.find_by_display_name(params[:display_name])
402 (@this_user.visible? or (@user and @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.user_id = @user.id
416 friend.friend_user_id = @new_friend.id
417 unless @user.is_friends_with?(@new_friend)
419 flash[:notice] = t 'user.make_friend.success', :name => @new_friend.display_name
420 Notifier.friend_notification(friend).deliver
422 friend.add_error(t('user.make_friend.failed', :name => @new_friend.display_name))
425 flash[:warning] = t 'user.make_friend.already_a_friend', :name => @new_friend.display_name
429 redirect_to params[:referer]
431 redirect_to :controller => 'user', :action => 'view'
435 render_unknown_user params[:display_name]
440 @friend = User.find_by_display_name(params[:display_name])
444 if @user.is_friends_with?(@friend)
445 Friend.delete_all "user_id = #{@user.id} AND friend_user_id = #{@friend.id}"
446 flash[:notice] = t 'user.remove_friend.success', :name => @friend.display_name
448 flash[:error] = t 'user.remove_friend.not_a_friend', :name => @friend.display_name
452 redirect_to params[:referer]
454 redirect_to :controller => 'user', :action => 'view'
458 render_unknown_user params[:display_name]
463 # sets a user's status
465 @this_user.status = params[:status]
467 redirect_to :controller => 'user', :action => 'view', :display_name => params[:display_name]
471 # delete a user, marking them as deleted and removing personal data
474 redirect_to :controller => 'user', :action => 'view', :display_name => params[:display_name]
478 # display a list of users matching specified criteria
481 ids = params[:user].keys.collect { |id| id.to_i }
483 User.update_all("status = 'confirmed'", :id => ids) if params[:confirm]
484 User.update_all("status = 'deleted'", :id => ids) if params[:hide]
486 redirect_to url_for(:status => params[:status], :ip => params[:ip], :page => params[:page])
488 conditions = Hash.new
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,
502 # handle password authentication
503 def password_authentication(username, password)
504 if user = User.authenticate(:username => username, :password => password)
505 successful_login(user)
506 elsif user = User.authenticate(:username => username, :password => password, :pending => true)
507 unconfirmed_login(user)
508 elsif User.authenticate(:username => username, :password => password, :suspended => true)
509 failed_login t('user.login.account is suspended', :webmaster => "mailto:webmaster@openstreetmap.org")
511 failed_login t('user.login.auth failure')
516 # handle OpenID authentication
517 def openid_authentication(openid_url)
518 # If we don't appear to have a user for this URL then ask the
519 # provider for some extra information to help with signup
520 if openid_url and User.find_by_openid_url(openid_url)
523 required = [:nickname, :email, "http://axschema.org/namePerson/friendly", "http://axschema.org/contact/email"]
526 # Start the authentication
527 authenticate_with_open_id(openid_expand_url(openid_url), :method => :get, :required => required) do |result, identity_url, sreg, ax|
528 if result.successful?
529 # We need to use the openid url passed back from the OpenID provider
530 # rather than the one supplied by the user, as these can be different.
532 # For example, you can simply enter yahoo.com in the login box rather
533 # than a user specific url. Only once it comes back from the provider
534 # provider do we know the unique address for the user.
535 if user = User.find_by_openid_url(identity_url)
538 unconfirmed_login(user)
539 when "active", "confirmed" then
540 successful_login(user)
541 when "suspended" then
542 failed_login t('user.login.account is suspended', :webmaster => "mailto:webmaster@openstreetmap.org")
544 failed_login t('user.login.auth failure')
547 # Guard against not getting any extension data
548 sreg = Hash.new if sreg.nil?
549 ax = Hash.new if ax.nil?
551 # We don't have a user registered to this OpenID, so redirect
552 # to the create account page with username and email filled
553 # in if they have been given by the OpenID provider through
554 # the simple registration protocol.
555 nickname = sreg["nickname"] || ax["http://axschema.org/namePerson/friendly"].first
556 email = sreg["email"] || ax["http://axschema.org/contact/email"].first
558 redirect_to :controller => 'user', :action => 'new', :nickname => nickname, :email => email, :openid => identity_url
560 elsif result.missing?
561 failed_login t('user.login.openid missing provider')
562 elsif result.invalid?
563 failed_login t('user.login.openid invalid')
565 failed_login t('user.login.auth failure')
571 # verify an OpenID URL
572 def openid_verify(openid_url, user)
573 user.openid_url = openid_url
575 authenticate_with_open_id(openid_expand_url(openid_url), :method => :get, :required => [:email, "http://axschema.org/contact/email"]) do |result, identity_url, sreg, ax|
576 if result.successful?
577 # Do we trust the emails this provider returns?
578 if openid_email_verified(identity_url)
579 # Guard against not getting any extension data
580 sreg = Hash.new if sreg.nil?
581 ax = Hash.new if ax.nil?
583 # Get the verified email
584 verified_email = sreg["email"] || ax["http://axschema.org/contact/email"].first
587 # We need to use the openid url passed back from the OpenID provider
588 # rather than the one supplied by the user, as these can be different.
590 # For example, you can simply enter yahoo.com in the login box rather
591 # than a user specific url. Only once it comes back from the provider
592 # provider do we know the unique address for the user.
593 user.openid_url = identity_url
594 yield user, verified_email
595 elsif result.missing?
596 flash.now[:error] = t 'user.login.openid missing provider'
597 elsif result.invalid?
598 flash.now[:error] = t 'user.login.openid invalid'
600 flash.now[:error] = t 'user.login.auth failure'
606 # special case some common OpenID providers by applying heuristics to
607 # try and come up with the correct URL based on what the user entered
608 def openid_expand_url(openid_url)
611 elsif openid_url.match(/(.*)gmail.com(\/?)$/) or openid_url.match(/(.*)googlemail.com(\/?)$/)
612 # Special case gmail.com as it is potentially a popular OpenID
613 # provider and, unlike yahoo.com, where it works automatically, Google
614 # have hidden their OpenID endpoint somewhere obscure this making it
615 # somewhat less user friendly.
616 return 'https://www.google.com/accounts/o8/id'
623 # check if we trust an OpenID provider to return a verified
624 # email, so that we can skpi verifying it ourselves
625 def openid_email_verified(openid_url)
626 openid_url.match(/https:\/\/www.google.com\/accounts\/o8\/id?(.*)/) or
627 openid_url.match(/https:\/\/me.yahoo.com\/(.*)/)
631 # process a successful login
632 def successful_login(user)
633 cookies.permanent["_osm_username"] = user.display_name
635 session[:user] = user.id
636 session_expires_after 28.days if session[:remember_me]
638 target = session[:referer] || url_for(:controller => :site, :action => :index)
640 # The user is logged in, so decide where to send them:
642 # - If they haven't seen the contributor terms, send them there.
643 # - If they have a block on them, show them that.
644 # - If they were referred to the login, send them back there.
645 # - Otherwise, send them to the home page.
646 if REQUIRE_TERMS_SEEN and not user.terms_seen
647 redirect_to :controller => :user, :action => :terms, :referer => target
648 elsif user.blocked_on_view
649 redirect_to user.blocked_on_view, :referer => target
654 session.delete(:remember_me)
655 session.delete(:referer)
659 # process a failed login
660 def failed_login(message)
661 flash[:error] = message
663 redirect_to :action => 'login', :referer => session[:referer]
665 session.delete(:remember_me)
666 session.delete(:referer)
671 def unconfirmed_login(user)
672 redirect_to :action => 'confirm', :display_name => user.display_name
674 session.delete(:remember_me)
675 session.delete(:referer)
679 # update a user's details
680 def update_user(user, params)
681 user.display_name = params[:user][:display_name]
682 user.new_email = params[:user][:new_email]
684 if params[:user][:pass_crypt].length > 0 or params[:user][:pass_crypt_confirmation].length > 0
685 user.pass_crypt = params[:user][:pass_crypt]
686 user.pass_crypt_confirmation = params[:user][:pass_crypt_confirmation]
689 if params[:user][:description] != user.description
690 user.description = params[:user][:description]
691 user.description_format = "markdown"
694 user.languages = params[:user][:languages].split(",")
696 case params[:image_action]
698 user.image = params[:user][:image]
699 user.image_use_gravatar = false
702 user.image_use_gravatar = false
705 user.image_use_gravatar = true
708 user.home_lat = params[:user][:home_lat]
709 user.home_lon = params[:user][:home_lon]
711 if params[:user][:preferred_editor] == "default"
712 user.preferred_editor = nil
714 user.preferred_editor = params[:user][:preferred_editor]
717 user.openid_url = nil if params[:user][:openid_url].blank?
722 cookies.permanent["_osm_username"] = user.display_name
724 if user.new_email.blank?
725 flash.now[:notice] = t 'user.account.flash update success'
727 user.email = user.new_email
730 flash.now[:notice] = t 'user.account.flash update success confirm needed'
733 Notifier.email_confirm(user, user.tokens.create).deliver
735 # Ignore errors sending email
738 @user.errors.set(:new_email, @user.errors.get(:email))
739 @user.errors.set(:email, [])
748 # require that the user is a administrator, or fill out a helpful error message
749 # and return them to the user page.
750 def require_administrator
751 if @user and not @user.administrator?
752 flash[:error] = t('user.filter.not_an_administrator')
754 if params[:display_name]
755 redirect_to :controller => 'user', :action => 'view', :display_name => params[:display_name]
757 redirect_to :controller => 'user', :action => 'login', :referer => request.fullpath
760 redirect_to :controller => 'user', :action => 'login', :referer => request.fullpath
765 # ensure that there is a "this_user" instance variable
766 def lookup_user_by_id
767 @this_user = User.find(params[:id])
771 # ensure that there is a "this_user" instance variable
772 def lookup_user_by_name
773 @this_user = User.find_by_display_name(params[:display_name])
774 rescue ActiveRecord::RecordNotFound
775 redirect_to :controller => 'user', :action => 'view', :display_name => params[:display_name] unless @this_user
779 # Choose the layout to use. See
780 # https://rails.lighthouseapp.com/projects/8994/tickets/5371-layout-with-onlyexcept-options-makes-other-actions-render-without-layouts
782 oauth_url = url_for(:controller => :oauth, :action => :authorize, :only_path => true)
784 if [ 'api_details' ].include? action_name
786 elsif params[:referer] and URI.parse(params[:referer]).path == oauth_url
795 def disable_terms_redirect
796 # this is necessary otherwise going to the user terms page, when
797 # having not agreed already would cause an infinite redirect loop.
798 # it's .now so that this doesn't propagate to other pages.
799 flash.now[:skip_terms] = true