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 filter_parameter_logging :password, :pass_crypt, :pass_crypt_confirmation
20 cache_sweeper :user_sweeper, :only => [:account, :set_status, :delete]
23 @legale = params[:legale] || OSM.IPToCountry(request.remote_ip) || DEFAULT_LEGALE
24 @text = OSM.legal_text_for_country(@legale)
27 render :update do |page|
28 page.replace_html "contributorTerms", :partial => "terms"
31 # The redirect from the OpenID provider reenters here
32 # again and we need to pass the parameters through to
33 # the open_id_authentication function
34 @user = session.delete(:new_user)
36 openid_verify(nil, @user) do |user|
39 if @user.openid_url.nil? or @user.invalid?
40 render :action => 'new'
42 render :action => 'terms'
45 session[:referer] = params[:referer]
47 @title = t 'user.terms.title'
48 @user = User.new(params[:user]) if params[:user]
50 if params[:user] and params[:user][:openid_url] and @user.pass_crypt.empty?
51 # We are creating an account with OpenID and no password
52 # was specified so create a random one
53 @user.pass_crypt = ActiveSupport::SecureRandom.base64(16)
54 @user.pass_crypt_confirmation = @user.pass_crypt
60 # Something is wrong with a new user, so rerender the form
61 render :action => :new
63 # Error in existing user, so go to account settings
64 flash[:errors] = @user.errors
65 redirect_to :action => :account, :display_name => @user.display_name
67 elsif @user.terms_agreed?
68 # Already agreed to terms, so just show settings
69 redirect_to :action => :account, :display_name => @user.display_name
70 elsif params[:user] and params[:user][:openid_url] and not params[:user][:openid_url].empty?
71 # Verify OpenID before moving on
72 session[:new_user] = @user
73 openid_verify(params[:user][:openid_url], @user)
76 # Not logged in, so redirect to the login page
77 redirect_to :action => :login, :referer => request.request_uri
83 @title = t 'user.new.title'
85 if Acl.find_by_address(request.remote_ip, :conditions => {:k => "no_account_creation"})
86 render :action => 'new'
87 elsif params[:decline]
89 @user.terms_seen = true
92 flash[:notice] = t 'user.new.terms declined', :url => t('user.new.terms declined url')
96 redirect_to params[:referer]
98 redirect_to :action => :account, :display_name => @user.display_name
101 redirect_to t('user.terms.declined')
104 if !@user.terms_agreed?
105 @user.consider_pd = params[:user][:consider_pd]
106 @user.terms_agreed = Time.now.getutc
107 @user.terms_seen = true
109 flash[:notice] = t 'user.new.terms accepted'
114 redirect_to params[:referer]
116 redirect_to :action => :account, :display_name => @user.display_name
119 @user = User.new(params[:user])
121 @user.status = "pending"
122 @user.data_public = true
123 @user.description = "" if @user.description.nil?
124 @user.creation_ip = request.remote_ip
125 @user.languages = request.user_preferred_languages
126 @user.terms_agreed = Time.now.getutc
127 @user.terms_seen = true
128 @user.openid_url = nil if @user.openid_url and @user.openid_url.empty?
131 flash[:piwik_goal] = PIWIK_SIGNUP_GOAL if defined?(PIWIK_SIGNUP_GOAL)
132 flash[:notice] = t 'user.new.flash create success message', :email => @user.email
133 Notifier.signup_confirm(@user, @user.tokens.create(:referer => session.delete(:referer))).deliver
134 session[:token] = @user.tokens.create.token
135 redirect_to :action => 'login', :referer => params[:referer]
137 render :action => 'new', :referer => params[:referer]
143 @title = t 'user.account.title'
144 @tokens = @user.oauth_tokens.find :all, :conditions => 'oauth_tokens.invalidated_at is null and oauth_tokens.authorized_at is not null'
146 if params[:user] and params[:user][:display_name] and params[:user][:description]
147 @user.display_name = params[:user][:display_name]
148 @user.new_email = params[:user][:new_email]
150 if params[:user][:pass_crypt].length > 0 or params[:user][:pass_crypt_confirmation].length > 0
151 @user.pass_crypt = params[:user][:pass_crypt]
152 @user.pass_crypt_confirmation = params[:user][:pass_crypt_confirmation]
155 @user.description = params[:user][:description]
156 @user.languages = params[:user][:languages].split(",")
158 case params[:image_action]
159 when "new" then @user.image = params[:user][:image]
160 when "delete" then @user.image = nil
163 @user.home_lat = params[:user][:home_lat]
164 @user.home_lon = params[:user][:home_lon]
166 if params[:user][:preferred_editor] == "default"
167 @user.preferred_editor = nil
169 @user.preferred_editor = params[:user][:preferred_editor]
172 @user.openid_url = nil if params[:user][:openid_url].empty?
174 if params[:user][:openid_url].length > 0 and
175 params[:user][:openid_url] != @user.openid_url
176 # If the OpenID has changed, we want to check that it is a
177 # valid OpenID and one the user has control over before saving
178 # it as a password equivalent for the user.
179 session[:new_user] = @user
180 openid_verify(params[:user][:openid_url], @user)
185 # The redirect from the OpenID provider reenters here
186 # again and we need to pass the parameters through to
187 # the open_id_authentication function
188 @user = session.delete(:new_user)
189 openid_verify(nil, @user) do |user|
194 flash[:errors].each do |attr,msg|
195 attr = "new_email" if attr == "email" and !@user.new_email.nil?
196 @user.errors.add(attr,msg)
203 @user.data_public = true
205 flash[:notice] = t 'user.go_public.flash success'
206 redirect_to :controller => 'user', :action => 'account', :display_name => @user.display_name
210 @title = t 'user.lost_password.title'
212 if params[:user] and params[:user][:email]
213 user = User.find_by_email(params[:user][:email], :conditions => {:status => ["pending", "active", "confirmed"]})
216 token = user.tokens.create
217 Notifier.lost_password(user, token).deliver
218 flash[:notice] = t 'user.lost_password.notice email on way'
219 redirect_to :action => 'login'
221 flash.now[:error] = t 'user.lost_password.notice email cannot find'
227 @title = t 'user.reset_password.title'
230 token = UserToken.find_by_token(params[:token])
236 @user.pass_crypt = params[:user][:pass_crypt]
237 @user.pass_crypt_confirmation = params[:user][:pass_crypt_confirmation]
238 @user.status = "active" if @user.status == "pending"
239 @user.email_valid = true
243 flash[:notice] = t 'user.reset_password.flash changed'
244 redirect_to :action => 'login'
248 flash[:error] = t 'user.reset_password.flash token bad'
249 redirect_to :action => 'lost_password'
255 @title = t 'user.new.title'
256 @referer = params[:referer] || session[:referer]
259 # The user is logged in already, so don't show them the signup
260 # page, instead send them to the home page
261 redirect_to :controller => 'site', :action => 'index'
262 elsif not params['openid'].nil?
263 flash.now[:notice] = t 'user.new.openid association'
268 if params[:username] or using_open_id?
269 session[:remember_me] ||= params[:remember_me]
270 session[:referer] ||= params[:referer]
273 openid_authentication(params[:openid_url])
275 password_authentication(params[:username], params[:password])
277 elsif flash[:notice].nil?
278 flash.now[:notice] = t 'user.login.notice'
283 @title = t 'user.logout.title'
285 if params[:session] == request.session_options[:id]
287 token = UserToken.find_by_token(session[:token])
291 session.delete(:token)
293 session.delete(:user)
294 session_expires_automatically
296 redirect_to params[:referer]
298 redirect_to :controller => 'site', :action => 'index'
305 if token = UserToken.find_by_token(params[:confirm_string])
306 if token.user.active?
307 flash[:error] = t('user.confirm.already active')
308 redirect_to :action => 'login'
311 user.status = "active"
312 user.email_valid = true
314 referer = token.referer
318 token = UserToken.find_by_token(session[:token])
319 session.delete(:token)
324 if token.nil? or token.user != user
325 flash[:notice] = t('user.confirm.success')
326 redirect_to :action => :login, :referer => referer
330 session[:user] = user.id
333 flash[:notice] = t('user.confirm.success') + "<br /><br />" + t('user.confirm.before you start')
334 redirect_to :action => :account, :display_name => user.display_name
336 flash[:notice] = t('user.confirm.success')
342 user = User.find_by_display_name(params[:display_name])
344 if user and user.active?
345 flash[:error] = t('user.confirm.already active')
347 flash[:error] = t('user.confirm.unknown token') + t('user.confirm.reconfirm', :reconfirm => url_for(:action => 'confirm_resend', :display_name => params[:display_name]))
349 flash[:error] = t('user.confirm.unknown token')
352 redirect_to :action => 'login'
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 redirect_to :action => 'account', :display_name => @user.display_name
385 flash[:error] = t 'user.confirm_email.failure'
386 redirect_to :action => 'account', :display_name => @user.display_name
392 doc = OSM::API.new.get_xml_doc
393 @user.traces.each do |trace|
394 doc.root << trace.to_xml_node() if trace.public? or trace.user == @user
396 render :text => doc.to_s, :content_type => "text/xml"
400 @this_user = User.find_by_display_name(params[:display_name])
403 (@this_user.visible? or (@user and @user.administrator?))
404 @title = @this_user.display_name
406 @title = t 'user.no_such_user.title'
407 @not_found_user = params[:display_name]
408 render :action => 'no_such_user', :status => :not_found
413 if params[:display_name]
414 name = params[:display_name]
415 new_friend = User.find_by_display_name(name, :conditions => {:status => ["active", "confirmed"]})
417 friend.user_id = @user.id
418 friend.friend_user_id = new_friend.id
419 unless @user.is_friends_with?(new_friend)
421 flash[:notice] = t 'user.make_friend.success', :name => name
422 Notifier.friend_notification(friend).deliver
424 friend.add_error(t('user.make_friend.failed', :name => name))
427 flash[:warning] = t 'user.make_friend.already_a_friend', :name => name
431 redirect_to params[:referer]
433 redirect_to :controller => 'user', :action => 'view'
439 if params[:display_name]
440 name = params[:display_name]
441 friend = User.find_by_display_name(name, :conditions => {:status => ["active", "confirmed"]})
442 if @user.is_friends_with?(friend)
443 Friend.delete_all "user_id = #{@user.id} AND friend_user_id = #{friend.id}"
444 flash[:notice] = t 'user.remove_friend.success', :name => friend.display_name
446 flash[:error] = t 'user.remove_friend.not_a_friend', :name => friend.display_name
450 redirect_to params[:referer]
452 redirect_to :controller => 'user', :action => 'view'
458 # sets a user's status
460 @this_user.update_attributes(:status => params[:status])
461 redirect_to :controller => 'user', :action => 'view', :display_name => params[:display_name]
465 # delete a user, marking them as deleted and removing personal data
468 redirect_to :controller => 'user', :action => 'view', :display_name => params[:display_name]
472 # display a list of users matching specified criteria
475 ids = params[:user].keys.collect { |id| id.to_i }
477 User.update_all("status = 'confirmed'", :id => ids) if params[:confirm]
478 User.update_all("status = 'deleted'", :id => ids) if params[:hide]
480 redirect_to url_for(:status => params[:status], :ip => params[:ip], :page => params[:page])
482 conditions = Hash.new
483 conditions[:status] = params[:status] if params[:status]
484 conditions[:creation_ip] = params[:ip] if params[:ip]
486 @user_pages, @users = paginate(:users,
487 :conditions => conditions,
496 # handle password authentication
497 def password_authentication(username, password)
498 if user = User.authenticate(:username => username, :password => password)
499 successful_login(user)
500 elsif user = User.authenticate(:username => username, :password => password, :pending => true)
501 failed_login t('user.login.account not active', :reconfirm => url_for(:action => 'confirm_resend', :display_name => user.display_name))
502 elsif User.authenticate(:username => username, :password => password, :suspended => true)
503 webmaster = link_to t('user.login.webmaster'), "mailto:webmaster@openstreetmap.org"
504 failed_login t('user.login.account suspended', :webmaster => webmaster)
506 failed_login t('user.login.auth failure')
511 # handle OpenID authentication
512 def openid_authentication(openid_url)
513 # If we don't appear to have a user for this URL then ask the
514 # provider for some extra information to help with signup
515 if openid_url and User.find_by_openid_url(openid_url)
518 required = [:nickname, :email, "http://axschema.org/namePerson/friendly", "http://axschema.org/contact/email"]
521 # Start the authentication
522 authenticate_with_open_id(openid_expand_url(openid_url), :required => required) do |result, identity_url, sreg, ax|
523 if result.successful?
524 # We need to use the openid url passed back from the OpenID provider
525 # rather than the one supplied by the user, as these can be different.
527 # For example, you can simply enter yahoo.com in the login box rather
528 # than a user specific url. Only once it comes back from the provider
529 # provider do we know the unique address for the user.
530 if user = User.find_by_openid_url(identity_url)
533 failed_login t('user.login.account not active')
534 when "active", "confirmed" then
535 successful_login(user)
536 when "suspended" then
537 webmaster = link_to t('user.login.webmaster'), "mailto:webmaster@openstreetmap.org"
538 failed_login t('user.login.account suspended', :webmaster => webmaster)
540 failed_login t('user.login.auth failure')
543 # Guard against not getting any extension data
544 sreg = Hash.new if sreg.nil?
545 ax = Hash.new if ax.nil?
547 # We don't have a user registered to this OpenID, so redirect
548 # to the create account page with username and email filled
549 # in if they have been given by the OpenID provider through
550 # the simple registration protocol.
551 nickname = sreg["nickname"] || ax["http://axschema.org/namePerson/friendly"]
552 email = sreg["email"] || ax["http://axschema.org/contact/email"]
553 redirect_to :controller => 'user', :action => 'new', :nickname => nickname, :email => email, :openid => identity_url
555 elsif result.missing?
556 failed_login t('user.login.openid missing provider')
557 elsif result.invalid?
558 failed_login t('user.login.openid invalid')
560 failed_login t('user.login.auth failure')
566 # verify an OpenID URL
567 def openid_verify(openid_url, user)
568 user.openid_url = openid_url
570 authenticate_with_open_id(openid_expand_url(openid_url)) do |result, identity_url|
571 if result.successful?
572 # We need to use the openid url passed back from the OpenID provider
573 # rather than the one supplied by the user, as these can be different.
575 # For example, you can simply enter yahoo.com in the login box rather
576 # than a user specific url. Only once it comes back from the provider
577 # provider do we know the unique address for the user.
578 user.openid_url = identity_url
580 elsif result.missing?
581 flash.now[:error] = t 'user.login.openid missing provider'
582 elsif result.invalid?
583 flash.now[:error] = t 'user.login.openid invalid'
585 flash.now[:error] = t 'user.login.auth failure'
591 # special case some common OpenID providers by applying heuristics to
592 # try and come up with the correct URL based on what the user entered
593 def openid_expand_url(openid_url)
596 elsif openid_url.match(/(.*)gmail.com(\/?)$/) or openid_url.match(/(.*)googlemail.com(\/?)$/)
597 # Special case gmail.com as it is potentially a popular OpenID
598 # provider and, unlike yahoo.com, where it works automatically, Google
599 # have hidden their OpenID endpoint somewhere obscure this making it
600 # somewhat less user friendly.
601 return 'https://www.google.com/accounts/o8/id'
608 # process a successful login
609 def successful_login(user)
610 session[:user] = user.id
611 session_expires_after 1.month if session[:remember_me]
613 target = session[:referer] || url_for(:controller => :site, :action => :index)
615 # The user is logged in, so decide where to send them:
617 # - If they haven't seen the contributor terms, send them there.
618 # - If they have a block on them, show them that.
619 # - If they were referred to the login, send them back there.
620 # - Otherwise, send them to the home page.
621 if REQUIRE_TERMS_SEEN and not user.terms_seen
622 redirect_to :controller => :user, :action => :terms, :referer => target
623 elsif user.blocked_on_view
624 redirect_to user.blocked_on_view, :referer => target
629 session.delete(:remember_me)
630 session.delete(:referer)
634 # process a failed login
635 def failed_login(message)
636 flash[:error] = message
638 redirect_to :action => 'login', :referer => session[:referer]
640 session.delete(:remember_me)
641 session.delete(:referer)
645 # update a user's details
646 def update_user(user)
650 if user.new_email.nil? or user.new_email.empty?
651 flash.now[:notice] = t 'user.account.flash update success'
653 flash.now[:notice] = t 'user.account.flash update success confirm needed'
656 Notifier.email_confirm(user, user.tokens.create).deliver
658 # Ignore errors sending email
665 # require that the user is a administrator, or fill out a helpful error message
666 # and return them to the user page.
667 def require_administrator
668 if @user and not @user.administrator?
669 flash[:error] = t('user.filter.not_an_administrator')
671 if params[:display_name]
672 redirect_to :controller => 'user', :action => 'view', :display_name => params[:display_name]
674 redirect_to :controller => 'user', :action => 'login', :referer => request.request_uri
677 redirect_to :controller => 'user', :action => 'login', :referer => request.request_uri
682 # ensure that there is a "this_user" instance variable
684 @this_user = User.find_by_display_name(params[:display_name])
685 rescue ActiveRecord::RecordNotFound
686 redirect_to :controller => 'user', :action => 'view', :display_name => params[:display_name] unless @this_user
690 # Choose the layout to use. See
691 # https://rails.lighthouseapp.com/projects/8994/tickets/5371-layout-with-onlyexcept-options-makes-other-actions-render-without-layouts
693 oauth_url = url_for(:controller => :oauth, :action => :oauthorize, :only_path => true)
695 if [ 'api_details' ].include? action_name
697 elsif params[:referer] and URI.parse(params[:referer]).path == oauth_url
706 def disable_terms_redirect
707 # this is necessary otherwise going to the user terms page, when
708 # having not agreed already would cause an infinite redirect loop.
709 # it's .now so that this doesn't propagate to other pages.
710 flash.now[:skip_terms] = true