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 => [:new, :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]
22 @legale = params[:legale] || OSM.IPToCountry(request.remote_ip) || DEFAULT_LEGALE
23 @text = OSM.legal_text_for_country(@legale)
26 render :partial => "terms"
28 @title = t 'user.terms.title'
30 if @user and @user.terms_agreed?
31 # Already agreed to terms, so just show settings
32 redirect_to :action => :account, :display_name => @user.display_name
33 elsif @user.nil? and session[:new_user].nil?
34 redirect_to :action => :login, :referer => request.fullpath
40 @title = t 'user.new.title'
44 @user.terms_seen = true
47 flash[:notice] = t 'user.new.terms declined', :url => t('user.new.terms declined url')
51 redirect_to params[:referer]
53 redirect_to :action => :account, :display_name => @user.display_name
56 redirect_to t('user.terms.declined')
59 if !@user.terms_agreed?
60 @user.consider_pd = params[:user][:consider_pd]
61 @user.terms_agreed = Time.now.getutc
62 @user.terms_seen = true
64 flash[:notice] = t 'user.new.terms accepted'
69 redirect_to params[:referer]
71 redirect_to :action => :account, :display_name => @user.display_name
74 @user = session.delete(:new_user)
76 if Acl.no_account_creation(request.remote_ip, @user.email.split("@").last)
77 render :action => 'blocked'
79 @user.data_public = true
80 @user.description = "" if @user.description.nil?
81 @user.creation_ip = request.remote_ip
82 @user.languages = http_accept_language.user_preferred_languages
83 @user.terms_agreed = Time.now.getutc
84 @user.terms_seen = true
85 @user.openid_url = nil if @user.openid_url and @user.openid_url.empty?
88 flash[:piwik_goal] = PIWIK["goals"]["signup"] if defined?(PIWIK)
90 referer = welcome_path
93 uri = URI(session[:referer])
94 /map=(.*)\/(.*)\/(.*)/.match(uri.fragment) do |m|
95 editor = Rack::Utils.parse_query(uri.query).slice('editor')
96 referer = welcome_path({'zoom' => m[1],
98 'lon' => m[3]}.merge(editor))
104 if @user.status == "active"
105 session[:referer] = referer
106 successful_login(@user)
108 session[:token] = @user.tokens.create.token
109 Notifier.signup_confirm(@user, @user.tokens.create(:referer => referer)).deliver
110 redirect_to :action => 'confirm', :display_name => @user.display_name
113 render :action => 'new', :referer => params[:referer]
120 @title = t 'user.account.title'
121 @tokens = @user.oauth_tokens.authorized
123 if params[:user] and params[:user][:display_name] and params[:user][:description]
124 if params[:user][:openid_url] and
125 params[:user][:openid_url].length > 0 and
126 params[:user][:openid_url] != @user.openid_url
127 # If the OpenID has changed, we want to check that it is a
128 # valid OpenID and one the user has control over before saving
129 # it as a password equivalent for the user.
130 session[:new_user_settings] = params
131 openid_verify(params[:user][:openid_url], @user)
133 update_user(@user, params)
136 # The redirect from the OpenID provider reenters here
137 # again and we need to pass the parameters through to
138 # the open_id_authentication function
139 settings = session.delete(:new_user_settings)
140 openid_verify(nil, @user) do |user|
141 update_user(user, settings)
147 @user.data_public = true
149 flash[:notice] = t 'user.go_public.flash success'
150 redirect_to :controller => 'user', :action => 'account', :display_name => @user.display_name
154 @title = t 'user.lost_password.title'
156 if params[:user] and params[:user][:email]
157 user = User.visible.find_by_email(params[:user][:email])
160 users = User.visible.where("LOWER(email) = LOWER(?)", params[:user][:email])
168 token = user.tokens.create
169 Notifier.lost_password(user, token).deliver
170 flash[:notice] = t 'user.lost_password.notice email on way'
171 redirect_to :action => 'login'
173 flash.now[:error] = t 'user.lost_password.notice email cannot find'
179 @title = t 'user.reset_password.title'
182 token = UserToken.find_by_token(params[:token])
188 @user.pass_crypt = params[:user][:pass_crypt]
189 @user.pass_crypt_confirmation = params[:user][:pass_crypt_confirmation]
190 @user.status = "active" if @user.status == "pending"
191 @user.email_valid = true
195 flash[:notice] = t 'user.reset_password.flash changed'
196 redirect_to :action => 'login'
200 flash[:error] = t 'user.reset_password.flash token bad'
201 redirect_to :action => 'lost_password'
207 @title = t 'user.new.title'
208 @referer = params[:referer] || session[:referer]
211 # The redirect from the OpenID provider reenters here
212 # again and we need to pass the parameters through to
213 # the open_id_authentication function
214 @user = session.delete(:new_user)
216 openid_verify(nil, @user) do |user, verified_email|
217 user.status = "active" if user.email == verified_email
220 if @user.openid_url.nil? or @user.invalid?
221 render :action => 'new'
223 session[:new_user] = @user
224 redirect_to :action => 'terms'
227 # The user is logged in already, so don't show them the signup
228 # page, instead send them to the home page
232 redirect_to :controller => 'site', :action => 'index'
234 elsif params.key?(:openid)
235 @user = User.new(:email => params[:email],
236 :email_confirmation => params[:email],
237 :display_name => params[:nickname],
238 :openid_url => params[:openid])
240 flash.now[:notice] = t 'user.new.openid association'
241 elsif Acl.no_account_creation(request.remote_ip)
242 render :action => 'blocked'
247 if params[:user] and Acl.no_account_creation(request.remote_ip, params[:user][:email].split("@").last)
248 render :action => 'blocked'
251 session[:referer] = params[:referer]
253 @user = User.new(user_params)
254 @user.status = "pending"
256 if @user.openid_url.present? && @user.pass_crypt.empty?
257 # We are creating an account with OpenID and no password
258 # was specified so create a random one
259 @user.pass_crypt = SecureRandom.base64(16)
260 @user.pass_crypt_confirmation = @user.pass_crypt
264 # Something is wrong with a new user, so rerender the form
265 render :action => "new"
266 elsif @user.openid_url.present?
267 # Verify OpenID before moving on
268 session[:new_user] = @user
269 openid_verify(@user.openid_url, @user)
271 # Save the user record
272 session[:new_user] = @user
273 redirect_to :action => :terms
279 if params[:username] or using_open_id?
280 session[:remember_me] ||= params[:remember_me]
281 session[:referer] ||= params[:referer]
284 openid_authentication(params[:openid_url])
286 password_authentication(params[:username], params[:password])
292 @title = t 'user.logout.title'
294 if params[:session] == request.session_options[:id]
296 token = UserToken.find_by_token(session[:token])
300 session.delete(:token)
302 session.delete(:user)
303 session_expires_automatically
305 redirect_to params[:referer]
307 redirect_to :controller => 'site', :action => 'index'
314 token = UserToken.find_by_token(params[:confirm_string])
315 if token && token.user.active?
316 flash[:error] = t('user.confirm.already active')
317 redirect_to :action => 'login'
318 elsif !token || token.expired?
319 flash[:error] = t('user.confirm.unknown token')
320 redirect_to :action => 'confirm'
323 user.status = "active"
324 user.email_valid = true
326 referer = token.referer
330 token = UserToken.find_by_token(session[:token])
331 session.delete(:token)
336 if token.nil? or token.user != user
337 flash[:notice] = t('user.confirm.success')
338 redirect_to :action => :login, :referer => referer
342 session[:user] = user.id
343 cookies.permanent["_osm_username"] = user.display_name
345 redirect_to referer || welcome_path
349 user = User.find_by_display_name(params[:display_name])
350 if !user || user.active?
351 redirect_to root_path
357 if user = User.find_by_display_name(params[:display_name])
358 Notifier.signup_confirm(user, user.tokens.create).deliver
359 flash[:notice] = t 'user.confirm_resend.success', :email => user.email
361 flash[:notice] = t 'user.confirm_resend.failure', :name => params[:display_name]
364 redirect_to :action => 'login'
369 token = UserToken.find_by_token(params[:confirm_string])
370 if token and token.user.new_email?
372 @user.email = @user.new_email
373 @user.new_email = nil
374 @user.email_valid = true
376 flash[:notice] = t 'user.confirm_email.success'
378 flash[:errors] = @user.errors
381 session[:user] = @user.id
382 cookies.permanent["_osm_username"] = @user.display_name
383 redirect_to :action => 'account', :display_name => @user.display_name
385 flash[:error] = t 'user.confirm_email.failure'
386 redirect_to :action => 'account', :display_name => @user.display_name
392 render :text => "", :status => :gone unless @this_user.visible?
397 render :action => :api_read
401 doc = OSM::API.new.get_xml_doc
402 @user.traces.each do |trace|
403 doc.root << trace.to_xml_node() if trace.public? or trace.user == @user
405 render :text => doc.to_s, :content_type => "text/xml"
409 @this_user = User.find_by_display_name(params[:display_name])
412 (@this_user.visible? or (@user and @user.administrator?))
413 @title = @this_user.display_name
415 render_unknown_user params[:display_name]
420 @new_friend = User.find_by_display_name(params[:display_name])
425 friend.user_id = @user.id
426 friend.friend_user_id = @new_friend.id
427 unless @user.is_friends_with?(@new_friend)
429 flash[:notice] = t 'user.make_friend.success', :name => @new_friend.display_name
430 Notifier.friend_notification(friend).deliver
432 friend.add_error(t('user.make_friend.failed', :name => @new_friend.display_name))
435 flash[:warning] = t 'user.make_friend.already_a_friend', :name => @new_friend.display_name
439 redirect_to params[:referer]
441 redirect_to :controller => 'user', :action => 'view'
445 render_unknown_user params[:display_name]
450 @friend = User.find_by_display_name(params[:display_name])
454 if @user.is_friends_with?(@friend)
455 Friend.delete_all "user_id = #{@user.id} AND friend_user_id = #{@friend.id}"
456 flash[:notice] = t 'user.remove_friend.success', :name => @friend.display_name
458 flash[:error] = t 'user.remove_friend.not_a_friend', :name => @friend.display_name
462 redirect_to params[:referer]
464 redirect_to :controller => 'user', :action => 'view'
468 render_unknown_user params[:display_name]
473 # sets a user's status
475 @this_user.status = params[:status]
477 redirect_to :controller => 'user', :action => 'view', :display_name => params[:display_name]
481 # delete a user, marking them as deleted and removing personal data
484 redirect_to :controller => 'user', :action => 'view', :display_name => params[:display_name]
488 # display a list of users matching specified criteria
491 ids = params[:user].keys.collect { |id| id.to_i }
493 User.update_all("status = 'confirmed'", :id => ids) if params[:confirm]
494 User.update_all("status = 'deleted'", :id => ids) if params[:hide]
496 redirect_to url_for(:status => params[:status], :ip => params[:ip], :page => params[:page])
498 conditions = Hash.new
499 conditions[:status] = params[:status] if params[:status]
500 conditions[:creation_ip] = params[:ip] if params[:ip]
502 @user_pages, @users = paginate(:users,
503 :conditions => conditions,
512 # handle password authentication
513 def password_authentication(username, password)
514 if user = User.authenticate(:username => username, :password => password)
515 successful_login(user)
516 elsif user = User.authenticate(:username => username, :password => password, :pending => true)
517 unconfirmed_login(user)
518 elsif User.authenticate(:username => username, :password => password, :suspended => true)
519 failed_login t('user.login.account is suspended', :webmaster => "mailto:webmaster@openstreetmap.org")
521 failed_login t('user.login.auth failure')
526 # handle OpenID authentication
527 def openid_authentication(openid_url)
528 # If we don't appear to have a user for this URL then ask the
529 # provider for some extra information to help with signup
530 if openid_url and User.find_by_openid_url(openid_url)
533 required = [:nickname, :email, "http://axschema.org/namePerson/friendly", "http://axschema.org/contact/email"]
536 # Start the authentication
537 authenticate_with_open_id(openid_expand_url(openid_url), :method => :get, :required => required) do |result, identity_url, sreg, ax|
538 if result.successful?
539 # We need to use the openid url passed back from the OpenID provider
540 # rather than the one supplied by the user, as these can be different.
542 # For example, you can simply enter yahoo.com in the login box rather
543 # than a user specific url. Only once it comes back from the provider
544 # provider do we know the unique address for the user.
545 if user = User.find_by_openid_url(identity_url)
548 unconfirmed_login(user)
549 when "active", "confirmed" then
550 successful_login(user)
551 when "suspended" then
552 failed_login t('user.login.account is suspended', :webmaster => "mailto:webmaster@openstreetmap.org")
554 failed_login t('user.login.auth failure')
557 # Guard against not getting any extension data
558 sreg = Hash.new if sreg.nil?
559 ax = Hash.new if ax.nil?
561 # We don't have a user registered to this OpenID, so redirect
562 # to the create account page with username and email filled
563 # in if they have been given by the OpenID provider through
564 # the simple registration protocol.
565 nickname = sreg["nickname"] || ax["http://axschema.org/namePerson/friendly"].first
566 email = sreg["email"] || ax["http://axschema.org/contact/email"].first
568 redirect_to :controller => 'user', :action => 'new', :nickname => nickname, :email => email, :openid => identity_url
570 elsif result.missing?
571 failed_login t('user.login.openid missing provider')
572 elsif result.invalid?
573 failed_login t('user.login.openid invalid')
575 failed_login t('user.login.auth failure')
581 # verify an OpenID URL
582 def openid_verify(openid_url, user)
583 user.openid_url = openid_url
585 authenticate_with_open_id(openid_expand_url(openid_url), :method => :get, :required => [:email, "http://axschema.org/contact/email"]) do |result, identity_url, sreg, ax|
586 if result.successful?
587 # Do we trust the emails this provider returns?
588 if openid_email_verified(identity_url)
589 # Guard against not getting any extension data
590 sreg = Hash.new if sreg.nil?
591 ax = Hash.new if ax.nil?
593 # Get the verified email
594 verified_email = sreg["email"] || ax["http://axschema.org/contact/email"].first
597 # We need to use the openid url passed back from the OpenID provider
598 # rather than the one supplied by the user, as these can be different.
600 # For example, you can simply enter yahoo.com in the login box rather
601 # than a user specific url. Only once it comes back from the provider
602 # provider do we know the unique address for the user.
603 user.openid_url = identity_url
604 yield user, verified_email
605 elsif result.missing?
606 flash.now[:error] = t 'user.login.openid missing provider'
607 elsif result.invalid?
608 flash.now[:error] = t 'user.login.openid invalid'
610 flash.now[:error] = t 'user.login.auth failure'
616 # special case some common OpenID providers by applying heuristics to
617 # try and come up with the correct URL based on what the user entered
618 def openid_expand_url(openid_url)
621 elsif openid_url.match(/(.*)gmail.com(\/?)$/) or openid_url.match(/(.*)googlemail.com(\/?)$/)
622 # Special case gmail.com as it is potentially a popular OpenID
623 # provider and, unlike yahoo.com, where it works automatically, Google
624 # have hidden their OpenID endpoint somewhere obscure this making it
625 # somewhat less user friendly.
626 return 'https://www.google.com/accounts/o8/id'
633 # check if we trust an OpenID provider to return a verified
634 # email, so that we can skpi verifying it ourselves
635 def openid_email_verified(openid_url)
636 openid_url.match(/https:\/\/www.google.com\/accounts\/o8\/id?(.*)/) or
637 openid_url.match(/https:\/\/me.yahoo.com\/(.*)/)
641 # process a successful login
642 def successful_login(user)
643 cookies.permanent["_osm_username"] = user.display_name
645 session[:user] = user.id
646 session_expires_after 28.days if session[:remember_me]
648 target = session[:referer] || url_for(:controller => :site, :action => :index)
650 # The user is logged in, so decide where to send them:
652 # - If they haven't seen the contributor terms, send them there.
653 # - If they have a block on them, show them that.
654 # - If they were referred to the login, send them back there.
655 # - Otherwise, send them to the home page.
656 if REQUIRE_TERMS_SEEN and not user.terms_seen
657 redirect_to :controller => :user, :action => :terms, :referer => target
658 elsif user.blocked_on_view
659 redirect_to user.blocked_on_view, :referer => target
664 session.delete(:remember_me)
665 session.delete(:referer)
669 # process a failed login
670 def failed_login(message)
671 flash[:error] = message
673 redirect_to :action => 'login', :referer => session[:referer]
675 session.delete(:remember_me)
676 session.delete(:referer)
681 def unconfirmed_login(user)
682 redirect_to :action => 'confirm', :display_name => user.display_name
684 session.delete(:remember_me)
685 session.delete(:referer)
689 # update a user's details
690 def update_user(user, params)
691 user.display_name = params[:user][:display_name]
692 user.new_email = params[:user][:new_email]
694 if params[:user][:pass_crypt].length > 0 or params[:user][:pass_crypt_confirmation].length > 0
695 user.pass_crypt = params[:user][:pass_crypt]
696 user.pass_crypt_confirmation = params[:user][:pass_crypt_confirmation]
699 if params[:user][:description] != user.description
700 user.description = params[:user][:description]
701 user.description_format = "markdown"
704 user.languages = params[:user][:languages].split(",")
706 case params[:image_action]
708 user.image = params[:user][:image]
709 user.image_use_gravatar = false
712 user.image_use_gravatar = false
715 user.image_use_gravatar = true
718 user.home_lat = params[:user][:home_lat]
719 user.home_lon = params[:user][:home_lon]
721 if params[:user][:preferred_editor] == "default"
722 user.preferred_editor = nil
724 user.preferred_editor = params[:user][:preferred_editor]
727 user.openid_url = nil if params[:user][:openid_url].blank?
732 cookies.permanent["_osm_username"] = user.display_name
734 if user.new_email.blank? or user.new_email == user.email
735 flash.now[:notice] = t 'user.account.flash update success'
737 user.email = user.new_email
740 flash.now[:notice] = t 'user.account.flash update success confirm needed'
743 Notifier.email_confirm(user, user.tokens.create).deliver
745 # Ignore errors sending email
748 @user.errors.set(:new_email, @user.errors.get(:email))
749 @user.errors.set(:email, [])
758 # require that the user is a administrator, or fill out a helpful error message
759 # and return them to the user page.
760 def require_administrator
761 if @user and not @user.administrator?
762 flash[:error] = t('user.filter.not_an_administrator')
764 if params[:display_name]
765 redirect_to :controller => 'user', :action => 'view', :display_name => params[:display_name]
767 redirect_to :controller => 'user', :action => 'login', :referer => request.fullpath
770 redirect_to :controller => 'user', :action => 'login', :referer => request.fullpath
775 # ensure that there is a "this_user" instance variable
776 def lookup_user_by_id
777 @this_user = User.find(params[:id])
781 # ensure that there is a "this_user" instance variable
782 def lookup_user_by_name
783 @this_user = User.find_by_display_name(params[:display_name])
784 rescue ActiveRecord::RecordNotFound
785 redirect_to :controller => 'user', :action => 'view', :display_name => params[:display_name] unless @this_user
789 # Choose the layout to use. See
790 # https://rails.lighthouseapp.com/projects/8994/tickets/5371-layout-with-onlyexcept-options-makes-other-actions-render-without-layouts
792 oauth_url = url_for(:controller => :oauth, :action => :authorize, :only_path => true)
794 if [ 'api_details' ].include? action_name
796 elsif params[:referer] and URI.parse(params[:referer]).path == oauth_url
805 def disable_terms_redirect
806 # this is necessary otherwise going to the user terms page, when
807 # having not agreed already would cause an infinite redirect loop.
808 # it's .now so that this doesn't propagate to other pages.
809 flash.now[:skip_terms] = true
813 # return permitted user parameters
815 params.require(:user).permit(:email, :email_confirmation, :display_name, :openid_url, :pass_crypt, :pass_crypt_confirmation)