1 class UserController < ApplicationController
4 before_filter :disable_terms_redirect, :only => [:terms, :save, :logout, :api_details]
5 before_filter :authorize, :only => [:api_details, :api_gpx_files]
6 before_filter :authorize_web, :except => [:api_details, :api_gpx_files]
7 before_filter :set_locale, :except => [:api_details, :api_gpx_files]
8 before_filter :require_user, :only => [:account, :go_public, :make_friend, :remove_friend]
9 before_filter :check_database_readable, :except => [:api_details, :api_gpx_files]
10 before_filter :check_database_writable, :only => [:login, :new, :account, :go_public, :make_friend, :remove_friend]
11 before_filter :check_api_readable, :only => [:api_details, :api_gpx_files]
12 before_filter :require_allow_read_prefs, :only => [:api_details]
13 before_filter :require_allow_read_gpx, :only => [:api_gpx_files]
14 before_filter :require_cookies, :only => [:login, :confirm]
15 before_filter :require_administrator, :only => [:set_status, :delete, :list]
16 before_filter :lookup_this_user, :only => [:set_status, :delete]
18 filter_parameter_logging :password, :pass_crypt, :pass_crypt_confirmation
20 cache_sweeper :user_sweeper, :only => [:account, :set_status, :delete], :unless => STATUS == :database_offline
23 @legale = params[:legale] || OSM.IPToCountry(request.remote_ip) || DEFAULT_LEGALE
24 @text = OSM.legal_text_for_country(@legale)
27 render :update do |page|
28 page.replace_html "contributorTerms", :partial => "terms"
31 @title = t 'user.terms.title'
32 @user = User.new(params[:user]) if params[:user]
37 render :action => :new
39 flash[:errors] = @user.errors
40 redirect_to :action => :account, :display_name => @user.display_name
42 elsif @user.terms_agreed?
43 redirect_to :action => :account, :display_name => @user.display_name
46 redirect_to :action => :login, :referer => request.request_uri
52 @title = t 'user.new.title'
54 if Acl.find_by_address(request.remote_ip, :conditions => {:k => "no_account_creation"})
55 render :action => 'new'
56 elsif params[:decline]
58 @user.terms_seen = true
61 flash[:notice] = t 'user.new.terms declined', :url => t('user.new.terms declined url')
65 redirect_to params[:referer]
67 redirect_to :action => :account, :display_name => @user.display_name
70 redirect_to t('user.terms.declined')
73 if !@user.terms_agreed?
74 @user.consider_pd = params[:user][:consider_pd]
75 @user.terms_agreed = Time.now.getutc
76 @user.terms_seen = true
78 flash[:notice] = t 'user.new.terms accepted'
83 redirect_to params[:referer]
85 redirect_to :action => :account, :display_name => @user.display_name
88 @user = User.new(params[:user])
90 @user.status = "pending"
91 @user.data_public = true
92 @user.description = "" if @user.description.nil?
93 @user.creation_ip = request.remote_ip
94 @user.languages = request.user_preferred_languages
95 @user.terms_agreed = Time.now.getutc
96 @user.terms_seen = true
99 flash[:notice] = t 'user.new.flash create success message', :email => @user.email
100 Notifier.deliver_signup_confirm(@user, @user.tokens.create(:referer => params[:referer]))
101 session[:token] = @user.tokens.create.token
102 redirect_to :action => 'login', :referer => params[:referer]
104 render :action => 'new', :referer => params[:referer]
110 @title = t 'user.account.title'
111 @tokens = @user.oauth_tokens.find :all, :conditions => 'oauth_tokens.invalidated_at is null and oauth_tokens.authorized_at is not null'
113 if params[:user] and params[:user][:display_name] and params[:user][:description]
114 @user.display_name = params[:user][:display_name]
115 @user.new_email = params[:user][:new_email]
117 if params[:user][:pass_crypt].length > 0 or params[:user][:pass_crypt_confirmation].length > 0
118 @user.pass_crypt = params[:user][:pass_crypt]
119 @user.pass_crypt_confirmation = params[:user][:pass_crypt_confirmation]
122 @user.description = params[:user][:description]
123 @user.languages = params[:user][:languages].split(",")
125 case params[:image_action]
126 when "new" then @user.image = params[:user][:image]
127 when "delete" then @user.image = nil
130 @user.home_lat = params[:user][:home_lat]
131 @user.home_lon = params[:user][:home_lon]
133 if params[:user][:preferred_editor] == "default"
134 @user.preferred_editor = nil
136 @user.preferred_editor = params[:user][:preferred_editor]
142 if @user.new_email.nil? or @user.new_email.empty?
143 flash[:notice] = t 'user.account.flash update success'
145 flash[:notice] = t 'user.account.flash update success confirm needed'
148 Notifier.deliver_email_confirm(@user, @user.tokens.create)
150 # Ignore errors sending email
154 redirect_to :action => "account", :display_name => @user.display_name
158 flash[:errors].each do |attr,msg|
159 attr = "new_email" if attr == "email" and !@user.new_email.nil?
160 @user.errors.add(attr,msg)
167 @user.data_public = true
169 flash[:notice] = t 'user.go_public.flash success'
170 redirect_to :controller => 'user', :action => 'account', :display_name => @user.display_name
174 @title = t 'user.lost_password.title'
176 if params[:user] and params[:user][:email]
177 user = User.find_by_email(params[:user][:email], :conditions => {:status => ["pending", "active", "confirmed"]})
180 token = user.tokens.create
181 Notifier.deliver_lost_password(user, token)
182 flash[:notice] = t 'user.lost_password.notice email on way'
183 redirect_to :action => 'login'
185 flash.now[:error] = t 'user.lost_password.notice email cannot find'
191 @title = t 'user.reset_password.title'
194 token = UserToken.find_by_token(params[:token])
200 @user.pass_crypt = params[:user][:pass_crypt]
201 @user.pass_crypt_confirmation = params[:user][:pass_crypt_confirmation]
202 @user.status = "active" if @user.status == "pending"
203 @user.email_valid = true
207 flash[:notice] = t 'user.reset_password.flash changed'
208 redirect_to :action => 'login'
212 flash[:error] = t 'user.reset_password.flash token bad'
213 redirect_to :action => 'lost_password'
219 @title = t 'user.new.title'
221 # The user is logged in already, so don't show them the signup
222 # page, instead send them to the home page
223 redirect_to :controller => 'site', :action => 'index' if session[:user]
227 @title = t 'user.login.title'
230 email_or_display_name = params[:user][:email]
231 pass = params[:user][:password]
232 user = User.authenticate(:username => email_or_display_name, :password => pass)
235 session[:user] = user.id
236 session_expires_after 1.month if params[:remember_me]
238 target = params[:referer] || url_for(:controller => :site, :action => :index)
240 # The user is logged in, so decide where to send them:
242 # - If they haven't seen the contributor terms, send them there.
243 # - If they have a block on them, show them that.
244 # - If they were referred to the login, send them back there.
245 # - Otherwise, send them to the home page.
246 if REQUIRE_TERMS_SEEN and not user.terms_seen
247 redirect_to :controller => :user, :action => :terms, :referer => target
248 elsif user.blocked_on_view
249 redirect_to user.blocked_on_view, :referer => target
253 elsif user = User.authenticate(:username => email_or_display_name, :password => pass, :pending => true)
254 flash.now[:error] = t 'user.login.account not active', :reconfirm => url_for(:action => 'confirm_resend', :display_name => user.display_name)
255 elsif User.authenticate(:username => email_or_display_name, :password => pass, :suspended => true)
256 webmaster = link_to t('user.login.webmaster'), "mailto:webmaster@openstreetmap.org"
257 flash.now[:error] = t 'user.login.account suspended', :webmaster => webmaster
259 flash.now[:error] = t 'user.login.auth failure'
261 elsif flash[:notice].nil?
262 flash.now[:notice] = t 'user.login.notice'
267 @title = t 'user.logout.title'
269 if params[:session] == request.session_options[:id]
271 token = UserToken.find_by_token(session[:token])
275 session.delete(:token)
277 session.delete(:user)
278 session_expires_automatically
280 redirect_to params[:referer]
282 redirect_to :controller => 'site', :action => 'index'
289 if token = UserToken.find_by_token(params[:confirm_string])
290 if token.user.active?
291 flash[:error] = t('user.confirm.already active')
292 redirect_to :action => 'login'
295 user.status = "active"
296 user.email_valid = true
298 referer = token.referer
302 token = UserToken.find_by_token(session[:token])
303 session.delete(:token)
308 if token.nil? or token.user != user
309 flash[:notice] = t('user.confirm.success')
310 redirect_to :action => :login, :referer => referer
314 session[:user] = user.id
317 flash[:notice] = t('user.confirm.success') + "<br /><br />" + t('user.confirm.before you start')
318 redirect_to :action => :account, :display_name => user.display_name
320 flash[:notice] = t('user.confirm.success')
326 user = User.find_by_display_name(params[:display_name])
328 if user and user.active?
329 flash[:error] = t('user.confirm.already active')
331 flash[:error] = t('user.confirm.unknown token') + t('user.confirm.reconfirm', :reconfirm => url_for(:action => 'confirm_resend', :display_name => params[:display_name]))
333 flash[:error] = t('user.confirm.unknown token')
336 redirect_to :action => 'login'
342 if user = User.find_by_display_name(params[:display_name])
343 Notifier.deliver_signup_confirm(user, user.tokens.create)
344 flash[:notice] = t 'user.confirm_resend.success', :email => user.email
346 flash[:notice] = t 'user.confirm_resend.failure', :name => params[:display_name]
349 redirect_to :action => 'login'
354 token = UserToken.find_by_token(params[:confirm_string])
355 if token and token.user.new_email?
357 @user.email = @user.new_email
358 @user.new_email = nil
359 @user.email_valid = true
361 flash[:notice] = t 'user.confirm_email.success'
363 flash[:errors] = @user.errors
366 session[:user] = @user.id
367 redirect_to :action => 'account', :display_name => @user.display_name
369 flash[:error] = t 'user.confirm_email.failure'
370 redirect_to :action => 'account', :display_name => @user.display_name
376 doc = OSM::API.new.get_xml_doc
377 @user.traces.each do |trace|
378 doc.root << trace.to_xml_node() if trace.public? or trace.user == @user
380 render :text => doc.to_s, :content_type => "text/xml"
384 @this_user = User.find_by_display_name(params[:display_name])
387 (@this_user.visible? or (@user and @user.administrator?))
388 @title = @this_user.display_name
390 @title = t 'user.no_such_user.title'
391 @not_found_user = params[:display_name]
392 render :action => 'no_such_user', :status => :not_found
397 if params[:display_name]
398 name = params[:display_name]
399 new_friend = User.find_by_display_name(name, :conditions => {:status => ["active", "confirmed"]})
401 friend.user_id = @user.id
402 friend.friend_user_id = new_friend.id
403 unless @user.is_friends_with?(new_friend)
405 flash[:notice] = t 'user.make_friend.success', :name => name
406 Notifier.deliver_friend_notification(friend)
408 friend.add_error(t('user.make_friend.failed', :name => name))
411 flash[:warning] = t 'user.make_friend.already_a_friend', :name => name
415 redirect_to params[:referer]
417 redirect_to :controller => 'user', :action => 'view'
423 if params[:display_name]
424 name = params[:display_name]
425 friend = User.find_by_display_name(name, :conditions => {:status => ["active", "confirmed"]})
426 if @user.is_friends_with?(friend)
427 Friend.delete_all "user_id = #{@user.id} AND friend_user_id = #{friend.id}"
428 flash[:notice] = t 'user.remove_friend.success', :name => friend.display_name
430 flash[:error] = t 'user.remove_friend.not_a_friend', :name => friend.display_name
434 redirect_to params[:referer]
436 redirect_to :controller => 'user', :action => 'view'
442 # sets a user's status
444 @this_user.update_attributes(:status => params[:status])
445 redirect_to :controller => 'user', :action => 'view', :display_name => params[:display_name]
449 # delete a user, marking them as deleted and removing personal data
452 redirect_to :controller => 'user', :action => 'view', :display_name => params[:display_name]
456 # display a list of users matching specified criteria
459 ids = params[:user].keys.collect { |id| id.to_i }
461 User.update_all("status = 'confirmed'", :id => ids) if params[:confirm]
462 User.update_all("status = 'deleted'", :id => ids) if params[:hide]
464 redirect_to url_for(:status => params[:status], :ip => params[:ip], :page => params[:page])
466 conditions = Hash.new
467 conditions[:status] = params[:status] if params[:status]
468 conditions[:creation_ip] = params[:ip] if params[:ip]
470 @user_pages, @users = paginate(:users,
471 :conditions => conditions,
480 # require that the user is a administrator, or fill out a helpful error message
481 # and return them to the user page.
482 def require_administrator
483 if @user and not @user.administrator?
484 flash[:error] = t('user.filter.not_an_administrator')
486 if params[:display_name]
487 redirect_to :controller => 'user', :action => 'view', :display_name => params[:display_name]
489 redirect_to :controller => 'user', :action => 'login', :referer => request.request_uri
492 redirect_to :controller => 'user', :action => 'login', :referer => request.request_uri
497 # ensure that there is a "this_user" instance variable
499 @this_user = User.find_by_display_name(params[:display_name])
500 rescue ActiveRecord::RecordNotFound
501 redirect_to :controller => 'user', :action => 'view', :display_name => params[:display_name] unless @this_user
505 # Choose the layout to use. See
506 # https://rails.lighthouseapp.com/projects/8994/tickets/5371-layout-with-onlyexcept-options-makes-other-actions-render-without-layouts
508 oauth_url = url_for(:controller => :oauth, :action => :oauthorize, :only_path => true)
510 if [ 'api_details' ].include? action_name
512 elsif params[:referer] and URI.parse(params[:referer]).path == oauth_url
521 def disable_terms_redirect
522 # this is necessary otherwise going to the user terms page, when
523 # having not agreed already would cause an infinite redirect loop.
524 # it's .now so that this doesn't propagate to other pages.
525 flash.now[:skip_terms] = true