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 :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'
42 session[:referer] = params[:referer]
44 @title = t 'user.terms.title'
45 @user = User.new(params[:user]) if params[:user]
47 if params[:user] and params[:user][:openid_url] and @user.pass_crypt.empty?
48 # We are creating an account with OpenID and no password
49 # was specified so create a random one
50 @user.pass_crypt = SecureRandom.base64(16)
51 @user.pass_crypt_confirmation = @user.pass_crypt
57 # Something is wrong with a new user, so rerender the form
58 render :action => :new
60 # Error in existing user, so go to account settings
61 flash[:errors] = @user.errors
62 redirect_to :action => :account, :display_name => @user.display_name
64 elsif @user.terms_agreed?
65 # Already agreed to terms, so just show settings
66 redirect_to :action => :account, :display_name => @user.display_name
67 elsif params[:user] and params[:user][:openid_url] and not params[:user][:openid_url].empty?
68 # Verify OpenID before moving on
69 session[:new_user] = @user
70 openid_verify(params[:user][:openid_url], @user)
73 # Not logged in, so redirect to the login page
74 redirect_to :action => :login, :referer => request.fullpath
80 @title = t 'user.new.title'
82 if Acl.address(request.remote_ip).where(:k => "no_account_creation").exists?
83 render :action => 'new'
84 elsif params[:decline]
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
116 @user = User.new(params[:user])
118 @user.status = "pending"
119 @user.data_public = true
120 @user.description = "" if @user.description.nil?
121 @user.creation_ip = request.remote_ip
122 @user.languages = request.user_preferred_languages
123 @user.terms_agreed = Time.now.getutc
124 @user.terms_seen = true
125 @user.openid_url = nil if @user.openid_url and @user.openid_url.empty?
128 flash[:piwik_goal] = PIWIK_SIGNUP_GOAL if defined?(PIWIK_SIGNUP_GOAL)
129 flash[:notice] = t 'user.new.flash create success message', :email => @user.email
130 Notifier.signup_confirm(@user, @user.tokens.create(:referer => session.delete(:referer))).deliver
131 session[:token] = @user.tokens.create.token
132 redirect_to :action => 'login', :referer => params[:referer]
134 render :action => 'new', :referer => params[:referer]
140 @title = t 'user.account.title'
141 @tokens = @user.oauth_tokens.authorized
143 if params[:user] and params[:user][:display_name] and params[:user][:description]
144 @user.display_name = params[:user][:display_name]
145 @user.new_email = params[:user][:new_email]
147 if params[:user][:pass_crypt].length > 0 or params[:user][:pass_crypt_confirmation].length > 0
148 @user.pass_crypt = params[:user][:pass_crypt]
149 @user.pass_crypt_confirmation = params[:user][:pass_crypt_confirmation]
152 @user.description = params[:user][:description]
153 @user.languages = params[:user][:languages].split(",")
155 case params[:image_action]
156 when "new" then @user.image = params[:user][:image]
157 when "delete" then @user.image = nil
160 @user.home_lat = params[:user][:home_lat]
161 @user.home_lon = params[:user][:home_lon]
163 if params[:user][:preferred_editor] == "default"
164 @user.preferred_editor = nil
166 @user.preferred_editor = params[:user][:preferred_editor]
169 @user.openid_url = nil if params[:user][:openid_url].blank?
171 if params[:user][:openid_url] and
172 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|
194 @user.data_public = true
196 flash[:notice] = t 'user.go_public.flash success'
197 redirect_to :controller => 'user', :action => 'account', :display_name => @user.display_name
201 @title = t 'user.lost_password.title'
203 if params[:user] and params[:user][:email]
204 user = User.visible.find_by_email(params[:user][:email])
207 users = User.visible.where("LOWER(email) = LOWER(?)", params[:user][:email])
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 params[:notice]
286 flash.now[:notice] = t "user.login.notice_#{params[:notice]}"
287 elsif flash[:notice].nil?
288 flash.now[:notice] = t 'user.login.notice'
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 if token = UserToken.find_by_token(params[:confirm_string])
316 if token.user.active?
317 flash[:error] = t('user.confirm.already active')
318 redirect_to :action => 'login'
321 user.status = "active"
322 user.email_valid = true
324 referer = token.referer
328 token = UserToken.find_by_token(session[:token])
329 session.delete(:token)
334 if token.nil? or token.user != user
335 flash[:notice] = t('user.confirm.success')
336 redirect_to :action => :login, :referer => referer
340 session[:user] = user.id
341 cookies.permanent["_osm_username"] = user.display_name
344 flash[:notice] = t('user.confirm.success') + "<br /><br />" + t('user.confirm.before you start')
345 redirect_to :action => :account, :display_name => user.display_name
347 flash[:notice] = t('user.confirm.success')
353 user = User.find_by_display_name(params[:display_name])
355 if user and user.active?
356 flash[:error] = t('user.confirm.already active')
358 flash[:error] = t('user.confirm.unknown token') + t('user.confirm.reconfirm', :reconfirm => url_for(:action => 'confirm_resend', :display_name => params[:display_name]))
360 flash[:error] = t('user.confirm.unknown token')
363 redirect_to :action => 'login'
369 if user = User.find_by_display_name(params[:display_name])
370 Notifier.signup_confirm(user, user.tokens.create).deliver
371 flash[:notice] = t 'user.confirm_resend.success', :email => user.email
373 flash[:notice] = t 'user.confirm_resend.failure', :name => params[:display_name]
376 redirect_to :action => 'login'
381 token = UserToken.find_by_token(params[:confirm_string])
382 if token and token.user.new_email?
384 @user.email = @user.new_email
385 @user.new_email = nil
386 @user.email_valid = true
388 flash[:notice] = t 'user.confirm_email.success'
390 flash[:errors] = @user.errors
393 session[:user] = @user.id
394 cookies.permanent["_osm_username"] = @user.display_name
395 redirect_to :action => 'account', :display_name => @user.display_name
397 flash[:error] = t 'user.confirm_email.failure'
398 redirect_to :action => 'account', :display_name => @user.display_name
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 @title = t 'user.no_such_user.title'
419 @not_found_user = params[:display_name]
420 render :action => 'no_such_user', :status => :not_found
425 if params[:display_name]
426 name = params[:display_name]
427 new_friend = User.active.where(:display_name => name).first
429 friend.user_id = @user.id
430 friend.friend_user_id = new_friend.id
431 unless @user.is_friends_with?(new_friend)
433 flash[:notice] = t 'user.make_friend.success', :name => name
434 Notifier.friend_notification(friend).deliver
436 friend.add_error(t('user.make_friend.failed', :name => name))
439 flash[:warning] = t 'user.make_friend.already_a_friend', :name => name
443 redirect_to params[:referer]
445 redirect_to :controller => 'user', :action => 'view'
451 if params[:display_name]
452 name = params[:display_name]
453 friend = User.active.where(:display_name => name).first
454 if @user.is_friends_with?(friend)
455 Friend.delete_all "user_id = #{@user.id} AND friend_user_id = #{friend.id}"
456 flash[:notice] = t 'user.remove_friend.success', :name => friend.display_name
458 flash[:error] = t 'user.remove_friend.not_a_friend', :name => friend.display_name
462 redirect_to params[:referer]
464 redirect_to :controller => 'user', :action => 'view'
470 # sets a user's status
472 @this_user.update_attributes(:status => params[:status])
473 redirect_to :controller => 'user', :action => 'view', :display_name => params[:display_name]
477 # delete a user, marking them as deleted and removing personal data
480 redirect_to :controller => 'user', :action => 'view', :display_name => params[:display_name]
484 # display a list of users matching specified criteria
487 ids = params[:user].keys.collect { |id| id.to_i }
489 User.update_all("status = 'confirmed'", :id => ids) if params[:confirm]
490 User.update_all("status = 'deleted'", :id => ids) if params[:hide]
492 redirect_to url_for(:status => params[:status], :ip => params[:ip], :page => params[:page])
494 conditions = Hash.new
495 conditions[:status] = params[:status] if params[:status]
496 conditions[:creation_ip] = params[:ip] if params[:ip]
498 @user_pages, @users = paginate(:users,
499 :conditions => conditions,
508 # handle password authentication
509 def password_authentication(username, password)
510 if user = User.authenticate(:username => username, :password => password)
511 successful_login(user)
512 elsif user = User.authenticate(:username => username, :password => password, :pending => true)
513 failed_login t('user.login.account not active', :reconfirm => url_for(:action => 'confirm_resend', :display_name => user.display_name))
514 elsif User.authenticate(:username => username, :password => password, :suspended => true)
515 webmaster = link_to t('user.login.webmaster'), "mailto:webmaster@openstreetmap.org"
516 failed_login t('user.login.account suspended', :webmaster => webmaster)
518 failed_login t('user.login.auth failure')
523 # handle OpenID authentication
524 def openid_authentication(openid_url)
525 # If we don't appear to have a user for this URL then ask the
526 # provider for some extra information to help with signup
527 if openid_url and User.find_by_openid_url(openid_url)
530 required = [:nickname, :email, "http://axschema.org/namePerson/friendly", "http://axschema.org/contact/email"]
533 # Start the authentication
534 authenticate_with_open_id(openid_expand_url(openid_url), :method => :get, :required => required) do |result, identity_url, sreg, ax|
535 if result.successful?
536 # We need to use the openid url passed back from the OpenID provider
537 # rather than the one supplied by the user, as these can be different.
539 # For example, you can simply enter yahoo.com in the login box rather
540 # than a user specific url. Only once it comes back from the provider
541 # provider do we know the unique address for the user.
542 if user = User.find_by_openid_url(identity_url)
545 failed_login t('user.login.account not active')
546 when "active", "confirmed" then
547 successful_login(user)
548 when "suspended" then
549 webmaster = link_to t('user.login.webmaster'), "mailto:webmaster@openstreetmap.org"
550 failed_login t('user.login.account suspended', :webmaster => webmaster)
552 failed_login t('user.login.auth failure')
555 # Guard against not getting any extension data
556 sreg = Hash.new if sreg.nil?
557 ax = Hash.new if ax.nil?
559 # We don't have a user registered to this OpenID, so redirect
560 # to the create account page with username and email filled
561 # in if they have been given by the OpenID provider through
562 # the simple registration protocol.
563 nickname = sreg["nickname"] || ax["http://axschema.org/namePerson/friendly"]
564 email = sreg["email"] || ax["http://axschema.org/contact/email"]
565 redirect_to :controller => 'user', :action => 'new', :nickname => nickname, :email => email, :openid => identity_url
567 elsif result.missing?
568 failed_login t('user.login.openid missing provider')
569 elsif result.invalid?
570 failed_login t('user.login.openid invalid')
572 failed_login t('user.login.auth failure')
578 # verify an OpenID URL
579 def openid_verify(openid_url, user)
580 user.openid_url = openid_url
582 authenticate_with_open_id(openid_expand_url(openid_url), :method => :get) do |result, identity_url|
583 if result.successful?
584 # We need to use the openid url passed back from the OpenID provider
585 # rather than the one supplied by the user, as these can be different.
587 # For example, you can simply enter yahoo.com in the login box rather
588 # than a user specific url. Only once it comes back from the provider
589 # provider do we know the unique address for the user.
590 user.openid_url = identity_url
592 elsif result.missing?
593 flash.now[:error] = t 'user.login.openid missing provider'
594 elsif result.invalid?
595 flash.now[:error] = t 'user.login.openid invalid'
597 flash.now[:error] = t 'user.login.auth failure'
603 # special case some common OpenID providers by applying heuristics to
604 # try and come up with the correct URL based on what the user entered
605 def openid_expand_url(openid_url)
608 elsif openid_url.match(/(.*)gmail.com(\/?)$/) or openid_url.match(/(.*)googlemail.com(\/?)$/)
609 # Special case gmail.com as it is potentially a popular OpenID
610 # provider and, unlike yahoo.com, where it works automatically, Google
611 # have hidden their OpenID endpoint somewhere obscure this making it
612 # somewhat less user friendly.
613 return 'https://www.google.com/accounts/o8/id'
620 # process a successful login
621 def successful_login(user)
622 cookies.permanent["_osm_username"] = user.display_name
624 session[:user] = user.id
625 session_expires_after 1.month if session[:remember_me]
627 target = session[:referer] || url_for(:controller => :site, :action => :index)
629 # The user is logged in, so decide where to send them:
631 # - If they haven't seen the contributor terms, send them there.
632 # - If they have a block on them, show them that.
633 # - If they were referred to the login, send them back there.
634 # - Otherwise, send them to the home page.
635 if REQUIRE_TERMS_SEEN and not user.terms_seen
636 redirect_to :controller => :user, :action => :terms, :referer => target
637 elsif user.blocked_on_view
638 redirect_to user.blocked_on_view, :referer => target
643 session.delete(:remember_me)
644 session.delete(:referer)
648 # process a failed login
649 def failed_login(message)
650 flash[:error] = message
652 redirect_to :action => 'login', :referer => session[:referer]
654 session.delete(:remember_me)
655 session.delete(:referer)
659 # update a user's details
660 def update_user(user)
664 if user.new_email.blank?
665 flash.now[:notice] = t 'user.account.flash update success'
667 user.email = user.new_email
670 flash.now[:notice] = t 'user.account.flash update success confirm needed'
673 Notifier.email_confirm(user, user.tokens.create).deliver
675 # Ignore errors sending email
678 @user.errors.set(:new_email, @user.errors.get(:email))
679 @user.errors.set(:email, [])
688 # require that the user is a administrator, or fill out a helpful error message
689 # and return them to the user page.
690 def require_administrator
691 if @user and not @user.administrator?
692 flash[:error] = t('user.filter.not_an_administrator')
694 if params[:display_name]
695 redirect_to :controller => 'user', :action => 'view', :display_name => params[:display_name]
697 redirect_to :controller => 'user', :action => 'login', :referer => request.fullpath
700 redirect_to :controller => 'user', :action => 'login', :referer => request.fullpath
705 # ensure that there is a "this_user" instance variable
707 @this_user = User.find_by_display_name(params[:display_name])
708 rescue ActiveRecord::RecordNotFound
709 redirect_to :controller => 'user', :action => 'view', :display_name => params[:display_name] unless @this_user
713 # Choose the layout to use. See
714 # https://rails.lighthouseapp.com/projects/8994/tickets/5371-layout-with-onlyexcept-options-makes-other-actions-render-without-layouts
716 oauth_url = url_for(:controller => :oauth, :action => :oauthorize, :only_path => true)
718 if [ 'api_details' ].include? action_name
720 elsif params[:referer] and URI.parse(params[:referer]).path == oauth_url
729 def disable_terms_redirect
730 # this is necessary otherwise going to the user terms page, when
731 # having not agreed already would cause an infinite redirect loop.
732 # it's .now so that this doesn't propagate to other pages.
733 flash.now[:skip_terms] = true