1 class UserController < ApplicationController
4 skip_before_filter :verify_authenticity_token, :only => [: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_details, :api_gpx_files]
8 before_filter :set_locale, :except => [: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_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_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 => [:login, :confirm]
16 before_filter :require_administrator, :only => [:set_status, :delete, :list]
17 before_filter :lookup_this_user, :only => [:set_status, :delete]
19 cache_sweeper :user_sweeper, :only => [:account, :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|
36 if @user.openid_url.nil? or @user.invalid?
37 render :action => 'new'
39 render :action => 'terms'
41 elsif params[:user] and Acl.no_account_creation(request.remote_ip, params[:user][:email].split("@").last)
42 render :action => 'blocked'
44 session[:referer] = params[:referer]
46 @title = t 'user.terms.title'
47 @user = User.new(params[:user]) if params[:user]
49 if params[:user] and params[:user][:openid_url] and @user.pass_crypt.empty?
50 # We are creating an account with OpenID and no password
51 # was specified so create a random one
52 @user.pass_crypt = SecureRandom.base64(16)
53 @user.pass_crypt_confirmation = @user.pass_crypt
59 # Something is wrong with a new user, so rerender the form
60 render :action => :new
62 # Error in existing user, so go to account settings
63 flash[:errors] = @user.errors
64 redirect_to :action => :account, :display_name => @user.display_name
66 elsif @user.terms_agreed?
67 # Already agreed to terms, so just show settings
68 redirect_to :action => :account, :display_name => @user.display_name
69 elsif params[:user] and params[:user][:openid_url] and not params[:user][:openid_url].empty?
70 # Verify OpenID before moving on
71 session[:new_user] = @user
72 openid_verify(params[:user][:openid_url], @user)
75 # Not logged in, so redirect to the login page
76 redirect_to :action => :login, :referer => request.fullpath
82 @title = t 'user.new.title'
86 @user.terms_seen = true
89 flash[:notice] = t 'user.new.terms declined', :url => t('user.new.terms declined url')
93 redirect_to params[:referer]
95 redirect_to :action => :account, :display_name => @user.display_name
98 redirect_to t('user.terms.declined')
101 if !@user.terms_agreed?
102 @user.consider_pd = params[:user][:consider_pd]
103 @user.terms_agreed = Time.now.getutc
104 @user.terms_seen = true
106 flash[:notice] = t 'user.new.terms accepted'
111 redirect_to params[:referer]
113 redirect_to :action => :account, :display_name => @user.display_name
115 elsif Acl.no_account_creation(request.remote_ip, params[:user][:email].split("@").last)
116 render :action => 'blocked'
118 @user = User.new(params[:user])
120 @user.status = "pending"
121 @user.data_public = true
122 @user.description = "" if @user.description.nil?
123 @user.creation_ip = request.remote_ip
124 @user.languages = request.user_preferred_languages
125 @user.terms_agreed = Time.now.getutc
126 @user.terms_seen = true
127 @user.openid_url = nil if @user.openid_url and @user.openid_url.empty?
130 flash[:piwik_goal] = PIWIK_SIGNUP_GOAL if defined?(PIWIK_SIGNUP_GOAL)
131 flash[:notice] = t 'user.new.flash create success message', :email => @user.email
132 Notifier.signup_confirm(@user, @user.tokens.create(:referer => session.delete(:referer))).deliver
133 session[:token] = @user.tokens.create.token
134 redirect_to :action => 'login', :referer => params[:referer]
136 render :action => 'new', :referer => params[:referer]
142 @title = t 'user.account.title'
143 @tokens = @user.oauth_tokens.authorized
145 if params[:user] and params[:user][:display_name] and params[:user][:description]
146 if params[:user][:openid_url] and
147 params[:user][:openid_url].length > 0 and
148 params[:user][:openid_url] != @user.openid_url
149 # If the OpenID has changed, we want to check that it is a
150 # valid OpenID and one the user has control over before saving
151 # it as a password equivalent for the user.
152 session[:new_user_settings] = params
153 openid_verify(params[:user][:openid_url], @user)
155 update_user(@user, params)
158 # The redirect from the OpenID provider reenters here
159 # again and we need to pass the parameters through to
160 # the open_id_authentication function
161 settings = session.delete(:new_user_settings)
162 openid_verify(nil, @user) do |user|
163 update_user(user, settings)
169 @user.data_public = true
171 flash[:notice] = t 'user.go_public.flash success'
172 redirect_to :controller => 'user', :action => 'account', :display_name => @user.display_name
176 @title = t 'user.lost_password.title'
178 if params[:user] and params[:user][:email]
179 user = User.visible.find_by_email(params[:user][:email])
182 users = User.visible.where("LOWER(email) = LOWER(?)", params[:user][:email])
190 token = user.tokens.create
191 Notifier.lost_password(user, token).deliver
192 flash[:notice] = t 'user.lost_password.notice email on way'
193 redirect_to :action => 'login'
195 flash.now[:error] = t 'user.lost_password.notice email cannot find'
201 @title = t 'user.reset_password.title'
204 token = UserToken.find_by_token(params[:token])
210 @user.pass_crypt = params[:user][:pass_crypt]
211 @user.pass_crypt_confirmation = params[:user][:pass_crypt_confirmation]
212 @user.status = "active" if @user.status == "pending"
213 @user.email_valid = true
217 flash[:notice] = t 'user.reset_password.flash changed'
218 redirect_to :action => 'login'
222 flash[:error] = t 'user.reset_password.flash token bad'
223 redirect_to :action => 'lost_password'
229 @title = t 'user.new.title'
230 @referer = params[:referer] || session[:referer]
233 # The user is logged in already, so don't show them the signup
234 # page, instead send them to the home page
238 redirect_to :controller => 'site', :action => 'index'
240 elsif params.key?(:openid)
241 @user = User.new(:email => params[:email],
242 :email_confirmation => params[:email],
243 :display_name => params[:nickname],
244 :openid_url => params[:openid])
246 flash.now[:notice] = t 'user.new.openid association'
247 elsif Acl.no_account_creation(request.remote_ip)
248 render :action => 'blocked'
253 if params[:username] or using_open_id?
254 session[:remember_me] ||= params[:remember_me]
255 session[:referer] ||= params[:referer]
258 openid_authentication(params[:openid_url])
260 password_authentication(params[:username], params[:password])
262 elsif params[:notice]
263 flash.now[:notice] = t "user.login.notice_#{params[:notice]}"
264 elsif flash[:notice].nil?
265 flash.now[:notice] = t 'user.login.notice'
270 @title = t 'user.logout.title'
272 if params[:session] == request.session_options[:id]
274 token = UserToken.find_by_token(session[:token])
278 session.delete(:token)
280 session.delete(:user)
281 session_expires_automatically
283 redirect_to params[:referer]
285 redirect_to :controller => 'site', :action => 'index'
292 if token = UserToken.find_by_token(params[:confirm_string])
293 if token.user.active?
294 flash[:error] = t('user.confirm.already active')
295 redirect_to :action => 'login'
298 user.status = "active"
299 user.email_valid = true
301 referer = token.referer
305 token = UserToken.find_by_token(session[:token])
306 session.delete(:token)
311 if token.nil? or token.user != user
312 flash[:notice] = t('user.confirm.success')
313 redirect_to :action => :login, :referer => referer
317 session[:user] = user.id
318 cookies.permanent["_osm_username"] = user.display_name
321 flash[:notice] = t('user.confirm.success') + "<br /><br />" + t('user.confirm.before you start')
322 redirect_to :action => :account, :display_name => user.display_name
324 flash[:notice] = t('user.confirm.success')
330 user = User.find_by_display_name(params[:display_name])
332 if user and user.active?
333 flash[:error] = t('user.confirm.already active')
335 flash[:error] = t('user.confirm.unknown token') + t('user.confirm.reconfirm', :reconfirm => url_for(:action => 'confirm_resend', :display_name => params[:display_name]))
337 flash[:error] = t('user.confirm.unknown token')
340 redirect_to :action => 'login'
346 if user = User.find_by_display_name(params[:display_name])
347 Notifier.signup_confirm(user, user.tokens.create).deliver
348 flash[:notice] = t 'user.confirm_resend.success', :email => user.email
350 flash[:notice] = t 'user.confirm_resend.failure', :name => params[:display_name]
353 redirect_to :action => 'login'
358 token = UserToken.find_by_token(params[:confirm_string])
359 if token and token.user.new_email?
361 @user.email = @user.new_email
362 @user.new_email = nil
363 @user.email_valid = true
365 flash[:notice] = t 'user.confirm_email.success'
367 flash[:errors] = @user.errors
370 session[:user] = @user.id
371 cookies.permanent["_osm_username"] = @user.display_name
372 redirect_to :action => 'account', :display_name => @user.display_name
374 flash[:error] = t 'user.confirm_email.failure'
375 redirect_to :action => 'account', :display_name => @user.display_name
381 doc = OSM::API.new.get_xml_doc
382 @user.traces.each do |trace|
383 doc.root << trace.to_xml_node() if trace.public? or trace.user == @user
385 render :text => doc.to_s, :content_type => "text/xml"
389 @this_user = User.find_by_display_name(params[:display_name])
392 (@this_user.visible? or (@user and @user.administrator?))
393 @title = @this_user.display_name
395 render_unknown_user params[:display_name]
400 @new_friend = User.find_by_display_name(params[:display_name])
405 friend.user_id = @user.id
406 friend.friend_user_id = @new_friend.id
407 unless @user.is_friends_with?(@new_friend)
409 flash[:notice] = t 'user.make_friend.success', :name => @new_friend.display_name
410 Notifier.friend_notification(friend).deliver
412 friend.add_error(t('user.make_friend.failed', :name => @new_friend.display_name))
415 flash[:warning] = t 'user.make_friend.already_a_friend', :name => @new_friend.display_name
419 redirect_to params[:referer]
421 redirect_to :controller => 'user', :action => 'view'
425 render_unknown_user params[:display_name]
430 @friend = User.find_by_display_name(params[:display_name])
434 if @user.is_friends_with?(@friend)
435 Friend.delete_all "user_id = #{@user.id} AND friend_user_id = #{@friend.id}"
436 flash[:notice] = t 'user.remove_friend.success', :name => @friend.display_name
438 flash[:error] = t 'user.remove_friend.not_a_friend', :name => @friend.display_name
442 redirect_to params[:referer]
444 redirect_to :controller => 'user', :action => 'view'
448 render_unknown_user params[:display_name]
453 # sets a user's status
455 @this_user.status = params[:status]
457 redirect_to :controller => 'user', :action => 'view', :display_name => params[:display_name]
461 # delete a user, marking them as deleted and removing personal data
464 redirect_to :controller => 'user', :action => 'view', :display_name => params[:display_name]
468 # display a list of users matching specified criteria
471 ids = params[:user].keys.collect { |id| id.to_i }
473 User.update_all("status = 'confirmed'", :id => ids) if params[:confirm]
474 User.update_all("status = 'deleted'", :id => ids) if params[:hide]
476 redirect_to url_for(:status => params[:status], :ip => params[:ip], :page => params[:page])
478 conditions = Hash.new
479 conditions[:status] = params[:status] if params[:status]
480 conditions[:creation_ip] = params[:ip] if params[:ip]
482 @user_pages, @users = paginate(:users,
483 :conditions => conditions,
492 # handle password authentication
493 def password_authentication(username, password)
494 if user = User.authenticate(:username => username, :password => password)
495 successful_login(user)
496 elsif user = User.authenticate(:username => username, :password => password, :pending => true)
497 failed_login t('user.login.account not active', :reconfirm => url_for(:action => 'confirm_resend', :display_name => user.display_name))
498 elsif User.authenticate(:username => username, :password => password, :suspended => true)
499 failed_login t('user.login.account is suspended', :webmaster => "mailto:webmaster@openstreetmap.org")
501 failed_login t('user.login.auth failure')
506 # handle OpenID authentication
507 def openid_authentication(openid_url)
508 # If we don't appear to have a user for this URL then ask the
509 # provider for some extra information to help with signup
510 if openid_url and User.find_by_openid_url(openid_url)
513 required = [:nickname, :email, "http://axschema.org/namePerson/friendly", "http://axschema.org/contact/email"]
516 # Start the authentication
517 authenticate_with_open_id(openid_expand_url(openid_url), :method => :get, :required => required) do |result, identity_url, sreg, ax|
518 if result.successful?
519 # We need to use the openid url passed back from the OpenID provider
520 # rather than the one supplied by the user, as these can be different.
522 # For example, you can simply enter yahoo.com in the login box rather
523 # than a user specific url. Only once it comes back from the provider
524 # provider do we know the unique address for the user.
525 if user = User.find_by_openid_url(identity_url)
528 failed_login t('user.login.account not active', :reconfirm => url_for(:action => 'confirm_resend', :display_name => user.display_name))
529 when "active", "confirmed" then
530 successful_login(user)
531 when "suspended" then
532 failed_login t('user.login.account is suspended', :webmaster => "mailto:webmaster@openstreetmap.org")
534 failed_login t('user.login.auth failure')
537 # Guard against not getting any extension data
538 sreg = Hash.new if sreg.nil?
539 ax = Hash.new if ax.nil?
541 # We don't have a user registered to this OpenID, so redirect
542 # to the create account page with username and email filled
543 # in if they have been given by the OpenID provider through
544 # the simple registration protocol.
545 nickname = sreg["nickname"] || ax["http://axschema.org/namePerson/friendly"].first
546 email = sreg["email"] || ax["http://axschema.org/contact/email"].first
547 redirect_to :controller => 'user', :action => 'new', :nickname => nickname, :email => email, :openid => identity_url
549 elsif result.missing?
550 failed_login t('user.login.openid missing provider')
551 elsif result.invalid?
552 failed_login t('user.login.openid invalid')
554 failed_login t('user.login.auth failure')
560 # verify an OpenID URL
561 def openid_verify(openid_url, user)
562 user.openid_url = openid_url
564 authenticate_with_open_id(openid_expand_url(openid_url), :method => :get) do |result, identity_url|
565 if result.successful?
566 # We need to use the openid url passed back from the OpenID provider
567 # rather than the one supplied by the user, as these can be different.
569 # For example, you can simply enter yahoo.com in the login box rather
570 # than a user specific url. Only once it comes back from the provider
571 # provider do we know the unique address for the user.
572 user.openid_url = identity_url
574 elsif result.missing?
575 flash.now[:error] = t 'user.login.openid missing provider'
576 elsif result.invalid?
577 flash.now[:error] = t 'user.login.openid invalid'
579 flash.now[:error] = t 'user.login.auth failure'
585 # special case some common OpenID providers by applying heuristics to
586 # try and come up with the correct URL based on what the user entered
587 def openid_expand_url(openid_url)
590 elsif openid_url.match(/(.*)gmail.com(\/?)$/) or openid_url.match(/(.*)googlemail.com(\/?)$/)
591 # Special case gmail.com as it is potentially a popular OpenID
592 # provider and, unlike yahoo.com, where it works automatically, Google
593 # have hidden their OpenID endpoint somewhere obscure this making it
594 # somewhat less user friendly.
595 return 'https://www.google.com/accounts/o8/id'
602 # process a successful login
603 def successful_login(user)
604 cookies.permanent["_osm_username"] = user.display_name
606 session[:user] = user.id
607 session_expires_after 28.days if session[:remember_me]
609 target = session[:referer] || url_for(:controller => :site, :action => :index)
611 # The user is logged in, so decide where to send them:
613 # - If they haven't seen the contributor terms, send them there.
614 # - If they have a block on them, show them that.
615 # - If they were referred to the login, send them back there.
616 # - Otherwise, send them to the home page.
617 if REQUIRE_TERMS_SEEN and not user.terms_seen
618 redirect_to :controller => :user, :action => :terms, :referer => target
619 elsif user.blocked_on_view
620 redirect_to user.blocked_on_view, :referer => target
625 session.delete(:remember_me)
626 session.delete(:referer)
630 # process a failed login
631 def failed_login(message)
632 flash[:error] = message
634 redirect_to :action => 'login', :referer => session[:referer]
636 session.delete(:remember_me)
637 session.delete(:referer)
641 # update a user's details
642 def update_user(user, params)
643 user.display_name = params[:user][:display_name]
644 user.new_email = params[:user][:new_email]
646 if params[:user][:pass_crypt].length > 0 or params[:user][:pass_crypt_confirmation].length > 0
647 user.pass_crypt = params[:user][:pass_crypt]
648 user.pass_crypt_confirmation = params[:user][:pass_crypt_confirmation]
651 if params[:user][:description] != user.description
652 user.description = params[:user][:description]
653 user.description_format = "markdown"
656 user.languages = params[:user][:languages].split(",")
658 case params[:image_action]
659 when "new" then user.image = params[:user][:image]
660 when "delete" then user.image = nil
663 user.home_lat = params[:user][:home_lat]
664 user.home_lon = params[:user][:home_lon]
666 if params[:user][:preferred_editor] == "default"
667 user.preferred_editor = nil
669 user.preferred_editor = params[:user][:preferred_editor]
672 user.openid_url = nil if params[:user][:openid_url].blank?
677 cookies.permanent["_osm_username"] = user.display_name
679 if user.new_email.blank?
680 flash.now[:notice] = t 'user.account.flash update success'
682 user.email = user.new_email
685 flash.now[:notice] = t 'user.account.flash update success confirm needed'
688 Notifier.email_confirm(user, user.tokens.create).deliver
690 # Ignore errors sending email
693 @user.errors.set(:new_email, @user.errors.get(:email))
694 @user.errors.set(:email, [])
703 # require that the user is a administrator, or fill out a helpful error message
704 # and return them to the user page.
705 def require_administrator
706 if @user and not @user.administrator?
707 flash[:error] = t('user.filter.not_an_administrator')
709 if params[:display_name]
710 redirect_to :controller => 'user', :action => 'view', :display_name => params[:display_name]
712 redirect_to :controller => 'user', :action => 'login', :referer => request.fullpath
715 redirect_to :controller => 'user', :action => 'login', :referer => request.fullpath
720 # ensure that there is a "this_user" instance variable
722 @this_user = User.find_by_display_name(params[:display_name])
723 rescue ActiveRecord::RecordNotFound
724 redirect_to :controller => 'user', :action => 'view', :display_name => params[:display_name] unless @this_user
728 # Choose the layout to use. See
729 # https://rails.lighthouseapp.com/projects/8994/tickets/5371-layout-with-onlyexcept-options-makes-other-actions-render-without-layouts
731 oauth_url = url_for(:controller => :oauth, :action => :authorize, :only_path => true)
733 if [ 'api_details' ].include? action_name
735 elsif params[:referer] and URI.parse(params[:referer]).path == oauth_url
744 def disable_terms_redirect
745 # this is necessary otherwise going to the user terms page, when
746 # having not agreed already would cause an infinite redirect loop.
747 # it's .now so that this doesn't propagate to other pages.
748 flash.now[:skip_terms] = true