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)
91 if @user.status == "active"
92 session[:referer] = welcome_path
93 successful_login(@user)
95 session[:token] = @user.tokens.create.token
96 Notifier.signup_confirm(@user, @user.tokens.create(:referer => welcome_path)).deliver
97 redirect_to :action => 'confirm', :display_name => @user.display_name
100 render :action => 'new', :referer => params[:referer]
107 @title = t 'user.account.title'
108 @tokens = @user.oauth_tokens.authorized
110 if params[:user] and params[:user][:display_name] and params[:user][:description]
111 if params[:user][:openid_url] and
112 params[:user][:openid_url].length > 0 and
113 params[:user][:openid_url] != @user.openid_url
114 # If the OpenID has changed, we want to check that it is a
115 # valid OpenID and one the user has control over before saving
116 # it as a password equivalent for the user.
117 session[:new_user_settings] = params
118 openid_verify(params[:user][:openid_url], @user)
120 update_user(@user, params)
123 # The redirect from the OpenID provider reenters here
124 # again and we need to pass the parameters through to
125 # the open_id_authentication function
126 settings = session.delete(:new_user_settings)
127 openid_verify(nil, @user) do |user|
128 update_user(user, settings)
134 @user.data_public = true
136 flash[:notice] = t 'user.go_public.flash success'
137 redirect_to :controller => 'user', :action => 'account', :display_name => @user.display_name
141 @title = t 'user.lost_password.title'
143 if params[:user] and params[:user][:email]
144 user = User.visible.find_by_email(params[:user][:email])
147 users = User.visible.where("LOWER(email) = LOWER(?)", params[:user][:email])
155 token = user.tokens.create
156 Notifier.lost_password(user, token).deliver
157 flash[:notice] = t 'user.lost_password.notice email on way'
158 redirect_to :action => 'login'
160 flash.now[:error] = t 'user.lost_password.notice email cannot find'
166 @title = t 'user.reset_password.title'
169 token = UserToken.find_by_token(params[:token])
175 @user.pass_crypt = params[:user][:pass_crypt]
176 @user.pass_crypt_confirmation = params[:user][:pass_crypt_confirmation]
177 @user.status = "active" if @user.status == "pending"
178 @user.email_valid = true
182 flash[:notice] = t 'user.reset_password.flash changed'
183 redirect_to :action => 'login'
187 flash[:error] = t 'user.reset_password.flash token bad'
188 redirect_to :action => 'lost_password'
194 @title = t 'user.new.title'
195 @referer = params[:referer] || session[:referer]
198 # The redirect from the OpenID provider reenters here
199 # again and we need to pass the parameters through to
200 # the open_id_authentication function
201 @user = session.delete(:new_user)
203 openid_verify(nil, @user) do |user, verified_email|
204 user.status = "active" if user.email == verified_email
207 if @user.openid_url.nil? or @user.invalid?
208 render :action => 'new'
210 session[:new_user] = @user
211 redirect_to :action => 'terms'
214 # The user is logged in already, so don't show them the signup
215 # page, instead send them to the home page
219 redirect_to :controller => 'site', :action => 'index'
221 elsif params.key?(:openid)
222 @user = User.new(:email => params[:email],
223 :email_confirmation => params[:email],
224 :display_name => params[:nickname],
225 :openid_url => params[:openid])
227 flash.now[:notice] = t 'user.new.openid association'
228 elsif Acl.no_account_creation(request.remote_ip)
229 render :action => 'blocked'
234 if params[:user] and Acl.no_account_creation(request.remote_ip, params[:user][:email].split("@").last)
235 render :action => 'blocked'
238 session[:referer] = params[:referer]
240 @user = User.new(params[:user])
241 @user.status = "pending"
243 if @user.openid_url.present? && @user.pass_crypt.empty?
244 # We are creating an account with OpenID and no password
245 # was specified so create a random one
246 @user.pass_crypt = SecureRandom.base64(16)
247 @user.pass_crypt_confirmation = @user.pass_crypt
251 # Something is wrong with a new user, so rerender the form
252 render :action => "new"
253 elsif @user.openid_url.present?
254 # Verify OpenID before moving on
255 session[:new_user] = @user
256 openid_verify(@user.openid_url, @user)
258 # Save the user record
259 session[:new_user] = @user
260 redirect_to :action => :terms
266 if params[:username] or using_open_id?
267 session[:remember_me] ||= params[:remember_me]
268 session[:referer] ||= params[:referer]
271 openid_authentication(params[:openid_url])
273 password_authentication(params[:username], params[:password])
279 @title = t 'user.logout.title'
281 if params[:session] == request.session_options[:id]
283 token = UserToken.find_by_token(session[:token])
287 session.delete(:token)
289 session.delete(:user)
290 session_expires_automatically
292 redirect_to params[:referer]
294 redirect_to :controller => 'site', :action => 'index'
300 if request.post? && (token = UserToken.find_by_token(params[:confirm_string]))
301 if token.user.active?
302 flash[:error] = t('user.confirm.already active')
303 redirect_to :action => 'login'
306 user.status = "active"
307 user.email_valid = true
309 referer = token.referer
313 token = UserToken.find_by_token(session[:token])
314 session.delete(:token)
319 if token.nil? or token.user != user
320 flash[:notice] = t('user.confirm.success')
321 redirect_to :action => :login, :referer => referer
325 session[:user] = user.id
326 cookies.permanent["_osm_username"] = user.display_name
328 redirect_to referer || welcome_path
332 user = User.find_by_display_name(params[:display_name])
333 if !user || user.active?
334 redirect_to root_path
340 if user = User.find_by_display_name(params[:display_name])
341 Notifier.signup_confirm(user, user.tokens.create).deliver
342 flash[:notice] = t 'user.confirm_resend.success', :email => user.email
344 flash[:notice] = t 'user.confirm_resend.failure', :name => params[:display_name]
347 redirect_to :action => 'login'
352 token = UserToken.find_by_token(params[:confirm_string])
353 if token and token.user.new_email?
355 @user.email = @user.new_email
356 @user.new_email = nil
357 @user.email_valid = true
359 flash[:notice] = t 'user.confirm_email.success'
361 flash[:errors] = @user.errors
364 session[:user] = @user.id
365 cookies.permanent["_osm_username"] = @user.display_name
366 redirect_to :action => 'account', :display_name => @user.display_name
368 flash[:error] = t 'user.confirm_email.failure'
369 redirect_to :action => 'account', :display_name => @user.display_name
375 render :text => "", :status => :gone unless @this_user.visible?
380 render :action => :api_read
384 doc = OSM::API.new.get_xml_doc
385 @user.traces.each do |trace|
386 doc.root << trace.to_xml_node() if trace.public? or trace.user == @user
388 render :text => doc.to_s, :content_type => "text/xml"
392 @this_user = User.find_by_display_name(params[:display_name])
395 (@this_user.visible? or (@user and @user.administrator?))
396 @title = @this_user.display_name
398 render_unknown_user params[:display_name]
403 @new_friend = User.find_by_display_name(params[:display_name])
408 friend.user_id = @user.id
409 friend.friend_user_id = @new_friend.id
410 unless @user.is_friends_with?(@new_friend)
412 flash[:notice] = t 'user.make_friend.success', :name => @new_friend.display_name
413 Notifier.friend_notification(friend).deliver
415 friend.add_error(t('user.make_friend.failed', :name => @new_friend.display_name))
418 flash[:warning] = t 'user.make_friend.already_a_friend', :name => @new_friend.display_name
422 redirect_to params[:referer]
424 redirect_to :controller => 'user', :action => 'view'
428 render_unknown_user params[:display_name]
433 @friend = User.find_by_display_name(params[:display_name])
437 if @user.is_friends_with?(@friend)
438 Friend.delete_all "user_id = #{@user.id} AND friend_user_id = #{@friend.id}"
439 flash[:notice] = t 'user.remove_friend.success', :name => @friend.display_name
441 flash[:error] = t 'user.remove_friend.not_a_friend', :name => @friend.display_name
445 redirect_to params[:referer]
447 redirect_to :controller => 'user', :action => 'view'
451 render_unknown_user params[:display_name]
456 # sets a user's status
458 @this_user.status = params[:status]
460 redirect_to :controller => 'user', :action => 'view', :display_name => params[:display_name]
464 # delete a user, marking them as deleted and removing personal data
467 redirect_to :controller => 'user', :action => 'view', :display_name => params[:display_name]
471 # display a list of users matching specified criteria
474 ids = params[:user].keys.collect { |id| id.to_i }
476 User.update_all("status = 'confirmed'", :id => ids) if params[:confirm]
477 User.update_all("status = 'deleted'", :id => ids) if params[:hide]
479 redirect_to url_for(:status => params[:status], :ip => params[:ip], :page => params[:page])
481 conditions = Hash.new
482 conditions[:status] = params[:status] if params[:status]
483 conditions[:creation_ip] = params[:ip] if params[:ip]
485 @user_pages, @users = paginate(:users,
486 :conditions => conditions,
495 # handle password authentication
496 def password_authentication(username, password)
497 if user = User.authenticate(:username => username, :password => password)
498 successful_login(user)
499 elsif user = User.authenticate(:username => username, :password => password, :pending => true)
500 unconfirmed_login(user)
501 elsif User.authenticate(:username => username, :password => password, :suspended => true)
502 failed_login t('user.login.account is suspended', :webmaster => "mailto:webmaster@openstreetmap.org")
504 failed_login t('user.login.auth failure')
509 # handle OpenID authentication
510 def openid_authentication(openid_url)
511 # If we don't appear to have a user for this URL then ask the
512 # provider for some extra information to help with signup
513 if openid_url and User.find_by_openid_url(openid_url)
516 required = [:nickname, :email, "http://axschema.org/namePerson/friendly", "http://axschema.org/contact/email"]
519 # Start the authentication
520 authenticate_with_open_id(openid_expand_url(openid_url), :method => :get, :required => required) do |result, identity_url, sreg, ax|
521 if result.successful?
522 # We need to use the openid url passed back from the OpenID provider
523 # rather than the one supplied by the user, as these can be different.
525 # For example, you can simply enter yahoo.com in the login box rather
526 # than a user specific url. Only once it comes back from the provider
527 # provider do we know the unique address for the user.
528 if user = User.find_by_openid_url(identity_url)
531 unconfirmed_login(user)
532 when "active", "confirmed" then
533 successful_login(user)
534 when "suspended" then
535 failed_login t('user.login.account is suspended', :webmaster => "mailto:webmaster@openstreetmap.org")
537 failed_login t('user.login.auth failure')
540 # Guard against not getting any extension data
541 sreg = Hash.new if sreg.nil?
542 ax = Hash.new if ax.nil?
544 # We don't have a user registered to this OpenID, so redirect
545 # to the create account page with username and email filled
546 # in if they have been given by the OpenID provider through
547 # the simple registration protocol.
548 nickname = sreg["nickname"] || ax["http://axschema.org/namePerson/friendly"].first
549 email = sreg["email"] || ax["http://axschema.org/contact/email"].first
551 redirect_to :controller => 'user', :action => 'new', :nickname => nickname, :email => email, :openid => identity_url
553 elsif result.missing?
554 failed_login t('user.login.openid missing provider')
555 elsif result.invalid?
556 failed_login t('user.login.openid invalid')
558 failed_login t('user.login.auth failure')
564 # verify an OpenID URL
565 def openid_verify(openid_url, user)
566 user.openid_url = openid_url
568 authenticate_with_open_id(openid_expand_url(openid_url), :method => :get, :required => [:email, "http://axschema.org/contact/email"]) do |result, identity_url, sreg, ax|
569 if result.successful?
570 # Do we trust the emails this provider returns?
571 if openid_email_verified(identity_url)
572 # Guard against not getting any extension data
573 sreg = Hash.new if sreg.nil?
574 ax = Hash.new if ax.nil?
576 # Get the verified email
577 verified_email = sreg["email"] || ax["http://axschema.org/contact/email"].first
580 # We need to use the openid url passed back from the OpenID provider
581 # rather than the one supplied by the user, as these can be different.
583 # For example, you can simply enter yahoo.com in the login box rather
584 # than a user specific url. Only once it comes back from the provider
585 # provider do we know the unique address for the user.
586 user.openid_url = identity_url
587 yield user, verified_email
588 elsif result.missing?
589 flash.now[:error] = t 'user.login.openid missing provider'
590 elsif result.invalid?
591 flash.now[:error] = t 'user.login.openid invalid'
593 flash.now[:error] = t 'user.login.auth failure'
599 # special case some common OpenID providers by applying heuristics to
600 # try and come up with the correct URL based on what the user entered
601 def openid_expand_url(openid_url)
604 elsif openid_url.match(/(.*)gmail.com(\/?)$/) or openid_url.match(/(.*)googlemail.com(\/?)$/)
605 # Special case gmail.com as it is potentially a popular OpenID
606 # provider and, unlike yahoo.com, where it works automatically, Google
607 # have hidden their OpenID endpoint somewhere obscure this making it
608 # somewhat less user friendly.
609 return 'https://www.google.com/accounts/o8/id'
616 # check if we trust an OpenID provider to return a verified
617 # email, so that we can skpi verifying it ourselves
618 def openid_email_verified(openid_url)
619 openid_url.match(/https:\/\/www.google.com\/accounts\/o8\/id?(.*)/) or
620 openid_url.match(/https:\/\/me.yahoo.com\/(.*)/)
624 # process a successful login
625 def successful_login(user)
626 cookies.permanent["_osm_username"] = user.display_name
628 session[:user] = user.id
629 session_expires_after 28.days if session[:remember_me]
631 target = 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 and not user.terms_seen
640 redirect_to :controller => :user, :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)
654 flash[:error] = message
656 redirect_to :action => 'login', :referer => session[:referer]
658 session.delete(:remember_me)
659 session.delete(:referer)
664 def unconfirmed_login(user)
665 redirect_to :action => 'confirm', :display_name => user.display_name
667 session.delete(:remember_me)
668 session.delete(:referer)
672 # update a user's details
673 def update_user(user, params)
674 user.display_name = params[:user][:display_name]
675 user.new_email = params[:user][:new_email]
677 if params[:user][:pass_crypt].length > 0 or params[:user][:pass_crypt_confirmation].length > 0
678 user.pass_crypt = params[:user][:pass_crypt]
679 user.pass_crypt_confirmation = params[:user][:pass_crypt_confirmation]
682 if params[:user][:description] != user.description
683 user.description = params[:user][:description]
684 user.description_format = "markdown"
687 user.languages = params[:user][:languages].split(",")
689 case params[:image_action]
691 user.image = params[:user][:image]
692 user.image_use_gravatar = false
695 user.image_use_gravatar = false
698 user.image_use_gravatar = true
701 user.home_lat = params[:user][:home_lat]
702 user.home_lon = params[:user][:home_lon]
704 if params[:user][:preferred_editor] == "default"
705 user.preferred_editor = nil
707 user.preferred_editor = params[:user][:preferred_editor]
710 user.openid_url = nil if params[:user][:openid_url].blank?
715 cookies.permanent["_osm_username"] = user.display_name
717 if user.new_email.blank?
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
728 # Ignore errors sending email
731 @user.errors.set(:new_email, @user.errors.get(:email))
732 @user.errors.set(: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 @user and not @user.administrator?
745 flash[:error] = t('user.filter.not_an_administrator')
747 if params[:display_name]
748 redirect_to :controller => 'user', :action => 'view', :display_name => params[:display_name]
750 redirect_to :controller => 'user', :action => 'login', :referer => request.fullpath
753 redirect_to :controller => 'user', :action => 'login', :referer => request.fullpath
758 # ensure that there is a "this_user" instance variable
759 def lookup_user_by_id
760 @this_user = User.find(params[:id])
764 # ensure that there is a "this_user" instance variable
765 def lookup_user_by_name
766 @this_user = User.find_by_display_name(params[:display_name])
767 rescue ActiveRecord::RecordNotFound
768 redirect_to :controller => 'user', :action => 'view', :display_name => params[:display_name] unless @this_user
772 # Choose the layout to use. See
773 # https://rails.lighthouseapp.com/projects/8994/tickets/5371-layout-with-onlyexcept-options-makes-other-actions-render-without-layouts
775 oauth_url = url_for(:controller => :oauth, :action => :authorize, :only_path => true)
777 if [ 'api_details' ].include? action_name
779 elsif params[:referer] and URI.parse(params[:referer]).path == oauth_url
788 def disable_terms_redirect
789 # this is necessary otherwise going to the user terms page, when
790 # having not agreed already would cause an infinite redirect loop.
791 # it's .now so that this doesn't propagate to other pages.
792 flash.now[:skip_terms] = true