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 => [:set_home, :account, :go_public, :make_friend, :remove_friend, :upload_image, :delete_image]
8 before_filter :check_database_readable, :except => [:api_details, :api_gpx_files]
9 before_filter :check_database_writable, :only => [:login, :new, :set_home, :account, :go_public, :make_friend, :remove_friend, :upload_image, :delete_image]
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 => [:activate, :deactivate, :hide, :unhide, :delete]
15 before_filter :lookup_this_user, :only => [:activate, :deactivate, :hide, :unhide, :delete]
17 filter_parameter_logging :password, :pass_crypt, :pass_crypt_confirmation
20 @title = t 'user.new.title'
22 if Acl.find_by_address(request.remote_ip, :conditions => {:k => "no_account_creation"})
23 render :action => 'new'
25 #The redirect from the OpenID provider reenters here again
26 #and we need to pass the parameters through to the
27 #open_id_authentication function
28 if params[:open_id_complete]
29 openid_verify('', true)
30 redirect_to :action => 'login'
34 @user = User.new(params[:user])
37 @user.data_public = true
38 @user.description = "" if @user.description.nil?
39 @user.creation_ip = request.remote_ip
40 @user.languages = request.user_preferred_languages
43 flash[:notice] = t 'user.new.flash create success message'
44 Notifier.deliver_signup_confirm(@user, @user.tokens.create(:referer => params[:referer]))
45 if (params[:user][:openid_url].length > 0)
47 session[:new_usr_name] = @user.display_name
48 @norm_openid_url = OpenIdAuthentication.normalize_identifier(params[:user][:openid_url])
49 #TODO: error messages in the openid_verify aren't correctly returned yet
50 openid_verify(@norm_openid_url, true)
51 #Will have sent the redirect_to in the if open_id_complete section of this method
53 flash.now[:error] = t 'user.login.openid invalid'
56 redirect_to :action => 'login'
59 render :action => 'new'
65 @title = t 'user.account.title'
66 @tokens = @user.oauth_tokens.find :all, :conditions => 'oauth_tokens.invalidated_at is null and oauth_tokens.authorized_at is not null'
68 #The redirect from the OpenID provider reenters here again
69 #and we need to pass the parameters through to the
70 #open_id_authentication function
71 if params[:open_id_complete]
72 openid_verify('', false)
76 if params[:user] and params[:user][:display_name] and params[:user][:description]
77 if params[:user][:email] != @user.email
78 @user.new_email = params[:user][:email]
81 @user.display_name = params[:user][:display_name]
83 if params[:user][:pass_crypt].length > 0 or params[:user][:pass_crypt_confirmation].length > 0
84 @user.pass_crypt = params[:user][:pass_crypt]
85 @user.pass_crypt_confirmation = params[:user][:pass_crypt_confirmation]
87 if (params[:user][:openid_url].length == 0)
88 #Clearing doesn't need OpenID validation, so we can set it here.
89 @user.openid_url = nil
92 @user.description = params[:user][:description]
93 @user.languages = params[:user][:languages].split(",")
94 @user.home_lat = params[:user][:home_lat]
95 @user.home_lon = params[:user][:home_lon]
100 if params[:user][:email] == @user.new_email
101 flash.now[:notice] = t 'user.account.flash update success confirm needed'
102 Notifier.deliver_email_confirm(@user, @user.tokens.create)
104 flash.now[:notice] = t 'user.account.flash update success'
108 if (params[:user][:openid_url].length > 0)
110 @norm_openid_url = OpenIdAuthentication.normalize_identifier(params[:user][:openid_url])
111 if (@norm_openid_url != @user.openid_url)
112 #If the OpenID has changed, we want to check that it is a valid OpenID and one
113 #the user has control over before saving the openID as a password equivalent for
115 openid_verify(@norm_openid_url, false)
118 flash.now[:error] = t 'user.login.openid invalid'
124 def openid_specialcase_mapping(openid_url)
125 #Special case gmail.com, as it is pontentially a popular OpenID provider and unlike
126 #yahoo.com, where it works automatically, Google have hidden their OpenID endpoint
127 #somewhere obscure making it less userfriendly.
128 if (openid_url.match(/(.*)gmail.com(\/?)$/) or openid_url.match(/(.*)googlemail.com(\/?)$/) )
129 return 'https://www.google.com/accounts/o8/id'
135 def openid_verify(openid_url,account_create)
136 authenticate_with_open_id(openid_url) do |result, identity_url|
137 if result.successful?
138 #We need to use the openid url passed back from the OpenID provider
139 #rather than the one supplied by the user, as these can be different.
140 #e.g. one can simply enter yahoo.com in the login box, i.e. no user specific url
141 #only once it comes back from the OpenID provider do we know the unique address for
143 @user = User.find_by_display_name(session[:new_usr_name]) unless @user
144 @user.openid_url = identity_url
146 flash.now[:notice] = t 'user.account.flash update success' unless account_create
148 else if result.missing?
149 mapped_id = openid_specialcase_mapping(openid_url)
151 openid_verify(mapped_id, account_create)
153 flash.now[:error] = t 'user.login.openid missing provider'
155 else if result.invalid?
156 flash.now[:error] = t 'user.login.openid invalid'
158 flash.now[:error] = t 'user.login.auth failure'
167 if params[:user][:home_lat] and params[:user][:home_lon]
168 @user.home_lat = params[:user][:home_lat].to_f
169 @user.home_lon = params[:user][:home_lon].to_f
171 flash[:notice] = t 'user.set_home.flash success'
172 redirect_to :controller => 'user', :action => 'account'
178 @user.data_public = true
180 flash[:notice] = t 'user.go_public.flash success'
181 redirect_to :controller => 'user', :action => 'account', :display_name => @user.display_name
185 @title = t 'user.lost_password.title'
187 if params[:user] and params[:user][:email]
188 user = User.find_by_email(params[:user][:email], :conditions => {:visible => true})
191 token = user.tokens.create
192 Notifier.deliver_lost_password(user, token)
193 flash.now[:notice] = t 'user.lost_password.notice email on way'
195 flash.now[:error] = t 'user.lost_password.notice email cannot find'
201 @title = t 'user.reset_password.title'
204 token = UserToken.find_by_token(params[:token])
210 @user.pass_crypt = params[:user][:pass_crypt]
211 @user.pass_crypt_confirmation = params[:user][:pass_crypt_confirmation]
213 @user.email_valid = true
217 flash[:notice] = t 'user.reset_password.flash changed'
218 redirect_to :action => 'login'
222 flash[:error] = t 'user.reset_password.flash token bad'
223 redirect_to :action => 'lost_password'
229 @title = t 'user.new.title'
231 # The user is logged in already, so don't show them the signup page, instead
232 # send them to the home page
233 redirect_to :controller => 'site', :action => 'index' if session[:user]
235 @nickname = params['nickname']
236 @email = params['email']
237 @openID = params['openid']
242 #The redirect from the OpenID provider reenters here again
243 #and we need to pass the parameters through to the
244 # open_id_authentication function
245 if params[:open_id_complete]
246 open_id_authentication('')
250 if params[:user] and session[:user].nil?
252 if !params[:user][:openid_url].empty?
253 open_id_authentication(params[:user][:openid_url])
255 email_or_display_name = params[:user][:email]
256 pass = params[:user][:password]
257 user = User.authenticate(:username => email_or_display_name, :password => pass)
259 session[:user] = user.id
260 elsif User.authenticate(:username => email_or_display_name, :password => pass, :inactive => true)
261 flash.now[:error] = t 'user.login.account not active'
263 flash.now[:error] = t 'user.login.auth failure'
269 # The user is logged in, if the referer param exists, redirect them to that
270 # unless they've also got a block on them, in which case redirect them to
271 # the block so they can clear it.
272 user = User.find(session[:user])
273 block = user.blocked_on_view
275 redirect_to block, :referrer => params[:referrer]
276 elsif params[:referer]
277 redirect_to params[:referer]
279 redirect_to :controller => 'site', :action => 'index'
284 @title = t 'user.login.title'
287 def open_id_authentication(openid_url)
288 #TODO: only ask for nickname and email, if we don't already have a user for that openID, in which case
289 #email and nickname are already filled out. I don't know how to do that with ruby syntax though, as we
290 #don't want to duplicate the do block
291 #On the other hand it also doesn't matter too much if we ask every time, as the OpenID provider should
292 #remember these results, and shouldn't repromt the user for these data each time.
293 authenticate_with_open_id(openid_url, :return_to => request.protocol + request.host_with_port + '/login?referer=' + params[:referer], :optional => [:nickname, :email]) do |result, identity_url, registration|
294 if result.successful?
295 #We need to use the openid url passed back from the OpenID provider
296 #rather than the one supplied by the user, as these can be different.
297 #e.g. one can simply enter yahoo.com in the login box, i.e. no user specific url
298 #only once it comes back from the OpenID provider do we know the unique address for
300 user = User.find_by_openid_url(identity_url)
302 if user.visible? and user.active?
303 session[:user] = user.id
306 flash.now[:error] = t 'user.login.account not active'
309 #We don't have a user registered to this OpenID. Redirect to the create account page
310 #with username and email filled in if they have been given by the OpenID provider through
311 #the simple registration protocol
312 redirect_to :controller => 'user', :action => 'new', :nickname => registration['nickname'], :email => registration['email'], :openid => identity_url
314 else if result.missing?
315 #Try and apply some heuristics to make common cases more userfriendly
316 mapped_id = openid_specialcase_mapping(openid_url)
318 open_id_authentication(mapped_id)
320 flash.now[:error] = t 'user.login.openid missing provider'
322 else if result.invalid?
323 flash.now[:error] = t 'user.login.openid invalid'
325 flash.now[:error] = t 'user.login.auth failure'
334 token = UserToken.find_by_token(session[:token])
338 session[:token] = nil
342 redirect_to params[:referer]
344 redirect_to :controller => 'site', :action => 'index'
349 if params[:confirm_action]
350 token = UserToken.find_by_token(params[:confirm_string])
351 if token and !token.user.active?
354 @user.email_valid = true
356 referer = token.referer
358 flash[:notice] = t 'user.confirm.success'
359 session[:user] = @user.id
363 redirect_to :action => 'account', :display_name => @user.display_name
366 flash.now[:error] = t 'user.confirm.failure'
372 if params[:confirm_action]
373 token = UserToken.find_by_token(params[:confirm_string])
374 if token and token.user.new_email?
376 @user.email = @user.new_email
377 @user.new_email = nil
379 @user.email_valid = true
382 flash[:notice] = t 'user.confirm_email.success'
383 session[:user] = @user.id
384 redirect_to :action => 'account', :display_name => @user.display_name
386 flash.now[:error] = t 'user.confirm_email.failure'
392 @user.image = params[:user][:image]
394 redirect_to :controller => 'user', :action => 'view', :display_name => @user.display_name
400 redirect_to :controller => 'user', :action => 'view', :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.find_by_display_name(name, :conditions => {:visible => true})
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.deliver_friend_notification(friend)
436 friend.add_error(t('user.make_friend.failed', :name => name))
439 flash[:warning] = t 'user.make_friend.already_a_friend', :name => name
442 redirect_to :controller => 'user', :action => 'view'
447 if params[:display_name]
448 name = params[:display_name]
449 friend = User.find_by_display_name(name, :conditions => {:visible => true})
450 if @user.is_friends_with?(friend)
451 Friend.delete_all "user_id = #{@user.id} AND friend_user_id = #{friend.id}"
452 flash[:notice] = t 'user.remove_friend.success', :name => friend.display_name
454 flash[:error] = t 'user.remove_friend.not_a_friend', :name => friend.display_name
457 redirect_to :controller => 'user', :action => 'view'
462 # activate a user, allowing them to log in
464 @this_user.update_attributes(:active => true)
465 redirect_to :controller => 'user', :action => 'view', :display_name => params[:display_name]
469 # deactivate a user, preventing them from logging in
471 @this_user.update_attributes(:active => false)
472 redirect_to :controller => 'user', :action => 'view', :display_name => params[:display_name]
476 # hide a user, marking them as logically deleted
478 @this_user.update_attributes(:visible => false)
479 redirect_to :controller => 'user', :action => 'view', :display_name => params[:display_name]
483 # unhide a user, clearing the logically deleted flag
485 @this_user.update_attributes(:visible => true)
486 redirect_to :controller => 'user', :action => 'view', :display_name => params[:display_name]
490 # delete a user, marking them as deleted and removing personal data
493 redirect_to :controller => 'user', :action => 'view', :display_name => params[:display_name]
497 # require that the user is a administrator, or fill out a helpful error message
498 # and return them to the user page.
499 def require_administrator
500 unless @user.administrator?
501 flash[:error] = t('user.filter.not_an_administrator')
502 redirect_to :controller => 'user', :action => 'view', :display_name => params[:display_name]
507 # ensure that there is a "this_user" instance variable
509 @this_user = User.find_by_display_name(params[:display_name])
510 rescue ActiveRecord::RecordNotFound
511 redirect_to :controller => 'user', :action => 'view', :display_name => params[:display_name] unless @this_user