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], :unless => OSM_STATUS == :database_offline
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"
31 session[:referer] = params[:referer]
33 @user = User.new(params[:user])
35 if params[:user][:openid_url] and @user.pass_crypt.empty?
36 # We are creating an account with OpenID and no password
37 # was specified so create a random one
38 @user.pass_crypt = ActiveSupport::SecureRandom.base64(16)
39 @user.pass_crypt_confirmation = @user.pass_crypt
43 if params[:user][:openid_url].nil? or
44 params[:user][:openid_url].empty?
45 # No OpenID so just move on to the terms
46 render :action => 'terms'
48 # Verify OpenID before moving on
49 session[:new_user] = @user
50 openid_verify(params[:user][:openid_url], @user)
53 # Something is wrong, so rerender the form
54 render :action => 'new'
57 # The redirect from the OpenID provider reenters here
58 # again and we need to pass the parameters through to
59 # the open_id_authentication function
60 @user = session.delete(:new_user)
62 openid_verify(nil, @user) do |user|
65 if @user.openid_url.nil? or @user.invalid?
66 render :action => 'new'
68 render :action => 'terms'
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[:user] and params[:user][:display_name] and params[:user][:description]
105 @user.display_name = params[:user][:display_name]
106 @user.new_email = params[:user][:new_email]
108 if params[:user][:pass_crypt].length > 0 or params[:user][:pass_crypt_confirmation].length > 0
109 @user.pass_crypt = params[:user][:pass_crypt]
110 @user.pass_crypt_confirmation = params[:user][:pass_crypt_confirmation]
113 @user.description = params[:user][:description]
114 @user.languages = params[:user][:languages].split(",")
116 case params[:image_action]
117 when "new" then @user.image = params[:user][:image]
118 when "delete" then @user.image = nil
121 @user.home_lat = params[:user][:home_lat]
122 @user.home_lon = params[:user][:home_lon]
124 @user.openid_url = nil if params[:user][:openid_url].empty?
126 if params[:user][:openid_url].length > 0 and
127 params[:user][:openid_url] != @user.openid_url
128 # If the OpenID has changed, we want to check that it is a
129 # valid OpenID and one the user has control over before saving
130 # it as a password equivalent for the user.
131 session[:new_user] = @user
132 openid_verify(params[:user][:openid_url], @user)
137 # The redirect from the OpenID provider reenters here
138 # again and we need to pass the parameters through to
139 # the open_id_authentication function
140 @user = session.delete(:new_user)
141 openid_verify(nil, @user) do |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'
213 if params[:username] or using_open_id?
214 session[:remember_me] ||= params[:remember_me]
215 session[:referer] ||= params[:referer]
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 webmaster = link_to t('user.login.webmaster'), "mailto:webmaster@openstreetmap.org"
406 failed_login t('user.login.account suspended', :webmaster => webmaster)
408 failed_login t('user.login.auth failure')
413 # handle OpenID authentication
414 def openid_authentication(openid_url)
415 # If we don't appear to have a user for this URL then ask the
416 # provider for some extra information to help with signup
417 if openid_url and User.find_by_openid_url(openid_url)
420 optional = [:nickname, :email]
423 # Start the authentication
424 authenticate_with_open_id(openid_expand_url(openid_url), :optional => optional) do |result, identity_url, registration|
425 if result.successful?
426 # We need to use the openid url passed back from the OpenID provider
427 # rather than the one supplied by the user, as these can be different.
429 # For example, you can simply enter yahoo.com in the login box rather
430 # than a user specific url. Only once it comes back from the provider
431 # provider do we know the unique address for the user.
432 if user = User.find_by_openid_url(identity_url)
435 failed_login t('user.login.account not active')
436 when "active", "confirmed" then
437 successful_login(user)
438 when "suspended" then
439 webmaster = link_to t('user.login.webmaster'), "mailto:webmaster@openstreetmap.org"
440 failed_login t('user.login.account suspended', :webmaster => webmaster)
442 failed_login t('user.login.auth failure')
445 # We don't have a user registered to this OpenID, so redirect
446 # to the create account page with username and email filled
447 # in if they have been given by the OpenID provider through
448 # the simple registration protocol.
449 redirect_to :controller => 'user', :action => 'new', :nickname => registration['nickname'], :email => registration['email'], :openid => identity_url
451 elsif result.missing?
452 failed_login t('user.login.openid missing provider')
453 elsif result.invalid?
454 failed_login t('user.login.openid invalid')
456 failed_login t('user.login.auth failure')
462 # verify an OpenID URL
463 def openid_verify(openid_url, user)
464 user.openid_url = openid_url
466 authenticate_with_open_id(openid_expand_url(openid_url)) do |result, identity_url|
467 if result.successful?
468 # We need to use the openid url passed back from the OpenID provider
469 # rather than the one supplied by the user, as these can be different.
471 # For example, you can simply enter yahoo.com in the login box rather
472 # than a user specific url. Only once it comes back from the provider
473 # provider do we know the unique address for the user.
474 user.openid_url = identity_url
476 elsif result.missing?
477 flash.now[:error] = t 'user.login.openid missing provider'
478 elsif result.invalid?
479 flash.now[:error] = t 'user.login.openid invalid'
481 flash.now[:error] = t 'user.login.auth failure'
487 # special case some common OpenID providers by applying heuristics to
488 # try and come up with the correct URL based on what the user entered
489 def openid_expand_url(openid_url)
492 elsif openid_url.match(/(.*)gmail.com(\/?)$/) or openid_url.match(/(.*)googlemail.com(\/?)$/)
493 # Special case gmail.com as it is potentially a popular OpenID
494 # provider and, unlike yahoo.com, where it works automatically, Google
495 # have hidden their OpenID endpoint somewhere obscure this making it
496 # somewhat less user friendly.
497 return 'https://www.google.com/accounts/o8/id'
504 # process a successful login
505 def successful_login(user)
506 session[:user] = user.id
508 session_expires_after 1.month if session[:remember_me]
510 if user.blocked_on_view
511 redirect_to user.blocked_on_view, :referer => params[:referer]
512 elsif session[:referer]
513 redirect_to session[:referer]
515 redirect_to :controller => 'site', :action => 'index'
518 session.delete(:remember_me)
519 session.delete(:referer)
523 # process a failed login
524 def failed_login(message)
525 flash[:error] = message
527 redirect_to :action => 'login', :referer => session[:referer]
529 session.delete(:remember_me)
530 session.delete(:referer)
534 # update a user's details
535 def update_user(user)
539 if user.new_email.nil? or user.new_email.empty?
540 flash.now[:notice] = t 'user.account.flash update success'
542 flash.now[:notice] = t 'user.account.flash update success confirm needed'
545 Notifier.deliver_email_confirm(user, user.tokens.create)
547 # Ignore errors sending email
554 # require that the user is a administrator, or fill out a helpful error message
555 # and return them to the user page.
556 def require_administrator
557 if @user and not @user.administrator?
558 flash[:error] = t('user.filter.not_an_administrator')
560 if params[:display_name]
561 redirect_to :controller => 'user', :action => 'view', :display_name => params[:display_name]
563 redirect_to :controller => 'user', :action => 'login', :referer => request.request_uri
566 redirect_to :controller => 'user', :action => 'login', :referer => request.request_uri
571 # ensure that there is a "this_user" instance variable
573 @this_user = User.find_by_display_name(params[:display_name])
574 rescue ActiveRecord::RecordNotFound
575 redirect_to :controller => 'user', :action => 'view', :display_name => params[:display_name] unless @this_user