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 #The redirect from the OpenID provider reenters here again
28 #and we need to pass the parameters through to the
29 #open_id_authentication function a second time
30 if params[:open_id_complete]
31 openid_verify('', true)
32 #We have set the user.openid_url to nil beforehand. If it hasn't
33 #been set to a new valid openid_url, it means the openid couldn't be validated
34 if @user.nil? or @user.openid_url.nil?
35 render :action => 'new'
39 @user = User.new(params[:user])
41 @user.status = "pending"
42 @user.data_public = true
43 @user.description = "" if @user.description.nil?
44 @user.creation_ip = request.remote_ip
45 @user.languages = request.user_preferred_languages
46 #Set the openid_url to nil as for one it is used
47 #to check if the openid could be validated and secondly
48 #to not get dupplicate conflicts for an empty openid
49 @user.openid_url = nil
51 if (!params[:user][:openid_url].nil? and params[:user][:openid_url].length > 0)
52 if (@user.pass_crypt.nil? or @user.pass_crypt.length == 0)
53 #if the password is empty, but we have a openid
54 #then generate a random passowrd to disable
55 #loging in via password
56 @user.pass_crypt = ActiveSupport::SecureRandom.base64(16)
57 @user.pass_crypt_confirmation = @user.pass_crypt
59 #Validate all of the other fields before
60 #redirecting to the openid provider
62 render :action => 'new'
64 #TODO: Is it a problem to store the user variable with respect to password safty in the session variables?
65 #Store the user variable in the session for it to be accessible when redirecting back from the openid provider
66 session[:new_usr] = @user
68 @norm_openid_url = OpenIdAuthentication.normalize_identifier(params[:user][:openid_url])
70 flash.now[:error] = t 'user.login.openid invalid'
71 render :action => 'new'
74 #Verify that the openid provided is valid and that the user is the owner of the id
75 openid_verify(@norm_openid_url, true)
76 #openid_verify can return in two ways:
77 #Either it returns with a redirect to the openid provider who then freshly
78 #redirects back to this url if the openid is valid, or if the openid is not plausible
79 #and no provider for it could be found it just returns
80 #we want to just let the redirect through
81 if response.headers["Location"].nil?
82 render :action => 'new'
85 #At this point there was either an error and the page has been rendered,
86 #or there is a redirect to the openid provider and the rest of the method
87 #gets executed whenn this method gets reentered after redirecting back
88 #from the openid provider
94 flash[:notice] = t 'user.new.flash create success message'
95 Notifier.deliver_signup_confirm(@user, @user.tokens.create(:referer => params[:referer]))
96 redirect_to :action => 'login'
98 render :action => 'new'
104 @title = t 'user.account.title'
105 @tokens = @user.oauth_tokens.find :all, :conditions => 'oauth_tokens.invalidated_at is null and oauth_tokens.authorized_at is not null'
107 #The redirect from the OpenID provider reenters here again
108 #and we need to pass the parameters through to the
109 #open_id_authentication function
110 if params[:open_id_complete]
111 openid_verify('', false)
116 if params[:user] and params[:user][:display_name] and params[:user][:description]
117 @user.display_name = params[:user][:display_name]
118 @user.new_email = params[:user][:new_email]
120 if params[:user][:pass_crypt].length > 0 or params[:user][:pass_crypt_confirmation].length > 0
121 @user.pass_crypt = params[:user][:pass_crypt]
122 @user.pass_crypt_confirmation = params[:user][:pass_crypt_confirmation]
125 @user.description = params[:user][:description]
126 @user.languages = params[:user][:languages].split(",")
128 case params[:image_action]
129 when "new" then @user.image = params[:user][:image]
130 when "delete" then @user.image = nil
133 @user.home_lat = params[:user][:home_lat]
134 @user.home_lon = params[:user][:home_lon]
136 @user.openid_url = nil if (params[:user][:openid_url].length == 0)
141 if @user.new_email.nil? or @user.new_email.empty?
142 flash[:notice] = t 'user.account.flash update success'
144 flash[:notice] = t 'user.account.flash update success confirm needed'
147 Notifier.deliver_email_confirm(@user, @user.tokens.create)
149 # Ignore errors sending email
153 redirect_to :action => "account", :display_name => @user.display_name
156 if (params[:user][:openid_url].length > 0)
158 @norm_openid_url = OpenIdAuthentication.normalize_identifier(params[:user][:openid_url])
159 if (@norm_openid_url != @user.openid_url)
160 #If the OpenID has changed, we want to check that it is a valid OpenID and one
161 #the user has control over before saving the openID as a password equivalent for
163 openid_verify(@norm_openid_url, false)
166 flash.now[:error] = t 'user.login.openid invalid'
172 flash[:errors].each do |attr,msg|
173 attr = "new_email" if attr == "email"
174 @user.errors.add(attr,msg)
180 def openid_verify(openid_url, account_create)
181 authenticate_with_open_id(openid_url) do |result, identity_url|
182 if result.successful?
183 #We need to use the openid url passed back from the OpenID provider
184 #rather than the one supplied by the user, as these can be different.
185 #e.g. one can simply enter yahoo.com in the login box, i.e. no user specific url
186 #only once it comes back from the OpenID provider do we know the unique address for
188 @user = session[:new_usr] unless @user #this is used for account creation when the user is not yet in the database
189 @user.openid_url = identity_url
190 elsif result.missing?
191 if openid_url = openid_alternate_url(openid_url)
192 openid_verify(openid_url, account_create)
194 flash.now[:error] = t 'user.login.openid missing provider'
196 elsif result.invalid?
197 flash.now[:error] = t 'user.login.openid invalid'
199 flash.now[:error] = t 'user.login.auth failure'
205 @user.data_public = true
207 flash[:notice] = t 'user.go_public.flash success'
208 redirect_to :controller => 'user', :action => 'account', :display_name => @user.display_name
212 @title = t 'user.lost_password.title'
214 if params[:user] and params[:user][:email]
215 user = User.find_by_email(params[:user][:email], :conditions => {:status => ["pending", "active", "confirmed"]})
218 token = user.tokens.create
219 Notifier.deliver_lost_password(user, token)
220 flash[:notice] = t 'user.lost_password.notice email on way'
221 redirect_to :action => 'login'
223 flash.now[:error] = t 'user.lost_password.notice email cannot find'
229 @title = t 'user.reset_password.title'
232 token = UserToken.find_by_token(params[:token])
238 @user.pass_crypt = params[:user][:pass_crypt]
239 @user.pass_crypt_confirmation = params[:user][:pass_crypt_confirmation]
240 @user.status = "active" if @user.status == "pending"
241 @user.email_valid = true
245 flash[:notice] = t 'user.reset_password.flash changed'
246 redirect_to :action => 'login'
250 flash[:error] = t 'user.reset_password.flash token bad'
251 redirect_to :action => 'lost_password'
257 @title = t 'user.new.title'
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' if session[:user]
263 @nickname = params['nickname']
264 @email = params['email']
265 @openID = params['openid']
267 if !params['openid'].nil?
268 flash.now[:notice] = t 'user.new.openid association'
274 session[:remember_me] ||= params[:remember_me]
275 session[:referer] ||= params[:referer]
277 if using_open_id?(params[:openid_url])
278 openid_authentication(params[:openid_url])
280 password_authentication(params[:username], params[:password])
283 @title = t 'user.login.title'
288 @title = t 'user.logout.title'
290 if params[:session] == request.session_options[:id]
292 token = UserToken.find_by_token(session[:token])
296 session[:token] = nil
299 session_expires_automatically
301 redirect_to params[:referer]
303 redirect_to :controller => 'site', :action => 'index'
309 if params[:confirm_action]
310 token = UserToken.find_by_token(params[:confirm_string])
311 if token and !token.user.active?
313 @user.status = "active"
314 @user.email_valid = true
316 referer = token.referer
318 flash[:notice] = t 'user.confirm.success'
319 session[:user] = @user.id
323 redirect_to :action => 'account', :display_name => @user.display_name
326 flash.now[:error] = t 'user.confirm.failure'
332 if params[:confirm_action]
333 token = UserToken.find_by_token(params[:confirm_string])
334 if token and token.user.new_email?
336 @user.email = @user.new_email
337 @user.new_email = nil
338 @user.email_valid = true
340 flash[:notice] = t 'user.confirm_email.success'
342 flash[:errors] = @user.errors
345 session[:user] = @user.id
346 redirect_to :action => 'account', :display_name => @user.display_name
348 flash.now[:error] = t 'user.confirm_email.failure'
354 doc = OSM::API.new.get_xml_doc
355 @user.traces.each do |trace|
356 doc.root << trace.to_xml_node() if trace.public? or trace.user == @user
358 render :text => doc.to_s, :content_type => "text/xml"
362 @this_user = User.find_by_display_name(params[:display_name])
365 (@this_user.visible? or (@user and @user.administrator?))
366 @title = @this_user.display_name
368 @title = t 'user.no_such_user.title'
369 @not_found_user = params[:display_name]
370 render :action => 'no_such_user', :status => :not_found
375 if params[:display_name]
376 name = params[:display_name]
377 new_friend = User.find_by_display_name(name, :conditions => {:status => ["active", "confirmed"]})
379 friend.user_id = @user.id
380 friend.friend_user_id = new_friend.id
381 unless @user.is_friends_with?(new_friend)
383 flash[:notice] = t 'user.make_friend.success', :name => name
384 Notifier.deliver_friend_notification(friend)
386 friend.add_error(t('user.make_friend.failed', :name => name))
389 flash[:warning] = t 'user.make_friend.already_a_friend', :name => name
393 redirect_to params[:referer]
395 redirect_to :controller => 'user', :action => 'view'
401 if params[:display_name]
402 name = params[:display_name]
403 friend = User.find_by_display_name(name, :conditions => {:status => ["active", "confirmed"]})
404 if @user.is_friends_with?(friend)
405 Friend.delete_all "user_id = #{@user.id} AND friend_user_id = #{friend.id}"
406 flash[:notice] = t 'user.remove_friend.success', :name => friend.display_name
408 flash[:error] = t 'user.remove_friend.not_a_friend', :name => friend.display_name
412 redirect_to params[:referer]
414 redirect_to :controller => 'user', :action => 'view'
420 # sets a user's status
422 @this_user.update_attributes(:status => params[:status])
423 redirect_to :controller => 'user', :action => 'view', :display_name => params[:display_name]
427 # delete a user, marking them as deleted and removing personal data
430 redirect_to :controller => 'user', :action => 'view', :display_name => params[:display_name]
434 # display a list of users matching specified criteria
437 ids = params[:user].keys.collect { |id| id.to_i }
439 User.update_all("status = 'confirmed'", :id => ids) if params[:confirm]
440 User.update_all("status = 'deleted'", :id => ids) if params[:hide]
442 redirect_to url_for(:status => params[:status], :ip => params[:ip], :page => params[:page])
444 conditions = Hash.new
445 conditions[:status] = params[:status] if params[:status]
446 conditions[:creation_ip] = params[:ip] if params[:ip]
448 @user_pages, @users = paginate(:users,
449 :conditions => conditions,
458 # handle password authentication
459 def password_authentication(username, password)
460 if user = User.authenticate(:username => username, :password => password)
461 successful_login(user)
462 elsif User.authenticate(:username => username, :password => password, :pending => true)
463 failed_login t('user.login.account not active')
464 elsif User.authenticate(:username => username, :password => password, :suspended => true)
465 failed_login t('user.login.account suspended')
467 failed_login t('user.login.auth failure')
472 # handle OpenID authentication
473 def openid_authentication(openid_url)
474 # If we don't appear to have a user for this URL then ask the
475 # provider for some extra information to help with signup
476 if openid_url and User.find_by_openid_url(openid_url)
479 optional = [:nickname, :email]
482 # Start the authentication
483 authenticate_with_open_id(openid_url, :optional => optional) do |result, identity_url, registration|
484 if result.successful?
485 # We need to use the openid url passed back from the OpenID provider
486 # rather than the one supplied by the user, as these can be different.
488 # For example, you can simply enter yahoo.com in the login box rather
489 # than a user specific url. Only once it comes back from the provider
490 # provider do we know the unique address for the user.
491 if user = User.find_by_openid_url(identity_url)
493 when "pending" then failed_login t('user.login.account not active')
494 when "active", "confirmed" then successful_login(user)
495 when "suspended" then failed_login t('user.login.account suspended')
496 else failed_login t('user.login.auth failure')
499 # We don't have a user registered to this OpenID, so redirect
500 # to the create account page with username and email filled
501 # in if they have been given by the OpenID provider through
502 # the simple registration protocol.
503 redirect_to :controller => 'user', :action => 'new', :nickname => registration['nickname'], :email => registration['email'], :openid => identity_url
505 elsif result.missing?
506 # Try and apply some heuristics to make common cases more user friendly
507 if openid_url = openid_alternate_url(openid_url)
508 openid_authentication(openid_url)
510 failed_login t('user.login.openid missing provider')
512 elsif result.invalid?
513 failed_login t('user.login.openid invalid')
515 failed_login t('user.login.auth failure')
521 # special case some common OpenID providers by applying heuristics
522 # to try and come up with an alternate URL if the supplied one fails
523 def openid_alternate_url(openid_url)
524 # Special case gmail.com as it is potentially a popular OpenID
525 # provider and, unlike yahoo.com, where it works automatically, Google
526 # have hidden their OpenID endpoint somewhere obscure this making it
527 # somewhat less user friendly.
528 if openid_url.match(/(.*)gmail.com(\/?)$/) or openid_url.match(/(.*)googlemail.com(\/?)$/)
529 return 'https://www.google.com/accounts/o8/id'
536 # process a successful login
537 def successful_login(user)
538 session[:user] = user.id
540 session_expires_after 1.month if session[:remember_me]
542 if user.blocked_on_view
543 redirect_to user.blocked_on_view, :referer => params[:referer]
544 elsif session[:referer]
545 redirect_to session[:referer]
547 redirect_to :controller => 'site', :action => 'index'
550 session.delete(:remember_me)
551 session.delete(:referer)
555 # process a failed login
556 def failed_login(message)
557 flash[:error] = message
559 redirect_to :action => 'login', :referer => session[:referer]
561 session.delete(:remember_me)
562 session.delete(:referer)
566 # require that the user is a administrator, or fill out a helpful error message
567 # and return them to the user page.
568 def require_administrator
569 if @user and not @user.administrator?
570 flash[:error] = t('user.filter.not_an_administrator')
572 if params[:display_name]
573 redirect_to :controller => 'user', :action => 'view', :display_name => params[:display_name]
575 redirect_to :controller => 'user', :action => 'login', :referer => request.request_uri
578 redirect_to :controller => 'user', :action => 'login', :referer => request.request_uri
583 # ensure that there is a "this_user" instance variable
585 @this_user = User.find_by_display_name(params[:display_name])
586 rescue ActiveRecord::RecordNotFound
587 redirect_to :controller => 'user', :action => 'view', :display_name => params[:display_name] unless @this_user