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 => [: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]
21 cache_sweeper :user_sweeper, :only => [:account, :set_status, :delete]
24 @legale = params[:legale] || OSM.IPToCountry(request.remote_ip) || DEFAULT_LEGALE
25 @text = OSM.legal_text_for_country(@legale)
28 render :partial => "terms"
30 # The redirect from the OpenID provider reenters here
31 # again and we need to pass the parameters through to
32 # the open_id_authentication function
33 @user = session.delete(:new_user)
35 openid_verify(nil, @user) do |user,verified_email|
36 user.status = "active" if user.email == verified_email
39 if @user.openid_url.nil? or @user.invalid?
40 render :action => 'new'
42 session[:new_user] = @user
43 render :action => 'terms'
45 elsif params[:user] and Acl.no_account_creation(request.remote_ip, params[:user][:email].split("@").last)
46 render :action => 'blocked'
48 session[:referer] = params[:referer]
50 @title = t 'user.terms.title'
51 @user = User.new(params[:user]) if params[:user]
53 if params[:user] and params[:user][:openid_url] and @user.pass_crypt.empty?
54 # We are creating an account with OpenID and no password
55 # was specified so create a random one
56 @user.pass_crypt = SecureRandom.base64(16)
57 @user.pass_crypt_confirmation = @user.pass_crypt
61 @user.status = "pending"
65 # Something is wrong with a new user, so rerender the form
66 render :action => :new
68 # Error in existing user, so go to account settings
69 flash[:errors] = @user.errors
70 redirect_to :action => :account, :display_name => @user.display_name
72 elsif @user.terms_agreed?
73 # Already agreed to terms, so just show settings
74 redirect_to :action => :account, :display_name => @user.display_name
75 elsif params[:user] and params[:user][:openid_url] and not params[:user][:openid_url].empty?
76 # Verify OpenID before moving on
77 session[:new_user] = @user
78 openid_verify(params[:user][:openid_url], @user)
79 elsif @user.new_record?
80 # Save the user record
81 session[:new_user] = @user
84 # Not logged in, so redirect to the login page
85 redirect_to :action => :login, :referer => request.fullpath
91 @title = t 'user.new.title'
95 @user.terms_seen = true
98 flash[:notice] = t 'user.new.terms declined', :url => t('user.new.terms declined url')
102 redirect_to params[:referer]
104 redirect_to :action => :account, :display_name => @user.display_name
107 redirect_to t('user.terms.declined')
110 if !@user.terms_agreed?
111 @user.consider_pd = params[:user][:consider_pd]
112 @user.terms_agreed = Time.now.getutc
113 @user.terms_seen = true
115 flash[:notice] = t 'user.new.terms accepted'
120 redirect_to params[:referer]
122 redirect_to :action => :account, :display_name => @user.display_name
125 @user = session.delete(:new_user)
127 if Acl.no_account_creation(request.remote_ip, @user.email.split("@").last)
128 render :action => 'blocked'
130 @user.data_public = true
131 @user.description = "" if @user.description.nil?
132 @user.creation_ip = request.remote_ip
133 @user.languages = request.user_preferred_languages
134 @user.terms_agreed = Time.now.getutc
135 @user.terms_seen = true
136 @user.openid_url = nil if @user.openid_url and @user.openid_url.empty?
139 flash[:piwik_goal] = PIWIK_SIGNUP_GOAL if defined?(PIWIK_SIGNUP_GOAL)
141 if @user.status == "active"
142 flash[:notice] = t 'user.new.flash welcome', :email => @user.email
144 Notifier.signup_confirm(@user, nil).deliver
146 successful_login(@user)
148 flash[:notice] = t 'user.new.flash create success message', :email => @user.email
149 session[:token] = @user.tokens.create.token
151 Notifier.signup_confirm(@user, @user.tokens.create(:referer => session.delete(:referer))).deliver
153 redirect_to :action => 'login', :referer => params[:referer]
156 render :action => 'new', :referer => params[:referer]
163 @title = t 'user.account.title'
164 @tokens = @user.oauth_tokens.authorized
166 if params[:user] and params[:user][:display_name] and params[:user][:description]
167 if params[:user][:openid_url] and
168 params[:user][:openid_url].length > 0 and
169 params[:user][:openid_url] != @user.openid_url
170 # If the OpenID has changed, we want to check that it is a
171 # valid OpenID and one the user has control over before saving
172 # it as a password equivalent for the user.
173 session[:new_user_settings] = params
174 openid_verify(params[:user][:openid_url], @user)
176 update_user(@user, params)
179 # The redirect from the OpenID provider reenters here
180 # again and we need to pass the parameters through to
181 # the open_id_authentication function
182 settings = session.delete(:new_user_settings)
183 openid_verify(nil, @user) do |user|
184 update_user(user, settings)
190 @user.data_public = true
192 flash[:notice] = t 'user.go_public.flash success'
193 redirect_to :controller => 'user', :action => 'account', :display_name => @user.display_name
197 @title = t 'user.lost_password.title'
199 if params[:user] and params[:user][:email]
200 user = User.visible.find_by_email(params[:user][:email])
203 users = User.visible.where("LOWER(email) = LOWER(?)", params[:user][:email])
211 token = user.tokens.create
212 Notifier.lost_password(user, token).deliver
213 flash[:notice] = t 'user.lost_password.notice email on way'
214 redirect_to :action => 'login'
216 flash.now[:error] = t 'user.lost_password.notice email cannot find'
222 @title = t 'user.reset_password.title'
225 token = UserToken.find_by_token(params[:token])
231 @user.pass_crypt = params[:user][:pass_crypt]
232 @user.pass_crypt_confirmation = params[:user][:pass_crypt_confirmation]
233 @user.status = "active" if @user.status == "pending"
234 @user.email_valid = true
238 flash[:notice] = t 'user.reset_password.flash changed'
239 redirect_to :action => 'login'
243 flash[:error] = t 'user.reset_password.flash token bad'
244 redirect_to :action => 'lost_password'
250 @title = t 'user.new.title'
251 @referer = params[:referer] || session[:referer]
254 # The user is logged in already, so don't show them the signup
255 # page, instead send them to the home page
259 redirect_to :controller => 'site', :action => 'index'
261 elsif params.key?(:openid)
262 @user = User.new(:email => params[:email],
263 :email_confirmation => params[:email],
264 :display_name => params[:nickname],
265 :openid_url => params[:openid])
267 flash.now[:notice] = t 'user.new.openid association'
268 elsif Acl.no_account_creation(request.remote_ip)
269 render :action => 'blocked'
274 if params[:username] or using_open_id?
275 session[:remember_me] ||= params[:remember_me]
276 session[:referer] ||= params[:referer]
279 openid_authentication(params[:openid_url])
281 password_authentication(params[:username], params[:password])
287 @title = t 'user.logout.title'
289 if params[:session] == request.session_options[:id]
291 token = UserToken.find_by_token(session[:token])
295 session.delete(:token)
297 session.delete(:user)
298 session_expires_automatically
300 redirect_to params[:referer]
302 redirect_to :controller => 'site', :action => 'index'
309 if token = UserToken.find_by_token(params[:confirm_string])
310 if token.user.active?
311 flash[:error] = t('user.confirm.already active')
312 redirect_to :action => 'login'
315 user.status = "active"
316 user.email_valid = true
318 referer = token.referer
322 token = UserToken.find_by_token(session[:token])
323 session.delete(:token)
328 if token.nil? or token.user != user
329 flash[:notice] = t('user.confirm.success')
330 redirect_to :action => :login, :referer => referer
334 session[:user] = user.id
335 cookies.permanent["_osm_username"] = user.display_name
338 flash[:notice] = t('user.confirm.success') + "<br /><br />" + t('user.confirm.before you start')
339 redirect_to :action => :account, :display_name => user.display_name
341 flash[:notice] = t('user.confirm.success')
347 user = User.find_by_display_name(params[:display_name])
349 if user and user.active?
350 flash[:error] = t('user.confirm.already active')
352 flash[:error] = t('user.confirm.unknown token') + t('user.confirm.reconfirm', :reconfirm => url_for(:action => 'confirm_resend', :display_name => params[:display_name]))
354 flash[:error] = t('user.confirm.unknown token')
357 redirect_to :action => 'login'
363 if user = User.find_by_display_name(params[:display_name])
364 Notifier.signup_confirm(user, user.tokens.create).deliver
365 flash[:notice] = t 'user.confirm_resend.success', :email => user.email
367 flash[:notice] = t 'user.confirm_resend.failure', :name => params[:display_name]
370 redirect_to :action => 'login'
375 token = UserToken.find_by_token(params[:confirm_string])
376 if token and token.user.new_email?
378 @user.email = @user.new_email
379 @user.new_email = nil
380 @user.email_valid = true
382 flash[:notice] = t 'user.confirm_email.success'
384 flash[:errors] = @user.errors
387 session[:user] = @user.id
388 cookies.permanent["_osm_username"] = @user.display_name
389 redirect_to :action => 'account', :display_name => @user.display_name
391 flash[:error] = t 'user.confirm_email.failure'
392 redirect_to :action => 'account', :display_name => @user.display_name
398 render :nothing => true, :status => :gone unless @this_user.visible?
403 render :action => :api_read
407 doc = OSM::API.new.get_xml_doc
408 @user.traces.each do |trace|
409 doc.root << trace.to_xml_node() if trace.public? or trace.user == @user
411 render :text => doc.to_s, :content_type => "text/xml"
415 @this_user = User.find_by_display_name(params[:display_name])
418 (@this_user.visible? or (@user and @user.administrator?))
419 @title = @this_user.display_name
421 render_unknown_user params[:display_name]
426 @new_friend = User.find_by_display_name(params[:display_name])
431 friend.user_id = @user.id
432 friend.friend_user_id = @new_friend.id
433 unless @user.is_friends_with?(@new_friend)
435 flash[:notice] = t 'user.make_friend.success', :name => @new_friend.display_name
436 Notifier.friend_notification(friend).deliver
438 friend.add_error(t('user.make_friend.failed', :name => @new_friend.display_name))
441 flash[:warning] = t 'user.make_friend.already_a_friend', :name => @new_friend.display_name
445 redirect_to params[:referer]
447 redirect_to :controller => 'user', :action => 'view'
451 render_unknown_user params[:display_name]
456 @friend = User.find_by_display_name(params[:display_name])
460 if @user.is_friends_with?(@friend)
461 Friend.delete_all "user_id = #{@user.id} AND friend_user_id = #{@friend.id}"
462 flash[:notice] = t 'user.remove_friend.success', :name => @friend.display_name
464 flash[:error] = t 'user.remove_friend.not_a_friend', :name => @friend.display_name
468 redirect_to params[:referer]
470 redirect_to :controller => 'user', :action => 'view'
474 render_unknown_user params[:display_name]
479 # sets a user's status
481 @this_user.status = params[:status]
483 redirect_to :controller => 'user', :action => 'view', :display_name => params[:display_name]
487 # delete a user, marking them as deleted and removing personal data
490 redirect_to :controller => 'user', :action => 'view', :display_name => params[:display_name]
494 # display a list of users matching specified criteria
497 ids = params[:user].keys.collect { |id| id.to_i }
499 User.update_all("status = 'confirmed'", :id => ids) if params[:confirm]
500 User.update_all("status = 'deleted'", :id => ids) if params[:hide]
502 redirect_to url_for(:status => params[:status], :ip => params[:ip], :page => params[:page])
504 conditions = Hash.new
505 conditions[:status] = params[:status] if params[:status]
506 conditions[:creation_ip] = params[:ip] if params[:ip]
508 @user_pages, @users = paginate(:users,
509 :conditions => conditions,
518 # handle password authentication
519 def password_authentication(username, password)
520 if user = User.authenticate(:username => username, :password => password)
521 successful_login(user)
522 elsif user = User.authenticate(:username => username, :password => password, :pending => true)
523 failed_login t('user.login.account not active', :reconfirm => url_for(:action => 'confirm_resend', :display_name => user.display_name))
524 elsif User.authenticate(:username => username, :password => password, :suspended => true)
525 failed_login t('user.login.account is suspended', :webmaster => "mailto:webmaster@openstreetmap.org")
527 failed_login t('user.login.auth failure')
532 # handle OpenID authentication
533 def openid_authentication(openid_url)
534 # If we don't appear to have a user for this URL then ask the
535 # provider for some extra information to help with signup
536 if openid_url and User.find_by_openid_url(openid_url)
539 required = [:nickname, :email, "http://axschema.org/namePerson/friendly", "http://axschema.org/contact/email"]
542 # Start the authentication
543 authenticate_with_open_id(openid_expand_url(openid_url), :method => :get, :required => required) do |result, identity_url, sreg, ax|
544 if result.successful?
545 # We need to use the openid url passed back from the OpenID provider
546 # rather than the one supplied by the user, as these can be different.
548 # For example, you can simply enter yahoo.com in the login box rather
549 # than a user specific url. Only once it comes back from the provider
550 # provider do we know the unique address for the user.
551 if user = User.find_by_openid_url(identity_url)
554 failed_login t('user.login.account not active', :reconfirm => url_for(:action => 'confirm_resend', :display_name => user.display_name))
555 when "active", "confirmed" then
556 successful_login(user)
557 when "suspended" then
558 failed_login t('user.login.account is suspended', :webmaster => "mailto:webmaster@openstreetmap.org")
560 failed_login t('user.login.auth failure')
563 # Guard against not getting any extension data
564 sreg = Hash.new if sreg.nil?
565 ax = Hash.new if ax.nil?
567 # We don't have a user registered to this OpenID, so redirect
568 # to the create account page with username and email filled
569 # in if they have been given by the OpenID provider through
570 # the simple registration protocol.
571 nickname = sreg["nickname"] || ax["http://axschema.org/namePerson/friendly"].first
572 email = sreg["email"] || ax["http://axschema.org/contact/email"].first
574 redirect_to :controller => 'user', :action => 'new', :nickname => nickname, :email => email, :openid => identity_url
576 elsif result.missing?
577 failed_login t('user.login.openid missing provider')
578 elsif result.invalid?
579 failed_login t('user.login.openid invalid')
581 failed_login t('user.login.auth failure')
587 # verify an OpenID URL
588 def openid_verify(openid_url, user)
589 user.openid_url = openid_url
591 authenticate_with_open_id(openid_expand_url(openid_url), :method => :get, :required => [:email, "http://axschema.org/contact/email"]) do |result, identity_url, sreg, ax|
592 if result.successful?
593 # Do we trust the emails this provider returns?
594 if openid_email_verified(identity_url)
595 # Guard against not getting any extension data
596 sreg = Hash.new if sreg.nil?
597 ax = Hash.new if ax.nil?
599 # Get the verified email
600 verified_email = sreg["email"] || ax["http://axschema.org/contact/email"].first
603 # We need to use the openid url passed back from the OpenID provider
604 # rather than the one supplied by the user, as these can be different.
606 # For example, you can simply enter yahoo.com in the login box rather
607 # than a user specific url. Only once it comes back from the provider
608 # provider do we know the unique address for the user.
609 user.openid_url = identity_url
610 yield user, verified_email
611 elsif result.missing?
612 flash.now[:error] = t 'user.login.openid missing provider'
613 elsif result.invalid?
614 flash.now[:error] = t 'user.login.openid invalid'
616 flash.now[:error] = t 'user.login.auth failure'
622 # special case some common OpenID providers by applying heuristics to
623 # try and come up with the correct URL based on what the user entered
624 def openid_expand_url(openid_url)
627 elsif openid_url.match(/(.*)gmail.com(\/?)$/) or openid_url.match(/(.*)googlemail.com(\/?)$/)
628 # Special case gmail.com as it is potentially a popular OpenID
629 # provider and, unlike yahoo.com, where it works automatically, Google
630 # have hidden their OpenID endpoint somewhere obscure this making it
631 # somewhat less user friendly.
632 return 'https://www.google.com/accounts/o8/id'
639 # check if we trust an OpenID provider to return a verified
640 # email, so that we can skpi verifying it ourselves
641 def openid_email_verified(openid_url)
642 openid_url.match(/https:\/\/www.google.com\/accounts\/o8\/id?(.*)/) or
643 openid_url.match(/https:\/\/me.yahoo.com\/(.*)/)
647 # process a successful login
648 def successful_login(user)
649 cookies.permanent["_osm_username"] = user.display_name
651 session[:user] = user.id
652 session_expires_after 28.days if session[:remember_me]
654 target = session[:referer] || url_for(:controller => :site, :action => :index)
656 # The user is logged in, so decide where to send them:
658 # - If they haven't seen the contributor terms, send them there.
659 # - If they have a block on them, show them that.
660 # - If they were referred to the login, send them back there.
661 # - Otherwise, send them to the home page.
662 if REQUIRE_TERMS_SEEN and not user.terms_seen
663 redirect_to :controller => :user, :action => :terms, :referer => target
664 elsif user.blocked_on_view
665 redirect_to user.blocked_on_view, :referer => target
670 session.delete(:remember_me)
671 session.delete(:referer)
675 # process a failed login
676 def failed_login(message)
677 flash[:error] = message
679 redirect_to :action => 'login', :referer => session[:referer]
681 session.delete(:remember_me)
682 session.delete(:referer)
686 # update a user's details
687 def update_user(user, params)
688 user.display_name = params[:user][:display_name]
689 user.new_email = params[:user][:new_email]
691 if params[:user][:pass_crypt].length > 0 or params[:user][:pass_crypt_confirmation].length > 0
692 user.pass_crypt = params[:user][:pass_crypt]
693 user.pass_crypt_confirmation = params[:user][:pass_crypt_confirmation]
696 if params[:user][:description] != user.description
697 user.description = params[:user][:description]
698 user.description_format = "markdown"
701 user.languages = params[:user][:languages].split(",")
703 case params[:image_action]
705 user.image = params[:user][:image]
706 user.image_use_gravatar = false
709 user.image_use_gravatar = false
712 user.image_use_gravatar = true
715 user.home_lat = params[:user][:home_lat]
716 user.home_lon = params[:user][:home_lon]
718 if params[:user][:preferred_editor] == "default"
719 user.preferred_editor = nil
721 user.preferred_editor = params[:user][:preferred_editor]
724 user.openid_url = nil if params[:user][:openid_url].blank?
729 cookies.permanent["_osm_username"] = user.display_name
731 if user.new_email.blank?
732 flash.now[:notice] = t 'user.account.flash update success'
734 user.email = user.new_email
737 flash.now[:notice] = t 'user.account.flash update success confirm needed'
740 Notifier.email_confirm(user, user.tokens.create).deliver
742 # Ignore errors sending email
745 @user.errors.set(:new_email, @user.errors.get(:email))
746 @user.errors.set(:email, [])
755 # require that the user is a administrator, or fill out a helpful error message
756 # and return them to the user page.
757 def require_administrator
758 if @user and not @user.administrator?
759 flash[:error] = t('user.filter.not_an_administrator')
761 if params[:display_name]
762 redirect_to :controller => 'user', :action => 'view', :display_name => params[:display_name]
764 redirect_to :controller => 'user', :action => 'login', :referer => request.fullpath
767 redirect_to :controller => 'user', :action => 'login', :referer => request.fullpath
772 # ensure that there is a "this_user" instance variable
773 def lookup_user_by_id
774 @this_user = User.find(params[:id])
778 # ensure that there is a "this_user" instance variable
779 def lookup_user_by_name
780 @this_user = User.find_by_display_name(params[:display_name])
781 rescue ActiveRecord::RecordNotFound
782 redirect_to :controller => 'user', :action => 'view', :display_name => params[:display_name] unless @this_user
786 # Choose the layout to use. See
787 # https://rails.lighthouseapp.com/projects/8994/tickets/5371-layout-with-onlyexcept-options-makes-other-actions-render-without-layouts
789 oauth_url = url_for(:controller => :oauth, :action => :authorize, :only_path => true)
791 if [ 'api_details' ].include? action_name
793 elsif params[:referer] and URI.parse(params[:referer]).path == oauth_url
802 def disable_terms_redirect
803 # this is necessary otherwise going to the user terms page, when
804 # having not agreed already would cause an infinite redirect loop.
805 # it's .now so that this doesn't propagate to other pages.
806 flash.now[:skip_terms] = true