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 a second time
28 if params[:open_id_complete]
29 openid_verify('', true)
30 #We have set the user.openid_url to nil beforehand. If it hasn't
31 #been set to a new valid openid_url, it means the openid couldn't be validated
32 if @user.nil? or @user.openid_url.nil?
33 render :action => 'new'
37 @user = User.new(params[:user])
40 @user.data_public = true
41 @user.description = "" if @user.description.nil?
42 @user.creation_ip = request.remote_ip
43 @user.languages = request.user_preferred_languages
44 #Set the openid_url to nil as for one it is used
45 #to check if the openid could be validated and secondly
46 #to not get dupplicate conflicts for an empty openid
47 @user.openid_url = nil
49 if (params[:user][:openid_url].length > 0)
50 #Validate all of the other fields before
51 #redirecting to the openid provider
53 render :action => 'new'
55 #TODO: Is it a problem to store the user variable with respect to password safty in the session variables?
56 #Store the user variable in the session for it to be accessible when redirecting back from the openid provider
57 session[:new_usr] = @user
59 @norm_openid_url = OpenIdAuthentication.normalize_identifier(params[:user][:openid_url])
61 flash.now[:error] = t 'user.login.openid invalid'
62 render :action => 'new'
65 #Verify that the openid provided is valid and that the user is the owner of the id
66 openid_verify(@norm_openid_url, true)
67 #openid_verify can return in two ways:
68 #Either it returns with a redirect to the openid provider who then freshly
69 #redirects back to this url if the openid is valid, or if the openid is not plausible
70 #and no provider for it could be found it just returns
71 #we want to just let the redirect through
72 if response.headers["Location"].nil?
73 render :action => 'new'
76 #At this point there was either an error and the page has been rendered,
77 #or there is a redirect to the openid provider and the rest of the method
78 #gets executed whenn this method gets reentered after redirecting back
79 #from the openid provider
84 flash[:notice] = t 'user.new.flash create success message'
85 Notifier.deliver_signup_confirm(@user, @user.tokens.create(:referer => params[:referer]))
86 redirect_to :action => 'login'
88 render :action => 'new'
94 @title = t 'user.account.title'
95 @tokens = @user.oauth_tokens.find :all, :conditions => 'oauth_tokens.invalidated_at is null and oauth_tokens.authorized_at is not null'
97 #The redirect from the OpenID provider reenters here again
98 #and we need to pass the parameters through to the
99 #open_id_authentication function
100 if params[:open_id_complete]
101 openid_verify('', false)
106 if params[:user] and params[:user][:display_name] and params[:user][:description]
107 if params[:user][:email] != @user.email
108 @user.new_email = params[:user][:email]
111 @user.display_name = params[:user][:display_name]
113 if params[:user][:pass_crypt].length > 0 or params[:user][:pass_crypt_confirmation].length > 0
114 @user.pass_crypt = params[:user][:pass_crypt]
115 @user.pass_crypt_confirmation = params[:user][:pass_crypt_confirmation]
117 if (params[:user][:openid_url].length == 0)
118 #Clearing doesn't need OpenID validation, so we can set it here.
119 @user.openid_url = nil
122 @user.description = params[:user][:description]
123 @user.languages = params[:user][:languages].split(",")
124 @user.home_lat = params[:user][:home_lat]
125 @user.home_lon = params[:user][:home_lon]
130 if params[:user][:email] == @user.new_email
131 flash.now[:notice] = t 'user.account.flash update success confirm needed'
132 Notifier.deliver_email_confirm(@user, @user.tokens.create)
134 flash.now[:notice] = t 'user.account.flash update success'
138 if (params[:user][:openid_url].length > 0)
140 @norm_openid_url = OpenIdAuthentication.normalize_identifier(params[:user][:openid_url])
141 if (@norm_openid_url != @user.openid_url)
142 #If the OpenID has changed, we want to check that it is a valid OpenID and one
143 #the user has control over before saving the openID as a password equivalent for
145 openid_verify(@norm_openid_url, false)
148 flash.now[:error] = t 'user.login.openid invalid'
155 def openid_specialcase_mapping(openid_url)
156 #Special case gmail.com, as it is pontentially a popular OpenID provider and unlike
157 #yahoo.com, where it works automatically, Google have hidden their OpenID endpoint
158 #somewhere obscure making it less userfriendly.
159 if (openid_url.match(/(.*)gmail.com(\/?)$/) or openid_url.match(/(.*)googlemail.com(\/?)$/) )
160 return 'https://www.google.com/accounts/o8/id'
166 def openid_verify(openid_url,account_create)
167 authenticate_with_open_id(openid_url) do |result, identity_url|
168 if result.successful?
169 #We need to use the openid url passed back from the OpenID provider
170 #rather than the one supplied by the user, as these can be different.
171 #e.g. one can simply enter yahoo.com in the login box, i.e. no user specific url
172 #only once it comes back from the OpenID provider do we know the unique address for
174 @user = session[:new_usr] unless @user #this is used for account creation when the user is not yet in the database
175 @user.openid_url = identity_url
176 elsif result.missing?
177 mapped_id = openid_specialcase_mapping(openid_url)
179 openid_verify(mapped_id, account_create)
181 flash.now[:error] = t 'user.login.openid missing provider'
183 elsif result.invalid?
184 flash.now[:error] = t 'user.login.openid invalid'
186 flash.now[:error] = t 'user.login.auth failure'
193 if params[:user][:home_lat] and params[:user][:home_lon]
194 @user.home_lat = params[:user][:home_lat].to_f
195 @user.home_lon = params[:user][:home_lon].to_f
197 flash[:notice] = t 'user.set_home.flash success'
198 redirect_to :controller => 'user', :action => 'account'
204 @user.data_public = true
206 flash[:notice] = t 'user.go_public.flash success'
207 redirect_to :controller => 'user', :action => 'account', :display_name => @user.display_name
211 @title = t 'user.lost_password.title'
213 if params[:user] and params[:user][:email]
214 user = User.find_by_email(params[:user][:email], :conditions => {:visible => true})
217 token = user.tokens.create
218 Notifier.deliver_lost_password(user, token)
219 flash.now[:notice] = t 'user.lost_password.notice email on way'
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]
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'
257 # The user is logged in already, so don't show them the signup page, instead
258 # send them to the home page
259 redirect_to :controller => 'site', :action => 'index' if session[:user]
261 @nickname = params['nickname']
262 @email = params['email']
263 @openID = params['openid']
268 #The redirect from the OpenID provider reenters here again
269 #and we need to pass the parameters through to the
270 # open_id_authentication function
271 if params[:open_id_complete]
272 open_id_authentication('')
276 if params[:user] and session[:user].nil?
278 if !params[:user][:openid_url].empty?
279 open_id_authentication(params[:user][:openid_url])
281 email_or_display_name = params[:user][:email]
282 pass = params[:user][:password]
283 user = User.authenticate(:username => email_or_display_name, :password => pass)
285 session[:user] = user.id
286 elsif User.authenticate(:username => email_or_display_name, :password => pass, :inactive => true)
287 flash.now[:error] = t 'user.login.account not active'
289 flash.now[:error] = t 'user.login.auth failure'
295 # The user is logged in, if the referer param exists, redirect them to that
296 # unless they've also got a block on them, in which case redirect them to
297 # the block so they can clear it.
298 user = User.find(session[:user])
299 block = user.blocked_on_view
301 redirect_to block, :referrer => params[:referrer]
302 elsif params[:referer]
303 redirect_to params[:referer]
305 redirect_to :controller => 'site', :action => 'index'
310 @title = t 'user.login.title'
313 def open_id_authentication(openid_url)
314 #TODO: only ask for nickname and email, if we don't already have a user for that openID, in which case
315 #email and nickname are already filled out. I don't know how to do that with ruby syntax though, as we
316 #don't want to duplicate the do block
317 #On the other hand it also doesn't matter too much if we ask every time, as the OpenID provider should
318 #remember these results, and shouldn't repromt the user for these data each time.
319 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|
320 if result.successful?
321 #We need to use the openid url passed back from the OpenID provider
322 #rather than the one supplied by the user, as these can be different.
323 #e.g. one can simply enter yahoo.com in the login box, i.e. no user specific url
324 #only once it comes back from the OpenID provider do we know the unique address for
326 user = User.find_by_openid_url(identity_url)
328 if user.visible? and user.active?
329 session[:user] = user.id
332 flash.now[:error] = t 'user.login.account not active'
335 #We don't have a user registered to this OpenID. Redirect to the create account page
336 #with username and email filled in if they have been given by the OpenID provider through
337 #the simple registration protocol
338 redirect_to :controller => 'user', :action => 'new', :nickname => registration['nickname'], :email => registration['email'], :openid => identity_url
340 else if result.missing?
341 #Try and apply some heuristics to make common cases more userfriendly
342 mapped_id = openid_specialcase_mapping(openid_url)
344 open_id_authentication(mapped_id)
346 flash.now[:error] = t 'user.login.openid missing provider'
348 else if result.invalid?
349 flash.now[:error] = t 'user.login.openid invalid'
351 flash.now[:error] = t 'user.login.auth failure'
360 token = UserToken.find_by_token(session[:token])
364 session[:token] = nil
368 redirect_to params[:referer]
370 redirect_to :controller => 'site', :action => 'index'
375 if params[:confirm_action]
376 token = UserToken.find_by_token(params[:confirm_string])
377 if token and !token.user.active?
380 @user.email_valid = true
382 referer = token.referer
384 flash[:notice] = t 'user.confirm.success'
385 session[:user] = @user.id
389 redirect_to :action => 'account', :display_name => @user.display_name
392 flash.now[:error] = t 'user.confirm.failure'
398 if params[:confirm_action]
399 token = UserToken.find_by_token(params[:confirm_string])
400 if token and token.user.new_email?
402 @user.email = @user.new_email
403 @user.new_email = nil
405 @user.email_valid = true
408 flash[:notice] = t 'user.confirm_email.success'
409 session[:user] = @user.id
410 redirect_to :action => 'account', :display_name => @user.display_name
412 flash.now[:error] = t 'user.confirm_email.failure'
418 @user.image = params[:user][:image]
420 redirect_to :controller => 'user', :action => 'view', :display_name => @user.display_name
426 redirect_to :controller => 'user', :action => 'view', :display_name => @user.display_name
430 doc = OSM::API.new.get_xml_doc
431 @user.traces.each do |trace|
432 doc.root << trace.to_xml_node() if trace.public? or trace.user == @user
434 render :text => doc.to_s, :content_type => "text/xml"
438 @this_user = User.find_by_display_name(params[:display_name])
441 (@this_user.visible? or (@user and @user.administrator?))
442 @title = @this_user.display_name
444 @title = t 'user.no_such_user.title'
445 @not_found_user = params[:display_name]
446 render :action => 'no_such_user', :status => :not_found
451 if params[:display_name]
452 name = params[:display_name]
453 new_friend = User.find_by_display_name(name, :conditions => {:visible => true})
455 friend.user_id = @user.id
456 friend.friend_user_id = new_friend.id
457 unless @user.is_friends_with?(new_friend)
459 flash[:notice] = t 'user.make_friend.success', :name => name
460 Notifier.deliver_friend_notification(friend)
462 friend.add_error(t('user.make_friend.failed', :name => name))
465 flash[:warning] = t 'user.make_friend.already_a_friend', :name => name
468 redirect_to :controller => 'user', :action => 'view'
473 if params[:display_name]
474 name = params[:display_name]
475 friend = User.find_by_display_name(name, :conditions => {:visible => true})
476 if @user.is_friends_with?(friend)
477 Friend.delete_all "user_id = #{@user.id} AND friend_user_id = #{friend.id}"
478 flash[:notice] = t 'user.remove_friend.success', :name => friend.display_name
480 flash[:error] = t 'user.remove_friend.not_a_friend', :name => friend.display_name
483 redirect_to :controller => 'user', :action => 'view'
488 # activate a user, allowing them to log in
490 @this_user.update_attributes(:active => true)
491 redirect_to :controller => 'user', :action => 'view', :display_name => params[:display_name]
495 # deactivate a user, preventing them from logging in
497 @this_user.update_attributes(:active => false)
498 redirect_to :controller => 'user', :action => 'view', :display_name => params[:display_name]
502 # hide a user, marking them as logically deleted
504 @this_user.update_attributes(:visible => false)
505 redirect_to :controller => 'user', :action => 'view', :display_name => params[:display_name]
509 # unhide a user, clearing the logically deleted flag
511 @this_user.update_attributes(:visible => true)
512 redirect_to :controller => 'user', :action => 'view', :display_name => params[:display_name]
516 # delete a user, marking them as deleted and removing personal data
519 redirect_to :controller => 'user', :action => 'view', :display_name => params[:display_name]
523 # require that the user is a administrator, or fill out a helpful error message
524 # and return them to the user page.
525 def require_administrator
526 unless @user.administrator?
527 flash[:error] = t('user.filter.not_an_administrator')
528 redirect_to :controller => 'user', :action => 'view', :display_name => params[:display_name]
533 # ensure that there is a "this_user" instance variable
535 @this_user = User.find_by_display_name(params[:display_name])
536 rescue ActiveRecord::RecordNotFound
537 redirect_to :controller => 'user', :action => 'view', :display_name => params[:display_name] unless @this_user