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 => STATUS == :database_offline
22 @legale = params[:legale] || OSM.IPToCountry(request.remote_ip) || DEFAULT_LEGALE
23 @text = OSM.legal_text_for_country(@legale)
26 render :update do |page|
27 page.replace_html "contributorTerms", :partial => "terms", :locals => { :has_decline => params[:has_decline] }
30 # The redirect from the OpenID provider reenters here
31 # again and we need to pass the parameters through to
32 # the open_id_authentication function
33 @user = session.delete(:new_user)
35 openid_verify(nil, @user) do |user|
38 if @user.openid_url.nil? or @user.invalid?
39 render :action => 'new'
41 render :action => 'terms'
44 session[:referer] = params[:referer]
46 @title = t 'user.terms.title'
47 @user = User.new(params[:user]) if params[:user]
49 if params[:user] and params[:user][:openid_url] and @user.pass_crypt.empty?
50 # We are creating an account with OpenID and no password
51 # was specified so create a random one
52 @user.pass_crypt = ActiveSupport::SecureRandom.base64(16)
53 @user.pass_crypt_confirmation = @user.pass_crypt
59 # Something is wrong with a new user, so rerender the form
60 render :action => :new
62 # Error in existing user, so go to account settings
63 flash[:errors] = @user.errors
64 redirect_to :action => :account, :display_name => @user.display_name
66 elsif @user.terms_agreed?
67 # Already agreed to terms, so just show settings
68 redirect_to :action => :account, :display_name => @user.display_name
69 elsif params[:user] and params[:user][:openid_url]
70 # Verify OpenID before moving on
71 session[:new_user] = @user
72 openid_verify(params[:user][:openid_url], @user)
75 # Not logged in, so redirect to the login page
76 redirect_to :action => :login, :referer => request.request_uri
82 @title = t 'user.new.title'
84 if Acl.find_by_address(request.remote_ip, :conditions => {:k => "no_account_creation"})
85 render :action => 'new'
86 elsif params[:decline]
87 redirect_to t('user.terms.declined')
89 if !@user.terms_agreed?
90 @user.consider_pd = params[:user][:consider_pd]
91 @user.terms_agreed = Time.now.getutc
93 flash[:notice] = t 'user.new.terms accepted'
97 redirect_to :action => :account, :display_name => @user.display_name
99 @user = User.new(params[:user])
101 @user.status = "pending"
102 @user.data_public = true
103 @user.description = "" if @user.description.nil?
104 @user.creation_ip = request.remote_ip
105 @user.languages = request.user_preferred_languages
106 @user.terms_agreed = Time.now.getutc
109 flash[:notice] = t 'user.new.flash create success message', :email => @user.email
110 Notifier.deliver_signup_confirm(@user, @user.tokens.create(:referer => session.delete(:referer)))
111 redirect_to :action => 'login'
113 render :action => 'new'
119 @title = t 'user.account.title'
120 @tokens = @user.oauth_tokens.find :all, :conditions => 'oauth_tokens.invalidated_at is null and oauth_tokens.authorized_at is not null'
122 if params[:user] and params[:user][:display_name] and params[:user][:description]
123 @user.display_name = params[:user][:display_name]
124 @user.new_email = params[:user][:new_email]
126 if params[:user][:pass_crypt].length > 0 or params[:user][:pass_crypt_confirmation].length > 0
127 @user.pass_crypt = params[:user][:pass_crypt]
128 @user.pass_crypt_confirmation = params[:user][:pass_crypt_confirmation]
131 @user.description = params[:user][:description]
132 @user.languages = params[:user][:languages].split(",")
134 case params[:image_action]
135 when "new" then @user.image = params[:user][:image]
136 when "delete" then @user.image = nil
139 @user.home_lat = params[:user][:home_lat]
140 @user.home_lon = params[:user][:home_lon]
142 @user.openid_url = nil if params[:user][:openid_url].empty?
144 if params[:user][:openid_url].length > 0 and
145 params[:user][:openid_url] != @user.openid_url
146 # If the OpenID has changed, we want to check that it is a
147 # valid OpenID and one the user has control over before saving
148 # it as a password equivalent for the user.
149 session[:new_user] = @user
150 openid_verify(params[:user][:openid_url], @user)
155 # The redirect from the OpenID provider reenters here
156 # again and we need to pass the parameters through to
157 # the open_id_authentication function
158 @user = session.delete(:new_user)
159 openid_verify(nil, @user) do |user|
164 flash[:errors].each do |attr,msg|
165 attr = "new_email" if attr == "email" and !@user.new_email.nil?
166 @user.errors.add(attr,msg)
173 @user.data_public = true
175 flash[:notice] = t 'user.go_public.flash success'
176 redirect_to :controller => 'user', :action => 'account', :display_name => @user.display_name
180 @title = t 'user.lost_password.title'
182 if params[:user] and params[:user][:email]
183 user = User.find_by_email(params[:user][:email], :conditions => {:status => ["pending", "active", "confirmed"]})
186 token = user.tokens.create
187 Notifier.deliver_lost_password(user, token)
188 flash[:notice] = t 'user.lost_password.notice email on way'
189 redirect_to :action => 'login'
191 flash.now[:error] = t 'user.lost_password.notice email cannot find'
197 @title = t 'user.reset_password.title'
200 token = UserToken.find_by_token(params[:token])
206 @user.pass_crypt = params[:user][:pass_crypt]
207 @user.pass_crypt_confirmation = params[:user][:pass_crypt_confirmation]
208 @user.status = "active" if @user.status == "pending"
209 @user.email_valid = true
213 flash[:notice] = t 'user.reset_password.flash changed'
214 redirect_to :action => 'login'
218 flash[:error] = t 'user.reset_password.flash token bad'
219 redirect_to :action => 'lost_password'
225 @title = t 'user.new.title'
226 @referer = params[:referer] || session[:referer]
229 # The user is logged in already, so don't show them the signup
230 # page, instead send them to the home page
231 redirect_to :controller => 'site', :action => 'index'
232 elsif not params['openid'].nil?
233 flash.now[:notice] = t 'user.new.openid association'
238 if params[:username] or using_open_id?
239 session[:remember_me] ||= params[:remember_me]
240 session[:referer] ||= params[:referer]
243 openid_authentication(params[:openid_url])
245 password_authentication(params[:username], params[:password])
247 elsif flash[:notice].nil?
248 flash.now[:notice] = t 'user.login.notice'
253 @title = t 'user.logout.title'
255 if params[:session] == request.session_options[:id]
257 token = UserToken.find_by_token(session[:token])
261 session[:token] = nil
264 session_expires_automatically
266 redirect_to params[:referer]
268 redirect_to :controller => 'site', :action => 'index'
275 if token = UserToken.find_by_token(params[:confirm_string])
276 if token.user.active?
277 flash[:error] = t('user.confirm.already active')
278 redirect_to :action => 'login'
281 user.status = "active"
282 user.email_valid = true
284 referer = token.referer
286 session[:user] = user.id
289 flash[:notice] = t('user.confirm.success')
292 flash[:notice] = t('user.confirm.success') + "<br /><br />" + t('user.confirm.before you start')
293 redirect_to :action => 'account', :display_name => user.display_name
297 user = User.find_by_display_name(params[:display_name])
299 if user and user.active?
300 flash[:error] = t('user.confirm.already active')
302 flash[:error] = t('user.confirm.unknown token') + t('user.confirm.reconfirm', :reconfirm => url_for(:action => 'confirm_resend', :display_name => params[:display_name]))
304 flash[:error] = t('user.confirm.unknown token')
307 redirect_to :action => 'login'
313 if user = User.find_by_display_name(params[:display_name])
314 Notifier.deliver_signup_confirm(user, user.tokens.create)
315 flash[:notice] = t 'user.confirm_resend.success', :email => user.email
317 flash[:notice] = t 'user.confirm_resend.failure', :name => params[:display_name]
320 redirect_to :action => 'login'
325 token = UserToken.find_by_token(params[:confirm_string])
326 if token and token.user.new_email?
328 @user.email = @user.new_email
329 @user.new_email = nil
330 @user.email_valid = true
332 flash[:notice] = t 'user.confirm_email.success'
334 flash[:errors] = @user.errors
337 session[:user] = @user.id
338 redirect_to :action => 'account', :display_name => @user.display_name
340 flash[:error] = t 'user.confirm_email.failure'
341 redirect_to :action => 'account', :display_name => @user.display_name
347 doc = OSM::API.new.get_xml_doc
348 @user.traces.each do |trace|
349 doc.root << trace.to_xml_node() if trace.public? or trace.user == @user
351 render :text => doc.to_s, :content_type => "text/xml"
355 @this_user = User.find_by_display_name(params[:display_name])
358 (@this_user.visible? or (@user and @user.administrator?))
359 @title = @this_user.display_name
361 @title = t 'user.no_such_user.title'
362 @not_found_user = params[:display_name]
363 render :action => 'no_such_user', :status => :not_found
368 if params[:display_name]
369 name = params[:display_name]
370 new_friend = User.find_by_display_name(name, :conditions => {:status => ["active", "confirmed"]})
372 friend.user_id = @user.id
373 friend.friend_user_id = new_friend.id
374 unless @user.is_friends_with?(new_friend)
376 flash[:notice] = t 'user.make_friend.success', :name => name
377 Notifier.deliver_friend_notification(friend)
379 friend.add_error(t('user.make_friend.failed', :name => name))
382 flash[:warning] = t 'user.make_friend.already_a_friend', :name => name
386 redirect_to params[:referer]
388 redirect_to :controller => 'user', :action => 'view'
394 if params[:display_name]
395 name = params[:display_name]
396 friend = User.find_by_display_name(name, :conditions => {:status => ["active", "confirmed"]})
397 if @user.is_friends_with?(friend)
398 Friend.delete_all "user_id = #{@user.id} AND friend_user_id = #{friend.id}"
399 flash[:notice] = t 'user.remove_friend.success', :name => friend.display_name
401 flash[:error] = t 'user.remove_friend.not_a_friend', :name => friend.display_name
405 redirect_to params[:referer]
407 redirect_to :controller => 'user', :action => 'view'
413 # sets a user's status
415 @this_user.update_attributes(:status => params[:status])
416 redirect_to :controller => 'user', :action => 'view', :display_name => params[:display_name]
420 # delete a user, marking them as deleted and removing personal data
423 redirect_to :controller => 'user', :action => 'view', :display_name => params[:display_name]
427 # display a list of users matching specified criteria
430 ids = params[:user].keys.collect { |id| id.to_i }
432 User.update_all("status = 'confirmed'", :id => ids) if params[:confirm]
433 User.update_all("status = 'deleted'", :id => ids) if params[:hide]
435 redirect_to url_for(:status => params[:status], :ip => params[:ip], :page => params[:page])
437 conditions = Hash.new
438 conditions[:status] = params[:status] if params[:status]
439 conditions[:creation_ip] = params[:ip] if params[:ip]
441 @user_pages, @users = paginate(:users,
442 :conditions => conditions,
451 # handle password authentication
452 def password_authentication(username, password)
453 if user = User.authenticate(:username => username, :password => password)
454 successful_login(user)
455 elsif user = User.authenticate(:username => username, :password => password, :pending => true)
456 failed_login t('user.login.account not active', :reconfirm => url_for(:action => 'confirm_resend', :display_name => user.display_name))
457 elsif User.authenticate(:username => username, :password => password, :suspended => true)
458 webmaster = link_to t('user.login.webmaster'), "mailto:webmaster@openstreetmap.org"
459 failed_login t('user.login.account suspended', :webmaster => webmaster)
461 failed_login t('user.login.auth failure')
466 # handle OpenID authentication
467 def openid_authentication(openid_url)
468 # If we don't appear to have a user for this URL then ask the
469 # provider for some extra information to help with signup
470 if openid_url and User.find_by_openid_url(openid_url)
473 required = [:nickname, :email, "http://axschema.org/namePerson/friendly", "http://axschema.org/contact/email"]
476 # Start the authentication
477 authenticate_with_open_id(openid_expand_url(openid_url), :required => required) do |result, identity_url, sreg, ax|
478 if result.successful?
479 # We need to use the openid url passed back from the OpenID provider
480 # rather than the one supplied by the user, as these can be different.
482 # For example, you can simply enter yahoo.com in the login box rather
483 # than a user specific url. Only once it comes back from the provider
484 # provider do we know the unique address for the user.
485 if user = User.find_by_openid_url(identity_url)
488 failed_login t('user.login.account not active')
489 when "active", "confirmed" then
490 successful_login(user)
491 when "suspended" then
492 webmaster = link_to t('user.login.webmaster'), "mailto:webmaster@openstreetmap.org"
493 failed_login t('user.login.account suspended', :webmaster => webmaster)
495 failed_login t('user.login.auth failure')
498 # We don't have a user registered to this OpenID, so redirect
499 # to the create account page with username and email filled
500 # in if they have been given by the OpenID provider through
501 # the simple registration protocol.
502 nickname = sreg["nickname"] || ax["http://axschema.org/namePerson/friendly"]
503 email = sreg["email"] || ax["http://axschema.org/contact/email"]
504 redirect_to :controller => 'user', :action => 'new', :nickname => nickname, :email => email, :openid => identity_url
506 elsif result.missing?
507 failed_login t('user.login.openid missing provider')
508 elsif result.invalid?
509 failed_login t('user.login.openid invalid')
511 failed_login t('user.login.auth failure')
517 # verify an OpenID URL
518 def openid_verify(openid_url, user)
519 user.openid_url = openid_url
521 authenticate_with_open_id(openid_expand_url(openid_url)) do |result, identity_url|
522 if result.successful?
523 # We need to use the openid url passed back from the OpenID provider
524 # rather than the one supplied by the user, as these can be different.
526 # For example, you can simply enter yahoo.com in the login box rather
527 # than a user specific url. Only once it comes back from the provider
528 # provider do we know the unique address for the user.
529 user.openid_url = identity_url
531 elsif result.missing?
532 flash.now[:error] = t 'user.login.openid missing provider'
533 elsif result.invalid?
534 flash.now[:error] = t 'user.login.openid invalid'
536 flash.now[:error] = t 'user.login.auth failure'
542 # special case some common OpenID providers by applying heuristics to
543 # try and come up with the correct URL based on what the user entered
544 def openid_expand_url(openid_url)
547 elsif openid_url.match(/(.*)gmail.com(\/?)$/) or openid_url.match(/(.*)googlemail.com(\/?)$/)
548 # Special case gmail.com as it is potentially a popular OpenID
549 # provider and, unlike yahoo.com, where it works automatically, Google
550 # have hidden their OpenID endpoint somewhere obscure this making it
551 # somewhat less user friendly.
552 return 'https://www.google.com/accounts/o8/id'
559 # process a successful login
560 def successful_login(user)
561 session[:user] = user.id
563 session_expires_after 1.month if session[:remember_me]
565 if user.blocked_on_view
566 redirect_to user.blocked_on_view, :referer => params[:referer]
567 elsif session[:referer]
568 redirect_to session[:referer]
570 redirect_to :controller => 'site', :action => 'index'
573 session.delete(:remember_me)
574 session.delete(:referer)
578 # process a failed login
579 def failed_login(message)
580 flash[:error] = message
582 redirect_to :action => 'login', :referer => session[:referer]
584 session.delete(:remember_me)
585 session.delete(:referer)
589 # update a user's details
590 def update_user(user)
594 if user.new_email.nil? or user.new_email.empty?
595 flash.now[:notice] = t 'user.account.flash update success'
597 flash.now[:notice] = t 'user.account.flash update success confirm needed'
600 Notifier.deliver_email_confirm(user, user.tokens.create)
602 # Ignore errors sending email
609 # require that the user is a administrator, or fill out a helpful error message
610 # and return them to the user page.
611 def require_administrator
612 if @user and not @user.administrator?
613 flash[:error] = t('user.filter.not_an_administrator')
615 if params[:display_name]
616 redirect_to :controller => 'user', :action => 'view', :display_name => params[:display_name]
618 redirect_to :controller => 'user', :action => 'login', :referer => request.request_uri
621 redirect_to :controller => 'user', :action => 'login', :referer => request.request_uri
626 # ensure that there is a "this_user" instance variable
628 @this_user = User.find_by_display_name(params[:display_name])
629 rescue ActiveRecord::RecordNotFound
630 redirect_to :controller => 'user', :action => 'view', :display_name => params[:display_name] unless @this_user