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 flash[:notice] = t 'user.new.flash welcome', :email => @user.email
93 session[:referer] = welcome_path
95 successful_login(@user)
97 flash[:notice] = t 'user.new.flash create success message', :email => @user.email
98 session[:token] = @user.tokens.create.token
100 Notifier.signup_confirm(@user, @user.tokens.create(:referer => welcome_path)).deliver
102 redirect_to :action => 'login', :referer => params[:referer]
105 render :action => 'new', :referer => params[:referer]
112 @title = t 'user.account.title'
113 @tokens = @user.oauth_tokens.authorized
115 if params[:user] and params[:user][:display_name] and params[:user][:description]
116 if params[:user][:openid_url] and
117 params[:user][:openid_url].length > 0 and
118 params[:user][:openid_url] != @user.openid_url
119 # If the OpenID has changed, we want to check that it is a
120 # valid OpenID and one the user has control over before saving
121 # it as a password equivalent for the user.
122 session[:new_user_settings] = params
123 openid_verify(params[:user][:openid_url], @user)
125 update_user(@user, params)
128 # The redirect from the OpenID provider reenters here
129 # again and we need to pass the parameters through to
130 # the open_id_authentication function
131 settings = session.delete(:new_user_settings)
132 openid_verify(nil, @user) do |user|
133 update_user(user, settings)
139 @user.data_public = true
141 flash[:notice] = t 'user.go_public.flash success'
142 redirect_to :controller => 'user', :action => 'account', :display_name => @user.display_name
146 @title = t 'user.lost_password.title'
148 if params[:user] and params[:user][:email]
149 user = User.visible.find_by_email(params[:user][:email])
152 users = User.visible.where("LOWER(email) = LOWER(?)", params[:user][:email])
160 token = user.tokens.create
161 Notifier.lost_password(user, token).deliver
162 flash[:notice] = t 'user.lost_password.notice email on way'
163 redirect_to :action => 'login'
165 flash.now[:error] = t 'user.lost_password.notice email cannot find'
171 @title = t 'user.reset_password.title'
174 token = UserToken.find_by_token(params[:token])
180 @user.pass_crypt = params[:user][:pass_crypt]
181 @user.pass_crypt_confirmation = params[:user][:pass_crypt_confirmation]
182 @user.status = "active" if @user.status == "pending"
183 @user.email_valid = true
187 flash[:notice] = t 'user.reset_password.flash changed'
188 redirect_to :action => 'login'
192 flash[:error] = t 'user.reset_password.flash token bad'
193 redirect_to :action => 'lost_password'
199 @title = t 'user.new.title'
200 @referer = params[:referer] || session[:referer]
203 # The redirect from the OpenID provider reenters here
204 # again and we need to pass the parameters through to
205 # the open_id_authentication function
206 @user = session.delete(:new_user)
208 openid_verify(nil, @user) do |user, verified_email|
209 user.status = "active" if user.email == verified_email
212 if @user.openid_url.nil? or @user.invalid?
213 render :action => 'new'
215 session[:new_user] = @user
216 redirect_to :action => 'terms'
219 # The user is logged in already, so don't show them the signup
220 # page, instead send them to the home page
224 redirect_to :controller => 'site', :action => 'index'
226 elsif params.key?(:openid)
227 @user = User.new(:email => params[:email],
228 :email_confirmation => params[:email],
229 :display_name => params[:nickname],
230 :openid_url => params[:openid])
232 flash.now[:notice] = t 'user.new.openid association'
233 elsif Acl.no_account_creation(request.remote_ip)
234 render :action => 'blocked'
239 if params[:user] and Acl.no_account_creation(request.remote_ip, params[:user][:email].split("@").last)
240 render :action => 'blocked'
243 session[:referer] = params[:referer]
245 @user = User.new(params[:user])
246 @user.status = "pending"
248 if @user.openid_url.present? && @user.pass_crypt.empty?
249 # We are creating an account with OpenID and no password
250 # was specified so create a random one
251 @user.pass_crypt = SecureRandom.base64(16)
252 @user.pass_crypt_confirmation = @user.pass_crypt
256 # Something is wrong with a new user, so rerender the form
257 render :action => "new"
258 elsif @user.openid_url.present?
259 # Verify OpenID before moving on
260 session[:new_user] = @user
261 openid_verify(@user.openid_url, @user)
263 # Save the user record
264 session[:new_user] = @user
265 redirect_to :action => :terms
271 if params[:username] or using_open_id?
272 session[:remember_me] ||= params[:remember_me]
273 session[:referer] ||= params[:referer]
276 openid_authentication(params[:openid_url])
278 password_authentication(params[:username], params[:password])
284 @title = t 'user.logout.title'
286 if params[:session] == request.session_options[:id]
288 token = UserToken.find_by_token(session[:token])
292 session.delete(:token)
294 session.delete(:user)
295 session_expires_automatically
297 redirect_to params[:referer]
299 redirect_to :controller => 'site', :action => 'index'
306 if token = UserToken.find_by_token(params[:confirm_string])
307 if token.user.active?
308 flash[:error] = t('user.confirm.already active')
309 redirect_to :action => 'login'
312 user.status = "active"
313 user.email_valid = true
315 referer = token.referer
319 token = UserToken.find_by_token(session[:token])
320 session.delete(:token)
325 if token.nil? or token.user != user
326 flash[:notice] = t('user.confirm.success')
327 redirect_to :action => :login, :referer => referer
331 session[:user] = user.id
332 cookies.permanent["_osm_username"] = user.display_name
335 flash[:notice] = t('user.confirm.success') + "<br /><br />" + t('user.confirm.before you start')
336 redirect_to :action => :account, :display_name => user.display_name
338 flash[:notice] = t('user.confirm.success')
344 user = User.find_by_display_name(params[:display_name])
346 if user and user.active?
347 flash[:error] = t('user.confirm.already active')
349 flash[:error] = t('user.confirm.unknown token') + t('user.confirm.reconfirm', :reconfirm => url_for(:action => 'confirm_resend', :display_name => params[:display_name]))
351 flash[:error] = t('user.confirm.unknown token')
354 redirect_to :action => 'login'
360 if user = User.find_by_display_name(params[:display_name])
361 Notifier.signup_confirm(user, user.tokens.create).deliver
362 flash[:notice] = t 'user.confirm_resend.success', :email => user.email
364 flash[:notice] = t 'user.confirm_resend.failure', :name => params[:display_name]
367 redirect_to :action => 'login'
372 token = UserToken.find_by_token(params[:confirm_string])
373 if token and token.user.new_email?
375 @user.email = @user.new_email
376 @user.new_email = nil
377 @user.email_valid = true
379 flash[:notice] = t 'user.confirm_email.success'
381 flash[:errors] = @user.errors
384 session[:user] = @user.id
385 cookies.permanent["_osm_username"] = @user.display_name
386 redirect_to :action => 'account', :display_name => @user.display_name
388 flash[:error] = t 'user.confirm_email.failure'
389 redirect_to :action => 'account', :display_name => @user.display_name
395 render :text => "", :status => :gone unless @this_user.visible?
400 render :action => :api_read
404 doc = OSM::API.new.get_xml_doc
405 @user.traces.each do |trace|
406 doc.root << trace.to_xml_node() if trace.public? or trace.user == @user
408 render :text => doc.to_s, :content_type => "text/xml"
412 @this_user = User.find_by_display_name(params[:display_name])
415 (@this_user.visible? or (@user and @user.administrator?))
416 @title = @this_user.display_name
418 render_unknown_user params[:display_name]
423 @new_friend = User.find_by_display_name(params[:display_name])
428 friend.user_id = @user.id
429 friend.friend_user_id = @new_friend.id
430 unless @user.is_friends_with?(@new_friend)
432 flash[:notice] = t 'user.make_friend.success', :name => @new_friend.display_name
433 Notifier.friend_notification(friend).deliver
435 friend.add_error(t('user.make_friend.failed', :name => @new_friend.display_name))
438 flash[:warning] = t 'user.make_friend.already_a_friend', :name => @new_friend.display_name
442 redirect_to params[:referer]
444 redirect_to :controller => 'user', :action => 'view'
448 render_unknown_user params[:display_name]
453 @friend = User.find_by_display_name(params[:display_name])
457 if @user.is_friends_with?(@friend)
458 Friend.delete_all "user_id = #{@user.id} AND friend_user_id = #{@friend.id}"
459 flash[:notice] = t 'user.remove_friend.success', :name => @friend.display_name
461 flash[:error] = t 'user.remove_friend.not_a_friend', :name => @friend.display_name
465 redirect_to params[:referer]
467 redirect_to :controller => 'user', :action => 'view'
471 render_unknown_user params[:display_name]
476 # sets a user's status
478 @this_user.status = params[:status]
480 redirect_to :controller => 'user', :action => 'view', :display_name => params[:display_name]
484 # delete a user, marking them as deleted and removing personal data
487 redirect_to :controller => 'user', :action => 'view', :display_name => params[:display_name]
491 # display a list of users matching specified criteria
494 ids = params[:user].keys.collect { |id| id.to_i }
496 User.update_all("status = 'confirmed'", :id => ids) if params[:confirm]
497 User.update_all("status = 'deleted'", :id => ids) if params[:hide]
499 redirect_to url_for(:status => params[:status], :ip => params[:ip], :page => params[:page])
501 conditions = Hash.new
502 conditions[:status] = params[:status] if params[:status]
503 conditions[:creation_ip] = params[:ip] if params[:ip]
505 @user_pages, @users = paginate(:users,
506 :conditions => conditions,
515 # handle password authentication
516 def password_authentication(username, password)
517 if user = User.authenticate(:username => username, :password => password)
518 successful_login(user)
519 elsif user = User.authenticate(:username => username, :password => password, :pending => true)
520 failed_login t('user.login.account not active', :reconfirm => url_for(:action => 'confirm_resend', :display_name => user.display_name))
521 elsif User.authenticate(:username => username, :password => password, :suspended => true)
522 failed_login t('user.login.account is suspended', :webmaster => "mailto:webmaster@openstreetmap.org")
524 failed_login t('user.login.auth failure')
529 # handle OpenID authentication
530 def openid_authentication(openid_url)
531 # If we don't appear to have a user for this URL then ask the
532 # provider for some extra information to help with signup
533 if openid_url and User.find_by_openid_url(openid_url)
536 required = [:nickname, :email, "http://axschema.org/namePerson/friendly", "http://axschema.org/contact/email"]
539 # Start the authentication
540 authenticate_with_open_id(openid_expand_url(openid_url), :method => :get, :required => required) do |result, identity_url, sreg, ax|
541 if result.successful?
542 # We need to use the openid url passed back from the OpenID provider
543 # rather than the one supplied by the user, as these can be different.
545 # For example, you can simply enter yahoo.com in the login box rather
546 # than a user specific url. Only once it comes back from the provider
547 # provider do we know the unique address for the user.
548 if user = User.find_by_openid_url(identity_url)
551 failed_login t('user.login.account not active', :reconfirm => url_for(:action => 'confirm_resend', :display_name => user.display_name))
552 when "active", "confirmed" then
553 successful_login(user)
554 when "suspended" then
555 failed_login t('user.login.account is suspended', :webmaster => "mailto:webmaster@openstreetmap.org")
557 failed_login t('user.login.auth failure')
560 # Guard against not getting any extension data
561 sreg = Hash.new if sreg.nil?
562 ax = Hash.new if ax.nil?
564 # We don't have a user registered to this OpenID, so redirect
565 # to the create account page with username and email filled
566 # in if they have been given by the OpenID provider through
567 # the simple registration protocol.
568 nickname = sreg["nickname"] || ax["http://axschema.org/namePerson/friendly"].first
569 email = sreg["email"] || ax["http://axschema.org/contact/email"].first
571 redirect_to :controller => 'user', :action => 'new', :nickname => nickname, :email => email, :openid => identity_url
573 elsif result.missing?
574 failed_login t('user.login.openid missing provider')
575 elsif result.invalid?
576 failed_login t('user.login.openid invalid')
578 failed_login t('user.login.auth failure')
584 # verify an OpenID URL
585 def openid_verify(openid_url, user)
586 user.openid_url = openid_url
588 authenticate_with_open_id(openid_expand_url(openid_url), :method => :get, :required => [:email, "http://axschema.org/contact/email"]) do |result, identity_url, sreg, ax|
589 if result.successful?
590 # Do we trust the emails this provider returns?
591 if openid_email_verified(identity_url)
592 # Guard against not getting any extension data
593 sreg = Hash.new if sreg.nil?
594 ax = Hash.new if ax.nil?
596 # Get the verified email
597 verified_email = sreg["email"] || ax["http://axschema.org/contact/email"].first
600 # We need to use the openid url passed back from the OpenID provider
601 # rather than the one supplied by the user, as these can be different.
603 # For example, you can simply enter yahoo.com in the login box rather
604 # than a user specific url. Only once it comes back from the provider
605 # provider do we know the unique address for the user.
606 user.openid_url = identity_url
607 yield user, verified_email
608 elsif result.missing?
609 flash.now[:error] = t 'user.login.openid missing provider'
610 elsif result.invalid?
611 flash.now[:error] = t 'user.login.openid invalid'
613 flash.now[:error] = t 'user.login.auth failure'
619 # special case some common OpenID providers by applying heuristics to
620 # try and come up with the correct URL based on what the user entered
621 def openid_expand_url(openid_url)
624 elsif openid_url.match(/(.*)gmail.com(\/?)$/) or openid_url.match(/(.*)googlemail.com(\/?)$/)
625 # Special case gmail.com as it is potentially a popular OpenID
626 # provider and, unlike yahoo.com, where it works automatically, Google
627 # have hidden their OpenID endpoint somewhere obscure this making it
628 # somewhat less user friendly.
629 return 'https://www.google.com/accounts/o8/id'
636 # check if we trust an OpenID provider to return a verified
637 # email, so that we can skpi verifying it ourselves
638 def openid_email_verified(openid_url)
639 openid_url.match(/https:\/\/www.google.com\/accounts\/o8\/id?(.*)/) or
640 openid_url.match(/https:\/\/me.yahoo.com\/(.*)/)
644 # process a successful login
645 def successful_login(user)
646 cookies.permanent["_osm_username"] = user.display_name
648 session[:user] = user.id
649 session_expires_after 28.days if session[:remember_me]
651 target = session[:referer] || url_for(:controller => :site, :action => :index)
653 # The user is logged in, so decide where to send them:
655 # - If they haven't seen the contributor terms, send them there.
656 # - If they have a block on them, show them that.
657 # - If they were referred to the login, send them back there.
658 # - Otherwise, send them to the home page.
659 if REQUIRE_TERMS_SEEN and not user.terms_seen
660 redirect_to :controller => :user, :action => :terms, :referer => target
661 elsif user.blocked_on_view
662 redirect_to user.blocked_on_view, :referer => target
667 session.delete(:remember_me)
668 session.delete(:referer)
672 # process a failed login
673 def failed_login(message)
674 flash[:error] = message
676 redirect_to :action => 'login', :referer => session[:referer]
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 if params[:user][:pass_crypt].length > 0 or params[:user][:pass_crypt_confirmation].length > 0
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 if params[:user][:preferred_editor] == "default"
716 user.preferred_editor = nil
718 user.preferred_editor = params[:user][:preferred_editor]
721 user.openid_url = nil if params[:user][:openid_url].blank?
726 cookies.permanent["_osm_username"] = user.display_name
728 if user.new_email.blank?
729 flash.now[:notice] = t 'user.account.flash update success'
731 user.email = user.new_email
734 flash.now[:notice] = t 'user.account.flash update success confirm needed'
737 Notifier.email_confirm(user, user.tokens.create).deliver
739 # Ignore errors sending email
742 @user.errors.set(:new_email, @user.errors.get(:email))
743 @user.errors.set(:email, [])
752 # require that the user is a administrator, or fill out a helpful error message
753 # and return them to the user page.
754 def require_administrator
755 if @user and not @user.administrator?
756 flash[:error] = t('user.filter.not_an_administrator')
758 if params[:display_name]
759 redirect_to :controller => 'user', :action => 'view', :display_name => params[:display_name]
761 redirect_to :controller => 'user', :action => 'login', :referer => request.fullpath
764 redirect_to :controller => 'user', :action => 'login', :referer => request.fullpath
769 # ensure that there is a "this_user" instance variable
770 def lookup_user_by_id
771 @this_user = User.find(params[:id])
775 # ensure that there is a "this_user" instance variable
776 def lookup_user_by_name
777 @this_user = User.find_by_display_name(params[:display_name])
778 rescue ActiveRecord::RecordNotFound
779 redirect_to :controller => 'user', :action => 'view', :display_name => params[:display_name] unless @this_user
783 # Choose the layout to use. See
784 # https://rails.lighthouseapp.com/projects/8994/tickets/5371-layout-with-onlyexcept-options-makes-other-actions-render-without-layouts
786 oauth_url = url_for(:controller => :oauth, :action => :authorize, :only_path => true)
788 if [ 'api_details' ].include? action_name
790 elsif params[:referer] and URI.parse(params[:referer]).path == oauth_url
799 def disable_terms_redirect
800 # this is necessary otherwise going to the user terms page, when
801 # having not agreed already would cause an infinite redirect loop.
802 # it's .now so that this doesn't propagate to other pages.
803 flash.now[:skip_terms] = true