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 session[:token] = @user.tokens.create.token
81 redirect_to :action => 'login'
83 render :action => 'new'
89 @title = t 'user.account.title'
90 @tokens = @user.oauth_tokens.find :all, :conditions => 'oauth_tokens.invalidated_at is null and oauth_tokens.authorized_at is not null'
92 if params[:user] and params[:user][:display_name] and params[:user][:description]
93 @user.display_name = params[:user][:display_name]
94 @user.new_email = params[:user][:new_email]
96 if params[:user][:pass_crypt].length > 0 or params[:user][:pass_crypt_confirmation].length > 0
97 @user.pass_crypt = params[:user][:pass_crypt]
98 @user.pass_crypt_confirmation = params[:user][:pass_crypt_confirmation]
101 @user.description = params[:user][:description]
102 @user.languages = params[:user][:languages].split(",")
104 case params[:image_action]
105 when "new" then @user.image = params[:user][:image]
106 when "delete" then @user.image = nil
109 @user.home_lat = params[:user][:home_lat]
110 @user.home_lon = params[:user][:home_lon]
115 if @user.new_email.nil? or @user.new_email.empty?
116 flash[:notice] = t 'user.account.flash update success'
118 flash[:notice] = t 'user.account.flash update success confirm needed'
121 Notifier.deliver_email_confirm(@user, @user.tokens.create)
123 # Ignore errors sending email
127 redirect_to :action => "account", :display_name => @user.display_name
131 flash[:errors].each do |attr,msg|
132 attr = "new_email" if attr == "email" and !@user.new_email.nil?
133 @user.errors.add(attr,msg)
140 @user.data_public = true
142 flash[:notice] = t 'user.go_public.flash success'
143 redirect_to :controller => 'user', :action => 'account', :display_name => @user.display_name
147 @title = t 'user.lost_password.title'
149 if params[:user] and params[:user][:email]
150 user = User.find_by_email(params[:user][:email], :conditions => {:status => ["pending", "active", "confirmed"]})
153 token = user.tokens.create
154 Notifier.deliver_lost_password(user, token)
155 flash[:notice] = t 'user.lost_password.notice email on way'
156 redirect_to :action => 'login'
158 flash.now[:error] = t 'user.lost_password.notice email cannot find'
164 @title = t 'user.reset_password.title'
167 token = UserToken.find_by_token(params[:token])
173 @user.pass_crypt = params[:user][:pass_crypt]
174 @user.pass_crypt_confirmation = params[:user][:pass_crypt_confirmation]
175 @user.status = "active" if @user.status == "pending"
176 @user.email_valid = true
180 flash[:notice] = t 'user.reset_password.flash changed'
181 redirect_to :action => 'login'
185 flash[:error] = t 'user.reset_password.flash token bad'
186 redirect_to :action => 'lost_password'
192 @title = t 'user.new.title'
194 # The user is logged in already, so don't show them the signup
195 # page, instead send them to the home page
196 redirect_to :controller => 'site', :action => 'index' if session[:user]
200 @title = t 'user.login.title'
203 email_or_display_name = params[:user][:email]
204 pass = params[:user][:password]
205 user = User.authenticate(:username => email_or_display_name, :password => pass)
208 session[:user] = user.id
209 session_expires_after 1.month if params[:remember_me]
211 # The user is logged in, if the referer param exists, redirect
212 # them to that unless they've also got a block on them, in
213 # which case redirect them to the block so they can clear it.
214 if user.blocked_on_view
215 redirect_to user.blocked_on_view, :referer => params[:referer]
216 elsif params[:referer]
217 redirect_to params[:referer]
219 redirect_to :controller => 'site', :action => 'index'
221 elsif user = User.authenticate(:username => email_or_display_name, :password => pass, :pending => true)
222 flash.now[:error] = t 'user.login.account not active', :reconfirm => url_for(:action => 'confirm_resend', :display_name => user.display_name)
223 elsif User.authenticate(:username => email_or_display_name, :password => pass, :suspended => true)
224 webmaster = link_to t('user.login.webmaster'), "mailto:webmaster@openstreetmap.org"
225 flash.now[:error] = t 'user.login.account suspended', :webmaster => webmaster
227 flash.now[:error] = t 'user.login.auth failure'
229 elsif flash[:notice].nil?
230 flash.now[:notice] = t 'user.login.notice'
235 @title = t 'user.logout.title'
237 if params[:session] == request.session_options[:id]
239 token = UserToken.find_by_token(session[:token])
243 session[:token] = nil
246 session_expires_automatically
248 redirect_to params[:referer]
250 redirect_to :controller => 'site', :action => 'index'
257 if token = UserToken.find_by_token(params[:confirm_string])
258 if token.user.active?
259 flash[:error] = t('user.confirm.already active')
260 redirect_to :action => 'login'
263 user.status = "active"
264 user.email_valid = true
266 referer = token.referer
270 token = UserToken.find_by_token(session[:token])
271 session.delete(:token)
276 if token.nil? or token.user != user
277 flash[:notice] = t('user.confirm.success')
278 redirect_to :action => :login, :referer => referer
282 session[:user] = user.id
285 flash[:notice] = t('user.confirm.success') + "<br /><br />" + t('user.confirm.before you start')
286 redirect_to :action => :account, :display_name => user.display_name
288 flash[:notice] = t('user.confirm.success')
294 user = User.find_by_display_name(params[:display_name])
296 if user and user.active?
297 flash[:error] = t('user.confirm.already active')
299 flash[:error] = t('user.confirm.unknown token') + t('user.confirm.reconfirm', :reconfirm => url_for(:action => 'confirm_resend', :display_name => params[:display_name]))
301 flash[:error] = t('user.confirm.unknown token')
304 redirect_to :action => 'login'
310 if user = User.find_by_display_name(params[:display_name])
311 Notifier.deliver_signup_confirm(user, user.tokens.create)
312 flash[:notice] = t 'user.confirm_resend.success', :email => user.email
314 flash[:notice] = t 'user.confirm_resend.failure', :name => params[:display_name]
317 redirect_to :action => 'login'
322 token = UserToken.find_by_token(params[:confirm_string])
323 if token and token.user.new_email?
325 @user.email = @user.new_email
326 @user.new_email = nil
327 @user.email_valid = true
329 flash[:notice] = t 'user.confirm_email.success'
331 flash[:errors] = @user.errors
334 session[:user] = @user.id
335 redirect_to :action => 'account', :display_name => @user.display_name
337 flash[:error] = t 'user.confirm_email.failure'
338 redirect_to :action => 'account', :display_name => @user.display_name
344 doc = OSM::API.new.get_xml_doc
345 @user.traces.each do |trace|
346 doc.root << trace.to_xml_node() if trace.public? or trace.user == @user
348 render :text => doc.to_s, :content_type => "text/xml"
352 @this_user = User.find_by_display_name(params[:display_name])
355 (@this_user.visible? or (@user and @user.administrator?))
356 @title = @this_user.display_name
358 @title = t 'user.no_such_user.title'
359 @not_found_user = params[:display_name]
360 render :action => 'no_such_user', :status => :not_found
365 if params[:display_name]
366 name = params[:display_name]
367 new_friend = User.find_by_display_name(name, :conditions => {:status => ["active", "confirmed"]})
369 friend.user_id = @user.id
370 friend.friend_user_id = new_friend.id
371 unless @user.is_friends_with?(new_friend)
373 flash[:notice] = t 'user.make_friend.success', :name => name
374 Notifier.deliver_friend_notification(friend)
376 friend.add_error(t('user.make_friend.failed', :name => name))
379 flash[:warning] = t 'user.make_friend.already_a_friend', :name => name
383 redirect_to params[:referer]
385 redirect_to :controller => 'user', :action => 'view'
391 if params[:display_name]
392 name = params[:display_name]
393 friend = User.find_by_display_name(name, :conditions => {:status => ["active", "confirmed"]})
394 if @user.is_friends_with?(friend)
395 Friend.delete_all "user_id = #{@user.id} AND friend_user_id = #{friend.id}"
396 flash[:notice] = t 'user.remove_friend.success', :name => friend.display_name
398 flash[:error] = t 'user.remove_friend.not_a_friend', :name => friend.display_name
402 redirect_to params[:referer]
404 redirect_to :controller => 'user', :action => 'view'
410 # sets a user's status
412 @this_user.update_attributes(:status => params[:status])
413 redirect_to :controller => 'user', :action => 'view', :display_name => params[:display_name]
417 # delete a user, marking them as deleted and removing personal data
420 redirect_to :controller => 'user', :action => 'view', :display_name => params[:display_name]
424 # display a list of users matching specified criteria
427 ids = params[:user].keys.collect { |id| id.to_i }
429 User.update_all("status = 'confirmed'", :id => ids) if params[:confirm]
430 User.update_all("status = 'deleted'", :id => ids) if params[:hide]
432 redirect_to url_for(:status => params[:status], :ip => params[:ip], :page => params[:page])
434 conditions = Hash.new
435 conditions[:status] = params[:status] if params[:status]
436 conditions[:creation_ip] = params[:ip] if params[:ip]
438 @user_pages, @users = paginate(:users,
439 :conditions => conditions,
448 # require that the user is a administrator, or fill out a helpful error message
449 # and return them to the user page.
450 def require_administrator
451 if @user and not @user.administrator?
452 flash[:error] = t('user.filter.not_an_administrator')
454 if params[:display_name]
455 redirect_to :controller => 'user', :action => 'view', :display_name => params[:display_name]
457 redirect_to :controller => 'user', :action => 'login', :referer => request.request_uri
460 redirect_to :controller => 'user', :action => 'login', :referer => request.request_uri
465 # ensure that there is a "this_user" instance variable
467 @this_user = User.find_by_display_name(params[:display_name])
468 rescue ActiveRecord::RecordNotFound
469 redirect_to :controller => 'user', :action => 'view', :display_name => params[:display_name] unless @this_user