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'
24 if Acl.find_by_address(request.remote_ip, :conditions => {:k => "no_account_creation"})
25 render :action => 'new'
27 if params[:open_id_complete]
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)
32 openid_verify(nil, @user) do |user|
36 session[:referer] = params[:referer]
38 @user = User.new(params[:user])
40 @user.status = "pending"
41 @user.data_public = true
42 @user.description = "" if @user.description.nil?
43 @user.creation_ip = request.remote_ip
44 @user.languages = request.user_preferred_languages
46 if params[:user][:openid_url] and @user.pass_crypt.empty?
47 # We are creating an account with OpenID and no password
48 # was specified so create a random one
49 @user.pass_crypt = ActiveSupport::SecureRandom.base64(16)
50 @user.pass_crypt_confirmation = @user.pass_crypt
54 if params[:user][:openid_url].nil? or
55 params[:user][:openid_url].empty?
56 # No OpenID so just save
59 # Verify OpenID before saving
60 session[:new_user] = @user
61 openid_verify(params[:user][:openid_url], @user)
66 # Render the signup page unless we have already been
67 # redirected or have managed to save the user
68 if response.location.nil? and @user.new_record?
69 render :action => "new"
75 @title = t 'user.account.title'
76 @tokens = @user.oauth_tokens.find :all, :conditions => 'oauth_tokens.invalidated_at is null and oauth_tokens.authorized_at is not null'
78 if params[:open_id_complete]
79 # The redirect from the OpenID provider reenters here
80 # again and we need to pass the parameters through to
81 # the open_id_authentication function
82 @user = session.delete(:new_user)
83 openid_verify(nil, @user) do |user|
86 elsif params[:user] and params[:user][:display_name] and params[:user][:description]
87 @user.display_name = params[:user][:display_name]
88 @user.new_email = params[:user][:new_email]
90 if params[:user][:pass_crypt].length > 0 or params[:user][:pass_crypt_confirmation].length > 0
91 @user.pass_crypt = params[:user][:pass_crypt]
92 @user.pass_crypt_confirmation = params[:user][:pass_crypt_confirmation]
95 @user.description = params[:user][:description]
96 @user.languages = params[:user][:languages].split(",")
98 case params[:image_action]
99 when "new" then @user.image = params[:user][:image]
100 when "delete" then @user.image = nil
103 @user.home_lat = params[:user][:home_lat]
104 @user.home_lon = params[:user][:home_lon]
106 @user.openid_url = nil if params[:user][:openid_url].empty?
108 if params[:user][:openid_url].length > 0 and
109 params[:user][:openid_url] != @user.openid_url
110 # If the OpenID has changed, we want to check that it is a
111 # valid OpenID and one the user has control over before saving
112 # it as a password equivalent for the user.
113 session[:new_user] = @user
114 openid_verify(params[:user][:openid_url], @user)
122 @user.data_public = true
124 flash[:notice] = t 'user.go_public.flash success'
125 redirect_to :controller => 'user', :action => 'account', :display_name => @user.display_name
129 @title = t 'user.lost_password.title'
131 if params[:user] and params[:user][:email]
132 user = User.find_by_email(params[:user][:email], :conditions => {:status => ["pending", "active", "confirmed"]})
135 token = user.tokens.create
136 Notifier.deliver_lost_password(user, token)
137 flash[:notice] = t 'user.lost_password.notice email on way'
138 redirect_to :action => 'login'
140 flash.now[:error] = t 'user.lost_password.notice email cannot find'
146 @title = t 'user.reset_password.title'
149 token = UserToken.find_by_token(params[:token])
155 @user.pass_crypt = params[:user][:pass_crypt]
156 @user.pass_crypt_confirmation = params[:user][:pass_crypt_confirmation]
157 @user.status = "active" if @user.status == "pending"
158 @user.email_valid = true
162 flash[:notice] = t 'user.reset_password.flash changed'
163 redirect_to :action => 'login'
167 flash[:error] = t 'user.reset_password.flash token bad'
168 redirect_to :action => 'lost_password'
174 @title = t 'user.new.title'
175 @referer = params[:referer] || session[:referer]
178 # The user is logged in already, so don't show them the signup
179 # page, instead send them to the home page
180 redirect_to :controller => 'site', :action => 'index'
181 elsif not params['openid'].nil?
182 flash.now[:notice] = t 'user.new.openid association'
188 session[:remember_me] ||= params[:remember_me]
189 session[:referer] ||= params[:referer]
191 if using_open_id?(params[:openid_url])
192 openid_authentication(params[:openid_url])
194 password_authentication(params[:username], params[:password])
197 @title = t 'user.login.title'
202 @title = t 'user.logout.title'
204 if params[:session] == request.session_options[:id]
206 token = UserToken.find_by_token(session[:token])
210 session[:token] = nil
213 session_expires_automatically
215 redirect_to params[:referer]
217 redirect_to :controller => 'site', :action => 'index'
223 if params[:confirm_action]
224 token = UserToken.find_by_token(params[:confirm_string])
225 if token and !token.user.active?
227 @user.status = "active"
228 @user.email_valid = true
230 referer = token.referer
232 flash[:notice] = t 'user.confirm.success'
233 session[:user] = @user.id
237 redirect_to :action => 'account', :display_name => @user.display_name
240 flash.now[:error] = t 'user.confirm.failure'
246 if params[:confirm_action]
247 token = UserToken.find_by_token(params[:confirm_string])
248 if token and token.user.new_email?
250 @user.email = @user.new_email
251 @user.new_email = nil
252 @user.email_valid = true
254 flash[:notice] = t 'user.confirm_email.success'
256 flash[:errors] = @user.errors
259 session[:user] = @user.id
260 redirect_to :action => 'account', :display_name => @user.display_name
262 flash.now[:error] = t 'user.confirm_email.failure'
268 doc = OSM::API.new.get_xml_doc
269 @user.traces.each do |trace|
270 doc.root << trace.to_xml_node() if trace.public? or trace.user == @user
272 render :text => doc.to_s, :content_type => "text/xml"
276 @this_user = User.find_by_display_name(params[:display_name])
279 (@this_user.visible? or (@user and @user.administrator?))
280 @title = @this_user.display_name
282 @title = t 'user.no_such_user.title'
283 @not_found_user = params[:display_name]
284 render :action => 'no_such_user', :status => :not_found
289 if params[:display_name]
290 name = params[:display_name]
291 new_friend = User.find_by_display_name(name, :conditions => {:status => ["active", "confirmed"]})
293 friend.user_id = @user.id
294 friend.friend_user_id = new_friend.id
295 unless @user.is_friends_with?(new_friend)
297 flash[:notice] = t 'user.make_friend.success', :name => name
298 Notifier.deliver_friend_notification(friend)
300 friend.add_error(t('user.make_friend.failed', :name => name))
303 flash[:warning] = t 'user.make_friend.already_a_friend', :name => name
307 redirect_to params[:referer]
309 redirect_to :controller => 'user', :action => 'view'
315 if params[:display_name]
316 name = params[:display_name]
317 friend = User.find_by_display_name(name, :conditions => {:status => ["active", "confirmed"]})
318 if @user.is_friends_with?(friend)
319 Friend.delete_all "user_id = #{@user.id} AND friend_user_id = #{friend.id}"
320 flash[:notice] = t 'user.remove_friend.success', :name => friend.display_name
322 flash[:error] = t 'user.remove_friend.not_a_friend', :name => friend.display_name
326 redirect_to params[:referer]
328 redirect_to :controller => 'user', :action => 'view'
334 # sets a user's status
336 @this_user.update_attributes(:status => params[:status])
337 redirect_to :controller => 'user', :action => 'view', :display_name => params[:display_name]
341 # delete a user, marking them as deleted and removing personal data
344 redirect_to :controller => 'user', :action => 'view', :display_name => params[:display_name]
348 # display a list of users matching specified criteria
351 ids = params[:user].keys.collect { |id| id.to_i }
353 User.update_all("status = 'confirmed'", :id => ids) if params[:confirm]
354 User.update_all("status = 'deleted'", :id => ids) if params[:hide]
356 redirect_to url_for(:status => params[:status], :ip => params[:ip], :page => params[:page])
358 conditions = Hash.new
359 conditions[:status] = params[:status] if params[:status]
360 conditions[:creation_ip] = params[:ip] if params[:ip]
362 @user_pages, @users = paginate(:users,
363 :conditions => conditions,
372 # handle password authentication
373 def password_authentication(username, password)
374 if user = User.authenticate(:username => username, :password => password)
375 successful_login(user)
376 elsif User.authenticate(:username => username, :password => password, :pending => true)
377 failed_login t('user.login.account not active')
378 elsif User.authenticate(:username => username, :password => password, :suspended => true)
379 failed_login t('user.login.account suspended')
381 failed_login t('user.login.auth failure')
386 # handle OpenID authentication
387 def openid_authentication(openid_url)
388 # If we don't appear to have a user for this URL then ask the
389 # provider for some extra information to help with signup
390 if openid_url and User.find_by_openid_url(openid_url)
393 optional = [:nickname, :email]
396 # Start the authentication
397 authenticate_with_open_id(openid_url, :optional => optional) do |result, identity_url, registration|
398 if result.successful?
399 # We need to use the openid url passed back from the OpenID provider
400 # rather than the one supplied by the user, as these can be different.
402 # For example, you can simply enter yahoo.com in the login box rather
403 # than a user specific url. Only once it comes back from the provider
404 # provider do we know the unique address for the user.
405 if user = User.find_by_openid_url(identity_url)
407 when "pending" then failed_login t('user.login.account not active')
408 when "active", "confirmed" then successful_login(user)
409 when "suspended" then failed_login t('user.login.account suspended')
410 else failed_login t('user.login.auth failure')
413 # We don't have a user registered to this OpenID, so redirect
414 # to the create account page with username and email filled
415 # in if they have been given by the OpenID provider through
416 # the simple registration protocol.
417 redirect_to :controller => 'user', :action => 'new', :nickname => registration['nickname'], :email => registration['email'], :openid => identity_url
419 elsif result.missing?
420 # Try and apply some heuristics to make common cases more user friendly
421 if openid_url = openid_alternate_url(openid_url)
422 openid_authentication(openid_url)
424 failed_login t('user.login.openid missing provider')
426 elsif result.invalid?
427 failed_login t('user.login.openid invalid')
429 failed_login t('user.login.auth failure')
435 # verify an OpenID URL
436 def openid_verify(openid_url, user)
437 user.openid_url = openid_url
439 authenticate_with_open_id(openid_url) do |result, identity_url|
440 if result.successful?
441 # We need to use the openid url passed back from the OpenID provider
442 # rather than the one supplied by the user, as these can be different.
444 # For example, you can simply enter yahoo.com in the login box rather
445 # than a user specific url. Only once it comes back from the provider
446 # provider do we know the unique address for the user.
447 user.openid_url = identity_url
449 elsif result.missing?
450 # Try and apply some heuristics to make common cases more user friendly
451 if openid_url = openid_alternate_url(openid_url)
452 openid_verify(openid_url, user)
454 flash.now[:error] = t 'user.login.openid missing provider'
456 elsif result.invalid?
457 flash.now[:error] = t 'user.login.openid invalid'
459 flash.now[:error] = t 'user.login.auth failure'
465 # special case some common OpenID providers by applying heuristics
466 # to try and come up with an alternate URL if the supplied one fails
467 def openid_alternate_url(openid_url)
468 # Special case gmail.com as it is potentially a popular OpenID
469 # provider and, unlike yahoo.com, where it works automatically, Google
470 # have hidden their OpenID endpoint somewhere obscure this making it
471 # somewhat less user friendly.
472 if openid_url.match(/(.*)gmail.com(\/?)$/) or openid_url.match(/(.*)googlemail.com(\/?)$/)
473 return 'https://www.google.com/accounts/o8/id'
480 # process a successful login
481 def successful_login(user)
482 session[:user] = user.id
484 session_expires_after 1.month if session[:remember_me]
486 if user.blocked_on_view
487 redirect_to user.blocked_on_view, :referer => params[:referer]
488 elsif session[:referer]
489 redirect_to session[:referer]
491 redirect_to :controller => 'site', :action => 'index'
494 session.delete(:remember_me)
495 session.delete(:referer)
499 # process a failed login
500 def failed_login(message)
501 flash[:error] = message
503 redirect_to :action => 'login', :referer => session[:referer]
505 session.delete(:remember_me)
506 session.delete(:referer)
511 def create_user(user)
513 flash[:notice] = t 'user.new.flash create success message'
515 Notifier.deliver_signup_confirm(user, user.tokens.create(:referer => session.delete(:referer)))
517 redirect_to :action => 'login'
522 # update a user's details
523 def update_user(user)
527 if user.new_email.nil? or user.new_email.empty?
528 flash.now[:notice] = t 'user.account.flash update success'
530 flash.now[:notice] = t 'user.account.flash update success confirm needed'
533 Notifier.deliver_email_confirm(user, user.tokens.create)
535 # Ignore errors sending email
542 # require that the user is a administrator, or fill out a helpful error message
543 # and return them to the user page.
544 def require_administrator
545 if @user and not @user.administrator?
546 flash[:error] = t('user.filter.not_an_administrator')
548 if params[:display_name]
549 redirect_to :controller => 'user', :action => 'view', :display_name => params[:display_name]
551 redirect_to :controller => 'user', :action => 'login', :referer => request.request_uri
554 redirect_to :controller => 'user', :action => 'login', :referer => request.request_uri
559 # ensure that there is a "this_user" instance variable
561 @this_user = User.find_by_display_name(params[:display_name])
562 rescue ActiveRecord::RecordNotFound
563 redirect_to :controller => 'user', :action => 'view', :display_name => params[:display_name] unless @this_user