1 class UserController < ApplicationController
2 layout "site", :except => [:api_details]
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 :require_self, :only => [:account]
11 before_filter :check_database_readable, :except => [:login, :api_read, :api_details, :api_gpx_files]
12 before_filter :check_database_writable, :only => [:new, :account, :confirm, :confirm_email, :lost_password, :reset_password, :go_public, :make_friend, :remove_friend]
13 before_filter :check_api_readable, :only => [:api_read, :api_details, :api_gpx_files]
14 before_filter :require_allow_read_prefs, :only => [:api_details]
15 before_filter :require_allow_read_gpx, :only => [:api_gpx_files]
16 before_filter :require_cookies, :only => [:new, :login, :confirm]
17 before_filter :require_administrator, :only => [:set_status, :delete, :list]
18 around_filter :api_call_handle_error, :only => [:api_read, :api_details, :api_gpx_files]
19 before_filter :lookup_user_by_id, :only => [:api_read]
20 before_filter :lookup_user_by_name, :only => [:set_status, :delete]
23 @legale = params[:legale] || OSM.ip_to_country(request.remote_ip) || DEFAULT_LEGALE
24 @text = OSM.legal_text_for_country(@legale)
27 render :partial => "terms"
29 @title = t "user.terms.title"
31 if @user && @user.terms_agreed?
32 # Already agreed to terms, so just show settings
33 redirect_to :action => :account, :display_name => @user.display_name
34 elsif @user.nil? && session[:new_user].nil?
35 redirect_to :action => :login, :referer => request.fullpath
41 @title = t "user.new.title"
45 @user.terms_seen = true
48 flash[:notice] = t "user.new.terms declined", :url => t("user.new.terms declined url")
52 redirect_to params[:referer]
54 redirect_to :action => :account, :display_name => @user.display_name
57 redirect_to t("user.terms.declined")
60 unless @user.terms_agreed?
61 @user.consider_pd = params[:user][:consider_pd]
62 @user.terms_agreed = Time.now.getutc
63 @user.terms_seen = true
65 flash[:notice] = t "user.new.terms accepted" if @user.save
69 redirect_to params[:referer]
71 redirect_to :action => :account, :display_name => @user.display_name
74 @user = session.delete(:new_user)
76 if check_signup_allowed(@user.email)
77 @user.data_public = true
78 @user.description = "" if @user.description.nil?
79 @user.creation_ip = request.remote_ip
80 @user.languages = http_accept_language.user_preferred_languages
81 @user.terms_agreed = Time.now.getutc
82 @user.terms_seen = true
83 @user.openid_url = nil if @user.openid_url && @user.openid_url.empty?
86 flash[:piwik_goal] = PIWIK["goals"]["signup"] if defined?(PIWIK)
88 referer = welcome_path
91 uri = URI(session[:referer])
92 %r{map=(.*)/(.*)/(.*)}.match(uri.fragment) do |m|
93 editor = Rack::Utils.parse_query(uri.query).slice("editor")
94 referer = welcome_path({ "zoom" => m[1],
96 "lon" => m[3] }.merge(editor))
102 if @user.status == "active"
103 session[:referer] = referer
104 successful_login(@user)
106 session[:token] = @user.tokens.create.token
107 Notifier.signup_confirm(@user, @user.tokens.create(:referer => referer)).deliver_now
108 redirect_to :action => "confirm", :display_name => @user.display_name
111 render :action => "new", :referer => params[:referer]
118 @title = t "user.account.title"
119 @tokens = @user.oauth_tokens.authorized
121 if params[:user] && params[:user][:display_name] && params[:user][:description]
122 if params[:user][:openid_url] &&
123 params[:user][:openid_url].length > 0 &&
124 params[:user][:openid_url] != @user.openid_url
125 # If the OpenID has changed, we want to check that it is a
126 # valid OpenID and one the user has control over before saving
127 # it as a password equivalent for the user.
128 session[:new_user_settings] = params
129 openid_verify(params[:user][:openid_url], @user)
131 update_user(@user, params)
134 # The redirect from the OpenID provider reenters here
135 # again and we need to pass the parameters through to
136 # the open_id_authentication function
137 settings = session.delete(:new_user_settings)
138 openid_verify(nil, @user) do |user|
139 update_user(user, settings)
145 @user.data_public = true
147 flash[:notice] = t "user.go_public.flash success"
148 redirect_to :controller => "user", :action => "account", :display_name => @user.display_name
152 @title = t "user.lost_password.title"
154 if params[:user] && params[:user][:email]
155 user = User.visible.find_by_email(params[:user][:email])
158 users = User.visible.where("LOWER(email) = LOWER(?)", params[:user][:email])
160 user = users.first if users.count == 1
164 token = user.tokens.create
165 Notifier.lost_password(user, token).deliver_now
166 flash[:notice] = t "user.lost_password.notice email on way"
167 redirect_to :action => "login"
169 flash.now[:error] = t "user.lost_password.notice email cannot find"
175 @title = t "user.reset_password.title"
178 token = UserToken.find_by_token(params[:token])
184 @user.pass_crypt = params[:user][:pass_crypt]
185 @user.pass_crypt_confirmation = params[:user][:pass_crypt_confirmation]
186 @user.status = "active" if @user.status == "pending"
187 @user.email_valid = true
191 flash[:notice] = t "user.reset_password.flash changed"
192 redirect_to :action => "login"
196 flash[:error] = t "user.reset_password.flash token bad"
197 redirect_to :action => "lost_password"
200 render :text => "", :status => :bad_request
205 @title = t "user.new.title"
206 @referer = params[:referer] || session[:referer]
209 # The redirect from the OpenID provider reenters here
210 # again and we need to pass the parameters through to
211 # the open_id_authentication function
212 @user = session.delete(:new_user)
214 openid_verify(nil, @user) do |user, verified_email|
215 user.status = "active" if user.email == verified_email
218 if @user.openid_url.nil? || @user.invalid?
219 render :action => "new"
221 session[:new_user] = @user
222 redirect_to :action => "terms"
225 # The user is logged in already, so don't show them the signup
226 # page, instead send them to the home page
230 redirect_to :controller => "site", :action => "index"
232 elsif params.key?(:openid)
233 @user = User.new(:email => params[:email],
234 :email_confirmation => params[:email],
235 :display_name => params[:nickname],
236 :openid_url => params[:openid])
238 flash.now[:notice] = t "user.new.openid association"
245 @user = User.new(user_params)
247 if check_signup_allowed(@user.email)
248 session[:referer] = params[:referer]
250 @user.status = "pending"
252 if @user.openid_url.present? && @user.pass_crypt.empty?
253 # We are creating an account with OpenID and no password
254 # was specified so create a random one
255 @user.pass_crypt = SecureRandom.base64(16)
256 @user.pass_crypt_confirmation = @user.pass_crypt
260 # Something is wrong with a new user, so rerender the form
261 render :action => "new"
262 elsif @user.openid_url.present?
263 # Verify OpenID before moving on
264 session[:new_user] = @user
265 openid_verify(@user.openid_url, @user)
267 # Save the user record
268 session[:new_user] = @user
269 redirect_to :action => :terms
275 if params[:username] || using_open_id?
276 session[:referer] ||= params[:referer]
279 session[:remember_me] ||= params[:remember_me_openid]
280 openid_authentication(params[:openid_url])
282 session[:remember_me] ||= params[:remember_me]
283 password_authentication(params[:username], params[:password])
289 @title = t "user.logout.title"
291 if params[:session] == request.session_options[:id]
293 token = UserToken.find_by_token(session[:token])
294 token.destroy if token
295 session.delete(:token)
297 session.delete(:user)
298 session_expires_automatically
300 redirect_to params[:referer]
302 redirect_to :controller => "site", :action => "index"
309 token = UserToken.find_by_token(params[:confirm_string])
310 if token && token.user.active?
311 flash[:error] = t("user.confirm.already active")
312 redirect_to :action => "login"
313 elsif !token || token.expired?
314 flash[:error] = t("user.confirm.unknown token")
315 redirect_to :action => "confirm"
318 user.status = "active"
319 user.email_valid = true
321 referer = token.referer
325 token = UserToken.find_by_token(session[:token])
326 session.delete(:token)
331 if token.nil? || token.user != user
332 flash[:notice] = t("user.confirm.success")
333 redirect_to :action => :login, :referer => referer
337 session[:user] = user.id
339 redirect_to referer || welcome_path
343 user = User.find_by_display_name(params[:display_name])
345 redirect_to root_path if !user || user.active?
350 if user = User.find_by_display_name(params[:display_name])
351 Notifier.signup_confirm(user, user.tokens.create).deliver_now
352 flash[:notice] = t "user.confirm_resend.success", :email => user.email
354 flash[:notice] = t "user.confirm_resend.failure", :name => params[:display_name]
357 redirect_to :action => "login"
362 token = UserToken.find_by_token(params[:confirm_string])
363 if token && token.user.new_email?
365 @user.email = @user.new_email
366 @user.new_email = nil
367 @user.email_valid = true
369 flash[:notice] = t "user.confirm_email.success"
371 flash[:errors] = @user.errors
374 session[:user] = @user.id
375 redirect_to :action => "account", :display_name => @user.display_name
377 flash[:error] = t "user.confirm_email.failure"
378 redirect_to :action => "account", :display_name => @user.display_name
384 render :text => "", :status => :gone unless @this_user.visible?
389 render :action => :api_read
393 doc = OSM::API.new.get_xml_doc
394 @user.traces.each do |trace|
395 doc.root << trace.to_xml_node if trace.public? || trace.user == @user
397 render :text => doc.to_s, :content_type => "text/xml"
401 @this_user = User.find_by_display_name(params[:display_name])
404 (@this_user.visible? || (@user && @user.administrator?))
405 @title = @this_user.display_name
407 render_unknown_user params[:display_name]
412 @new_friend = User.find_by_display_name(params[:display_name])
417 friend.user_id = @user.id
418 friend.friend_user_id = @new_friend.id
419 if @user.is_friends_with?(@new_friend)
420 flash[:warning] = t "user.make_friend.already_a_friend", :name => @new_friend.display_name
423 flash[:notice] = t "user.make_friend.success", :name => @new_friend.display_name
424 Notifier.friend_notification(friend).deliver_now
426 friend.add_error(t("user.make_friend.failed", :name => @new_friend.display_name))
431 redirect_to params[:referer]
433 redirect_to :controller => "user", :action => "view"
437 render_unknown_user params[:display_name]
442 @friend = User.find_by_display_name(params[:display_name])
446 if @user.is_friends_with?(@friend)
447 Friend.delete_all "user_id = #{@user.id} AND friend_user_id = #{@friend.id}"
448 flash[:notice] = t "user.remove_friend.success", :name => @friend.display_name
450 flash[:error] = t "user.remove_friend.not_a_friend", :name => @friend.display_name
454 redirect_to params[:referer]
456 redirect_to :controller => "user", :action => "view"
460 render_unknown_user params[:display_name]
465 # sets a user's status
467 @this_user.status = params[:status]
469 redirect_to :controller => "user", :action => "view", :display_name => params[:display_name]
473 # delete a user, marking them as deleted and removing personal data
476 redirect_to :controller => "user", :action => "view", :display_name => params[:display_name]
480 # display a list of users matching specified criteria
483 ids = params[:user].keys.collect(&:to_i)
485 User.update_all("status = 'confirmed'", :id => ids) if params[:confirm]
486 User.update_all("status = 'deleted'", :id => ids) if params[:hide]
488 redirect_to url_for(:status => params[:status], :ip => params[:ip], :page => params[:page])
491 conditions[:status] = params[:status] if params[:status]
492 conditions[:creation_ip] = params[:ip] if params[:ip]
494 @user_pages, @users = paginate(:users,
495 :conditions => conditions,
504 # handle password authentication
505 def password_authentication(username, password)
506 if user = User.authenticate(:username => username, :password => password)
507 successful_login(user)
508 elsif user = User.authenticate(:username => username, :password => password, :pending => true)
509 unconfirmed_login(user)
510 elsif User.authenticate(:username => username, :password => password, :suspended => true)
511 failed_login t("user.login.account is suspended", :webmaster => "mailto:webmaster@openstreetmap.org")
513 failed_login t("user.login.auth failure")
518 # handle OpenID authentication
519 def openid_authentication(openid_url)
520 # If we don't appear to have a user for this URL then ask the
521 # provider for some extra information to help with signup
522 if openid_url && User.find_by_openid_url(openid_url)
525 required = [:nickname, :email, "http://axschema.org/namePerson/friendly", "http://axschema.org/contact/email"]
528 # Start the authentication
529 authenticate_with_open_id(openid_expand_url(openid_url), :method => :get, :required => required) do |result, identity_url, sreg, ax|
530 if result.successful?
531 # We need to use the openid url passed back from the OpenID provider
532 # rather than the one supplied by the user, as these can be different.
534 # For example, you can simply enter yahoo.com in the login box rather
535 # than a user specific url. Only once it comes back from the provider
536 # provider do we know the unique address for the user.
537 if user = User.find_by_openid_url(identity_url)
540 unconfirmed_login(user)
541 when "active", "confirmed" then
542 successful_login(user)
543 when "suspended" then
544 failed_login t("user.login.account is suspended", :webmaster => "mailto:webmaster@openstreetmap.org")
546 failed_login t("user.login.auth failure")
549 # Guard against not getting any extension data
550 sreg = {} if sreg.nil?
553 # We don't have a user registered to this OpenID, so redirect
554 # to the create account page with username and email filled
555 # in if they have been given by the OpenID provider through
556 # the simple registration protocol.
557 nickname = sreg["nickname"] || ax["http://axschema.org/namePerson/friendly"].first
558 email = sreg["email"] || ax["http://axschema.org/contact/email"].first
560 redirect_to :controller => "user", :action => "new", :nickname => nickname, :email => email, :openid => identity_url
562 elsif result.missing?
563 failed_login t("user.login.openid missing provider")
564 elsif result.invalid?
565 failed_login t("user.login.openid invalid")
567 failed_login t("user.login.auth failure")
573 # verify an OpenID URL
574 def openid_verify(openid_url, user)
575 user.openid_url = openid_url
577 authenticate_with_open_id(openid_expand_url(openid_url), :method => :get, :required => [:email, "http://axschema.org/contact/email"]) do |result, identity_url, sreg, ax|
578 if result.successful?
579 # Do we trust the emails this provider returns?
580 if openid_email_verified(identity_url)
581 # Guard against not getting any extension data
582 sreg = {} if sreg.nil?
585 # Get the verified email
586 verified_email = sreg["email"] || ax["http://axschema.org/contact/email"].first
589 # We need to use the openid url passed back from the OpenID provider
590 # rather than the one supplied by the user, as these can be different.
592 # For example, you can simply enter yahoo.com in the login box rather
593 # than a user specific url. Only once it comes back from the provider
594 # provider do we know the unique address for the user.
595 user.openid_url = identity_url
596 yield user, verified_email
597 elsif result.missing?
598 flash.now[:error] = t "user.login.openid missing provider"
599 elsif result.invalid?
600 flash.now[:error] = t "user.login.openid invalid"
602 flash.now[:error] = t "user.login.auth failure"
608 # special case some common OpenID providers by applying heuristics to
609 # try and come up with the correct URL based on what the user entered
610 def openid_expand_url(openid_url)
613 elsif openid_url.match(/(.*)gmail.com(\/?)$/) || openid_url.match(/(.*)googlemail.com(\/?)$/)
614 # Special case gmail.com as it is potentially a popular OpenID
615 # provider and, unlike yahoo.com, where it works automatically, Google
616 # have hidden their OpenID endpoint somewhere obscure this making it
617 # somewhat less user friendly.
618 return "https://www.google.com/accounts/o8/id"
625 # check if we trust an OpenID provider to return a verified
626 # email, so that we can skpi verifying it ourselves
627 def openid_email_verified(openid_url)
628 openid_url.match(%r{https://www.google.com/accounts/o8/id?(.*)}) ||
629 openid_url.match(%r{https://me.yahoo.com/(.*)})
633 # process a successful login
634 def successful_login(user)
635 session[:user] = user.id
636 session_expires_after 28.days if session[:remember_me]
638 target = session[:referer] || url_for(:controller => :site, :action => :index)
640 # The user is logged in, so decide where to send them:
642 # - If they haven't seen the contributor terms, send them there.
643 # - If they have a block on them, show them that.
644 # - If they were referred to the login, send them back there.
645 # - Otherwise, send them to the home page.
646 if REQUIRE_TERMS_SEEN && !user.terms_seen
647 redirect_to :controller => :user, :action => :terms, :referer => target
648 elsif user.blocked_on_view
649 redirect_to user.blocked_on_view, :referer => target
654 session.delete(:remember_me)
655 session.delete(:referer)
659 # process a failed login
660 def failed_login(message)
661 flash[:error] = message
663 redirect_to :action => "login", :referer => session[:referer]
665 session.delete(:remember_me)
666 session.delete(:referer)
671 def unconfirmed_login(user)
672 redirect_to :action => "confirm", :display_name => user.display_name
674 session.delete(:remember_me)
675 session.delete(:referer)
679 # update a user's details
680 def update_user(user, params)
681 user.display_name = params[:user][:display_name]
682 user.new_email = params[:user][:new_email]
684 if params[:user][:pass_crypt].length > 0 || params[:user][:pass_crypt_confirmation].length > 0
685 user.pass_crypt = params[:user][:pass_crypt]
686 user.pass_crypt_confirmation = params[:user][:pass_crypt_confirmation]
689 if params[:user][:description] != user.description
690 user.description = params[:user][:description]
691 user.description_format = "markdown"
694 user.languages = params[:user][:languages].split(",")
696 case params[:image_action]
698 user.image = params[:user][:image]
699 user.image_use_gravatar = false
702 user.image_use_gravatar = false
705 user.image_use_gravatar = true
708 user.home_lat = params[:user][:home_lat]
709 user.home_lon = params[:user][:home_lon]
711 if params[:user][:preferred_editor] == "default"
712 user.preferred_editor = nil
714 user.preferred_editor = params[:user][:preferred_editor]
717 user.openid_url = nil if params[:user][:openid_url].blank?
722 if user.new_email.blank? || user.new_email == user.email
723 flash.now[:notice] = t "user.account.flash update success"
725 user.email = user.new_email
728 flash.now[:notice] = t "user.account.flash update success confirm needed"
731 Notifier.email_confirm(user, user.tokens.create).deliver_now
733 # Ignore errors sending email
736 @user.errors.set(:new_email, @user.errors.get(:email))
737 @user.errors.set(:email, [])
746 # require that the user is a administrator, or fill out a helpful error message
747 # and return them to the user page.
748 def require_administrator
749 if @user && !@user.administrator?
750 flash[:error] = t("user.filter.not_an_administrator")
752 if params[:display_name]
753 redirect_to :controller => "user", :action => "view", :display_name => params[:display_name]
755 redirect_to :controller => "user", :action => "login", :referer => request.fullpath
758 redirect_to :controller => "user", :action => "login", :referer => request.fullpath
763 # require that the user in the URL is the logged in user
765 if params[:display_name] != @user.display_name
766 render :text => "", :status => :forbidden
771 # ensure that there is a "this_user" instance variable
772 def lookup_user_by_id
773 @this_user = User.find(params[:id])
777 # ensure that there is a "this_user" instance variable
778 def lookup_user_by_name
779 @this_user = User.find_by_display_name(params[:display_name])
780 rescue ActiveRecord::RecordNotFound
781 redirect_to :controller => "user", :action => "view", :display_name => params[:display_name] unless @this_user
786 def disable_terms_redirect
787 # this is necessary otherwise going to the user terms page, when
788 # having not agreed already would cause an infinite redirect loop.
789 # it's .now so that this doesn't propagate to other pages.
790 flash.now[:skip_terms] = true
794 # return permitted user parameters
796 params.require(:user).permit(:email, :email_confirmation, :display_name, :openid_url, :pass_crypt, :pass_crypt_confirmation)
801 def check_signup_allowed(email = nil)
805 domain = email.split("@").last
808 if blocked = Acl.no_account_creation(request.remote_ip, domain)
809 logger.info "Blocked signup from #{request.remote_ip} for #{email}"
811 render :action => "blocked"