1 class UserController < ApplicationController
2 layout 'site', :except => :api_details
4 before_filter :authorize, :only => [:api_details, :api_gpx_files]
5 before_filter :authorize_web, :except => [:api_details, :api_gpx_files]
6 before_filter :set_locale, :except => [:api_details, :api_gpx_files]
7 before_filter :require_user, :only => [:account, :go_public, :make_friend, :remove_friend]
8 before_filter :check_database_readable, :except => [:api_details, :api_gpx_files]
9 before_filter :check_database_writable, :only => [:login, :new, :account, :go_public, :make_friend, :remove_friend]
10 before_filter :check_api_readable, :only => [:api_details, :api_gpx_files]
11 before_filter :require_allow_read_prefs, :only => [:api_details]
12 before_filter :require_allow_read_gpx, :only => [:api_gpx_files]
13 before_filter :require_cookies, :only => [:login, :confirm]
14 before_filter :require_administrator, :only => [:set_status, :delete, :list]
15 before_filter :lookup_this_user, :only => [:set_status, :delete]
17 filter_parameter_logging :password, :pass_crypt, :pass_crypt_confirmation
19 cache_sweeper :user_sweeper, :only => [:account, :set_status, :delete], :unless => STATUS == :database_offline
22 @legale = params[:legale] || OSM.IPToCountry(request.remote_ip) || DEFAULT_LEGALE
23 @text = OSM.legal_text_for_country(@legale)
26 render :update do |page|
27 page.replace_html "contributorTerms", :partial => "terms", :locals => { :has_decline => params[:has_decline] }
30 @title = t 'user.terms.title'
31 @user = User.new(params[:user]) if params[:user]
36 render :action => :new
38 flash[:errors] = @user.errors
39 redirect_to :action => :account, :display_name => @user.display_name
41 elsif @user.terms_agreed?
42 redirect_to :action => :account, :display_name => @user.display_name
45 redirect_to :action => :login, :referer => request.request_uri
51 @title = t 'user.new.title'
53 if Acl.find_by_address(request.remote_ip, :conditions => {:k => "no_account_creation"})
54 render :action => 'new'
55 elsif params[:decline]
56 redirect_to t('user.terms.declined')
58 if !@user.terms_agreed?
59 @user.consider_pd = params[:user][:consider_pd]
60 @user.terms_agreed = Time.now.getutc
62 flash[:notice] = t 'user.new.terms accepted'
66 redirect_to :action => :account, :display_name => @user.display_name
68 @user = User.new(params[:user])
70 @user.status = "pending"
71 @user.data_public = true
72 @user.description = "" if @user.description.nil?
73 @user.creation_ip = request.remote_ip
74 @user.languages = request.user_preferred_languages
75 @user.terms_agreed = Time.now.getutc
78 flash[:notice] = t 'user.new.flash create success message', :email => @user.email
79 Notifier.deliver_signup_confirm(@user, @user.tokens.create(:referer => params[:referer]))
80 redirect_to :action => 'login'
82 render :action => 'new'
88 @title = t 'user.account.title'
89 @tokens = @user.oauth_tokens.find :all, :conditions => 'oauth_tokens.invalidated_at is null and oauth_tokens.authorized_at is not null'
91 if params[:user] and params[:user][:display_name] and params[:user][:description]
92 @user.display_name = params[:user][:display_name]
93 @user.new_email = params[:user][:new_email]
95 if params[:user][:pass_crypt].length > 0 or params[:user][:pass_crypt_confirmation].length > 0
96 @user.pass_crypt = params[:user][:pass_crypt]
97 @user.pass_crypt_confirmation = params[:user][:pass_crypt_confirmation]
100 @user.description = params[:user][:description]
101 @user.languages = params[:user][:languages].split(",")
103 case params[:image_action]
104 when "new" then @user.image = params[:user][:image]
105 when "delete" then @user.image = nil
108 @user.home_lat = params[:user][:home_lat]
109 @user.home_lon = params[:user][:home_lon]
114 if @user.new_email.nil? or @user.new_email.empty?
115 flash[:notice] = t 'user.account.flash update success'
117 flash[:notice] = t 'user.account.flash update success confirm needed'
120 Notifier.deliver_email_confirm(@user, @user.tokens.create)
122 # Ignore errors sending email
126 redirect_to :action => "account", :display_name => @user.display_name
130 flash[:errors].each do |attr,msg|
131 attr = "new_email" if attr == "email" and !@user.new_email.nil?
132 @user.errors.add(attr,msg)
139 @user.data_public = true
141 flash[:notice] = t 'user.go_public.flash success'
142 redirect_to :controller => 'user', :action => 'account', :display_name => @user.display_name
146 @title = t 'user.lost_password.title'
148 if params[:user] and params[:user][:email]
149 user = User.find_by_email(params[:user][:email], :conditions => {:status => ["pending", "active", "confirmed"]})
152 token = user.tokens.create
153 Notifier.deliver_lost_password(user, token)
154 flash[:notice] = t 'user.lost_password.notice email on way'
155 redirect_to :action => 'login'
157 flash.now[:error] = t 'user.lost_password.notice email cannot find'
163 @title = t 'user.reset_password.title'
166 token = UserToken.find_by_token(params[:token])
172 @user.pass_crypt = params[:user][:pass_crypt]
173 @user.pass_crypt_confirmation = params[:user][:pass_crypt_confirmation]
174 @user.status = "active" if @user.status == "pending"
175 @user.email_valid = true
179 flash[:notice] = t 'user.reset_password.flash changed'
180 redirect_to :action => 'login'
184 flash[:error] = t 'user.reset_password.flash token bad'
185 redirect_to :action => 'lost_password'
191 @title = t 'user.new.title'
193 # The user is logged in already, so don't show them the signup
194 # page, instead send them to the home page
195 redirect_to :controller => 'site', :action => 'index' if session[:user]
199 @title = t 'user.login.title'
202 email_or_display_name = params[:user][:email]
203 pass = params[:user][:password]
204 user = User.authenticate(:username => email_or_display_name, :password => pass)
207 session[:user] = user.id
208 session_expires_after 1.month if params[:remember_me]
210 # The user is logged in, if the referer param exists, redirect
211 # them to that unless they've also got a block on them, in
212 # which case redirect them to the block so they can clear it.
213 if user.blocked_on_view
214 redirect_to user.blocked_on_view, :referer => params[:referer]
215 elsif params[:referer]
216 redirect_to params[:referer]
218 redirect_to :controller => 'site', :action => 'index'
220 elsif user = User.authenticate(:username => email_or_display_name, :password => pass, :pending => true)
221 flash.now[:error] = t 'user.login.account not active', :reconfirm => url_for(:action => 'confirm_resend', :display_name => user.display_name)
222 elsif User.authenticate(:username => email_or_display_name, :password => pass, :suspended => true)
223 webmaster = link_to t('user.login.webmaster'), "mailto:webmaster@openstreetmap.org"
224 flash.now[:error] = t 'user.login.account suspended', :webmaster => webmaster
226 flash.now[:error] = t 'user.login.auth failure'
228 elsif flash[:notice].nil?
229 flash.now[:notice] = t 'user.login.notice'
234 @title = t 'user.logout.title'
236 if params[:session] == request.session_options[:id]
238 token = UserToken.find_by_token(session[:token])
242 session[:token] = nil
245 session_expires_automatically
247 redirect_to params[:referer]
249 redirect_to :controller => 'site', :action => 'index'
256 if token = UserToken.find_by_token(params[:confirm_string])
257 if token.user.active?
258 flash[:error] = t('user.confirm.already active')
259 redirect_to :action => 'login'
262 user.status = "active"
263 user.email_valid = true
265 referer = token.referer
267 session[:user] = user.id
270 flash[:notice] = t('user.confirm.success')
273 flash[:notice] = t('user.confirm.success') + "<br /><br />" + t('user.confirm.before you start')
274 redirect_to :action => 'account', :display_name => user.display_name
278 user = User.find_by_display_name(params[:display_name])
280 if user and user.active?
281 flash[:error] = t('user.confirm.already active')
283 flash[:error] = t('user.confirm.unknown token') + t('user.confirm.reconfirm', :reconfirm => url_for(:action => 'confirm_resend', :display_name => params[:display_name]))
285 flash[:error] = t('user.confirm.unknown token')
288 redirect_to :action => 'login'
294 if user = User.find_by_display_name(params[:display_name])
295 Notifier.deliver_signup_confirm(user, user.tokens.create)
296 flash[:notice] = t 'user.confirm_resend.success', :email => user.email
298 flash[:notice] = t 'user.confirm_resend.failure', :name => params[:display_name]
301 redirect_to :action => 'login'
306 token = UserToken.find_by_token(params[:confirm_string])
307 if token and token.user.new_email?
309 @user.email = @user.new_email
310 @user.new_email = nil
311 @user.email_valid = true
313 flash[:notice] = t 'user.confirm_email.success'
315 flash[:errors] = @user.errors
318 session[:user] = @user.id
319 redirect_to :action => 'account', :display_name => @user.display_name
321 flash[:error] = t 'user.confirm_email.failure'
322 redirect_to :action => 'account', :display_name => @user.display_name
328 doc = OSM::API.new.get_xml_doc
329 @user.traces.each do |trace|
330 doc.root << trace.to_xml_node() if trace.public? or trace.user == @user
332 render :text => doc.to_s, :content_type => "text/xml"
336 @this_user = User.find_by_display_name(params[:display_name])
339 (@this_user.visible? or (@user and @user.administrator?))
340 @title = @this_user.display_name
342 @title = t 'user.no_such_user.title'
343 @not_found_user = params[:display_name]
344 render :action => 'no_such_user', :status => :not_found
349 if params[:display_name]
350 name = params[:display_name]
351 new_friend = User.find_by_display_name(name, :conditions => {:status => ["active", "confirmed"]})
353 friend.user_id = @user.id
354 friend.friend_user_id = new_friend.id
355 unless @user.is_friends_with?(new_friend)
357 flash[:notice] = t 'user.make_friend.success', :name => name
358 Notifier.deliver_friend_notification(friend)
360 friend.add_error(t('user.make_friend.failed', :name => name))
363 flash[:warning] = t 'user.make_friend.already_a_friend', :name => name
367 redirect_to params[:referer]
369 redirect_to :controller => 'user', :action => 'view'
375 if params[:display_name]
376 name = params[:display_name]
377 friend = User.find_by_display_name(name, :conditions => {:status => ["active", "confirmed"]})
378 if @user.is_friends_with?(friend)
379 Friend.delete_all "user_id = #{@user.id} AND friend_user_id = #{friend.id}"
380 flash[:notice] = t 'user.remove_friend.success', :name => friend.display_name
382 flash[:error] = t 'user.remove_friend.not_a_friend', :name => friend.display_name
386 redirect_to params[:referer]
388 redirect_to :controller => 'user', :action => 'view'
394 # sets a user's status
396 @this_user.update_attributes(:status => params[:status])
397 redirect_to :controller => 'user', :action => 'view', :display_name => params[:display_name]
401 # delete a user, marking them as deleted and removing personal data
404 redirect_to :controller => 'user', :action => 'view', :display_name => params[:display_name]
408 # display a list of users matching specified criteria
411 ids = params[:user].keys.collect { |id| id.to_i }
413 User.update_all("status = 'confirmed'", :id => ids) if params[:confirm]
414 User.update_all("status = 'deleted'", :id => ids) if params[:hide]
416 redirect_to url_for(:status => params[:status], :ip => params[:ip], :page => params[:page])
418 conditions = Hash.new
419 conditions[:status] = params[:status] if params[:status]
420 conditions[:creation_ip] = params[:ip] if params[:ip]
422 @user_pages, @users = paginate(:users,
423 :conditions => conditions,
432 # require that the user is a administrator, or fill out a helpful error message
433 # and return them to the user page.
434 def require_administrator
435 if @user and not @user.administrator?
436 flash[:error] = t('user.filter.not_an_administrator')
438 if params[:display_name]
439 redirect_to :controller => 'user', :action => 'view', :display_name => params[:display_name]
441 redirect_to :controller => 'user', :action => 'login', :referer => request.request_uri
444 redirect_to :controller => 'user', :action => 'login', :referer => request.request_uri
449 # ensure that there is a "this_user" instance variable
451 @this_user = User.find_by_display_name(params[:display_name])
452 rescue ActiveRecord::RecordNotFound
453 redirect_to :controller => 'user', :action => 'view', :display_name => params[:display_name] unless @this_user