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, :auth_success]
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_url = openid_expand_url(params[:user][:openid_url])
130 redirect_to auth_path(:provider => "openid", :openid_url => openid_url, :origin => request.path)
132 update_user(@user, params)
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?(:openid)
210 @user = User.new(:email => params[:email],
211 :email_confirmation => params[:email],
212 :display_name => params[:nickname],
213 :openid_url => params[:openid])
215 flash.now[:notice] = t "user.new.openid association"
222 @user = User.new(user_params)
224 if check_signup_allowed(@user.email)
225 session[:referer] = params[:referer]
227 @user.status = "pending"
229 if @user.openid_url.present? && @user.pass_crypt.empty?
230 # We are creating an account with OpenID and no password
231 # was specified so create a random one
232 @user.pass_crypt = SecureRandom.base64(16)
233 @user.pass_crypt_confirmation = @user.pass_crypt
237 # Something is wrong with a new user, so rerender the form
238 render :action => "new"
239 elsif @user.openid_url.present?
240 # Verify OpenID before moving on
241 session[:new_user] = @user
242 openid_url = openid_expand_url(@user.openid_url)
243 redirect_to auth_path(:provider => "openid", :openid_url => openid_url, :origin => request.path)
245 # Save the user record
246 session[:new_user] = @user
247 redirect_to :action => :terms
253 if params[:username] || params[:openid_url]
254 session[:referer] ||= params[:referer]
256 if params[:openid_url].present?
257 session[:remember_me] ||= params[:remember_me_openid]
258 openid_url = openid_expand_url(params[:openid_url])
259 redirect_to auth_path(:provider => "openid", :openid_url => openid_url, :origin => request.path)
261 session[:remember_me] ||= params[:remember_me]
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])
273 token.destroy if token
274 session.delete(:token)
276 session.delete(:user)
277 session_expires_automatically
279 redirect_to params[:referer]
281 redirect_to :controller => "site", :action => "index"
288 token = UserToken.find_by_token(params[:confirm_string])
289 if token && token.user.active?
290 flash[:error] = t("user.confirm.already active")
291 redirect_to :action => "login"
292 elsif !token || token.expired?
293 flash[:error] = t("user.confirm.unknown token")
294 redirect_to :action => "confirm"
297 user.status = "active"
298 user.email_valid = true
300 referer = token.referer
304 token = UserToken.find_by_token(session[:token])
305 session.delete(:token)
310 if token.nil? || token.user != user
311 flash[:notice] = t("user.confirm.success")
312 redirect_to :action => :login, :referer => referer
316 session[:user] = user.id
318 redirect_to referer || welcome_path
322 user = User.find_by_display_name(params[:display_name])
324 redirect_to root_path if !user || user.active?
329 if user = User.find_by_display_name(params[:display_name])
330 Notifier.signup_confirm(user, user.tokens.create).deliver_now
331 flash[:notice] = t "user.confirm_resend.success", :email => user.email
333 flash[:notice] = t "user.confirm_resend.failure", :name => params[:display_name]
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 => @user.display_name
363 render :text => "", :status => :gone unless @this_user.visible?
368 render :action => :api_read
372 doc = OSM::API.new.get_xml_doc
373 @user.traces.each do |trace|
374 doc.root << trace.to_xml_node if trace.public? || trace.user == @user
376 render :text => doc.to_s, :content_type => "text/xml"
380 @this_user = User.find_by_display_name(params[:display_name])
383 (@this_user.visible? || (@user && @user.administrator?))
384 @title = @this_user.display_name
386 render_unknown_user params[:display_name]
391 @new_friend = User.find_by_display_name(params[:display_name])
396 friend.user_id = @user.id
397 friend.friend_user_id = @new_friend.id
398 if @user.is_friends_with?(@new_friend)
399 flash[:warning] = t "user.make_friend.already_a_friend", :name => @new_friend.display_name
402 flash[:notice] = t "user.make_friend.success", :name => @new_friend.display_name
403 Notifier.friend_notification(friend).deliver_now
405 friend.add_error(t("user.make_friend.failed", :name => @new_friend.display_name))
410 redirect_to params[:referer]
412 redirect_to :controller => "user", :action => "view"
416 render_unknown_user params[:display_name]
421 @friend = User.find_by_display_name(params[:display_name])
425 if @user.is_friends_with?(@friend)
426 Friend.delete_all "user_id = #{@user.id} AND friend_user_id = #{@friend.id}"
427 flash[:notice] = t "user.remove_friend.success", :name => @friend.display_name
429 flash[:error] = t "user.remove_friend.not_a_friend", :name => @friend.display_name
433 redirect_to params[:referer]
435 redirect_to :controller => "user", :action => "view"
439 render_unknown_user params[:display_name]
444 # sets a user's status
446 @this_user.status = params[:status]
448 redirect_to :controller => "user", :action => "view", :display_name => params[:display_name]
452 # delete a user, marking them as deleted and removing personal data
455 redirect_to :controller => "user", :action => "view", :display_name => params[:display_name]
459 # display a list of users matching specified criteria
462 ids = params[:user].keys.collect(&:to_i)
464 User.update_all("status = 'confirmed'", :id => ids) if params[:confirm]
465 User.update_all("status = 'deleted'", :id => ids) if params[:hide]
467 redirect_to url_for(:status => params[:status], :ip => params[:ip], :page => params[:page])
470 conditions[:status] = params[:status] if params[:status]
471 conditions[:creation_ip] = params[:ip] if params[:ip]
473 @user_pages, @users = paginate(:users,
474 :conditions => conditions,
481 # omniauth success callback
483 auth_info = env["omniauth.auth"]
485 openid_url = auth_info[:uid]
486 name = auth_info[:info][:name]
487 email = auth_info[:info][:email]
489 if user = User.find_by_openid_url(openid_url)
492 unconfirmed_login(user)
493 when "active", "confirmed" then
494 successful_login(user)
495 when "suspended" then
496 failed_login t("user.login.account is suspended", :webmaster => "mailto:webmaster@openstreetmap.org")
498 failed_login t("user.login.auth failure")
500 elsif settings = session.delete(:new_user_settings)
501 @user.openid_url = openid_url
503 update_user(@user, settings)
505 redirect_to :action => "account", :display_name => @user.display_name
506 elsif session[:new_user]
507 session[:new_user].openid_url = openid_url
509 if email == session[:new_user].email && openid_email_verified(email)
510 session[:new_user].status = "active"
513 redirect_to :action => "terms"
515 redirect_to :action => "new", :nickname => name, :email => email, :openid => openid_url
520 # omniauth failure callback
522 flash[:error] = t("user.auth_failure." + params[:message])
523 redirect_to params[:origin]
529 # handle password authentication
530 def password_authentication(username, password)
531 if user = User.authenticate(:username => username, :password => password)
532 successful_login(user)
533 elsif user = User.authenticate(:username => username, :password => password, :pending => true)
534 unconfirmed_login(user)
535 elsif User.authenticate(:username => username, :password => password, :suspended => true)
536 failed_login t("user.login.account is suspended", :webmaster => "mailto:webmaster@openstreetmap.org")
538 failed_login t("user.login.auth failure")
543 # special case some common OpenID providers by applying heuristics to
544 # try and come up with the correct URL based on what the user entered
545 def openid_expand_url(openid_url)
548 elsif openid_url.match(/(.*)gmail.com(\/?)$/) || openid_url.match(/(.*)googlemail.com(\/?)$/)
549 # Special case gmail.com as it is potentially a popular OpenID
550 # provider and, unlike yahoo.com, where it works automatically, Google
551 # have hidden their OpenID endpoint somewhere obscure this making it
552 # somewhat less user friendly.
553 return "https://www.google.com/accounts/o8/id"
560 # check if we trust an OpenID provider to return a verified
561 # email, so that we can skpi verifying it ourselves
562 def openid_email_verified(openid_url)
563 openid_url.match(%r{https://www.google.com/accounts/o8/id?(.*)}) ||
564 openid_url.match(%r{https://me.yahoo.com/(.*)})
568 # process a successful login
569 def successful_login(user)
570 session[:user] = user.id
571 session_expires_after 28.days if session[:remember_me]
573 target = session[:referer] || url_for(:controller => :site, :action => :index)
575 # The user is logged in, so decide where to send them:
577 # - If they haven't seen the contributor terms, send them there.
578 # - If they have a block on them, show them that.
579 # - If they were referred to the login, send them back there.
580 # - Otherwise, send them to the home page.
581 if REQUIRE_TERMS_SEEN && !user.terms_seen
582 redirect_to :controller => :user, :action => :terms, :referer => target
583 elsif user.blocked_on_view
584 redirect_to user.blocked_on_view, :referer => target
589 session.delete(:remember_me)
590 session.delete(:referer)
594 # process a failed login
595 def failed_login(message)
596 flash[:error] = message
598 redirect_to :action => "login", :referer => session[:referer]
600 session.delete(:remember_me)
601 session.delete(:referer)
606 def unconfirmed_login(user)
607 redirect_to :action => "confirm", :display_name => user.display_name
609 session.delete(:remember_me)
610 session.delete(:referer)
614 # update a user's details
615 def update_user(user, params)
616 user.display_name = params[:user][:display_name]
617 user.new_email = params[:user][:new_email]
619 if params[:user][:pass_crypt].length > 0 || params[:user][:pass_crypt_confirmation].length > 0
620 user.pass_crypt = params[:user][:pass_crypt]
621 user.pass_crypt_confirmation = params[:user][:pass_crypt_confirmation]
624 if params[:user][:description] != user.description
625 user.description = params[:user][:description]
626 user.description_format = "markdown"
629 user.languages = params[:user][:languages].split(",")
631 case params[:image_action]
633 user.image = params[:user][:image]
634 user.image_use_gravatar = false
637 user.image_use_gravatar = false
640 user.image_use_gravatar = true
643 user.home_lat = params[:user][:home_lat]
644 user.home_lon = params[:user][:home_lon]
646 if params[:user][:preferred_editor] == "default"
647 user.preferred_editor = nil
649 user.preferred_editor = params[:user][:preferred_editor]
652 user.openid_url = nil if params[:user][:openid_url].blank?
657 if user.new_email.blank? || user.new_email == user.email
658 flash.now[:notice] = t "user.account.flash update success"
660 user.email = user.new_email
663 flash.now[:notice] = t "user.account.flash update success confirm needed"
666 Notifier.email_confirm(user, user.tokens.create).deliver_now
668 # Ignore errors sending email
671 @user.errors.set(:new_email, @user.errors.get(:email))
672 @user.errors.set(:email, [])
681 # require that the user is a administrator, or fill out a helpful error message
682 # and return them to the user page.
683 def require_administrator
684 if @user && !@user.administrator?
685 flash[:error] = t("user.filter.not_an_administrator")
687 if params[:display_name]
688 redirect_to :controller => "user", :action => "view", :display_name => params[:display_name]
690 redirect_to :controller => "user", :action => "login", :referer => request.fullpath
693 redirect_to :controller => "user", :action => "login", :referer => request.fullpath
698 # require that the user in the URL is the logged in user
700 if params[:display_name] != @user.display_name
701 render :text => "", :status => :forbidden
706 # ensure that there is a "this_user" instance variable
707 def lookup_user_by_id
708 @this_user = User.find(params[:id])
712 # ensure that there is a "this_user" instance variable
713 def lookup_user_by_name
714 @this_user = User.find_by_display_name(params[:display_name])
715 rescue ActiveRecord::RecordNotFound
716 redirect_to :controller => "user", :action => "view", :display_name => params[:display_name] unless @this_user
721 def disable_terms_redirect
722 # this is necessary otherwise going to the user terms page, when
723 # having not agreed already would cause an infinite redirect loop.
724 # it's .now so that this doesn't propagate to other pages.
725 flash.now[:skip_terms] = true
729 # return permitted user parameters
731 params.require(:user).permit(:email, :email_confirmation, :display_name, :openid_url, :pass_crypt, :pass_crypt_confirmation)
736 def check_signup_allowed(email = nil)
740 domain = email.split("@").last
743 if blocked = Acl.no_account_creation(request.remote_ip, domain)
744 logger.info "Blocked signup from #{request.remote_ip} for #{email}"
746 render :action => "blocked"