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]
35 render :action => :new
36 elsif @user.terms_agreed?
37 redirect_to :action => :account, :display_name => @user.display_name
40 redirect_to :action => :login, :referer => request.request_uri
46 @title = t 'user.new.title'
48 if Acl.find_by_address(request.remote_ip, :conditions => {:k => "no_account_creation"})
49 render :action => 'new'
50 elsif params[:decline]
51 redirect_to t('user.terms.declined')
53 if !@user.terms_agreed?
54 @user.consider_pd = params[:user][:consider_pd]
55 @user.terms_agreed = Time.now.getutc
57 flash[:notice] = t 'user.new.terms accepted'
61 redirect_to :action => :account, :display_name => @user.display_name
63 @user = User.new(params[:user])
65 @user.status = "pending"
66 @user.data_public = true
67 @user.description = "" if @user.description.nil?
68 @user.creation_ip = request.remote_ip
69 @user.languages = request.user_preferred_languages
70 @user.terms_agreed = Time.now.getutc
73 flash[:notice] = t 'user.new.flash create success message'
74 Notifier.deliver_signup_confirm(@user, @user.tokens.create(:referer => params[:referer]))
75 redirect_to :action => 'login'
77 render :action => 'new'
83 @title = t 'user.account.title'
84 @tokens = @user.oauth_tokens.find :all, :conditions => 'oauth_tokens.invalidated_at is null and oauth_tokens.authorized_at is not null'
86 if params[:user] and params[:user][:display_name] and params[:user][:description]
87 @user.display_name = params[:user][:display_name]
88 @user.new_email = params[:user][:new_email]
90 if params[:user][:pass_crypt].length > 0 or params[:user][:pass_crypt_confirmation].length > 0
91 @user.pass_crypt = params[:user][:pass_crypt]
92 @user.pass_crypt_confirmation = params[:user][:pass_crypt_confirmation]
95 @user.description = params[:user][:description]
96 @user.languages = params[:user][:languages].split(",")
98 case params[:image_action]
99 when "new" then @user.image = params[:user][:image]
100 when "delete" then @user.image = nil
103 @user.home_lat = params[:user][:home_lat]
104 @user.home_lon = params[:user][:home_lon]
109 if @user.new_email.nil? or @user.new_email.empty?
110 flash[:notice] = t 'user.account.flash update success'
112 flash[:notice] = t 'user.account.flash update success confirm needed'
115 Notifier.deliver_email_confirm(@user, @user.tokens.create)
117 # Ignore errors sending email
121 redirect_to :action => "account", :display_name => @user.display_name
125 flash[:errors].each do |attr,msg|
126 attr = "new_email" if attr == "email"
127 @user.errors.add(attr,msg)
134 @user.data_public = true
136 flash[:notice] = t 'user.go_public.flash success'
137 redirect_to :controller => 'user', :action => 'account', :display_name => @user.display_name
141 @title = t 'user.lost_password.title'
143 if params[:user] and params[:user][:email]
144 user = User.find_by_email(params[:user][:email], :conditions => {:status => ["pending", "active", "confirmed"]})
147 token = user.tokens.create
148 Notifier.deliver_lost_password(user, token)
149 flash[:notice] = t 'user.lost_password.notice email on way'
150 redirect_to :action => 'login'
152 flash.now[:error] = t 'user.lost_password.notice email cannot find'
158 @title = t 'user.reset_password.title'
161 token = UserToken.find_by_token(params[:token])
167 @user.pass_crypt = params[:user][:pass_crypt]
168 @user.pass_crypt_confirmation = params[:user][:pass_crypt_confirmation]
169 @user.status = "active" if @user.status == "pending"
170 @user.email_valid = true
174 flash[:notice] = t 'user.reset_password.flash changed'
175 redirect_to :action => 'login'
179 flash[:error] = t 'user.reset_password.flash token bad'
180 redirect_to :action => 'lost_password'
186 @title = t 'user.new.title'
188 # The user is logged in already, so don't show them the signup
189 # page, instead send them to the home page
190 redirect_to :controller => 'site', :action => 'index' if session[:user]
194 @title = t 'user.login.title'
197 email_or_display_name = params[:user][:email]
198 pass = params[:user][:password]
199 user = User.authenticate(:username => email_or_display_name, :password => pass)
202 session[:user] = user.id
203 session_expires_after 1.month if params[:remember_me]
205 # The user is logged in, if the referer param exists, redirect
206 # them to that unless they've also got a block on them, in
207 # which case redirect them to the block so they can clear it.
208 if user.blocked_on_view
209 redirect_to user.blocked_on_view, :referer => params[:referer]
210 elsif params[:referer]
211 redirect_to params[:referer]
213 redirect_to :controller => 'site', :action => 'index'
215 elsif User.authenticate(:username => email_or_display_name, :password => pass, :pending => true)
216 flash.now[:error] = t 'user.login.account not active'
217 elsif User.authenticate(:username => email_or_display_name, :password => pass, :suspended => true)
218 webmaster = link_to t('user.login.webmaster'), "mailto:webmaster@openstreetmap.org"
219 flash.now[:error] = t 'user.login.account suspended', :webmaster => webmaster
221 flash.now[:error] = t 'user.login.auth failure'
227 @title = t 'user.logout.title'
229 if params[:session] == request.session_options[:id]
231 token = UserToken.find_by_token(session[:token])
235 session[:token] = nil
238 session_expires_automatically
240 redirect_to params[:referer]
242 redirect_to :controller => 'site', :action => 'index'
248 if params[:confirm_action]
249 token = UserToken.find_by_token(params[:confirm_string])
250 if token and !token.user.active?
252 @user.status = "active"
253 @user.email_valid = true
255 referer = token.referer
257 flash[:notice] = t 'user.confirm.success'
258 session[:user] = @user.id
262 redirect_to :action => 'account', :display_name => @user.display_name
265 flash.now[:error] = t 'user.confirm.failure'
271 if params[:confirm_action]
272 token = UserToken.find_by_token(params[:confirm_string])
273 if token and token.user.new_email?
275 @user.email = @user.new_email
276 @user.new_email = nil
277 @user.email_valid = true
279 flash[:notice] = t 'user.confirm_email.success'
281 flash[:errors] = @user.errors
284 session[:user] = @user.id
285 redirect_to :action => 'account', :display_name => @user.display_name
287 flash.now[:error] = t 'user.confirm_email.failure'
293 doc = OSM::API.new.get_xml_doc
294 @user.traces.each do |trace|
295 doc.root << trace.to_xml_node() if trace.public? or trace.user == @user
297 render :text => doc.to_s, :content_type => "text/xml"
301 @this_user = User.find_by_display_name(params[:display_name])
304 (@this_user.visible? or (@user and @user.administrator?))
305 @title = @this_user.display_name
307 @title = t 'user.no_such_user.title'
308 @not_found_user = params[:display_name]
309 render :action => 'no_such_user', :status => :not_found
314 if params[:display_name]
315 name = params[:display_name]
316 new_friend = User.find_by_display_name(name, :conditions => {:status => ["active", "confirmed"]})
318 friend.user_id = @user.id
319 friend.friend_user_id = new_friend.id
320 unless @user.is_friends_with?(new_friend)
322 flash[:notice] = t 'user.make_friend.success', :name => name
323 Notifier.deliver_friend_notification(friend)
325 friend.add_error(t('user.make_friend.failed', :name => name))
328 flash[:warning] = t 'user.make_friend.already_a_friend', :name => name
332 redirect_to params[:referer]
334 redirect_to :controller => 'user', :action => 'view'
340 if params[:display_name]
341 name = params[:display_name]
342 friend = User.find_by_display_name(name, :conditions => {:status => ["active", "confirmed"]})
343 if @user.is_friends_with?(friend)
344 Friend.delete_all "user_id = #{@user.id} AND friend_user_id = #{friend.id}"
345 flash[:notice] = t 'user.remove_friend.success', :name => friend.display_name
347 flash[:error] = t 'user.remove_friend.not_a_friend', :name => friend.display_name
351 redirect_to params[:referer]
353 redirect_to :controller => 'user', :action => 'view'
359 # sets a user's status
361 @this_user.update_attributes(:status => params[:status])
362 redirect_to :controller => 'user', :action => 'view', :display_name => params[:display_name]
366 # delete a user, marking them as deleted and removing personal data
369 redirect_to :controller => 'user', :action => 'view', :display_name => params[:display_name]
373 # display a list of users matching specified criteria
376 ids = params[:user].keys.collect { |id| id.to_i }
378 User.update_all("status = 'confirmed'", :id => ids) if params[:confirm]
379 User.update_all("status = 'deleted'", :id => ids) if params[:hide]
381 redirect_to url_for(:status => params[:status], :ip => params[:ip], :page => params[:page])
383 conditions = Hash.new
384 conditions[:status] = params[:status] if params[:status]
385 conditions[:creation_ip] = params[:ip] if params[:ip]
387 @user_pages, @users = paginate(:users,
388 :conditions => conditions,
397 # require that the user is a administrator, or fill out a helpful error message
398 # and return them to the user page.
399 def require_administrator
400 if @user and not @user.administrator?
401 flash[:error] = t('user.filter.not_an_administrator')
403 if params[:display_name]
404 redirect_to :controller => 'user', :action => 'view', :display_name => params[:display_name]
406 redirect_to :controller => 'user', :action => 'login', :referer => request.request_uri
409 redirect_to :controller => 'user', :action => 'login', :referer => request.request_uri
414 # ensure that there is a "this_user" instance variable
416 @this_user = User.find_by_display_name(params[:display_name])
417 rescue ActiveRecord::RecordNotFound
418 redirect_to :controller => 'user', :action => 'view', :display_name => params[:display_name] unless @this_user