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 => OSM_STATUS == :database_offline
22 @title = t 'user.new.title'
23 @user = User.new(params[:user])
25 @legale = params[:legale] || OSM.IPToCountry(request.remote_ip) || APP_CONFIG['default_legale']
26 @text = OSM.legal_text_for_country(@legale)
29 render :update do |page|
30 page.replace_html "contributorTerms", :partial => "terms"
33 render :action => 'new'
38 @title = t 'user.new.title'
40 if Acl.find_by_address(request.remote_ip, :conditions => {:k => "no_account_creation"})
41 render :action => 'new'
42 elsif params[:decline]
43 redirect_to t('user.terms.declined')
45 @user = User.new(params[:user])
47 @user.status = "pending"
48 @user.data_public = true
49 @user.description = "" if @user.description.nil?
50 @user.creation_ip = request.remote_ip
51 @user.languages = request.user_preferred_languages
52 @user.terms_agreed = Time.now.getutc
55 flash[:notice] = t 'user.new.flash create success message'
56 Notifier.deliver_signup_confirm(@user, @user.tokens.create(:referer => params[:referer]))
57 redirect_to :action => 'login'
59 render :action => 'new'
65 @title = t 'user.account.title'
66 @tokens = @user.oauth_tokens.find :all, :conditions => 'oauth_tokens.invalidated_at is null and oauth_tokens.authorized_at is not null'
68 if params[:user] and params[:user][:display_name] and params[:user][:description]
69 @user.display_name = params[:user][:display_name]
70 @user.new_email = params[:user][:new_email]
72 if params[:user][:pass_crypt].length > 0 or params[:user][:pass_crypt_confirmation].length > 0
73 @user.pass_crypt = params[:user][:pass_crypt]
74 @user.pass_crypt_confirmation = params[:user][:pass_crypt_confirmation]
77 @user.description = params[:user][:description]
78 @user.languages = params[:user][:languages].split(",")
80 case params[:image_action]
81 when "new" then @user.image = params[:user][:image]
82 when "delete" then @user.image = nil
85 @user.home_lat = params[:user][:home_lat]
86 @user.home_lon = params[:user][:home_lon]
91 if @user.new_email.nil? or @user.new_email.empty?
92 flash[:notice] = t 'user.account.flash update success'
94 flash[:notice] = t 'user.account.flash update success confirm needed'
97 Notifier.deliver_email_confirm(@user, @user.tokens.create)
99 # Ignore errors sending email
103 redirect_to :action => "account", :display_name => @user.display_name
107 flash[:errors].each do |attr,msg|
108 attr = "new_email" if attr == "email"
109 @user.errors.add(attr,msg)
116 @user.data_public = true
118 flash[:notice] = t 'user.go_public.flash success'
119 redirect_to :controller => 'user', :action => 'account', :display_name => @user.display_name
123 @title = t 'user.lost_password.title'
125 if params[:user] and params[:user][:email]
126 user = User.find_by_email(params[:user][:email], :conditions => {:status => ["pending", "active", "confirmed"]})
129 token = user.tokens.create
130 Notifier.deliver_lost_password(user, token)
131 flash[:notice] = t 'user.lost_password.notice email on way'
132 redirect_to :action => 'login'
134 flash.now[:error] = t 'user.lost_password.notice email cannot find'
140 @title = t 'user.reset_password.title'
143 token = UserToken.find_by_token(params[:token])
149 @user.pass_crypt = params[:user][:pass_crypt]
150 @user.pass_crypt_confirmation = params[:user][:pass_crypt_confirmation]
151 @user.status = "active" if @user.status == "pending"
152 @user.email_valid = true
156 flash[:notice] = t 'user.reset_password.flash changed'
157 redirect_to :action => 'login'
161 flash[:error] = t 'user.reset_password.flash token bad'
162 redirect_to :action => 'lost_password'
168 @title = t 'user.new.title'
170 # The user is logged in already, so don't show them the signup
171 # page, instead send them to the home page
172 redirect_to :controller => 'site', :action => 'index' if session[:user]
176 @title = t 'user.login.title'
179 email_or_display_name = params[:user][:email]
180 pass = params[:user][:password]
181 user = User.authenticate(:username => email_or_display_name, :password => pass)
184 session[:user] = user.id
185 session_expires_after 1.month if params[:remember_me]
187 # The user is logged in, if the referer param exists, redirect
188 # them to that unless they've also got a block on them, in
189 # which case redirect them to the block so they can clear it.
190 if user.blocked_on_view
191 redirect_to user.blocked_on_view, :referrer => params[:referrer]
192 elsif params[:referer]
193 redirect_to params[:referer]
195 redirect_to :controller => 'site', :action => 'index'
197 elsif User.authenticate(:username => email_or_display_name, :password => pass, :pending => true)
198 flash.now[:error] = t 'user.login.account not active'
199 elsif User.authenticate(:username => email_or_display_name, :password => pass, :suspended => true)
200 webmaster = link_to t('user.login.webmaster'), "mailto:webmaster@openstreetmap.org"
201 flash.now[:error] = t 'user.login.account suspended', :webmaster => webmaster
203 flash.now[:error] = t 'user.login.auth failure'
209 @title = t 'user.logout.title'
211 if params[:session] == request.session_options[:id]
213 token = UserToken.find_by_token(session[:token])
217 session[:token] = nil
220 session_expires_automatically
222 redirect_to params[:referer]
224 redirect_to :controller => 'site', :action => 'index'
230 if params[:confirm_action]
231 token = UserToken.find_by_token(params[:confirm_string])
232 if token and !token.user.active?
234 @user.status = "active"
235 @user.email_valid = true
237 referer = token.referer
239 flash[:notice] = t 'user.confirm.success'
240 session[:user] = @user.id
244 redirect_to :action => 'account', :display_name => @user.display_name
247 flash.now[:error] = t 'user.confirm.failure'
253 if params[:confirm_action]
254 token = UserToken.find_by_token(params[:confirm_string])
255 if token and token.user.new_email?
257 @user.email = @user.new_email
258 @user.new_email = nil
259 @user.email_valid = true
261 flash[:notice] = t 'user.confirm_email.success'
263 flash[:errors] = @user.errors
266 session[:user] = @user.id
267 redirect_to :action => 'account', :display_name => @user.display_name
269 flash.now[:error] = t 'user.confirm_email.failure'
275 doc = OSM::API.new.get_xml_doc
276 @user.traces.each do |trace|
277 doc.root << trace.to_xml_node() if trace.public? or trace.user == @user
279 render :text => doc.to_s, :content_type => "text/xml"
283 @this_user = User.find_by_display_name(params[:display_name])
286 (@this_user.visible? or (@user and @user.administrator?))
287 @title = @this_user.display_name
289 @title = t 'user.no_such_user.title'
290 @not_found_user = params[:display_name]
291 render :action => 'no_such_user', :status => :not_found
296 if params[:display_name]
297 name = params[:display_name]
298 new_friend = User.find_by_display_name(name, :conditions => {:status => ["active", "confirmed"]})
300 friend.user_id = @user.id
301 friend.friend_user_id = new_friend.id
302 unless @user.is_friends_with?(new_friend)
304 flash[:notice] = t 'user.make_friend.success', :name => name
305 Notifier.deliver_friend_notification(friend)
307 friend.add_error(t('user.make_friend.failed', :name => name))
310 flash[:warning] = t 'user.make_friend.already_a_friend', :name => name
314 redirect_to params[:referer]
316 redirect_to :controller => 'user', :action => 'view'
322 if params[:display_name]
323 name = params[:display_name]
324 friend = User.find_by_display_name(name, :conditions => {:status => ["active", "confirmed"]})
325 if @user.is_friends_with?(friend)
326 Friend.delete_all "user_id = #{@user.id} AND friend_user_id = #{friend.id}"
327 flash[:notice] = t 'user.remove_friend.success', :name => friend.display_name
329 flash[:error] = t 'user.remove_friend.not_a_friend', :name => friend.display_name
333 redirect_to params[:referer]
335 redirect_to :controller => 'user', :action => 'view'
341 # sets a user's status
343 @this_user.update_attributes(:status => params[:status])
344 redirect_to :controller => 'user', :action => 'view', :display_name => params[:display_name]
348 # delete a user, marking them as deleted and removing personal data
351 redirect_to :controller => 'user', :action => 'view', :display_name => params[:display_name]
355 # display a list of users matching specified criteria
358 ids = params[:user].keys.collect { |id| id.to_i }
360 User.update_all("status = 'confirmed'", :id => ids) if params[:confirm]
361 User.update_all("status = 'deleted'", :id => ids) if params[:hide]
363 redirect_to url_for(:status => params[:status], :ip => params[:ip], :page => params[:page])
365 conditions = Hash.new
366 conditions[:status] = params[:status] if params[:status]
367 conditions[:creation_ip] = params[:ip] if params[:ip]
369 @user_pages, @users = paginate(:users,
370 :conditions => conditions,
379 # require that the user is a administrator, or fill out a helpful error message
380 # and return them to the user page.
381 def require_administrator
382 if @user and not @user.administrator?
383 flash[:error] = t('user.filter.not_an_administrator')
385 if params[:display_name]
386 redirect_to :controller => 'user', :action => 'view', :display_name => params[:display_name]
388 redirect_to :controller => 'user', :action => 'login', :referer => request.request_uri
391 redirect_to :controller => 'user', :action => 'login', :referer => request.request_uri
396 # ensure that there is a "this_user" instance variable
398 @this_user = User.find_by_display_name(params[:display_name])
399 rescue ActiveRecord::RecordNotFound
400 redirect_to :controller => 'user', :action => 'view', :display_name => params[:display_name] unless @this_user