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 referer = welcome_path
94 uri = URI(session[:referer])
95 /map=(.*)\/(.*)\/(.*)/.match(uri.fragment) do |m|
96 editor = Rack::Utils.parse_query(uri.query).slice('editor')
97 referer = welcome_path({'zoom' => m[1],
99 'lon' => m[3]}.merge(editor))
105 if @user.status == "active"
106 session[:referer] = referer
107 successful_login(@user)
109 session[:token] = @user.tokens.create.token
110 Notifier.signup_confirm(@user, @user.tokens.create(:referer => referer)).deliver
111 redirect_to :action => 'confirm', :display_name => @user.display_name
114 render :action => 'new', :referer => params[:referer]
121 @title = t 'user.account.title'
122 @tokens = @user.oauth_tokens.authorized
124 if params[:user] and params[:user][:display_name] and params[:user][:description]
125 if params[:user][:openid_url] and
126 params[:user][:openid_url].length > 0 and
127 params[:user][:openid_url] != @user.openid_url
128 # If the OpenID has changed, we want to check that it is a
129 # valid OpenID and one the user has control over before saving
130 # it as a password equivalent for the user.
131 session[:new_user_settings] = params
132 openid_verify(params[:user][:openid_url], @user)
134 update_user(@user, params)
137 # The redirect from the OpenID provider reenters here
138 # again and we need to pass the parameters through to
139 # the open_id_authentication function
140 settings = session.delete(:new_user_settings)
141 openid_verify(nil, @user) do |user|
142 update_user(user, settings)
148 @user.data_public = true
150 flash[:notice] = t 'user.go_public.flash success'
151 redirect_to :controller => 'user', :action => 'account', :display_name => @user.display_name
155 @title = t 'user.lost_password.title'
157 if params[:user] and params[:user][:email]
158 user = User.visible.find_by_email(params[:user][:email])
161 users = User.visible.where("LOWER(email) = LOWER(?)", params[:user][:email])
169 token = user.tokens.create
170 Notifier.lost_password(user, token).deliver
171 flash[:notice] = t 'user.lost_password.notice email on way'
172 redirect_to :action => 'login'
174 flash.now[:error] = t 'user.lost_password.notice email cannot find'
180 @title = t 'user.reset_password.title'
183 token = UserToken.find_by_token(params[:token])
189 @user.pass_crypt = params[:user][:pass_crypt]
190 @user.pass_crypt_confirmation = params[:user][:pass_crypt_confirmation]
191 @user.status = "active" if @user.status == "pending"
192 @user.email_valid = true
196 flash[:notice] = t 'user.reset_password.flash changed'
197 redirect_to :action => 'login'
201 flash[:error] = t 'user.reset_password.flash token bad'
202 redirect_to :action => 'lost_password'
208 @title = t 'user.new.title'
209 @referer = params[:referer] || session[:referer]
212 # The redirect from the OpenID provider reenters here
213 # again and we need to pass the parameters through to
214 # the open_id_authentication function
215 @user = session.delete(:new_user)
217 openid_verify(nil, @user) do |user, verified_email|
218 user.status = "active" if user.email == verified_email
221 if @user.openid_url.nil? or @user.invalid?
222 render :action => 'new'
224 session[:new_user] = @user
225 redirect_to :action => 'terms'
228 # The user is logged in already, so don't show them the signup
229 # page, instead send them to the home page
233 redirect_to :controller => 'site', :action => 'index'
235 elsif params.key?(:openid)
236 @user = User.new(:email => params[:email],
237 :email_confirmation => params[:email],
238 :display_name => params[:nickname],
239 :openid_url => params[:openid])
241 flash.now[:notice] = t 'user.new.openid association'
242 elsif Acl.no_account_creation(request.remote_ip)
243 render :action => 'blocked'
248 if params[:user] and Acl.no_account_creation(request.remote_ip, params[:user][:email].split("@").last)
249 render :action => 'blocked'
252 session[:referer] = params[:referer]
254 @user = User.new(params[:user])
255 @user.status = "pending"
257 if @user.openid_url.present? && @user.pass_crypt.empty?
258 # We are creating an account with OpenID and no password
259 # was specified so create a random one
260 @user.pass_crypt = SecureRandom.base64(16)
261 @user.pass_crypt_confirmation = @user.pass_crypt
265 # Something is wrong with a new user, so rerender the form
266 render :action => "new"
267 elsif @user.openid_url.present?
268 # Verify OpenID before moving on
269 session[:new_user] = @user
270 openid_verify(@user.openid_url, @user)
272 # Save the user record
273 session[:new_user] = @user
274 redirect_to :action => :terms
280 if params[:username] or using_open_id?
281 session[:remember_me] ||= params[:remember_me]
282 session[:referer] ||= params[:referer]
285 openid_authentication(params[:openid_url])
287 password_authentication(params[:username], params[:password])
293 @title = t 'user.logout.title'
295 if params[:session] == request.session_options[:id]
297 token = UserToken.find_by_token(session[:token])
301 session.delete(:token)
303 session.delete(:user)
304 session_expires_automatically
306 redirect_to params[:referer]
308 redirect_to :controller => 'site', :action => 'index'
315 token = UserToken.find_by_token(params[:confirm_string])
316 if token && token.user.active?
317 flash[:error] = t('user.confirm.already active')
318 redirect_to :action => 'login'
319 elsif !token || token.expired?
320 flash[:error] = t('user.confirm.unknown token')
321 redirect_to :action => 'confirm'
324 user.status = "active"
325 user.email_valid = true
327 referer = token.referer
331 token = UserToken.find_by_token(session[:token])
332 session.delete(:token)
337 if token.nil? or token.user != user
338 flash[:notice] = t('user.confirm.success')
339 redirect_to :action => :login, :referer => referer
343 session[:user] = user.id
344 cookies.permanent["_osm_username"] = user.display_name
346 redirect_to referer || welcome_path
350 user = User.find_by_display_name(params[:display_name])
351 if !user || user.active?
352 redirect_to root_path
358 if user = User.find_by_display_name(params[:display_name])
359 Notifier.signup_confirm(user, user.tokens.create).deliver
360 flash[:notice] = t 'user.confirm_resend.success', :email => user.email
362 flash[:notice] = t 'user.confirm_resend.failure', :name => params[:display_name]
365 redirect_to :action => 'login'
370 token = UserToken.find_by_token(params[:confirm_string])
371 if token and token.user.new_email?
373 @user.email = @user.new_email
374 @user.new_email = nil
375 @user.email_valid = true
377 flash[:notice] = t 'user.confirm_email.success'
379 flash[:errors] = @user.errors
382 session[:user] = @user.id
383 cookies.permanent["_osm_username"] = @user.display_name
384 redirect_to :action => 'account', :display_name => @user.display_name
386 flash[:error] = t 'user.confirm_email.failure'
387 redirect_to :action => 'account', :display_name => @user.display_name
393 render :text => "", :status => :gone unless @this_user.visible?
398 render :action => :api_read
402 doc = OSM::API.new.get_xml_doc
403 @user.traces.each do |trace|
404 doc.root << trace.to_xml_node() if trace.public? or trace.user == @user
406 render :text => doc.to_s, :content_type => "text/xml"
410 @this_user = User.find_by_display_name(params[:display_name])
413 (@this_user.visible? or (@user and @user.administrator?))
414 @title = @this_user.display_name
416 render_unknown_user params[:display_name]
421 @new_friend = User.find_by_display_name(params[:display_name])
426 friend.user_id = @user.id
427 friend.friend_user_id = @new_friend.id
428 unless @user.is_friends_with?(@new_friend)
430 flash[:notice] = t 'user.make_friend.success', :name => @new_friend.display_name
431 Notifier.friend_notification(friend).deliver
433 friend.add_error(t('user.make_friend.failed', :name => @new_friend.display_name))
436 flash[:warning] = t 'user.make_friend.already_a_friend', :name => @new_friend.display_name
440 redirect_to params[:referer]
442 redirect_to :controller => 'user', :action => 'view'
446 render_unknown_user params[:display_name]
451 @friend = User.find_by_display_name(params[:display_name])
455 if @user.is_friends_with?(@friend)
456 Friend.delete_all "user_id = #{@user.id} AND friend_user_id = #{@friend.id}"
457 flash[:notice] = t 'user.remove_friend.success', :name => @friend.display_name
459 flash[:error] = t 'user.remove_friend.not_a_friend', :name => @friend.display_name
463 redirect_to params[:referer]
465 redirect_to :controller => 'user', :action => 'view'
469 render_unknown_user params[:display_name]
474 # sets a user's status
476 @this_user.status = params[:status]
478 redirect_to :controller => 'user', :action => 'view', :display_name => params[:display_name]
482 # delete a user, marking them as deleted and removing personal data
485 redirect_to :controller => 'user', :action => 'view', :display_name => params[:display_name]
489 # display a list of users matching specified criteria
492 ids = params[:user].keys.collect { |id| id.to_i }
494 User.update_all("status = 'confirmed'", :id => ids) if params[:confirm]
495 User.update_all("status = 'deleted'", :id => ids) if params[:hide]
497 redirect_to url_for(:status => params[:status], :ip => params[:ip], :page => params[:page])
499 conditions = Hash.new
500 conditions[:status] = params[:status] if params[:status]
501 conditions[:creation_ip] = params[:ip] if params[:ip]
503 @user_pages, @users = paginate(:users,
504 :conditions => conditions,
513 # handle password authentication
514 def password_authentication(username, password)
515 if user = User.authenticate(:username => username, :password => password)
516 successful_login(user)
517 elsif user = User.authenticate(:username => username, :password => password, :pending => true)
518 unconfirmed_login(user)
519 elsif User.authenticate(:username => username, :password => password, :suspended => true)
520 failed_login t('user.login.account is suspended', :webmaster => "mailto:webmaster@openstreetmap.org")
522 failed_login t('user.login.auth failure')
527 # handle OpenID authentication
528 def openid_authentication(openid_url)
529 # If we don't appear to have a user for this URL then ask the
530 # provider for some extra information to help with signup
531 if openid_url and User.find_by_openid_url(openid_url)
534 required = [:nickname, :email, "http://axschema.org/namePerson/friendly", "http://axschema.org/contact/email"]
537 # Start the authentication
538 authenticate_with_open_id(openid_expand_url(openid_url), :method => :get, :required => required) do |result, identity_url, sreg, ax|
539 if result.successful?
540 # We need to use the openid url passed back from the OpenID provider
541 # rather than the one supplied by the user, as these can be different.
543 # For example, you can simply enter yahoo.com in the login box rather
544 # than a user specific url. Only once it comes back from the provider
545 # provider do we know the unique address for the user.
546 if user = User.find_by_openid_url(identity_url)
549 unconfirmed_login(user)
550 when "active", "confirmed" then
551 successful_login(user)
552 when "suspended" then
553 failed_login t('user.login.account is suspended', :webmaster => "mailto:webmaster@openstreetmap.org")
555 failed_login t('user.login.auth failure')
558 # Guard against not getting any extension data
559 sreg = Hash.new if sreg.nil?
560 ax = Hash.new if ax.nil?
562 # We don't have a user registered to this OpenID, so redirect
563 # to the create account page with username and email filled
564 # in if they have been given by the OpenID provider through
565 # the simple registration protocol.
566 nickname = sreg["nickname"] || ax["http://axschema.org/namePerson/friendly"].first
567 email = sreg["email"] || ax["http://axschema.org/contact/email"].first
569 redirect_to :controller => 'user', :action => 'new', :nickname => nickname, :email => email, :openid => identity_url
571 elsif result.missing?
572 failed_login t('user.login.openid missing provider')
573 elsif result.invalid?
574 failed_login t('user.login.openid invalid')
576 failed_login t('user.login.auth failure')
582 # verify an OpenID URL
583 def openid_verify(openid_url, user)
584 user.openid_url = openid_url
586 authenticate_with_open_id(openid_expand_url(openid_url), :method => :get, :required => [:email, "http://axschema.org/contact/email"]) do |result, identity_url, sreg, ax|
587 if result.successful?
588 # Do we trust the emails this provider returns?
589 if openid_email_verified(identity_url)
590 # Guard against not getting any extension data
591 sreg = Hash.new if sreg.nil?
592 ax = Hash.new if ax.nil?
594 # Get the verified email
595 verified_email = sreg["email"] || ax["http://axschema.org/contact/email"].first
598 # We need to use the openid url passed back from the OpenID provider
599 # rather than the one supplied by the user, as these can be different.
601 # For example, you can simply enter yahoo.com in the login box rather
602 # than a user specific url. Only once it comes back from the provider
603 # provider do we know the unique address for the user.
604 user.openid_url = identity_url
605 yield user, verified_email
606 elsif result.missing?
607 flash.now[:error] = t 'user.login.openid missing provider'
608 elsif result.invalid?
609 flash.now[:error] = t 'user.login.openid invalid'
611 flash.now[:error] = t 'user.login.auth failure'
617 # special case some common OpenID providers by applying heuristics to
618 # try and come up with the correct URL based on what the user entered
619 def openid_expand_url(openid_url)
622 elsif openid_url.match(/(.*)gmail.com(\/?)$/) or openid_url.match(/(.*)googlemail.com(\/?)$/)
623 # Special case gmail.com as it is potentially a popular OpenID
624 # provider and, unlike yahoo.com, where it works automatically, Google
625 # have hidden their OpenID endpoint somewhere obscure this making it
626 # somewhat less user friendly.
627 return 'https://www.google.com/accounts/o8/id'
634 # check if we trust an OpenID provider to return a verified
635 # email, so that we can skpi verifying it ourselves
636 def openid_email_verified(openid_url)
637 openid_url.match(/https:\/\/www.google.com\/accounts\/o8\/id?(.*)/) or
638 openid_url.match(/https:\/\/me.yahoo.com\/(.*)/)
642 # process a successful login
643 def successful_login(user)
644 cookies.permanent["_osm_username"] = user.display_name
646 session[:user] = user.id
647 session_expires_after 28.days if session[:remember_me]
649 target = session[:referer] || url_for(:controller => :site, :action => :index)
651 # The user is logged in, so decide where to send them:
653 # - If they haven't seen the contributor terms, send them there.
654 # - If they have a block on them, show them that.
655 # - If they were referred to the login, send them back there.
656 # - Otherwise, send them to the home page.
657 if REQUIRE_TERMS_SEEN and not user.terms_seen
658 redirect_to :controller => :user, :action => :terms, :referer => target
659 elsif user.blocked_on_view
660 redirect_to user.blocked_on_view, :referer => target
665 session.delete(:remember_me)
666 session.delete(:referer)
670 # process a failed login
671 def failed_login(message)
672 flash[:error] = message
674 redirect_to :action => 'login', :referer => session[:referer]
676 session.delete(:remember_me)
677 session.delete(:referer)
682 def unconfirmed_login(user)
683 redirect_to :action => 'confirm', :display_name => user.display_name
685 session.delete(:remember_me)
686 session.delete(:referer)
690 # update a user's details
691 def update_user(user, params)
692 user.display_name = params[:user][:display_name]
693 user.new_email = params[:user][:new_email]
695 if params[:user][:pass_crypt].length > 0 or params[:user][:pass_crypt_confirmation].length > 0
696 user.pass_crypt = params[:user][:pass_crypt]
697 user.pass_crypt_confirmation = params[:user][:pass_crypt_confirmation]
700 if params[:user][:description] != user.description
701 user.description = params[:user][:description]
702 user.description_format = "markdown"
705 user.languages = params[:user][:languages].split(",")
707 case params[:image_action]
709 user.image = params[:user][:image]
710 user.image_use_gravatar = false
713 user.image_use_gravatar = false
716 user.image_use_gravatar = true
719 user.home_lat = params[:user][:home_lat]
720 user.home_lon = params[:user][:home_lon]
722 if params[:user][:preferred_editor] == "default"
723 user.preferred_editor = nil
725 user.preferred_editor = params[:user][:preferred_editor]
728 user.openid_url = nil if params[:user][:openid_url].blank?
733 cookies.permanent["_osm_username"] = user.display_name
735 if user.new_email.blank?
736 flash.now[:notice] = t 'user.account.flash update success'
738 user.email = user.new_email
741 flash.now[:notice] = t 'user.account.flash update success confirm needed'
744 Notifier.email_confirm(user, user.tokens.create).deliver
746 # Ignore errors sending email
749 @user.errors.set(:new_email, @user.errors.get(:email))
750 @user.errors.set(:email, [])
759 # require that the user is a administrator, or fill out a helpful error message
760 # and return them to the user page.
761 def require_administrator
762 if @user and not @user.administrator?
763 flash[:error] = t('user.filter.not_an_administrator')
765 if params[:display_name]
766 redirect_to :controller => 'user', :action => 'view', :display_name => params[:display_name]
768 redirect_to :controller => 'user', :action => 'login', :referer => request.fullpath
771 redirect_to :controller => 'user', :action => 'login', :referer => request.fullpath
776 # ensure that there is a "this_user" instance variable
777 def lookup_user_by_id
778 @this_user = User.find(params[:id])
782 # ensure that there is a "this_user" instance variable
783 def lookup_user_by_name
784 @this_user = User.find_by_display_name(params[:display_name])
785 rescue ActiveRecord::RecordNotFound
786 redirect_to :controller => 'user', :action => 'view', :display_name => params[:display_name] unless @this_user
790 # Choose the layout to use. See
791 # https://rails.lighthouseapp.com/projects/8994/tickets/5371-layout-with-onlyexcept-options-makes-other-actions-render-without-layouts
793 oauth_url = url_for(:controller => :oauth, :action => :authorize, :only_path => true)
795 if [ 'api_details' ].include? action_name
797 elsif params[:referer] and URI.parse(params[:referer]).path == oauth_url
806 def disable_terms_redirect
807 # this is necessary otherwise going to the user terms page, when
808 # having not agreed already would cause an infinite redirect loop.
809 # it's .now so that this doesn't propagate to other pages.
810 flash.now[:skip_terms] = true