1 class UserController < ApplicationController
4 before_filter :disable_terms_redirect, :only => [:terms, :save, :logout, :api_details]
5 before_filter :authorize, :only => [:api_details, :api_gpx_files]
6 before_filter :authorize_web, :except => [:api_details, :api_gpx_files]
7 before_filter :set_locale, :except => [:api_details, :api_gpx_files]
8 before_filter :require_user, :only => [:account, :go_public, :make_friend, :remove_friend]
9 before_filter :check_database_readable, :except => [:api_details, :api_gpx_files]
10 before_filter :check_database_writable, :only => [:login, :new, :account, :go_public, :make_friend, :remove_friend]
11 before_filter :check_api_readable, :only => [:api_details, :api_gpx_files]
12 before_filter :require_allow_read_prefs, :only => [:api_details]
13 before_filter :require_allow_read_gpx, :only => [:api_gpx_files]
14 before_filter :require_cookies, :only => [:login, :confirm]
15 before_filter :require_administrator, :only => [:set_status, :delete, :list]
16 before_filter :lookup_this_user, :only => [:set_status, :delete]
18 cache_sweeper :user_sweeper, :only => [:account, :set_status, :delete]
21 @legale = params[:legale] || OSM.IPToCountry(request.remote_ip) || DEFAULT_LEGALE
22 @text = OSM.legal_text_for_country(@legale)
25 render :update do |page|
26 page.replace_html "contributorTerms", :partial => "terms"
29 # The redirect from the OpenID provider reenters here
30 # again and we need to pass the parameters through to
31 # the open_id_authentication function
32 @user = session.delete(:new_user)
34 openid_verify(nil, @user) do |user|
37 if @user.openid_url.nil? or @user.invalid?
38 render :action => 'new'
40 render :action => 'terms'
43 session[:referer] = params[:referer]
45 @title = t 'user.terms.title'
46 @user = User.new(params[:user]) if params[:user]
48 if params[:user] and params[:user][:openid_url] and @user.pass_crypt.empty?
49 # We are creating an account with OpenID and no password
50 # was specified so create a random one
51 @user.pass_crypt = ActiveSupport::SecureRandom.base64(16)
52 @user.pass_crypt_confirmation = @user.pass_crypt
58 # Something is wrong with a new user, so rerender the form
59 render :action => :new
61 # Error in existing user, so go to account settings
62 flash[:errors] = @user.errors
63 redirect_to :action => :account, :display_name => @user.display_name
65 elsif @user.terms_agreed?
66 # Already agreed to terms, so just show settings
67 redirect_to :action => :account, :display_name => @user.display_name
68 elsif params[:user] and params[:user][:openid_url] and not params[:user][:openid_url].empty?
69 # Verify OpenID before moving on
70 session[:new_user] = @user
71 openid_verify(params[:user][:openid_url], @user)
74 # Not logged in, so redirect to the login page
75 redirect_to :action => :login, :referer => request.fullpath
81 @title = t 'user.new.title'
83 if Acl.find_by_address(request.remote_ip, :conditions => {:k => "no_account_creation"})
84 render :action => 'new'
85 elsif params[:decline]
87 @user.terms_seen = true
90 flash[:notice] = t 'user.new.terms declined', :url => t('user.new.terms declined url')
94 redirect_to params[:referer]
96 redirect_to :action => :account, :display_name => @user.display_name
99 redirect_to t('user.terms.declined')
102 if !@user.terms_agreed?
103 @user.consider_pd = params[:user][:consider_pd]
104 @user.terms_agreed = Time.now.getutc
105 @user.terms_seen = true
107 flash[:notice] = t 'user.new.terms accepted'
112 redirect_to params[:referer]
114 redirect_to :action => :account, :display_name => @user.display_name
117 @user = User.new(params[:user])
119 @user.status = "pending"
120 @user.data_public = true
121 @user.description = "" if @user.description.nil?
122 @user.creation_ip = request.remote_ip
123 @user.languages = request.user_preferred_languages
124 @user.terms_agreed = Time.now.getutc
125 @user.terms_seen = true
126 @user.openid_url = nil if @user.openid_url and @user.openid_url.empty?
129 flash[:piwik_goal] = PIWIK_SIGNUP_GOAL if defined?(PIWIK_SIGNUP_GOAL)
130 flash[:notice] = t 'user.new.flash create success message', :email => @user.email
131 Notifier.signup_confirm(@user, @user.tokens.create(:referer => session.delete(:referer))).deliver
132 session[:token] = @user.tokens.create.token
133 redirect_to :action => 'login', :referer => params[:referer]
135 render :action => 'new', :referer => params[:referer]
141 @title = t 'user.account.title'
142 @tokens = @user.oauth_tokens.find :all, :conditions => 'oauth_tokens.invalidated_at is null and oauth_tokens.authorized_at is not null'
144 if params[:user] and params[:user][:display_name] and params[:user][:description]
145 @user.display_name = params[:user][:display_name]
146 @user.new_email = params[:user][:new_email]
148 if params[:user][:pass_crypt].length > 0 or params[:user][:pass_crypt_confirmation].length > 0
149 @user.pass_crypt = params[:user][:pass_crypt]
150 @user.pass_crypt_confirmation = params[:user][:pass_crypt_confirmation]
153 @user.description = params[:user][:description]
154 @user.languages = params[:user][:languages].split(",")
156 case params[:image_action]
157 when "new" then @user.image = params[:user][:image]
158 when "delete" then @user.image = nil
161 @user.home_lat = params[:user][:home_lat]
162 @user.home_lon = params[:user][:home_lon]
164 if params[:user][:preferred_editor] == "default"
165 @user.preferred_editor = nil
167 @user.preferred_editor = params[:user][:preferred_editor]
170 @user.openid_url = nil if params[:user][:openid_url].empty?
172 if params[:user][:openid_url].length > 0 and
173 params[:user][:openid_url] != @user.openid_url
174 # If the OpenID has changed, we want to check that it is a
175 # valid OpenID and one the user has control over before saving
176 # it as a password equivalent for the user.
177 session[:new_user] = @user
178 openid_verify(params[:user][:openid_url], @user)
183 # The redirect from the OpenID provider reenters here
184 # again and we need to pass the parameters through to
185 # the open_id_authentication function
186 @user = session.delete(:new_user)
187 openid_verify(nil, @user) do |user|
192 flash[:errors].each do |attr,msg|
193 attr = "new_email" if attr == "email" and !@user.new_email.nil?
194 @user.errors.add(attr,msg)
201 @user.data_public = true
203 flash[:notice] = t 'user.go_public.flash success'
204 redirect_to :controller => 'user', :action => 'account', :display_name => @user.display_name
208 @title = t 'user.lost_password.title'
210 if params[:user] and params[:user][:email]
211 user = User.find_by_email(params[:user][:email], :conditions => {:status => ["pending", "active", "confirmed"]})
214 token = user.tokens.create
215 Notifier.lost_password(user, token).deliver
216 flash[:notice] = t 'user.lost_password.notice email on way'
217 redirect_to :action => 'login'
219 flash.now[:error] = t 'user.lost_password.notice email cannot find'
225 @title = t 'user.reset_password.title'
228 token = UserToken.find_by_token(params[:token])
234 @user.pass_crypt = params[:user][:pass_crypt]
235 @user.pass_crypt_confirmation = params[:user][:pass_crypt_confirmation]
236 @user.status = "active" if @user.status == "pending"
237 @user.email_valid = true
241 flash[:notice] = t 'user.reset_password.flash changed'
242 redirect_to :action => 'login'
246 flash[:error] = t 'user.reset_password.flash token bad'
247 redirect_to :action => 'lost_password'
253 @title = t 'user.new.title'
254 @referer = params[:referer] || session[:referer]
257 # The user is logged in already, so don't show them the signup
258 # page, instead send them to the home page
259 redirect_to :controller => 'site', :action => 'index'
260 elsif not params['openid'].nil?
261 flash.now[:notice] = t 'user.new.openid association'
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])
275 elsif flash[:notice].nil?
276 flash.now[:notice] = t 'user.login.notice'
281 @title = t 'user.logout.title'
283 if params[:session] == request.session_options[:id]
285 token = UserToken.find_by_token(session[:token])
289 session.delete(:token)
291 session.delete(:user)
292 session_expires_automatically
294 redirect_to params[:referer]
296 redirect_to :controller => 'site', :action => 'index'
303 if token = UserToken.find_by_token(params[:confirm_string])
304 if token.user.active?
305 flash[:error] = t('user.confirm.already active')
306 redirect_to :action => 'login'
309 user.status = "active"
310 user.email_valid = true
312 referer = token.referer
316 token = UserToken.find_by_token(session[:token])
317 session.delete(:token)
322 if token.nil? or token.user != user
323 flash[:notice] = t('user.confirm.success')
324 redirect_to :action => :login, :referer => referer
328 session[:user] = user.id
331 flash[:notice] = t('user.confirm.success') + "<br /><br />" + t('user.confirm.before you start')
332 redirect_to :action => :account, :display_name => user.display_name
334 flash[:notice] = t('user.confirm.success')
340 user = User.find_by_display_name(params[:display_name])
342 if user and user.active?
343 flash[:error] = t('user.confirm.already active')
345 flash[:error] = t('user.confirm.unknown token') + t('user.confirm.reconfirm', :reconfirm => url_for(:action => 'confirm_resend', :display_name => params[:display_name]))
347 flash[:error] = t('user.confirm.unknown token')
350 redirect_to :action => 'login'
356 if user = User.find_by_display_name(params[:display_name])
357 Notifier.signup_confirm(user, user.tokens.create).deliver
358 flash[:notice] = t 'user.confirm_resend.success', :email => user.email
360 flash[:notice] = t 'user.confirm_resend.failure', :name => params[:display_name]
363 redirect_to :action => 'login'
368 token = UserToken.find_by_token(params[:confirm_string])
369 if token and token.user.new_email?
371 @user.email = @user.new_email
372 @user.new_email = nil
373 @user.email_valid = true
375 flash[:notice] = t 'user.confirm_email.success'
377 flash[:errors] = @user.errors
380 session[:user] = @user.id
381 redirect_to :action => 'account', :display_name => @user.display_name
383 flash[:error] = t 'user.confirm_email.failure'
384 redirect_to :action => 'account', :display_name => @user.display_name
390 doc = OSM::API.new.get_xml_doc
391 @user.traces.each do |trace|
392 doc.root << trace.to_xml_node() if trace.public? or trace.user == @user
394 render :text => doc.to_s, :content_type => "text/xml"
398 @this_user = User.find_by_display_name(params[:display_name])
401 (@this_user.visible? or (@user and @user.administrator?))
402 @title = @this_user.display_name
404 @title = t 'user.no_such_user.title'
405 @not_found_user = params[:display_name]
406 render :action => 'no_such_user', :status => :not_found
411 if params[:display_name]
412 name = params[:display_name]
413 new_friend = User.find_by_display_name(name, :conditions => {:status => ["active", "confirmed"]})
415 friend.user_id = @user.id
416 friend.friend_user_id = new_friend.id
417 unless @user.is_friends_with?(new_friend)
419 flash[:notice] = t 'user.make_friend.success', :name => name
420 Notifier.friend_notification(friend).deliver
422 friend.add_error(t('user.make_friend.failed', :name => name))
425 flash[:warning] = t 'user.make_friend.already_a_friend', :name => name
429 redirect_to params[:referer]
431 redirect_to :controller => 'user', :action => 'view'
437 if params[:display_name]
438 name = params[:display_name]
439 friend = User.find_by_display_name(name, :conditions => {:status => ["active", "confirmed"]})
440 if @user.is_friends_with?(friend)
441 Friend.delete_all "user_id = #{@user.id} AND friend_user_id = #{friend.id}"
442 flash[:notice] = t 'user.remove_friend.success', :name => friend.display_name
444 flash[:error] = t 'user.remove_friend.not_a_friend', :name => friend.display_name
448 redirect_to params[:referer]
450 redirect_to :controller => 'user', :action => 'view'
456 # sets a user's status
458 @this_user.update_attributes(:status => params[:status])
459 redirect_to :controller => 'user', :action => 'view', :display_name => params[:display_name]
463 # delete a user, marking them as deleted and removing personal data
466 redirect_to :controller => 'user', :action => 'view', :display_name => params[:display_name]
470 # display a list of users matching specified criteria
473 ids = params[:user].keys.collect { |id| id.to_i }
475 User.update_all("status = 'confirmed'", :id => ids) if params[:confirm]
476 User.update_all("status = 'deleted'", :id => ids) if params[:hide]
478 redirect_to url_for(:status => params[:status], :ip => params[:ip], :page => params[:page])
480 conditions = Hash.new
481 conditions[:status] = params[:status] if params[:status]
482 conditions[:creation_ip] = params[:ip] if params[:ip]
484 @user_pages, @users = paginate(:users,
485 :conditions => conditions,
494 # handle password authentication
495 def password_authentication(username, password)
496 if user = User.authenticate(:username => username, :password => password)
497 successful_login(user)
498 elsif user = User.authenticate(:username => username, :password => password, :pending => true)
499 failed_login t('user.login.account not active', :reconfirm => url_for(:action => 'confirm_resend', :display_name => user.display_name))
500 elsif User.authenticate(:username => username, :password => password, :suspended => true)
501 webmaster = link_to t('user.login.webmaster'), "mailto:webmaster@openstreetmap.org"
502 failed_login t('user.login.account suspended', :webmaster => webmaster)
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), :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 failed_login t('user.login.account not active')
532 when "active", "confirmed" then
533 successful_login(user)
534 when "suspended" then
535 webmaster = link_to t('user.login.webmaster'), "mailto:webmaster@openstreetmap.org"
536 failed_login t('user.login.account suspended', :webmaster => webmaster)
538 failed_login t('user.login.auth failure')
541 # Guard against not getting any extension data
542 sreg = Hash.new if sreg.nil?
543 ax = Hash.new if ax.nil?
545 # We don't have a user registered to this OpenID, so redirect
546 # to the create account page with username and email filled
547 # in if they have been given by the OpenID provider through
548 # the simple registration protocol.
549 nickname = sreg["nickname"] || ax["http://axschema.org/namePerson/friendly"]
550 email = sreg["email"] || ax["http://axschema.org/contact/email"]
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)) do |result, identity_url|
569 if result.successful?
570 # We need to use the openid url passed back from the OpenID provider
571 # rather than the one supplied by the user, as these can be different.
573 # For example, you can simply enter yahoo.com in the login box rather
574 # than a user specific url. Only once it comes back from the provider
575 # provider do we know the unique address for the user.
576 user.openid_url = identity_url
578 elsif result.missing?
579 flash.now[:error] = t 'user.login.openid missing provider'
580 elsif result.invalid?
581 flash.now[:error] = t 'user.login.openid invalid'
583 flash.now[:error] = t 'user.login.auth failure'
589 # special case some common OpenID providers by applying heuristics to
590 # try and come up with the correct URL based on what the user entered
591 def openid_expand_url(openid_url)
594 elsif openid_url.match(/(.*)gmail.com(\/?)$/) or openid_url.match(/(.*)googlemail.com(\/?)$/)
595 # Special case gmail.com as it is potentially a popular OpenID
596 # provider and, unlike yahoo.com, where it works automatically, Google
597 # have hidden their OpenID endpoint somewhere obscure this making it
598 # somewhat less user friendly.
599 return 'https://www.google.com/accounts/o8/id'
606 # process a successful login
607 def successful_login(user)
608 session[:user] = user.id
609 session_expires_after 1.month if session[:remember_me]
611 target = session[:referer] || url_for(:controller => :site, :action => :index)
613 # The user is logged in, so decide where to send them:
615 # - If they haven't seen the contributor terms, send them there.
616 # - If they have a block on them, show them that.
617 # - If they were referred to the login, send them back there.
618 # - Otherwise, send them to the home page.
619 if REQUIRE_TERMS_SEEN and not user.terms_seen
620 redirect_to :controller => :user, :action => :terms, :referer => target
621 elsif user.blocked_on_view
622 redirect_to user.blocked_on_view, :referer => target
627 session.delete(:remember_me)
628 session.delete(:referer)
632 # process a failed login
633 def failed_login(message)
634 flash[:error] = message
636 redirect_to :action => 'login', :referer => session[:referer]
638 session.delete(:remember_me)
639 session.delete(:referer)
643 # update a user's details
644 def update_user(user)
648 if user.new_email.nil? or user.new_email.empty?
649 flash.now[:notice] = t 'user.account.flash update success'
651 flash.now[:notice] = t 'user.account.flash update success confirm needed'
654 Notifier.email_confirm(user, user.tokens.create).deliver
656 # Ignore errors sending email
663 # require that the user is a administrator, or fill out a helpful error message
664 # and return them to the user page.
665 def require_administrator
666 if @user and not @user.administrator?
667 flash[:error] = t('user.filter.not_an_administrator')
669 if params[:display_name]
670 redirect_to :controller => 'user', :action => 'view', :display_name => params[:display_name]
672 redirect_to :controller => 'user', :action => 'login', :referer => request.fullpath
675 redirect_to :controller => 'user', :action => 'login', :referer => request.fullpath
680 # ensure that there is a "this_user" instance variable
682 @this_user = User.find_by_display_name(params[:display_name])
683 rescue ActiveRecord::RecordNotFound
684 redirect_to :controller => 'user', :action => 'view', :display_name => params[:display_name] unless @this_user
688 # Choose the layout to use. See
689 # https://rails.lighthouseapp.com/projects/8994/tickets/5371-layout-with-onlyexcept-options-makes-other-actions-render-without-layouts
691 oauth_url = url_for(:controller => :oauth, :action => :oauthorize, :only_path => true)
693 if [ 'api_details' ].include? action_name
695 elsif params[:referer] and URI.parse(params[:referer]).path == oauth_url
704 def disable_terms_redirect
705 # this is necessary otherwise going to the user terms page, when
706 # having not agreed already would cause an infinite redirect loop.
707 # it's .now so that this doesn't propagate to other pages.
708 flash.now[:skip_terms] = true