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"
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]
57 @user.terms_seen = true
61 redirect_to params[:referer]
63 redirect_to :action => :account, :display_name => @user.display_name
66 redirect_to t('user.terms.declined')
69 if !@user.terms_agreed?
70 @user.consider_pd = params[:user][:consider_pd]
71 @user.terms_agreed = Time.now.getutc
72 @user.terms_seen = true
74 flash[:notice] = t 'user.new.terms accepted'
79 redirect_to params[:referer]
81 redirect_to :action => :account, :display_name => @user.display_name
84 @user = User.new(params[:user])
86 @user.status = "pending"
87 @user.data_public = true
88 @user.description = "" if @user.description.nil?
89 @user.creation_ip = request.remote_ip
90 @user.languages = request.user_preferred_languages
91 @user.terms_agreed = Time.now.getutc
92 @user.terms_seen = true
95 flash[:notice] = t 'user.new.flash create success message', :email => @user.email
96 Notifier.deliver_signup_confirm(@user, @user.tokens.create(:referer => params[:referer]))
97 session[:token] = @user.tokens.create.token
98 redirect_to :action => 'login'
100 render :action => 'new'
106 @title = t 'user.account.title'
107 @tokens = @user.oauth_tokens.find :all, :conditions => 'oauth_tokens.invalidated_at is null and oauth_tokens.authorized_at is not null'
109 if params[:user] and params[:user][:display_name] and params[:user][:description]
110 @user.display_name = params[:user][:display_name]
111 @user.new_email = params[:user][:new_email]
113 if params[:user][:pass_crypt].length > 0 or params[:user][:pass_crypt_confirmation].length > 0
114 @user.pass_crypt = params[:user][:pass_crypt]
115 @user.pass_crypt_confirmation = params[:user][:pass_crypt_confirmation]
118 @user.description = params[:user][:description]
119 @user.languages = params[:user][:languages].split(",")
121 case params[:image_action]
122 when "new" then @user.image = params[:user][:image]
123 when "delete" then @user.image = nil
126 @user.home_lat = params[:user][:home_lat]
127 @user.home_lon = params[:user][:home_lon]
129 if params[:user][:preferred_editor] == "default"
130 @user.preferred_editor = nil
132 @user.preferred_editor = params[:user][:preferred_editor]
138 if @user.new_email.nil? or @user.new_email.empty?
139 flash[:notice] = t 'user.account.flash update success'
141 flash[:notice] = t 'user.account.flash update success confirm needed'
144 Notifier.deliver_email_confirm(@user, @user.tokens.create)
146 # Ignore errors sending email
150 redirect_to :action => "account", :display_name => @user.display_name
154 flash[:errors].each do |attr,msg|
155 attr = "new_email" if attr == "email" and !@user.new_email.nil?
156 @user.errors.add(attr,msg)
163 @user.data_public = true
165 flash[:notice] = t 'user.go_public.flash success'
166 redirect_to :controller => 'user', :action => 'account', :display_name => @user.display_name
170 @title = t 'user.lost_password.title'
172 if params[:user] and params[:user][:email]
173 user = User.find_by_email(params[:user][:email], :conditions => {:status => ["pending", "active", "confirmed"]})
176 token = user.tokens.create
177 Notifier.deliver_lost_password(user, token)
178 flash[:notice] = t 'user.lost_password.notice email on way'
179 redirect_to :action => 'login'
181 flash.now[:error] = t 'user.lost_password.notice email cannot find'
187 @title = t 'user.reset_password.title'
190 token = UserToken.find_by_token(params[:token])
196 @user.pass_crypt = params[:user][:pass_crypt]
197 @user.pass_crypt_confirmation = params[:user][:pass_crypt_confirmation]
198 @user.status = "active" if @user.status == "pending"
199 @user.email_valid = true
203 flash[:notice] = t 'user.reset_password.flash changed'
204 redirect_to :action => 'login'
208 flash[:error] = t 'user.reset_password.flash token bad'
209 redirect_to :action => 'lost_password'
215 @title = t 'user.new.title'
217 # The user is logged in already, so don't show them the signup
218 # page, instead send them to the home page
219 redirect_to :controller => 'site', :action => 'index' if session[:user]
223 @title = t 'user.login.title'
226 email_or_display_name = params[:user][:email]
227 pass = params[:user][:password]
228 user = User.authenticate(:username => email_or_display_name, :password => pass)
231 session[:user] = user.id
232 session_expires_after 1.month if params[:remember_me]
234 target = params[:referer] || url_for(:controller => :site, :action => :index)
236 # The user is logged in, so decide where to send them:
238 # - If they haven't seen the contributor terms, send them there.
239 # - If they have a block on them, show them that.
240 # - If they were referred to the login, send them back there.
241 # - Otherwise, send them to the home page.
242 if REQUIRE_TERMS_SEEN and not user.terms_seen
243 redirect_to :controller => :user, :action => :terms, :referer => target
244 elsif user.blocked_on_view
245 redirect_to user.blocked_on_view, :referer => target
249 elsif user = User.authenticate(:username => email_or_display_name, :password => pass, :pending => true)
250 flash.now[:error] = t 'user.login.account not active', :reconfirm => url_for(:action => 'confirm_resend', :display_name => user.display_name)
251 elsif User.authenticate(:username => email_or_display_name, :password => pass, :suspended => true)
252 webmaster = link_to t('user.login.webmaster'), "mailto:webmaster@openstreetmap.org"
253 flash.now[:error] = t 'user.login.account suspended', :webmaster => webmaster
255 flash.now[:error] = t 'user.login.auth failure'
257 elsif flash[:notice].nil?
258 flash.now[:notice] = t 'user.login.notice'
263 @title = t 'user.logout.title'
265 if params[:session] == request.session_options[:id]
267 token = UserToken.find_by_token(session[:token])
271 session[:token] = nil
274 session_expires_automatically
276 redirect_to params[:referer]
278 redirect_to :controller => 'site', :action => 'index'
285 if token = UserToken.find_by_token(params[:confirm_string])
286 if token.user.active?
287 flash[:error] = t('user.confirm.already active')
288 redirect_to :action => 'login'
291 user.status = "active"
292 user.email_valid = true
294 referer = token.referer
298 token = UserToken.find_by_token(session[:token])
299 session.delete(:token)
304 if token.nil? or token.user != user
305 flash[:notice] = t('user.confirm.success')
306 redirect_to :action => :login, :referer => referer
310 session[:user] = user.id
313 flash[:notice] = t('user.confirm.success') + "<br /><br />" + t('user.confirm.before you start')
314 redirect_to :action => :account, :display_name => user.display_name
316 flash[:notice] = t('user.confirm.success')
322 user = User.find_by_display_name(params[:display_name])
324 if user and user.active?
325 flash[:error] = t('user.confirm.already active')
327 flash[:error] = t('user.confirm.unknown token') + t('user.confirm.reconfirm', :reconfirm => url_for(:action => 'confirm_resend', :display_name => params[:display_name]))
329 flash[:error] = t('user.confirm.unknown token')
332 redirect_to :action => 'login'
338 if user = User.find_by_display_name(params[:display_name])
339 Notifier.deliver_signup_confirm(user, user.tokens.create)
340 flash[:notice] = t 'user.confirm_resend.success', :email => user.email
342 flash[:notice] = t 'user.confirm_resend.failure', :name => params[:display_name]
345 redirect_to :action => 'login'
350 token = UserToken.find_by_token(params[:confirm_string])
351 if token and token.user.new_email?
353 @user.email = @user.new_email
354 @user.new_email = nil
355 @user.email_valid = true
357 flash[:notice] = t 'user.confirm_email.success'
359 flash[:errors] = @user.errors
362 session[:user] = @user.id
363 redirect_to :action => 'account', :display_name => @user.display_name
365 flash[:error] = t 'user.confirm_email.failure'
366 redirect_to :action => 'account', :display_name => @user.display_name
372 doc = OSM::API.new.get_xml_doc
373 @user.traces.each do |trace|
374 doc.root << trace.to_xml_node() if trace.public? or 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? or (@user and @user.administrator?))
384 @title = @this_user.display_name
386 @title = t 'user.no_such_user.title'
387 @not_found_user = params[:display_name]
388 render :action => 'no_such_user', :status => :not_found
393 if params[:display_name]
394 name = params[:display_name]
395 new_friend = User.find_by_display_name(name, :conditions => {:status => ["active", "confirmed"]})
397 friend.user_id = @user.id
398 friend.friend_user_id = new_friend.id
399 unless @user.is_friends_with?(new_friend)
401 flash[:notice] = t 'user.make_friend.success', :name => name
402 Notifier.deliver_friend_notification(friend)
404 friend.add_error(t('user.make_friend.failed', :name => name))
407 flash[:warning] = t 'user.make_friend.already_a_friend', :name => name
411 redirect_to params[:referer]
413 redirect_to :controller => 'user', :action => 'view'
419 if params[:display_name]
420 name = params[:display_name]
421 friend = User.find_by_display_name(name, :conditions => {:status => ["active", "confirmed"]})
422 if @user.is_friends_with?(friend)
423 Friend.delete_all "user_id = #{@user.id} AND friend_user_id = #{friend.id}"
424 flash[:notice] = t 'user.remove_friend.success', :name => friend.display_name
426 flash[:error] = t 'user.remove_friend.not_a_friend', :name => friend.display_name
430 redirect_to params[:referer]
432 redirect_to :controller => 'user', :action => 'view'
438 # sets a user's status
440 @this_user.update_attributes(:status => params[:status])
441 redirect_to :controller => 'user', :action => 'view', :display_name => params[:display_name]
445 # delete a user, marking them as deleted and removing personal data
448 redirect_to :controller => 'user', :action => 'view', :display_name => params[:display_name]
452 # display a list of users matching specified criteria
455 ids = params[:user].keys.collect { |id| id.to_i }
457 User.update_all("status = 'confirmed'", :id => ids) if params[:confirm]
458 User.update_all("status = 'deleted'", :id => ids) if params[:hide]
460 redirect_to url_for(:status => params[:status], :ip => params[:ip], :page => params[:page])
462 conditions = Hash.new
463 conditions[:status] = params[:status] if params[:status]
464 conditions[:creation_ip] = params[:ip] if params[:ip]
466 @user_pages, @users = paginate(:users,
467 :conditions => conditions,
476 # require that the user is a administrator, or fill out a helpful error message
477 # and return them to the user page.
478 def require_administrator
479 if @user and not @user.administrator?
480 flash[:error] = t('user.filter.not_an_administrator')
482 if params[:display_name]
483 redirect_to :controller => 'user', :action => 'view', :display_name => params[:display_name]
485 redirect_to :controller => 'user', :action => 'login', :referer => request.request_uri
488 redirect_to :controller => 'user', :action => 'login', :referer => request.request_uri
493 # ensure that there is a "this_user" instance variable
495 @this_user = User.find_by_display_name(params[:display_name])
496 rescue ActiveRecord::RecordNotFound
497 redirect_to :controller => 'user', :action => 'view', :display_name => params[:display_name] unless @this_user