1 class UserController < ApplicationController
2 layout "site", :except => [:api_details]
4 skip_before_action :verify_authenticity_token, :only => [:api_read, :api_details, :api_gpx_files, :auth_success]
5 before_action :disable_terms_redirect, :only => [:terms, :save, :logout, :api_details]
6 before_action :authorize, :only => [:api_details, :api_gpx_files]
7 before_action :authorize_web, :except => [:api_read, :api_details, :api_gpx_files]
8 before_action :set_locale, :except => [:api_read, :api_details, :api_gpx_files]
9 before_action :require_user, :only => [:account, :go_public, :make_friend, :remove_friend]
10 before_action :require_self, :only => [:account]
11 before_action :check_database_readable, :except => [:login, :api_read, :api_details, :api_gpx_files]
12 before_action :check_database_writable, :only => [:new, :account, :confirm, :confirm_email, :lost_password, :reset_password, :go_public, :make_friend, :remove_friend]
13 before_action :check_api_readable, :only => [:api_read, :api_details, :api_gpx_files]
14 before_action :require_allow_read_prefs, :only => [:api_details]
15 before_action :require_allow_read_gpx, :only => [:api_gpx_files]
16 before_action :require_cookies, :only => [:new, :login, :confirm]
17 before_action :require_administrator, :only => [:set_status, :delete, :list]
18 around_action :api_call_handle_error, :only => [:api_read, :api_details, :api_gpx_files]
19 before_action :lookup_user_by_id, :only => [:api_read]
20 before_action :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")).html_safe
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
84 if @user.auth_uid.nil? || @user.auth_uid.empty?
85 @user.auth_provider = nil
90 flash[:piwik_goal] = PIWIK["goals"]["signup"] if defined?(PIWIK)
92 referer = welcome_path
95 uri = URI(session[:referer])
96 %r{map=(.*)/(.*)/(.*)}.match(uri.fragment) do |m|
97 editor = Rack::Utils.parse_query(uri.query).slice("editor")
98 referer = welcome_path({ "zoom" => m[1],
100 "lon" => m[3] }.merge(editor))
106 if @user.status == "active"
107 session[:referer] = referer
108 successful_login(@user)
110 session[:token] = @user.tokens.create.token
111 Notifier.signup_confirm(@user, @user.tokens.create(:referer => referer)).deliver_now
112 redirect_to :action => "confirm", :display_name => @user.display_name
115 render :action => "new", :referer => params[:referer]
122 @title = t "user.account.title"
123 @tokens = @user.oauth_tokens.authorized
125 if params[:user] && params[:user][:display_name] && params[:user][:description]
126 if params[:user][:auth_provider].blank? ||
127 (params[:user][:auth_provider] == @user.auth_provider &&
128 params[:user][:auth_uid] == @user.auth_uid)
129 update_user(@user, params)
131 session[:new_user_settings] = params
132 redirect_to auth_url(params[:user][:auth_provider], params[:user][:auth_uid])
138 @user.data_public = true
140 flash[:notice] = t "user.go_public.flash success"
141 redirect_to :controller => "user", :action => "account", :display_name => @user.display_name
145 @title = t "user.lost_password.title"
147 if params[:user] && params[:user][:email]
148 user = User.visible.find_by_email(params[:user][:email])
151 users = User.visible.where("LOWER(email) = LOWER(?)", params[:user][:email])
153 user = users.first if users.count == 1
157 token = user.tokens.create
158 Notifier.lost_password(user, token).deliver_now
159 flash[:notice] = t "user.lost_password.notice email on way"
160 redirect_to :action => "login"
162 flash.now[:error] = t "user.lost_password.notice email cannot find"
168 @title = t "user.reset_password.title"
171 token = UserToken.find_by_token(params[:token])
177 @user.pass_crypt = params[:user][:pass_crypt]
178 @user.pass_crypt_confirmation = params[:user][:pass_crypt_confirmation]
179 @user.status = "active" if @user.status == "pending"
180 @user.email_valid = true
184 flash[:notice] = t "user.reset_password.flash changed"
185 redirect_to :action => "login"
189 flash[:error] = t "user.reset_password.flash token bad"
190 redirect_to :action => "lost_password"
193 render :text => "", :status => :bad_request
198 @title = t "user.new.title"
199 @referer = params[:referer] || session[:referer]
202 # The user is logged in already, so don't show them the signup
203 # page, instead send them to the home page
207 redirect_to :controller => "site", :action => "index"
209 elsif params.key?(:auth_provider) && params.key?(:auth_uid)
210 @user = User.new(:email => params[:email],
211 :email_confirmation => params[:email],
212 :display_name => params[:nickname],
213 :auth_provider => params[:auth_provider],
214 :auth_uid => params[:auth_uid])
216 flash.now[:notice] = render_to_string :partial => "auth_association"
223 @user = User.new(user_params)
225 if check_signup_allowed(@user.email)
226 session[:referer] = params[:referer]
228 @user.status = "pending"
230 if @user.auth_provider.present? && @user.pass_crypt.empty?
231 # We are creating an account with external authentication and
232 # no password was specified so create a random one
233 @user.pass_crypt = SecureRandom.base64(16)
234 @user.pass_crypt_confirmation = @user.pass_crypt
238 # Something is wrong with a new user, so rerender the form
239 render :action => "new"
240 elsif @user.auth_provider.present?
241 # Verify external authenticator before moving on
242 session[:new_user] = @user
243 redirect_to auth_url(@user.auth_provider, @user.auth_uid)
245 # Save the user record
246 session[:new_user] = @user
247 redirect_to :action => :terms
253 session[:referer] = params[:referer] if params[:referer]
255 if params[:username].present? && params[:password].present?
256 session[:remember_me] ||= params[:remember_me]
257 password_authentication(params[:username], params[:password])
258 elsif params[:openid_url].present?
259 session[:remember_me] ||= params[:remember_me_openid]
260 redirect_to auth_url("openid", params[:openid_url], params[:referer])
265 @title = t "user.logout.title"
267 if params[:session] == request.session_options[:id]
269 token = UserToken.find_by_token(session[:token])
270 token.destroy if token
271 session.delete(:token)
273 session.delete(:user)
274 session_expires_automatically
276 redirect_to params[:referer]
278 redirect_to :controller => "site", :action => "index"
285 token = UserToken.find_by_token(params[:confirm_string])
286 if token && token.user.active?
287 flash[:error] = t("user.confirm.already active")
288 redirect_to :action => "login"
289 elsif !token || token.expired?
290 flash[:error] = t("user.confirm.unknown token")
291 redirect_to :action => "confirm"
294 user.status = "active"
295 user.email_valid = true
297 referer = token.referer
301 token = UserToken.find_by_token(session[:token])
302 session.delete(:token)
307 if token.nil? || token.user != user
308 flash[:notice] = t("user.confirm.success")
309 redirect_to :action => :login, :referer => referer
313 session[:user] = user.id
315 redirect_to referer || welcome_path
319 user = User.find_by_display_name(params[:display_name])
321 redirect_to root_path if user.nil? || user.active?
326 user = User.find_by_display_name(params[:display_name])
327 token = UserToken.find_by_token(session[:token])
329 if user.nil? || token.nil? || token.user != user
330 flash[:error] = t "user.confirm_resend.failure", :name => params[:display_name]
332 Notifier.signup_confirm(user, user.tokens.create).deliver_now
333 flash[:notice] = t("user.confirm_resend.success", :email => user.email).html_safe
336 redirect_to :action => "login"
341 token = UserToken.find_by_token(params[:confirm_string])
342 if token && token.user.new_email?
344 @user.email = @user.new_email
345 @user.new_email = nil
346 @user.email_valid = true
348 flash[:notice] = t "user.confirm_email.success"
350 flash[:errors] = @user.errors
353 session[:user] = @user.id
354 redirect_to :action => "account", :display_name => @user.display_name
356 flash[:error] = t "user.confirm_email.failure"
357 redirect_to :action => "account", :display_name => token.user.display_name
359 flash[:error] = t "user.confirm_email.unknown_token"
365 if @this_user.visible?
366 render :action => :api_read, :content_type => "text/xml"
368 render :text => "", :status => :gone
374 render :action => :api_read, :content_type => "text/xml"
378 doc = OSM::API.new.get_xml_doc
379 @user.traces.reload.each do |trace|
380 doc.root << trace.to_xml_node
382 render :text => doc.to_s, :content_type => "text/xml"
386 @this_user = User.find_by_display_name(params[:display_name])
389 (@this_user.visible? || (@user && @user.administrator?))
390 @title = @this_user.display_name
392 render_unknown_user params[:display_name]
397 @new_friend = User.find_by_display_name(params[:display_name])
402 friend.user_id = @user.id
403 friend.friend_user_id = @new_friend.id
404 if @user.is_friends_with?(@new_friend)
405 flash[:warning] = t "user.make_friend.already_a_friend", :name => @new_friend.display_name
408 flash[:notice] = t "user.make_friend.success", :name => @new_friend.display_name
409 Notifier.friend_notification(friend).deliver_now
411 friend.add_error(t("user.make_friend.failed", :name => @new_friend.display_name))
416 redirect_to params[:referer]
418 redirect_to :controller => "user", :action => "view"
422 render_unknown_user params[:display_name]
427 @friend = User.find_by_display_name(params[:display_name])
431 if @user.is_friends_with?(@friend)
432 Friend.delete_all "user_id = #{@user.id} AND friend_user_id = #{@friend.id}"
433 flash[:notice] = t "user.remove_friend.success", :name => @friend.display_name
435 flash[:error] = t "user.remove_friend.not_a_friend", :name => @friend.display_name
439 redirect_to params[:referer]
441 redirect_to :controller => "user", :action => "view"
445 render_unknown_user params[:display_name]
450 # sets a user's status
452 @this_user.status = params[:status]
454 redirect_to :controller => "user", :action => "view", :display_name => params[:display_name]
458 # delete a user, marking them as deleted and removing personal data
461 redirect_to :controller => "user", :action => "view", :display_name => params[:display_name]
465 # display a list of users matching specified criteria
468 ids = params[:user].keys.collect(&:to_i)
470 User.where(:id => ids).update_all(:status => "confirmed") if params[:confirm]
471 User.where(:id => ids).update_all(:status => "deleted") if params[:hide]
473 redirect_to url_for(:status => params[:status], :ip => params[:ip], :page => params[:page])
476 conditions[:status] = params[:status] if params[:status]
477 conditions[:creation_ip] = params[:ip] if params[:ip]
479 @user_pages, @users = paginate(:users,
480 :conditions => conditions,
487 # omniauth success callback
489 auth_info = env["omniauth.auth"]
491 provider = auth_info[:provider]
492 uid = auth_info[:uid]
493 name = auth_info[:info][:name]
494 email = auth_info[:info][:email]
498 email_verified = uid.match(%r{https://www.google.com/accounts/o8/id?(.*)}) ||
499 uid.match(%r{https://me.yahoo.com/(.*)})
500 when "google", "facebook"
501 email_verified = true
503 email_verified = false
506 user = User.find_by_auth_provider_and_auth_uid(provider, uid)
508 if user.nil? && provider == "google"
509 openid_url = auth_info[:extra][:id_info]["openid_id"]
510 user = User.find_by_auth_provider_and_auth_uid("openid", openid_url) if openid_url
511 user.update(:auth_provider => provider, :auth_uid => uid) if user
517 unconfirmed_login(user)
518 when "active", "confirmed" then
519 successful_login(user, env["omniauth.params"]["referer"])
520 when "suspended" then
521 failed_login t("user.login.account is suspended", :webmaster => "mailto:webmaster@openstreetmap.org")
523 failed_login t("user.login.auth failure")
525 elsif settings = session.delete(:new_user_settings)
526 @user.auth_provider = provider
529 update_user(@user, settings)
531 redirect_to :action => "account", :display_name => @user.display_name
532 elsif session[:new_user]
533 session[:new_user].auth_provider = provider
534 session[:new_user].auth_uid = uid
536 if email_verified && email == session[:new_user].email
537 session[:new_user].status = "active"
540 redirect_to :action => "terms"
542 redirect_to :action => "new", :nickname => name, :email => email,
543 :auth_provider => provider, :auth_uid => uid
548 # omniauth failure callback
550 flash[:error] = t("user.auth_failure." + params[:message])
551 redirect_to params[:origin] || login_url
557 # handle password authentication
558 def password_authentication(username, password)
559 if user = User.authenticate(:username => username, :password => password)
560 successful_login(user)
561 elsif user = User.authenticate(:username => username, :password => password, :pending => true)
562 unconfirmed_login(user)
563 elsif User.authenticate(:username => username, :password => password, :suspended => true)
564 failed_login t("user.login.account is suspended", :webmaster => "mailto:webmaster@openstreetmap.org")
566 failed_login t("user.login.auth failure")
571 # return the URL to use for authentication
572 def auth_url(provider, uid, referer = nil)
573 params = { :provider => provider }
575 params[:openid_url] = openid_expand_url(uid) if provider == "openid"
578 params[:origin] = request.path
580 params[:origin] = request.path + "?referer=" + CGI.escape(referer)
581 params[:referer] = referer
588 # special case some common OpenID providers by applying heuristics to
589 # try and come up with the correct URL based on what the user entered
590 def openid_expand_url(openid_url)
593 elsif openid_url.match(%r{(.*)gmail.com(/?)$}) || openid_url.match(%r{(.*)googlemail.com(/?)$})
594 # Special case gmail.com as it is potentially a popular OpenID
595 # provider and, unlike yahoo.com, where it works automatically, Google
596 # have hidden their OpenID endpoint somewhere obscure this making it
597 # somewhat less user friendly.
598 return "https://www.google.com/accounts/o8/id"
605 # process a successful login
606 def successful_login(user, referer = nil)
607 session[:user] = user.id
608 session_expires_after 28.days if session[:remember_me]
610 target = referer || session[:referer] || url_for(:controller => :site, :action => :index)
612 # The user is logged in, so decide where to send them:
614 # - If they haven't seen the contributor terms, send them there.
615 # - If they have a block on them, show them that.
616 # - If they were referred to the login, send them back there.
617 # - Otherwise, send them to the home page.
618 if REQUIRE_TERMS_SEEN && !user.terms_seen
619 redirect_to :controller => :user, :action => :terms, :referer => target
620 elsif user.blocked_on_view
621 redirect_to user.blocked_on_view, :referer => target
626 session.delete(:remember_me)
627 session.delete(:referer)
631 # process a failed login
632 def failed_login(message)
633 flash[:error] = message
635 redirect_to :action => "login", :referer => session[:referer]
637 session.delete(:remember_me)
638 session.delete(:referer)
643 def unconfirmed_login(user)
644 session[:token] = user.tokens.create.token
646 redirect_to :action => "confirm", :display_name => user.display_name
648 session.delete(:remember_me)
649 session.delete(:referer)
653 # update a user's details
654 def update_user(user, params)
655 user.display_name = params[:user][:display_name]
656 user.new_email = params[:user][:new_email]
658 if params[:user][:pass_crypt].length > 0 || params[:user][:pass_crypt_confirmation].length > 0
659 user.pass_crypt = params[:user][:pass_crypt]
660 user.pass_crypt_confirmation = params[:user][:pass_crypt_confirmation]
663 if params[:user][:description] != user.description
664 user.description = params[:user][:description]
665 user.description_format = "markdown"
668 user.languages = params[:user][:languages].split(",")
670 case params[:image_action]
672 user.image = params[:user][:image]
673 user.image_use_gravatar = false
676 user.image_use_gravatar = false
679 user.image_use_gravatar = true
682 user.home_lat = params[:user][:home_lat]
683 user.home_lon = params[:user][:home_lon]
685 if params[:user][:preferred_editor] == "default"
686 user.preferred_editor = nil
688 user.preferred_editor = params[:user][:preferred_editor]
691 if params[:user][:auth_provider].nil? || params[:user][:auth_provider].blank?
692 user.auth_provider = nil
699 if user.new_email.blank? || user.new_email == user.email
700 flash.now[:notice] = t "user.account.flash update success"
702 user.email = user.new_email
705 flash.now[:notice] = t "user.account.flash update success confirm needed"
708 Notifier.email_confirm(user, user.tokens.create).deliver_now
710 # Ignore errors sending email
713 @user.errors.set(:new_email, @user.errors.get(:email))
714 @user.errors.set(:email, [])
723 # require that the user is a administrator, or fill out a helpful error message
724 # and return them to the user page.
725 def require_administrator
726 if @user && !@user.administrator?
727 flash[:error] = t("user.filter.not_an_administrator")
729 if params[:display_name]
730 redirect_to :controller => "user", :action => "view", :display_name => params[:display_name]
732 redirect_to :controller => "user", :action => "login", :referer => request.fullpath
735 redirect_to :controller => "user", :action => "login", :referer => request.fullpath
740 # require that the user in the URL is the logged in user
742 if params[:display_name] != @user.display_name
743 render :text => "", :status => :forbidden
748 # ensure that there is a "this_user" instance variable
749 def lookup_user_by_id
750 @this_user = User.find(params[:id])
754 # ensure that there is a "this_user" instance variable
755 def lookup_user_by_name
756 @this_user = User.find_by_display_name(params[:display_name])
757 rescue ActiveRecord::RecordNotFound
758 redirect_to :controller => "user", :action => "view", :display_name => params[:display_name] unless @this_user
763 def disable_terms_redirect
764 # this is necessary otherwise going to the user terms page, when
765 # having not agreed already would cause an infinite redirect loop.
766 # it's .now so that this doesn't propagate to other pages.
767 flash.now[:skip_terms] = true
771 # return permitted user parameters
773 params.require(:user).permit(:email, :email_confirmation, :display_name,
774 :auth_provider, :auth_uid,
775 :pass_crypt, :pass_crypt_confirmation)
780 def check_signup_allowed(email = nil)
784 domain = email.split("@").last
787 if blocked = Acl.no_account_creation(request.remote_ip, domain)
788 logger.info "Blocked signup from #{request.remote_ip} for #{email}"
790 render :action => "blocked"