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 => [:api_details, :api_gpx_files]
11 before_filter :check_database_writable, :only => [:login, :new, :account, :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 :update do |page|
27 page.replace_html "contributorTerms", :partial => "terms"
30 # The redirect from the OpenID provider reenters here
31 # again and we need to pass the parameters through to
32 # the open_id_authentication function
33 @user = session.delete(:new_user)
35 openid_verify(nil, @user) do |user|
38 if @user.openid_url.nil? or @user.invalid?
39 render :action => 'new'
41 render :action => 'terms'
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'
84 if Acl.address(request.remote_ip).where(:k => "no_account_creation").exists?
85 render :action => 'new'
86 elsif params[:decline]
88 @user.terms_seen = true
91 flash[:notice] = t 'user.new.terms declined', :url => t('user.new.terms declined url')
95 redirect_to params[:referer]
97 redirect_to :action => :account, :display_name => @user.display_name
100 redirect_to t('user.terms.declined')
103 if !@user.terms_agreed?
104 @user.consider_pd = params[:user][:consider_pd]
105 @user.terms_agreed = Time.now.getutc
106 @user.terms_seen = true
108 flash[:notice] = t 'user.new.terms accepted'
113 redirect_to params[:referer]
115 redirect_to :action => :account, :display_name => @user.display_name
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 @user.display_name = params[:user][:display_name]
147 @user.new_email = params[:user][:new_email]
149 if params[:user][:pass_crypt].length > 0 or params[:user][:pass_crypt_confirmation].length > 0
150 @user.pass_crypt = params[:user][:pass_crypt]
151 @user.pass_crypt_confirmation = params[:user][:pass_crypt_confirmation]
154 @user.description = params[:user][:description]
155 @user.languages = params[:user][:languages].split(",")
157 case params[:image_action]
158 when "new" then @user.image = params[:user][:image]
159 when "delete" then @user.image = nil
162 @user.home_lat = params[:user][:home_lat]
163 @user.home_lon = params[:user][:home_lon]
165 if params[:user][:preferred_editor] == "default"
166 @user.preferred_editor = nil
168 @user.preferred_editor = params[:user][:preferred_editor]
171 @user.openid_url = nil if params[:user][:openid_url].empty?
173 if params[:user][:openid_url].length > 0 and
174 params[:user][:openid_url] != @user.openid_url
175 # If the OpenID has changed, we want to check that it is a
176 # valid OpenID and one the user has control over before saving
177 # it as a password equivalent for the user.
178 session[:new_user] = @user
179 openid_verify(params[:user][:openid_url], @user)
184 # The redirect from the OpenID provider reenters here
185 # again and we need to pass the parameters through to
186 # the open_id_authentication function
187 @user = session.delete(:new_user)
188 openid_verify(nil, @user) do |user|
193 flash[:errors].each do |attr,msg|
194 attr = "new_email" if attr == "email" and !@user.new_email.nil?
195 @user.errors.add(attr,msg)
202 @user.data_public = true
204 flash[:notice] = t 'user.go_public.flash success'
205 redirect_to :controller => 'user', :action => 'account', :display_name => @user.display_name
209 @title = t 'user.lost_password.title'
211 if params[:user] and params[:user][:email]
212 user = User.visible.where(:email => params[:user][:email]).first
215 token = user.tokens.create
216 Notifier.lost_password(user, token).deliver
217 flash[:notice] = t 'user.lost_password.notice email on way'
218 redirect_to :action => 'login'
220 flash.now[:error] = t 'user.lost_password.notice email cannot find'
226 @title = t 'user.reset_password.title'
229 token = UserToken.find_by_token(params[:token])
235 @user.pass_crypt = params[:user][:pass_crypt]
236 @user.pass_crypt_confirmation = params[:user][:pass_crypt_confirmation]
237 @user.status = "active" if @user.status == "pending"
238 @user.email_valid = true
242 flash[:notice] = t 'user.reset_password.flash changed'
243 redirect_to :action => 'login'
247 flash[:error] = t 'user.reset_password.flash token bad'
248 redirect_to :action => 'lost_password'
254 @title = t 'user.new.title'
255 @referer = params[:referer] || session[:referer]
258 # The user is logged in already, so don't show them the signup
259 # page, instead send them to the home page
263 redirect_to :controller => 'site', :action => 'index'
265 elsif params.key?(:openid)
266 @user = User.new(:email => params[:email],
267 :email_confirmation => params[:email],
268 :display_name => params[:nickname],
269 :openid_url => params[:openid])
271 flash.now[:notice] = t 'user.new.openid association'
276 if params[:username] or using_open_id?
277 session[:remember_me] ||= params[:remember_me]
278 session[:referer] ||= params[:referer]
281 openid_authentication(params[:openid_url])
283 password_authentication(params[:username], params[:password])
285 elsif flash[:notice].nil?
286 flash.now[:notice] = t 'user.login.notice'
291 @title = t 'user.logout.title'
293 if params[:session] == request.session_options[:id]
295 token = UserToken.find_by_token(session[:token])
299 session.delete(:token)
301 session.delete(:user)
302 session_expires_automatically
304 redirect_to params[:referer]
306 redirect_to :controller => 'site', :action => 'index'
313 if token = UserToken.find_by_token(params[:confirm_string])
314 if token.user.active?
315 flash[:error] = t('user.confirm.already active')
316 redirect_to :action => 'login'
319 user.status = "active"
320 user.email_valid = true
322 referer = token.referer
326 token = UserToken.find_by_token(session[:token])
327 session.delete(:token)
332 if token.nil? or token.user != user
333 flash[:notice] = t('user.confirm.success')
334 redirect_to :action => :login, :referer => referer
338 session[:user] = user.id
339 cookies.permanent["_osm_username"] = user.display_name
342 flash[:notice] = t('user.confirm.success') + "<br /><br />" + t('user.confirm.before you start')
343 redirect_to :action => :account, :display_name => user.display_name
345 flash[:notice] = t('user.confirm.success')
351 user = User.find_by_display_name(params[:display_name])
353 if user and user.active?
354 flash[:error] = t('user.confirm.already active')
356 flash[:error] = t('user.confirm.unknown token') + t('user.confirm.reconfirm', :reconfirm => url_for(:action => 'confirm_resend', :display_name => params[:display_name]))
358 flash[:error] = t('user.confirm.unknown token')
361 redirect_to :action => 'login'
367 if user = User.find_by_display_name(params[:display_name])
368 Notifier.signup_confirm(user, user.tokens.create).deliver
369 flash[:notice] = t 'user.confirm_resend.success', :email => user.email
371 flash[:notice] = t 'user.confirm_resend.failure', :name => params[:display_name]
374 redirect_to :action => 'login'
379 token = UserToken.find_by_token(params[:confirm_string])
380 if token and token.user.new_email?
382 @user.email = @user.new_email
383 @user.new_email = nil
384 @user.email_valid = true
386 flash[:notice] = t 'user.confirm_email.success'
388 flash[:errors] = @user.errors
391 session[:user] = @user.id
392 cookies.permanent["_osm_username"] = @user.display_name
393 redirect_to :action => 'account', :display_name => @user.display_name
395 flash[:error] = t 'user.confirm_email.failure'
396 redirect_to :action => 'account', :display_name => @user.display_name
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 @title = t 'user.no_such_user.title'
417 @not_found_user = params[:display_name]
418 render :action => 'no_such_user', :status => :not_found
423 if params[:display_name]
424 name = params[:display_name]
425 new_friend = User.active.where(:display_name => name).first
427 friend.user_id = @user.id
428 friend.friend_user_id = new_friend.id
429 unless @user.is_friends_with?(new_friend)
431 flash[:notice] = t 'user.make_friend.success', :name => name
432 Notifier.friend_notification(friend).deliver
434 friend.add_error(t('user.make_friend.failed', :name => name))
437 flash[:warning] = t 'user.make_friend.already_a_friend', :name => name
441 redirect_to params[:referer]
443 redirect_to :controller => 'user', :action => 'view'
449 if params[:display_name]
450 name = params[:display_name]
451 friend = User.active.where(:display_name => name).first
452 if @user.is_friends_with?(friend)
453 Friend.delete_all "user_id = #{@user.id} AND friend_user_id = #{friend.id}"
454 flash[:notice] = t 'user.remove_friend.success', :name => friend.display_name
456 flash[:error] = t 'user.remove_friend.not_a_friend', :name => friend.display_name
460 redirect_to params[:referer]
462 redirect_to :controller => 'user', :action => 'view'
468 # sets a user's status
470 @this_user.update_attributes(:status => params[:status])
471 redirect_to :controller => 'user', :action => 'view', :display_name => params[:display_name]
475 # delete a user, marking them as deleted and removing personal data
478 redirect_to :controller => 'user', :action => 'view', :display_name => params[:display_name]
482 # display a list of users matching specified criteria
485 ids = params[:user].keys.collect { |id| id.to_i }
487 User.update_all("status = 'confirmed'", :id => ids) if params[:confirm]
488 User.update_all("status = 'deleted'", :id => ids) if params[:hide]
490 redirect_to url_for(:status => params[:status], :ip => params[:ip], :page => params[:page])
492 conditions = Hash.new
493 conditions[:status] = params[:status] if params[:status]
494 conditions[:creation_ip] = params[:ip] if params[:ip]
496 @user_pages, @users = paginate(:users,
497 :conditions => conditions,
506 # handle password authentication
507 def password_authentication(username, password)
508 if user = User.authenticate(:username => username, :password => password)
509 successful_login(user)
510 elsif user = User.authenticate(:username => username, :password => password, :pending => true)
511 failed_login t('user.login.account not active', :reconfirm => url_for(:action => 'confirm_resend', :display_name => user.display_name))
512 elsif User.authenticate(:username => username, :password => password, :suspended => true)
513 webmaster = link_to t('user.login.webmaster'), "mailto:webmaster@openstreetmap.org"
514 failed_login t('user.login.account suspended', :webmaster => webmaster)
516 failed_login t('user.login.auth failure')
521 # handle OpenID authentication
522 def openid_authentication(openid_url)
523 # If we don't appear to have a user for this URL then ask the
524 # provider for some extra information to help with signup
525 if openid_url and User.find_by_openid_url(openid_url)
528 required = [:nickname, :email, "http://axschema.org/namePerson/friendly", "http://axschema.org/contact/email"]
531 # Start the authentication
532 authenticate_with_open_id(openid_expand_url(openid_url), :method => :get, :required => required) do |result, identity_url, sreg, ax|
533 if result.successful?
534 # We need to use the openid url passed back from the OpenID provider
535 # rather than the one supplied by the user, as these can be different.
537 # For example, you can simply enter yahoo.com in the login box rather
538 # than a user specific url. Only once it comes back from the provider
539 # provider do we know the unique address for the user.
540 if user = User.find_by_openid_url(identity_url)
543 failed_login t('user.login.account not active')
544 when "active", "confirmed" then
545 successful_login(user)
546 when "suspended" then
547 webmaster = link_to t('user.login.webmaster'), "mailto:webmaster@openstreetmap.org"
548 failed_login t('user.login.account suspended', :webmaster => webmaster)
550 failed_login t('user.login.auth failure')
553 # Guard against not getting any extension data
554 sreg = Hash.new if sreg.nil?
555 ax = Hash.new if ax.nil?
557 # We don't have a user registered to this OpenID, so redirect
558 # to the create account page with username and email filled
559 # in if they have been given by the OpenID provider through
560 # the simple registration protocol.
561 nickname = sreg["nickname"] || ax["http://axschema.org/namePerson/friendly"]
562 email = sreg["email"] || ax["http://axschema.org/contact/email"]
563 redirect_to :controller => 'user', :action => 'new', :nickname => nickname, :email => email, :openid => identity_url
565 elsif result.missing?
566 failed_login t('user.login.openid missing provider')
567 elsif result.invalid?
568 failed_login t('user.login.openid invalid')
570 failed_login t('user.login.auth failure')
576 # verify an OpenID URL
577 def openid_verify(openid_url, user)
578 user.openid_url = openid_url
580 authenticate_with_open_id(openid_expand_url(openid_url), :method => :get) do |result, identity_url|
581 if result.successful?
582 # We need to use the openid url passed back from the OpenID provider
583 # rather than the one supplied by the user, as these can be different.
585 # For example, you can simply enter yahoo.com in the login box rather
586 # than a user specific url. Only once it comes back from the provider
587 # provider do we know the unique address for the user.
588 user.openid_url = identity_url
590 elsif result.missing?
591 flash.now[:error] = t 'user.login.openid missing provider'
592 elsif result.invalid?
593 flash.now[:error] = t 'user.login.openid invalid'
595 flash.now[:error] = t 'user.login.auth failure'
601 # special case some common OpenID providers by applying heuristics to
602 # try and come up with the correct URL based on what the user entered
603 def openid_expand_url(openid_url)
606 elsif openid_url.match(/(.*)gmail.com(\/?)$/) or openid_url.match(/(.*)googlemail.com(\/?)$/)
607 # Special case gmail.com as it is potentially a popular OpenID
608 # provider and, unlike yahoo.com, where it works automatically, Google
609 # have hidden their OpenID endpoint somewhere obscure this making it
610 # somewhat less user friendly.
611 return 'https://www.google.com/accounts/o8/id'
618 # process a successful login
619 def successful_login(user)
620 cookies.permanent["_osm_username"] = user.display_name
622 session[:user] = user.id
623 session_expires_after 1.month if session[:remember_me]
625 target = session[:referer] || url_for(:controller => :site, :action => :index)
627 # The user is logged in, so decide where to send them:
629 # - If they haven't seen the contributor terms, send them there.
630 # - If they have a block on them, show them that.
631 # - If they were referred to the login, send them back there.
632 # - Otherwise, send them to the home page.
633 if REQUIRE_TERMS_SEEN and not user.terms_seen
634 redirect_to :controller => :user, :action => :terms, :referer => target
635 elsif user.blocked_on_view
636 redirect_to user.blocked_on_view, :referer => target
641 session.delete(:remember_me)
642 session.delete(:referer)
646 # process a failed login
647 def failed_login(message)
648 flash[:error] = message
650 redirect_to :action => 'login', :referer => session[:referer]
652 session.delete(:remember_me)
653 session.delete(:referer)
657 # update a user's details
658 def update_user(user)
662 if user.new_email.nil? or user.new_email.empty?
663 flash.now[:notice] = t 'user.account.flash update success'
665 flash.now[:notice] = t 'user.account.flash update success confirm needed'
668 Notifier.email_confirm(user, user.tokens.create).deliver
670 # Ignore errors sending email
677 # require that the user is a administrator, or fill out a helpful error message
678 # and return them to the user page.
679 def require_administrator
680 if @user and not @user.administrator?
681 flash[:error] = t('user.filter.not_an_administrator')
683 if params[:display_name]
684 redirect_to :controller => 'user', :action => 'view', :display_name => params[:display_name]
686 redirect_to :controller => 'user', :action => 'login', :referer => request.fullpath
689 redirect_to :controller => 'user', :action => 'login', :referer => request.fullpath
694 # ensure that there is a "this_user" instance variable
696 @this_user = User.find_by_display_name(params[:display_name])
697 rescue ActiveRecord::RecordNotFound
698 redirect_to :controller => 'user', :action => 'view', :display_name => params[:display_name] unless @this_user
702 # Choose the layout to use. See
703 # https://rails.lighthouseapp.com/projects/8994/tickets/5371-layout-with-onlyexcept-options-makes-other-actions-render-without-layouts
705 oauth_url = url_for(:controller => :oauth, :action => :oauthorize, :only_path => true)
707 if [ 'api_details' ].include? action_name
709 elsif params[:referer] and URI.parse(params[:referer]).path == oauth_url
718 def disable_terms_redirect
719 # this is necessary otherwise going to the user terms page, when
720 # having not agreed already would cause an infinite redirect loop.
721 # it's .now so that this doesn't propagate to other pages.
722 flash.now[:skip_terms] = true