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 # The redirect from the OpenID provider reenters here
29 # again and we need to pass the parameters through to
30 # the open_id_authentication function
31 @user = session.delete(:new_user)
33 openid_verify(nil, @user) do |user,verified_email|
34 user.status = "active" if user.email == verified_email
37 if @user.openid_url.nil? or @user.invalid?
38 render :action => 'new'
40 session[:new_user] = @user
41 render :action => 'terms'
43 elsif params[:user] and Acl.no_account_creation(request.remote_ip, params[:user][:email].split("@").last)
44 render :action => 'blocked'
46 session[:referer] = params[:referer]
48 @title = t 'user.terms.title'
49 @user = User.new(params[:user]) if params[:user]
51 if params[:user] and params[:user][:openid_url] and @user.pass_crypt.empty?
52 # We are creating an account with OpenID and no password
53 # was specified so create a random one
54 @user.pass_crypt = SecureRandom.base64(16)
55 @user.pass_crypt_confirmation = @user.pass_crypt
59 @user.status = "pending"
63 # Something is wrong with a new user, so rerender the form
64 render :action => :new
66 # Error in existing user, so go to account settings
67 flash[:errors] = @user.errors
68 redirect_to :action => :account, :display_name => @user.display_name
70 elsif @user.terms_agreed?
71 # Already agreed to terms, so just show settings
72 redirect_to :action => :account, :display_name => @user.display_name
73 elsif params[:user] and params[:user][:openid_url] and not params[:user][:openid_url].empty?
74 # Verify OpenID before moving on
75 session[:new_user] = @user
76 openid_verify(params[:user][:openid_url], @user)
77 elsif @user.new_record?
78 # Save the user record
79 session[:new_user] = @user
82 # Not logged in, so redirect to the login page
83 redirect_to :action => :login, :referer => request.fullpath
89 @title = t 'user.new.title'
93 @user.terms_seen = true
96 flash[:notice] = t 'user.new.terms declined', :url => t('user.new.terms declined url')
100 redirect_to params[:referer]
102 redirect_to :action => :account, :display_name => @user.display_name
105 redirect_to t('user.terms.declined')
108 if !@user.terms_agreed?
109 @user.consider_pd = params[:user][:consider_pd]
110 @user.terms_agreed = Time.now.getutc
111 @user.terms_seen = true
113 flash[:notice] = t 'user.new.terms accepted'
118 redirect_to params[:referer]
120 redirect_to :action => :account, :display_name => @user.display_name
123 @user = session.delete(:new_user)
125 if Acl.no_account_creation(request.remote_ip, @user.email.split("@").last)
126 render :action => 'blocked'
128 @user.data_public = true
129 @user.description = "" if @user.description.nil?
130 @user.creation_ip = request.remote_ip
131 @user.languages = request.user_preferred_languages
132 @user.terms_agreed = Time.now.getutc
133 @user.terms_seen = true
134 @user.openid_url = nil if @user.openid_url and @user.openid_url.empty?
137 flash[:piwik_goal] = PIWIK_SIGNUP_GOAL if defined?(PIWIK_SIGNUP_GOAL)
139 if @user.status == "active"
140 flash[:notice] = t 'user.new.flash welcome', :email => @user.email
142 Notifier.signup_confirm(@user, nil).deliver
144 successful_login(@user)
146 flash[:notice] = t 'user.new.flash create success message', :email => @user.email
147 session[:token] = @user.tokens.create.token
149 Notifier.signup_confirm(@user, @user.tokens.create(:referer => session.delete(:referer))).deliver
151 redirect_to :action => 'login', :referer => params[:referer]
154 render :action => 'new', :referer => params[:referer]
161 @title = t 'user.account.title'
162 @tokens = @user.oauth_tokens.authorized
164 if params[:user] and params[:user][:display_name] and params[:user][:description]
165 if params[:user][:openid_url] and
166 params[:user][:openid_url].length > 0 and
167 params[:user][:openid_url] != @user.openid_url
168 # If the OpenID has changed, we want to check that it is a
169 # valid OpenID and one the user has control over before saving
170 # it as a password equivalent for the user.
171 session[:new_user_settings] = params
172 openid_verify(params[:user][:openid_url], @user)
174 update_user(@user, params)
177 # The redirect from the OpenID provider reenters here
178 # again and we need to pass the parameters through to
179 # the open_id_authentication function
180 settings = session.delete(:new_user_settings)
181 openid_verify(nil, @user) do |user|
182 update_user(user, settings)
188 @user.data_public = true
190 flash[:notice] = t 'user.go_public.flash success'
191 redirect_to :controller => 'user', :action => 'account', :display_name => @user.display_name
195 @title = t 'user.lost_password.title'
197 if params[:user] and params[:user][:email]
198 user = User.visible.find_by_email(params[:user][:email])
201 users = User.visible.where("LOWER(email) = LOWER(?)", params[:user][:email])
209 token = user.tokens.create
210 Notifier.lost_password(user, token).deliver
211 flash[:notice] = t 'user.lost_password.notice email on way'
212 redirect_to :action => 'login'
214 flash.now[:error] = t 'user.lost_password.notice email cannot find'
220 @title = t 'user.reset_password.title'
223 token = UserToken.find_by_token(params[:token])
229 @user.pass_crypt = params[:user][:pass_crypt]
230 @user.pass_crypt_confirmation = params[:user][:pass_crypt_confirmation]
231 @user.status = "active" if @user.status == "pending"
232 @user.email_valid = true
236 flash[:notice] = t 'user.reset_password.flash changed'
237 redirect_to :action => 'login'
241 flash[:error] = t 'user.reset_password.flash token bad'
242 redirect_to :action => 'lost_password'
248 @title = t 'user.new.title'
249 @referer = params[:referer] || session[:referer]
252 # The user is logged in already, so don't show them the signup
253 # page, instead send them to the home page
257 redirect_to :controller => 'site', :action => 'index'
259 elsif params.key?(:openid)
260 @user = User.new(:email => params[:email],
261 :email_confirmation => params[:email],
262 :display_name => params[:nickname],
263 :openid_url => params[:openid])
265 flash.now[:notice] = t 'user.new.openid association'
266 elsif Acl.no_account_creation(request.remote_ip)
267 render :action => 'blocked'
272 if params[:username] or using_open_id?
273 session[:remember_me] ||= params[:remember_me]
274 session[:referer] ||= params[:referer]
277 openid_authentication(params[:openid_url])
279 password_authentication(params[:username], params[:password])
285 @title = t 'user.logout.title'
287 if params[:session] == request.session_options[:id]
289 token = UserToken.find_by_token(session[:token])
293 session.delete(:token)
295 session.delete(:user)
296 session_expires_automatically
298 redirect_to params[:referer]
300 redirect_to :controller => 'site', :action => 'index'
307 if 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
336 flash[:notice] = t('user.confirm.success') + "<br /><br />" + t('user.confirm.before you start')
337 redirect_to :action => :account, :display_name => user.display_name
339 flash[:notice] = t('user.confirm.success')
345 user = User.find_by_display_name(params[:display_name])
347 if user and user.active?
348 flash[:error] = t('user.confirm.already active')
350 flash[:error] = t('user.confirm.unknown token') + t('user.confirm.reconfirm', :reconfirm => url_for(:action => 'confirm_resend', :display_name => params[:display_name]))
352 flash[:error] = t('user.confirm.unknown token')
355 redirect_to :action => 'login'
361 if user = User.find_by_display_name(params[:display_name])
362 Notifier.signup_confirm(user, user.tokens.create).deliver
363 flash[:notice] = t 'user.confirm_resend.success', :email => user.email
365 flash[:notice] = t 'user.confirm_resend.failure', :name => params[:display_name]
368 redirect_to :action => 'login'
373 token = UserToken.find_by_token(params[:confirm_string])
374 if token and token.user.new_email?
376 @user.email = @user.new_email
377 @user.new_email = nil
378 @user.email_valid = true
380 flash[:notice] = t 'user.confirm_email.success'
382 flash[:errors] = @user.errors
385 session[:user] = @user.id
386 cookies.permanent["_osm_username"] = @user.display_name
387 redirect_to :action => 'account', :display_name => @user.display_name
389 flash[:error] = t 'user.confirm_email.failure'
390 redirect_to :action => 'account', :display_name => @user.display_name
396 render :nothing => true, :status => :gone unless @this_user.visible?
401 render :action => :api_read
405 doc = OSM::API.new.get_xml_doc
406 @user.traces.each do |trace|
407 doc.root << trace.to_xml_node() if trace.public? or trace.user == @user
409 render :text => doc.to_s, :content_type => "text/xml"
413 @this_user = User.find_by_display_name(params[:display_name])
416 (@this_user.visible? or (@user and @user.administrator?))
417 @title = @this_user.display_name
419 render_unknown_user params[:display_name]
424 @new_friend = User.find_by_display_name(params[:display_name])
429 friend.user_id = @user.id
430 friend.friend_user_id = @new_friend.id
431 unless @user.is_friends_with?(@new_friend)
433 flash[:notice] = t 'user.make_friend.success', :name => @new_friend.display_name
434 Notifier.friend_notification(friend).deliver
436 friend.add_error(t('user.make_friend.failed', :name => @new_friend.display_name))
439 flash[:warning] = t 'user.make_friend.already_a_friend', :name => @new_friend.display_name
443 redirect_to params[:referer]
445 redirect_to :controller => 'user', :action => 'view'
449 render_unknown_user params[:display_name]
454 @friend = User.find_by_display_name(params[:display_name])
458 if @user.is_friends_with?(@friend)
459 Friend.delete_all "user_id = #{@user.id} AND friend_user_id = #{@friend.id}"
460 flash[:notice] = t 'user.remove_friend.success', :name => @friend.display_name
462 flash[:error] = t 'user.remove_friend.not_a_friend', :name => @friend.display_name
466 redirect_to params[:referer]
468 redirect_to :controller => 'user', :action => 'view'
472 render_unknown_user params[:display_name]
477 # sets a user's status
479 @this_user.status = params[:status]
481 redirect_to :controller => 'user', :action => 'view', :display_name => params[:display_name]
485 # delete a user, marking them as deleted and removing personal data
488 redirect_to :controller => 'user', :action => 'view', :display_name => params[:display_name]
492 # display a list of users matching specified criteria
495 ids = params[:user].keys.collect { |id| id.to_i }
497 User.update_all("status = 'confirmed'", :id => ids) if params[:confirm]
498 User.update_all("status = 'deleted'", :id => ids) if params[:hide]
500 redirect_to url_for(:status => params[:status], :ip => params[:ip], :page => params[:page])
502 conditions = Hash.new
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,
516 # handle password authentication
517 def password_authentication(username, password)
518 if user = User.authenticate(:username => username, :password => password)
519 successful_login(user)
520 elsif user = User.authenticate(:username => username, :password => password, :pending => true)
521 failed_login t('user.login.account not active', :reconfirm => url_for(:action => 'confirm_resend', :display_name => user.display_name))
522 elsif User.authenticate(:username => username, :password => password, :suspended => true)
523 failed_login t('user.login.account is suspended', :webmaster => "mailto:webmaster@openstreetmap.org")
525 failed_login t('user.login.auth failure')
530 # handle OpenID authentication
531 def openid_authentication(openid_url)
532 # If we don't appear to have a user for this URL then ask the
533 # provider for some extra information to help with signup
534 if openid_url and User.find_by_openid_url(openid_url)
537 required = [:nickname, :email, "http://axschema.org/namePerson/friendly", "http://axschema.org/contact/email"]
540 # Start the authentication
541 authenticate_with_open_id(openid_expand_url(openid_url), :method => :get, :required => required) do |result, identity_url, sreg, ax|
542 if result.successful?
543 # We need to use the openid url passed back from the OpenID provider
544 # rather than the one supplied by the user, as these can be different.
546 # For example, you can simply enter yahoo.com in the login box rather
547 # than a user specific url. Only once it comes back from the provider
548 # provider do we know the unique address for the user.
549 if user = User.find_by_openid_url(identity_url)
552 failed_login t('user.login.account not active', :reconfirm => url_for(:action => 'confirm_resend', :display_name => user.display_name))
553 when "active", "confirmed" then
554 successful_login(user)
555 when "suspended" then
556 failed_login t('user.login.account is suspended', :webmaster => "mailto:webmaster@openstreetmap.org")
558 failed_login t('user.login.auth failure')
561 # Guard against not getting any extension data
562 sreg = Hash.new if sreg.nil?
563 ax = Hash.new if ax.nil?
565 # We don't have a user registered to this OpenID, so redirect
566 # to the create account page with username and email filled
567 # in if they have been given by the OpenID provider through
568 # the simple registration protocol.
569 nickname = sreg["nickname"] || ax["http://axschema.org/namePerson/friendly"].first
570 email = sreg["email"] || ax["http://axschema.org/contact/email"].first
572 redirect_to :controller => 'user', :action => 'new', :nickname => nickname, :email => email, :openid => identity_url
574 elsif result.missing?
575 failed_login t('user.login.openid missing provider')
576 elsif result.invalid?
577 failed_login t('user.login.openid invalid')
579 failed_login t('user.login.auth failure')
585 # verify an OpenID URL
586 def openid_verify(openid_url, user)
587 user.openid_url = openid_url
589 authenticate_with_open_id(openid_expand_url(openid_url), :method => :get, :required => [:email, "http://axschema.org/contact/email"]) do |result, identity_url, sreg, ax|
590 if result.successful?
591 # Do we trust the emails this provider returns?
592 if openid_email_verified(identity_url)
593 # Guard against not getting any extension data
594 sreg = Hash.new if sreg.nil?
595 ax = Hash.new if ax.nil?
597 # Get the verified email
598 verified_email = sreg["email"] || ax["http://axschema.org/contact/email"].first
601 # We need to use the openid url passed back from the OpenID provider
602 # rather than the one supplied by the user, as these can be different.
604 # For example, you can simply enter yahoo.com in the login box rather
605 # than a user specific url. Only once it comes back from the provider
606 # provider do we know the unique address for the user.
607 user.openid_url = identity_url
608 yield user, verified_email
609 elsif result.missing?
610 flash.now[:error] = t 'user.login.openid missing provider'
611 elsif result.invalid?
612 flash.now[:error] = t 'user.login.openid invalid'
614 flash.now[:error] = t 'user.login.auth failure'
620 # special case some common OpenID providers by applying heuristics to
621 # try and come up with the correct URL based on what the user entered
622 def openid_expand_url(openid_url)
625 elsif openid_url.match(/(.*)gmail.com(\/?)$/) or openid_url.match(/(.*)googlemail.com(\/?)$/)
626 # Special case gmail.com as it is potentially a popular OpenID
627 # provider and, unlike yahoo.com, where it works automatically, Google
628 # have hidden their OpenID endpoint somewhere obscure this making it
629 # somewhat less user friendly.
630 return 'https://www.google.com/accounts/o8/id'
637 # check if we trust an OpenID provider to return a verified
638 # email, so that we can skpi verifying it ourselves
639 def openid_email_verified(openid_url)
640 openid_url.match(/https:\/\/www.google.com\/accounts\/o8\/id?(.*)/) or
641 openid_url.match(/https:\/\/me.yahoo.com\/(.*)/)
645 # process a successful login
646 def successful_login(user)
647 cookies.permanent["_osm_username"] = user.display_name
649 session[:user] = user.id
650 session_expires_after 28.days if session[:remember_me]
652 target = session[:referer] || url_for(:controller => :site, :action => :index)
654 # The user is logged in, so decide where to send them:
656 # - If they haven't seen the contributor terms, send them there.
657 # - If they have a block on them, show them that.
658 # - If they were referred to the login, send them back there.
659 # - Otherwise, send them to the home page.
660 if REQUIRE_TERMS_SEEN and not user.terms_seen
661 redirect_to :controller => :user, :action => :terms, :referer => target
662 elsif user.blocked_on_view
663 redirect_to user.blocked_on_view, :referer => target
668 session.delete(:remember_me)
669 session.delete(:referer)
673 # process a failed login
674 def failed_login(message)
675 flash[:error] = message
677 redirect_to :action => 'login', :referer => session[:referer]
679 session.delete(:remember_me)
680 session.delete(:referer)
684 # update a user's details
685 def update_user(user, params)
686 user.display_name = params[:user][:display_name]
687 user.new_email = params[:user][:new_email]
689 if params[:user][:pass_crypt].length > 0 or params[:user][:pass_crypt_confirmation].length > 0
690 user.pass_crypt = params[:user][:pass_crypt]
691 user.pass_crypt_confirmation = params[:user][:pass_crypt_confirmation]
694 if params[:user][:description] != user.description
695 user.description = params[:user][:description]
696 user.description_format = "markdown"
699 user.languages = params[:user][:languages].split(",")
701 case params[:image_action]
703 user.image = params[:user][:image]
704 user.image_use_gravatar = false
707 user.image_use_gravatar = false
710 user.image_use_gravatar = true
713 user.home_lat = params[:user][:home_lat]
714 user.home_lon = params[:user][:home_lon]
716 if params[:user][:preferred_editor] == "default"
717 user.preferred_editor = nil
719 user.preferred_editor = params[:user][:preferred_editor]
722 user.openid_url = nil if params[:user][:openid_url].blank?
727 cookies.permanent["_osm_username"] = user.display_name
729 if user.new_email.blank?
730 flash.now[:notice] = t 'user.account.flash update success'
732 user.email = user.new_email
735 flash.now[:notice] = t 'user.account.flash update success confirm needed'
738 Notifier.email_confirm(user, user.tokens.create).deliver
740 # Ignore errors sending email
743 @user.errors.set(:new_email, @user.errors.get(:email))
744 @user.errors.set(:email, [])
753 # require that the user is a administrator, or fill out a helpful error message
754 # and return them to the user page.
755 def require_administrator
756 if @user and not @user.administrator?
757 flash[:error] = t('user.filter.not_an_administrator')
759 if params[:display_name]
760 redirect_to :controller => 'user', :action => 'view', :display_name => params[:display_name]
762 redirect_to :controller => 'user', :action => 'login', :referer => request.fullpath
765 redirect_to :controller => 'user', :action => 'login', :referer => request.fullpath
770 # ensure that there is a "this_user" instance variable
771 def lookup_user_by_id
772 @this_user = User.find(params[:id])
776 # ensure that there is a "this_user" instance variable
777 def lookup_user_by_name
778 @this_user = User.find_by_display_name(params[:display_name])
779 rescue ActiveRecord::RecordNotFound
780 redirect_to :controller => 'user', :action => 'view', :display_name => params[:display_name] unless @this_user
784 # Choose the layout to use. See
785 # https://rails.lighthouseapp.com/projects/8994/tickets/5371-layout-with-onlyexcept-options-makes-other-actions-render-without-layouts
787 oauth_url = url_for(:controller => :oauth, :action => :authorize, :only_path => true)
789 if [ 'api_details' ].include? action_name
791 elsif params[:referer] and URI.parse(params[:referer]).path == oauth_url
800 def disable_terms_redirect
801 # this is necessary otherwise going to the user terms page, when
802 # having not agreed already would cause an infinite redirect loop.
803 # it's .now so that this doesn't propagate to other pages.
804 flash.now[:skip_terms] = true