1 class UserController < ApplicationController
2 layout 'site', :except => :api_details
4 before_filter :authorize, :only => [:api_details, :api_gpx_files]
5 before_filter :authorize_web, :except => [:api_details, :api_gpx_files]
6 before_filter :set_locale, :except => [:api_details, :api_gpx_files]
7 before_filter :require_user, :only => [:account, :go_public, :make_friend, :remove_friend]
8 before_filter :check_database_readable, :except => [:api_details, :api_gpx_files]
9 before_filter :check_database_writable, :only => [:login, :new, :account, :go_public, :make_friend, :remove_friend]
10 before_filter :check_api_readable, :only => [:api_details, :api_gpx_files]
11 before_filter :require_allow_read_prefs, :only => [:api_details]
12 before_filter :require_allow_read_gpx, :only => [:api_gpx_files]
13 before_filter :require_cookies, :only => [:login, :confirm]
14 before_filter :require_administrator, :only => [:set_status, :delete, :list]
15 before_filter :lookup_this_user, :only => [:set_status, :delete]
17 filter_parameter_logging :password, :pass_crypt, :pass_crypt_confirmation
19 cache_sweeper :user_sweeper, :only => [:account, :set_status, :delete]
22 @title = t 'user.new.title'
23 @legale = params[:legale] || OSM.IPToCountry(request.remote_ip) || APP_CONFIG['default_legale']
24 @text = OSM.legal_text_for_country(@legale)
27 render :update do |page|
28 page.replace_html "contributorTerms", :partial => "terms"
30 elsif params[:open_id_complete]
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 @user = User.new(params[:user])
49 if 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 = ActiveSupport::SecureRandom.base64(16)
53 @user.pass_crypt_confirmation = @user.pass_crypt
57 if params[:user][:openid_url].nil? or
58 params[:user][:openid_url].empty?
59 # No OpenID so just move on to the terms
60 render :action => 'terms'
62 # Verify OpenID before moving on
63 session[:new_user] = @user
64 openid_verify(params[:user][:openid_url], @user)
67 # Something is wrong, so rerender the form
68 render :action => 'new'
74 @title = t 'user.new.title'
76 if Acl.find_by_address(request.remote_ip, :conditions => {:k => "no_account_creation"})
77 render :action => 'new'
78 elsif params[:decline]
79 redirect_to t('user.terms.declined')
81 @user = User.new(params[:user])
83 @user.status = "pending"
84 @user.data_public = true
85 @user.description = "" if @user.description.nil?
86 @user.creation_ip = request.remote_ip
87 @user.languages = request.user_preferred_languages
88 @user.terms_agreed = Time.now.getutc
91 flash[:notice] = t 'user.new.flash create success message'
92 Notifier.deliver_signup_confirm(@user, @user.tokens.create(:referer => session.delete(:referer)))
93 redirect_to :action => 'login'
95 render :action => 'new'
101 @title = t 'user.account.title'
102 @tokens = @user.oauth_tokens.find :all, :conditions => 'oauth_tokens.invalidated_at is null and oauth_tokens.authorized_at is not null'
104 if params[:open_id_complete]
105 # The redirect from the OpenID provider reenters here
106 # again and we need to pass the parameters through to
107 # the open_id_authentication function
108 @user = session.delete(:new_user)
109 openid_verify(nil, @user) do |user|
112 elsif params[:user] and params[:user][:display_name] and params[:user][:description]
113 @user.display_name = params[:user][:display_name]
114 @user.new_email = params[:user][:new_email]
116 if params[:user][:pass_crypt].length > 0 or params[:user][:pass_crypt_confirmation].length > 0
117 @user.pass_crypt = params[:user][:pass_crypt]
118 @user.pass_crypt_confirmation = params[:user][:pass_crypt_confirmation]
121 @user.description = params[:user][:description]
122 @user.languages = params[:user][:languages].split(",")
124 case params[:image_action]
125 when "new" then @user.image = params[:user][:image]
126 when "delete" then @user.image = nil
129 @user.home_lat = params[:user][:home_lat]
130 @user.home_lon = params[:user][:home_lon]
132 @user.openid_url = nil if params[:user][:openid_url].empty?
134 if params[:user][:openid_url].length > 0 and
135 params[:user][:openid_url] != @user.openid_url
136 # If the OpenID has changed, we want to check that it is a
137 # valid OpenID and one the user has control over before saving
138 # it as a password equivalent for the user.
139 session[:new_user] = @user
140 openid_verify(params[:user][:openid_url], @user)
148 @user.data_public = true
150 flash[:notice] = t 'user.go_public.flash success'
151 redirect_to :controller => 'user', :action => 'account', :display_name => @user.display_name
155 @title = t 'user.lost_password.title'
157 if params[:user] and params[:user][:email]
158 user = User.find_by_email(params[:user][:email], :conditions => {:status => ["pending", "active", "confirmed"]})
161 token = user.tokens.create
162 Notifier.deliver_lost_password(user, token)
163 flash[:notice] = t 'user.lost_password.notice email on way'
164 redirect_to :action => 'login'
166 flash.now[:error] = t 'user.lost_password.notice email cannot find'
172 @title = t 'user.reset_password.title'
175 token = UserToken.find_by_token(params[:token])
181 @user.pass_crypt = params[:user][:pass_crypt]
182 @user.pass_crypt_confirmation = params[:user][:pass_crypt_confirmation]
183 @user.status = "active" if @user.status == "pending"
184 @user.email_valid = true
188 flash[:notice] = t 'user.reset_password.flash changed'
189 redirect_to :action => 'login'
193 flash[:error] = t 'user.reset_password.flash token bad'
194 redirect_to :action => 'lost_password'
200 @title = t 'user.new.title'
201 @referer = params[:referer] || session[:referer]
204 # The user is logged in already, so don't show them the signup
205 # page, instead send them to the home page
206 redirect_to :controller => 'site', :action => 'index'
207 elsif not params['openid'].nil?
208 flash.now[:notice] = t 'user.new.openid association'
214 session[:remember_me] ||= params[:remember_me]
215 session[:referer] ||= params[:referer]
217 if using_open_id?(params[:openid_url])
218 openid_authentication(params[:openid_url])
220 password_authentication(params[:username], params[:password])
223 @title = t 'user.login.title'
228 @title = t 'user.logout.title'
230 if params[:session] == request.session_options[:id]
232 token = UserToken.find_by_token(session[:token])
236 session[:token] = nil
239 session_expires_automatically
241 redirect_to params[:referer]
243 redirect_to :controller => 'site', :action => 'index'
249 if params[:confirm_action]
250 token = UserToken.find_by_token(params[:confirm_string])
251 if token and !token.user.active?
253 @user.status = "active"
254 @user.email_valid = true
256 referer = token.referer
258 flash[:notice] = t 'user.confirm.success'
259 session[:user] = @user.id
263 redirect_to :action => 'account', :display_name => @user.display_name
266 flash.now[:error] = t 'user.confirm.failure'
272 if params[:confirm_action]
273 token = UserToken.find_by_token(params[:confirm_string])
274 if token and token.user.new_email?
276 @user.email = @user.new_email
277 @user.new_email = nil
278 @user.email_valid = true
280 flash[:notice] = t 'user.confirm_email.success'
282 flash[:errors] = @user.errors
285 session[:user] = @user.id
286 redirect_to :action => 'account', :display_name => @user.display_name
288 flash.now[:error] = t 'user.confirm_email.failure'
294 doc = OSM::API.new.get_xml_doc
295 @user.traces.each do |trace|
296 doc.root << trace.to_xml_node() if trace.public? or trace.user == @user
298 render :text => doc.to_s, :content_type => "text/xml"
302 @this_user = User.find_by_display_name(params[:display_name])
305 (@this_user.visible? or (@user and @user.administrator?))
306 @title = @this_user.display_name
308 @title = t 'user.no_such_user.title'
309 @not_found_user = params[:display_name]
310 render :action => 'no_such_user', :status => :not_found
315 if params[:display_name]
316 name = params[:display_name]
317 new_friend = User.find_by_display_name(name, :conditions => {:status => ["active", "confirmed"]})
319 friend.user_id = @user.id
320 friend.friend_user_id = new_friend.id
321 unless @user.is_friends_with?(new_friend)
323 flash[:notice] = t 'user.make_friend.success', :name => name
324 Notifier.deliver_friend_notification(friend)
326 friend.add_error(t('user.make_friend.failed', :name => name))
329 flash[:warning] = t 'user.make_friend.already_a_friend', :name => name
333 redirect_to params[:referer]
335 redirect_to :controller => 'user', :action => 'view'
341 if params[:display_name]
342 name = params[:display_name]
343 friend = User.find_by_display_name(name, :conditions => {:status => ["active", "confirmed"]})
344 if @user.is_friends_with?(friend)
345 Friend.delete_all "user_id = #{@user.id} AND friend_user_id = #{friend.id}"
346 flash[:notice] = t 'user.remove_friend.success', :name => friend.display_name
348 flash[:error] = t 'user.remove_friend.not_a_friend', :name => friend.display_name
352 redirect_to params[:referer]
354 redirect_to :controller => 'user', :action => 'view'
360 # sets a user's status
362 @this_user.update_attributes(:status => params[:status])
363 redirect_to :controller => 'user', :action => 'view', :display_name => params[:display_name]
367 # delete a user, marking them as deleted and removing personal data
370 redirect_to :controller => 'user', :action => 'view', :display_name => params[:display_name]
374 # display a list of users matching specified criteria
377 ids = params[:user].keys.collect { |id| id.to_i }
379 User.update_all("status = 'confirmed'", :id => ids) if params[:confirm]
380 User.update_all("status = 'deleted'", :id => ids) if params[:hide]
382 redirect_to url_for(:status => params[:status], :ip => params[:ip], :page => params[:page])
384 conditions = Hash.new
385 conditions[:status] = params[:status] if params[:status]
386 conditions[:creation_ip] = params[:ip] if params[:ip]
388 @user_pages, @users = paginate(:users,
389 :conditions => conditions,
398 # handle password authentication
399 def password_authentication(username, password)
400 if user = User.authenticate(:username => username, :password => password)
401 successful_login(user)
402 elsif User.authenticate(:username => username, :password => password, :pending => true)
403 failed_login t('user.login.account not active')
404 elsif User.authenticate(:username => username, :password => password, :suspended => true)
405 failed_login t('user.login.account suspended')
407 failed_login t('user.login.auth failure')
412 # handle OpenID authentication
413 def openid_authentication(openid_url)
414 # If we don't appear to have a user for this URL then ask the
415 # provider for some extra information to help with signup
416 if openid_url and User.find_by_openid_url(openid_url)
419 optional = [:nickname, :email]
422 # Start the authentication
423 authenticate_with_open_id(openid_url, :optional => optional) do |result, identity_url, registration|
424 if result.successful?
425 # We need to use the openid url passed back from the OpenID provider
426 # rather than the one supplied by the user, as these can be different.
428 # For example, you can simply enter yahoo.com in the login box rather
429 # than a user specific url. Only once it comes back from the provider
430 # provider do we know the unique address for the user.
431 if user = User.find_by_openid_url(identity_url)
433 when "pending" then failed_login t('user.login.account not active')
434 when "active", "confirmed" then successful_login(user)
435 when "suspended" then failed_login t('user.login.account suspended')
436 else failed_login t('user.login.auth failure')
439 # We don't have a user registered to this OpenID, so redirect
440 # to the create account page with username and email filled
441 # in if they have been given by the OpenID provider through
442 # the simple registration protocol.
443 redirect_to :controller => 'user', :action => 'new', :nickname => registration['nickname'], :email => registration['email'], :openid => identity_url
445 elsif result.missing?
446 # Try and apply some heuristics to make common cases more user friendly
447 if openid_url = openid_alternate_url(openid_url)
448 openid_authentication(openid_url)
450 failed_login t('user.login.openid missing provider')
452 elsif result.invalid?
453 failed_login t('user.login.openid invalid')
455 failed_login t('user.login.auth failure')
461 # verify an OpenID URL
462 def openid_verify(openid_url, user)
463 user.openid_url = openid_url
465 authenticate_with_open_id(openid_url) do |result, identity_url|
466 if result.successful?
467 # We need to use the openid url passed back from the OpenID provider
468 # rather than the one supplied by the user, as these can be different.
470 # For example, you can simply enter yahoo.com in the login box rather
471 # than a user specific url. Only once it comes back from the provider
472 # provider do we know the unique address for the user.
473 user.openid_url = identity_url
475 elsif result.missing?
476 # Try and apply some heuristics to make common cases more user friendly
477 if openid_url = openid_alternate_url(openid_url)
478 openid_verify(openid_url, user)
480 flash.now[:error] = t 'user.login.openid missing provider'
482 elsif result.invalid?
483 flash.now[:error] = t 'user.login.openid invalid'
485 flash.now[:error] = t 'user.login.auth failure'
491 # special case some common OpenID providers by applying heuristics
492 # to try and come up with an alternate URL if the supplied one fails
493 def openid_alternate_url(openid_url)
494 # Special case gmail.com as it is potentially a popular OpenID
495 # provider and, unlike yahoo.com, where it works automatically, Google
496 # have hidden their OpenID endpoint somewhere obscure this making it
497 # somewhat less user friendly.
498 if openid_url.match(/(.*)gmail.com(\/?)$/) or openid_url.match(/(.*)googlemail.com(\/?)$/)
499 return 'https://www.google.com/accounts/o8/id'
506 # process a successful login
507 def successful_login(user)
508 session[:user] = user.id
510 session_expires_after 1.month if session[:remember_me]
512 if user.blocked_on_view
513 redirect_to user.blocked_on_view, :referer => params[:referer]
514 elsif session[:referer]
515 redirect_to session[:referer]
517 redirect_to :controller => 'site', :action => 'index'
520 session.delete(:remember_me)
521 session.delete(:referer)
525 # process a failed login
526 def failed_login(message)
527 flash[:error] = message
529 redirect_to :action => 'login', :referer => session[:referer]
531 session.delete(:remember_me)
532 session.delete(:referer)
536 # update a user's details
537 def update_user(user)
541 if user.new_email.nil? or user.new_email.empty?
542 flash.now[:notice] = t 'user.account.flash update success'
544 flash.now[:notice] = t 'user.account.flash update success confirm needed'
547 Notifier.deliver_email_confirm(user, user.tokens.create)
549 # Ignore errors sending email
556 # require that the user is a administrator, or fill out a helpful error message
557 # and return them to the user page.
558 def require_administrator
559 if @user and not @user.administrator?
560 flash[:error] = t('user.filter.not_an_administrator')
562 if params[:display_name]
563 redirect_to :controller => 'user', :action => 'view', :display_name => params[:display_name]
565 redirect_to :controller => 'user', :action => 'login', :referer => request.request_uri
568 redirect_to :controller => 'user', :action => 'login', :referer => request.request_uri
573 # ensure that there is a "this_user" instance variable
575 @this_user = User.find_by_display_name(params[:display_name])
576 rescue ActiveRecord::RecordNotFound
577 redirect_to :controller => 'user', :action => 'view', :display_name => params[:display_name] unless @this_user