1 class UserController < ApplicationController
4 skip_before_filter :verify_authenticity_token, :only => [:api_read, :api_details, :api_gpx_files]
5 before_filter :disable_terms_redirect, :only => [:terms, :save, :logout, :api_details]
6 before_filter :authorize, :only => [:api_details, :api_gpx_files]
7 before_filter :authorize_web, :except => [:api_read, :api_details, :api_gpx_files]
8 before_filter :set_locale, :except => [:api_read, :api_details, :api_gpx_files]
9 before_filter :require_user, :only => [:account, :go_public, :make_friend, :remove_friend]
10 before_filter :check_database_readable, :except => [:login, :api_read, :api_details, :api_gpx_files]
11 before_filter :check_database_writable, :only => [:new, :account, :confirm, :confirm_email, :lost_password, :reset_password, :go_public, :make_friend, :remove_friend]
12 before_filter :check_api_readable, :only => [:api_read, :api_details, :api_gpx_files]
13 before_filter :require_allow_read_prefs, :only => [:api_details]
14 before_filter :require_allow_read_gpx, :only => [:api_gpx_files]
15 before_filter :require_cookies, :only => [:login, :confirm]
16 before_filter :require_administrator, :only => [:set_status, :delete, :list]
17 around_filter :api_call_handle_error, :only => [:api_read, :api_details, :api_gpx_files]
18 before_filter :lookup_user_by_id, :only => [:api_read]
19 before_filter :lookup_user_by_name, :only => [:set_status, :delete]
21 cache_sweeper :user_sweeper, :only => [:account, :set_status, :delete]
24 @legale = params[:legale] || OSM.IPToCountry(request.remote_ip) || DEFAULT_LEGALE
25 @text = OSM.legal_text_for_country(@legale)
28 render :partial => "terms"
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'
43 elsif params[:user] and Acl.no_account_creation(request.remote_ip, params[:user][:email].split("@").last)
44 render :action => 'blocked'
46 session[:referer] = params[:referer]
48 @title = t 'user.terms.title'
49 @user = User.new(params[:user]) if params[:user]
51 if params[:user] and params[:user][:openid_url] and @user.pass_crypt.empty?
52 # We are creating an account with OpenID and no password
53 # was specified so create a random one
54 @user.pass_crypt = SecureRandom.base64(16)
55 @user.pass_crypt_confirmation = @user.pass_crypt
61 # Something is wrong with a new user, so rerender the form
62 render :action => :new
64 # Error in existing user, so go to account settings
65 flash[:errors] = @user.errors
66 redirect_to :action => :account, :display_name => @user.display_name
68 elsif @user.terms_agreed?
69 # Already agreed to terms, so just show settings
70 redirect_to :action => :account, :display_name => @user.display_name
71 elsif params[:user] and params[:user][:openid_url] and not params[:user][:openid_url].empty?
72 # Verify OpenID before moving on
73 session[:new_user] = @user
74 openid_verify(params[:user][:openid_url], @user)
77 # Not logged in, so redirect to the login page
78 redirect_to :action => :login, :referer => request.fullpath
84 @title = t 'user.new.title'
88 @user.terms_seen = true
91 flash[:notice] = t 'user.new.terms declined', :url => t('user.new.terms declined url')
95 redirect_to params[:referer]
97 redirect_to :action => :account, :display_name => @user.display_name
100 redirect_to t('user.terms.declined')
103 if !@user.terms_agreed?
104 @user.consider_pd = params[:user][:consider_pd]
105 @user.terms_agreed = Time.now.getutc
106 @user.terms_seen = true
108 flash[:notice] = t 'user.new.terms accepted'
113 redirect_to params[:referer]
115 redirect_to :action => :account, :display_name => @user.display_name
117 elsif Acl.no_account_creation(request.remote_ip, params[:user][:email].split("@").last)
118 render :action => 'blocked'
120 @user = User.new(params[:user])
122 @user.status = "pending"
123 @user.data_public = true
124 @user.description = "" if @user.description.nil?
125 @user.creation_ip = request.remote_ip
126 @user.languages = request.user_preferred_languages
127 @user.terms_agreed = Time.now.getutc
128 @user.terms_seen = true
129 @user.openid_url = nil if @user.openid_url and @user.openid_url.empty?
132 flash[:piwik_goal] = PIWIK_SIGNUP_GOAL if defined?(PIWIK_SIGNUP_GOAL)
133 flash[:notice] = t 'user.new.flash create success message', :email => @user.email
134 Notifier.signup_confirm(@user, @user.tokens.create(:referer => session.delete(:referer))).deliver
135 session[:token] = @user.tokens.create.token
136 redirect_to :action => 'login', :referer => params[:referer]
138 render :action => 'new', :referer => params[:referer]
144 @title = t 'user.account.title'
145 @tokens = @user.oauth_tokens.authorized
147 if params[:user] and params[:user][:display_name] and params[:user][:description]
148 if params[:user][:openid_url] and
149 params[:user][:openid_url].length > 0 and
150 params[:user][:openid_url] != @user.openid_url
151 # If the OpenID has changed, we want to check that it is a
152 # valid OpenID and one the user has control over before saving
153 # it as a password equivalent for the user.
154 session[:new_user_settings] = params
155 openid_verify(params[:user][:openid_url], @user)
157 update_user(@user, params)
160 # The redirect from the OpenID provider reenters here
161 # again and we need to pass the parameters through to
162 # the open_id_authentication function
163 settings = session.delete(:new_user_settings)
164 openid_verify(nil, @user) do |user|
165 update_user(user, settings)
171 @user.data_public = true
173 flash[:notice] = t 'user.go_public.flash success'
174 redirect_to :controller => 'user', :action => 'account', :display_name => @user.display_name
178 @title = t 'user.lost_password.title'
180 if params[:user] and params[:user][:email]
181 user = User.visible.find_by_email(params[:user][:email])
184 users = User.visible.where("LOWER(email) = LOWER(?)", params[:user][:email])
192 token = user.tokens.create
193 Notifier.lost_password(user, token).deliver
194 flash[:notice] = t 'user.lost_password.notice email on way'
195 redirect_to :action => 'login'
197 flash.now[:error] = t 'user.lost_password.notice email cannot find'
203 @title = t 'user.reset_password.title'
206 token = UserToken.find_by_token(params[:token])
212 @user.pass_crypt = params[:user][:pass_crypt]
213 @user.pass_crypt_confirmation = params[:user][:pass_crypt_confirmation]
214 @user.status = "active" if @user.status == "pending"
215 @user.email_valid = true
219 flash[:notice] = t 'user.reset_password.flash changed'
220 redirect_to :action => 'login'
224 flash[:error] = t 'user.reset_password.flash token bad'
225 redirect_to :action => 'lost_password'
231 @title = t 'user.new.title'
232 @referer = params[:referer] || session[:referer]
235 # The user is logged in already, so don't show them the signup
236 # page, instead send them to the home page
240 redirect_to :controller => 'site', :action => 'index'
242 elsif params.key?(:openid)
243 @user = User.new(:email => params[:email],
244 :email_confirmation => params[:email],
245 :display_name => params[:nickname],
246 :openid_url => params[:openid])
248 flash.now[:notice] = t 'user.new.openid association'
249 elsif Acl.no_account_creation(request.remote_ip)
250 render :action => 'blocked'
255 if params[:username] or using_open_id?
256 session[:remember_me] ||= params[:remember_me]
257 session[:referer] ||= params[:referer]
260 openid_authentication(params[:openid_url])
262 password_authentication(params[:username], params[:password])
268 @title = t 'user.logout.title'
270 if params[:session] == request.session_options[:id]
272 token = UserToken.find_by_token(session[:token])
276 session.delete(:token)
278 session.delete(:user)
279 session_expires_automatically
281 redirect_to params[:referer]
283 redirect_to :controller => 'site', :action => 'index'
290 if token = UserToken.find_by_token(params[:confirm_string])
291 if token.user.active?
292 flash[:error] = t('user.confirm.already active')
293 redirect_to :action => 'login'
296 user.status = "active"
297 user.email_valid = true
299 referer = token.referer
303 token = UserToken.find_by_token(session[:token])
304 session.delete(:token)
309 if token.nil? or token.user != user
310 flash[:notice] = t('user.confirm.success')
311 redirect_to :action => :login, :referer => referer
315 session[:user] = user.id
316 cookies.permanent["_osm_username"] = user.display_name
319 flash[:notice] = t('user.confirm.success') + "<br /><br />" + t('user.confirm.before you start')
320 redirect_to :action => :account, :display_name => user.display_name
322 flash[:notice] = t('user.confirm.success')
328 user = User.find_by_display_name(params[:display_name])
330 if user and user.active?
331 flash[:error] = t('user.confirm.already active')
333 flash[:error] = t('user.confirm.unknown token') + t('user.confirm.reconfirm', :reconfirm => url_for(:action => 'confirm_resend', :display_name => params[:display_name]))
335 flash[:error] = t('user.confirm.unknown token')
338 redirect_to :action => 'login'
344 if user = User.find_by_display_name(params[:display_name])
345 Notifier.signup_confirm(user, user.tokens.create).deliver
346 flash[:notice] = t 'user.confirm_resend.success', :email => user.email
348 flash[:notice] = t 'user.confirm_resend.failure', :name => params[:display_name]
351 redirect_to :action => 'login'
356 token = UserToken.find_by_token(params[:confirm_string])
357 if token and token.user.new_email?
359 @user.email = @user.new_email
360 @user.new_email = nil
361 @user.email_valid = true
363 flash[:notice] = t 'user.confirm_email.success'
365 flash[:errors] = @user.errors
368 session[:user] = @user.id
369 cookies.permanent["_osm_username"] = @user.display_name
370 redirect_to :action => 'account', :display_name => @user.display_name
372 flash[:error] = t 'user.confirm_email.failure'
373 redirect_to :action => 'account', :display_name => @user.display_name
379 render :nothing => true, :status => :gone unless @this_user.visible?
384 render :action => :api_read
388 doc = OSM::API.new.get_xml_doc
389 @user.traces.each do |trace|
390 doc.root << trace.to_xml_node() if trace.public? or trace.user == @user
392 render :text => doc.to_s, :content_type => "text/xml"
396 @this_user = User.find_by_display_name(params[:display_name])
399 (@this_user.visible? or (@user and @user.administrator?))
400 @title = @this_user.display_name
402 render_unknown_user params[:display_name]
407 @new_friend = User.find_by_display_name(params[:display_name])
412 friend.user_id = @user.id
413 friend.friend_user_id = @new_friend.id
414 unless @user.is_friends_with?(@new_friend)
416 flash[:notice] = t 'user.make_friend.success', :name => @new_friend.display_name
417 Notifier.friend_notification(friend).deliver
419 friend.add_error(t('user.make_friend.failed', :name => @new_friend.display_name))
422 flash[:warning] = t 'user.make_friend.already_a_friend', :name => @new_friend.display_name
426 redirect_to params[:referer]
428 redirect_to :controller => 'user', :action => 'view'
432 render_unknown_user params[:display_name]
437 @friend = User.find_by_display_name(params[:display_name])
441 if @user.is_friends_with?(@friend)
442 Friend.delete_all "user_id = #{@user.id} AND friend_user_id = #{@friend.id}"
443 flash[:notice] = t 'user.remove_friend.success', :name => @friend.display_name
445 flash[:error] = t 'user.remove_friend.not_a_friend', :name => @friend.display_name
449 redirect_to params[:referer]
451 redirect_to :controller => 'user', :action => 'view'
455 render_unknown_user params[:display_name]
460 # sets a user's status
462 @this_user.status = params[:status]
464 redirect_to :controller => 'user', :action => 'view', :display_name => params[:display_name]
468 # delete a user, marking them as deleted and removing personal data
471 redirect_to :controller => 'user', :action => 'view', :display_name => params[:display_name]
475 # display a list of users matching specified criteria
478 ids = params[:user].keys.collect { |id| id.to_i }
480 User.update_all("status = 'confirmed'", :id => ids) if params[:confirm]
481 User.update_all("status = 'deleted'", :id => ids) if params[:hide]
483 redirect_to url_for(:status => params[:status], :ip => params[:ip], :page => params[:page])
485 conditions = Hash.new
486 conditions[:status] = params[:status] if params[:status]
487 conditions[:creation_ip] = params[:ip] if params[:ip]
489 @user_pages, @users = paginate(:users,
490 :conditions => conditions,
499 # handle password authentication
500 def password_authentication(username, password)
501 if user = User.authenticate(:username => username, :password => password)
502 successful_login(user)
503 elsif user = User.authenticate(:username => username, :password => password, :pending => true)
504 failed_login t('user.login.account not active', :reconfirm => url_for(:action => 'confirm_resend', :display_name => user.display_name))
505 elsif User.authenticate(:username => username, :password => password, :suspended => true)
506 failed_login t('user.login.account is suspended', :webmaster => "mailto:webmaster@openstreetmap.org")
508 failed_login t('user.login.auth failure')
513 # handle OpenID authentication
514 def openid_authentication(openid_url)
515 # If we don't appear to have a user for this URL then ask the
516 # provider for some extra information to help with signup
517 if openid_url and User.find_by_openid_url(openid_url)
520 required = [:nickname, :email, "http://axschema.org/namePerson/friendly", "http://axschema.org/contact/email"]
523 # Start the authentication
524 authenticate_with_open_id(openid_expand_url(openid_url), :method => :get, :required => required) do |result, identity_url, sreg, ax|
525 if result.successful?
526 # We need to use the openid url passed back from the OpenID provider
527 # rather than the one supplied by the user, as these can be different.
529 # For example, you can simply enter yahoo.com in the login box rather
530 # than a user specific url. Only once it comes back from the provider
531 # provider do we know the unique address for the user.
532 if user = User.find_by_openid_url(identity_url)
535 failed_login t('user.login.account not active', :reconfirm => url_for(:action => 'confirm_resend', :display_name => user.display_name))
536 when "active", "confirmed" then
537 successful_login(user)
538 when "suspended" then
539 failed_login t('user.login.account is suspended', :webmaster => "mailto:webmaster@openstreetmap.org")
541 failed_login t('user.login.auth failure')
544 # Guard against not getting any extension data
545 sreg = Hash.new if sreg.nil?
546 ax = Hash.new if ax.nil?
548 # We don't have a user registered to this OpenID, so redirect
549 # to the create account page with username and email filled
550 # in if they have been given by the OpenID provider through
551 # the simple registration protocol.
552 nickname = sreg["nickname"] || ax["http://axschema.org/namePerson/friendly"].first
553 email = sreg["email"] || ax["http://axschema.org/contact/email"].first
554 redirect_to :controller => 'user', :action => 'new', :nickname => nickname, :email => email, :openid => identity_url
556 elsif result.missing?
557 failed_login t('user.login.openid missing provider')
558 elsif result.invalid?
559 failed_login t('user.login.openid invalid')
561 failed_login t('user.login.auth failure')
567 # verify an OpenID URL
568 def openid_verify(openid_url, user)
569 user.openid_url = openid_url
571 authenticate_with_open_id(openid_expand_url(openid_url), :method => :get) do |result, identity_url|
572 if result.successful?
573 # We need to use the openid url passed back from the OpenID provider
574 # rather than the one supplied by the user, as these can be different.
576 # For example, you can simply enter yahoo.com in the login box rather
577 # than a user specific url. Only once it comes back from the provider
578 # provider do we know the unique address for the user.
579 user.openid_url = identity_url
581 elsif result.missing?
582 flash.now[:error] = t 'user.login.openid missing provider'
583 elsif result.invalid?
584 flash.now[:error] = t 'user.login.openid invalid'
586 flash.now[:error] = t 'user.login.auth failure'
592 # special case some common OpenID providers by applying heuristics to
593 # try and come up with the correct URL based on what the user entered
594 def openid_expand_url(openid_url)
597 elsif openid_url.match(/(.*)gmail.com(\/?)$/) or openid_url.match(/(.*)googlemail.com(\/?)$/)
598 # Special case gmail.com as it is potentially a popular OpenID
599 # provider and, unlike yahoo.com, where it works automatically, Google
600 # have hidden their OpenID endpoint somewhere obscure this making it
601 # somewhat less user friendly.
602 return 'https://www.google.com/accounts/o8/id'
609 # process a successful login
610 def successful_login(user)
611 cookies.permanent["_osm_username"] = user.display_name
613 session[:user] = user.id
614 session_expires_after 28.days if session[:remember_me]
616 target = session[:referer] || url_for(:controller => :site, :action => :index)
618 # The user is logged in, so decide where to send them:
620 # - If they haven't seen the contributor terms, send them there.
621 # - If they have a block on them, show them that.
622 # - If they were referred to the login, send them back there.
623 # - Otherwise, send them to the home page.
624 if REQUIRE_TERMS_SEEN and not user.terms_seen
625 redirect_to :controller => :user, :action => :terms, :referer => target
626 elsif user.blocked_on_view
627 redirect_to user.blocked_on_view, :referer => target
632 session.delete(:remember_me)
633 session.delete(:referer)
637 # process a failed login
638 def failed_login(message)
639 flash[:error] = message
641 redirect_to :action => 'login', :referer => session[:referer]
643 session.delete(:remember_me)
644 session.delete(:referer)
648 # update a user's details
649 def update_user(user, params)
650 user.display_name = params[:user][:display_name]
651 user.new_email = params[:user][:new_email]
653 if params[:user][:pass_crypt].length > 0 or params[:user][:pass_crypt_confirmation].length > 0
654 user.pass_crypt = params[:user][:pass_crypt]
655 user.pass_crypt_confirmation = params[:user][:pass_crypt_confirmation]
658 if params[:user][:description] != user.description
659 user.description = params[:user][:description]
660 user.description_format = "markdown"
663 user.languages = params[:user][:languages].split(",")
665 case params[:image_action]
667 user.image = params[:user][:image]
668 user.image_use_gravatar = false
671 user.image_use_gravatar = false
674 user.image_use_gravatar = true
677 user.home_lat = params[:user][:home_lat]
678 user.home_lon = params[:user][:home_lon]
680 if params[:user][:preferred_editor] == "default"
681 user.preferred_editor = nil
683 user.preferred_editor = params[:user][:preferred_editor]
686 user.openid_url = nil if params[:user][:openid_url].blank?
691 cookies.permanent["_osm_username"] = user.display_name
693 if user.new_email.blank?
694 flash.now[:notice] = t 'user.account.flash update success'
696 user.email = user.new_email
699 flash.now[:notice] = t 'user.account.flash update success confirm needed'
702 Notifier.email_confirm(user, user.tokens.create).deliver
704 # Ignore errors sending email
707 @user.errors.set(:new_email, @user.errors.get(:email))
708 @user.errors.set(:email, [])
717 # require that the user is a administrator, or fill out a helpful error message
718 # and return them to the user page.
719 def require_administrator
720 if @user and not @user.administrator?
721 flash[:error] = t('user.filter.not_an_administrator')
723 if params[:display_name]
724 redirect_to :controller => 'user', :action => 'view', :display_name => params[:display_name]
726 redirect_to :controller => 'user', :action => 'login', :referer => request.fullpath
729 redirect_to :controller => 'user', :action => 'login', :referer => request.fullpath
734 # ensure that there is a "this_user" instance variable
735 def lookup_user_by_id
736 @this_user = User.find(params[:id])
740 # ensure that there is a "this_user" instance variable
741 def lookup_user_by_name
742 @this_user = User.find_by_display_name(params[:display_name])
743 rescue ActiveRecord::RecordNotFound
744 redirect_to :controller => 'user', :action => 'view', :display_name => params[:display_name] unless @this_user
748 # Choose the layout to use. See
749 # https://rails.lighthouseapp.com/projects/8994/tickets/5371-layout-with-onlyexcept-options-makes-other-actions-render-without-layouts
751 oauth_url = url_for(:controller => :oauth, :action => :authorize, :only_path => true)
753 if [ 'api_details' ].include? action_name
755 elsif params[:referer] and URI.parse(params[:referer]).path == oauth_url
764 def disable_terms_redirect
765 # this is necessary otherwise going to the user terms page, when
766 # having not agreed already would cause an infinite redirect loop.
767 # it's .now so that this doesn't propagate to other pages.
768 flash.now[:skip_terms] = true