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 => [:activate, :deactivate, :hide, :unhide, :delete]
15 before_filter :lookup_this_user, :only => [:activate, :deactivate, :hide, :unhide, :delete]
17 filter_parameter_logging :password, :pass_crypt, :pass_crypt_confirmation
19 cache_sweeper :user_sweeper, :only => [:account, :hide, :unhide, :delete]
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'
43 @user = User.new(params[:user])
46 @user.data_public = true
47 @user.description = "" if @user.description.nil?
48 @user.creation_ip = request.remote_ip
49 @user.languages = request.user_preferred_languages
50 @user.terms_agreed = Time.now.getutc
53 flash[:notice] = t 'user.new.flash create success message'
54 Notifier.deliver_signup_confirm(@user, @user.tokens.create(:referer => params[:referer]))
55 redirect_to :action => 'login'
57 render :action => 'new'
63 @title = t 'user.account.title'
64 @tokens = @user.oauth_tokens.find :all, :conditions => 'oauth_tokens.invalidated_at is null and oauth_tokens.authorized_at is not null'
66 if params[:user] and params[:user][:display_name] and params[:user][:description]
67 @user.display_name = params[:user][:display_name]
68 @user.new_email = params[:user][:new_email]
70 if params[:user][:pass_crypt].length > 0 or params[:user][:pass_crypt_confirmation].length > 0
71 @user.pass_crypt = params[:user][:pass_crypt]
72 @user.pass_crypt_confirmation = params[:user][:pass_crypt_confirmation]
75 @user.description = params[:user][:description]
76 @user.languages = params[:user][:languages].split(",")
78 case params[:image_action]
79 when "new" then @user.image = params[:user][:image]
80 when "delete" then @user.image = nil
83 @user.home_lat = params[:user][:home_lat]
84 @user.home_lon = params[:user][:home_lon]
89 if @user.new_email.nil? or @user.new_email.empty?
90 flash.now[:notice] = t 'user.account.flash update success'
92 flash.now[:notice] = t 'user.account.flash update success confirm needed'
95 Notifier.deliver_email_confirm(@user, @user.tokens.create)
97 # Ignore errors sending email
103 flash[:errors].each do |attr,msg|
104 attr = "new_email" if attr == "email"
105 @user.errors.add(attr,msg)
112 @user.data_public = true
114 flash[:notice] = t 'user.go_public.flash success'
115 redirect_to :controller => 'user', :action => 'account', :display_name => @user.display_name
119 @title = t 'user.lost_password.title'
121 if params[:user] and params[:user][:email]
122 user = User.find_by_email(params[:user][:email], :conditions => {:visible => true})
125 token = user.tokens.create
126 Notifier.deliver_lost_password(user, token)
127 flash[:notice] = t 'user.lost_password.notice email on way'
128 redirect_to :action => 'login'
130 flash.now[:error] = t 'user.lost_password.notice email cannot find'
136 @title = t 'user.reset_password.title'
139 token = UserToken.find_by_token(params[:token])
145 @user.pass_crypt = params[:user][:pass_crypt]
146 @user.pass_crypt_confirmation = params[:user][:pass_crypt_confirmation]
148 @user.email_valid = true
152 flash[:notice] = t 'user.reset_password.flash changed'
153 redirect_to :action => 'login'
157 flash[:error] = t 'user.reset_password.flash token bad'
158 redirect_to :action => 'lost_password'
164 @title = t 'user.new.title'
166 # The user is logged in already, so don't show them the signup
167 # page, instead send them to the home page
168 redirect_to :controller => 'site', :action => 'index' if session[:user]
172 @title = t 'user.login.title'
175 email_or_display_name = params[:user][:email]
176 pass = params[:user][:password]
177 user = User.authenticate(:username => email_or_display_name, :password => pass)
180 session[:user] = user.id
181 session_expires_after 1.month if params[:remember_me]
183 # The user is logged in, if the referer param exists, redirect
184 # them to that unless they've also got a block on them, in
185 # which case redirect them to the block so they can clear it.
186 if user.blocked_on_view
187 redirect_to user.blocked_on_view, :referrer => params[:referrer]
188 elsif params[:referer]
189 redirect_to params[:referer]
191 redirect_to :controller => 'site', :action => 'index'
193 elsif User.authenticate(:username => email_or_display_name, :password => pass, :inactive => true)
194 flash.now[:error] = t 'user.login.account not active'
196 flash.now[:error] = t 'user.login.auth failure'
202 @title = t 'user.logout.title'
204 if params[:session] == request.session_options[:id]
206 token = UserToken.find_by_token(session[:token])
210 session[:token] = nil
213 session_expires_automatically
215 redirect_to params[:referer]
217 redirect_to :controller => 'site', :action => 'index'
223 if params[:confirm_action]
224 token = UserToken.find_by_token(params[:confirm_string])
225 if token and !token.user.active?
228 @user.email_valid = true
230 referer = token.referer
232 flash[:notice] = t 'user.confirm.success'
233 session[:user] = @user.id
237 redirect_to :action => 'account', :display_name => @user.display_name
240 flash.now[:error] = t 'user.confirm.failure'
246 if params[:confirm_action]
247 token = UserToken.find_by_token(params[:confirm_string])
248 if token and token.user.new_email?
250 @user.email = @user.new_email
251 @user.new_email = nil
253 @user.email_valid = true
255 flash[:notice] = t 'user.confirm_email.success'
257 flash[:errors] = @user.errors
260 session[:user] = @user.id
261 redirect_to :action => 'account', :display_name => @user.display_name
263 flash.now[:error] = t 'user.confirm_email.failure'
269 doc = OSM::API.new.get_xml_doc
270 @user.traces.each do |trace|
271 doc.root << trace.to_xml_node() if trace.public? or trace.user == @user
273 render :text => doc.to_s, :content_type => "text/xml"
277 @this_user = User.find_by_display_name(params[:display_name])
280 (@this_user.visible? or (@user and @user.administrator?))
281 @title = @this_user.display_name
283 @title = t 'user.no_such_user.title'
284 @not_found_user = params[:display_name]
285 render :action => 'no_such_user', :status => :not_found
290 if params[:display_name]
291 name = params[:display_name]
292 new_friend = User.find_by_display_name(name, :conditions => {:visible => true})
294 friend.user_id = @user.id
295 friend.friend_user_id = new_friend.id
296 unless @user.is_friends_with?(new_friend)
298 flash[:notice] = t 'user.make_friend.success', :name => name
299 Notifier.deliver_friend_notification(friend)
301 friend.add_error(t('user.make_friend.failed', :name => name))
304 flash[:warning] = t 'user.make_friend.already_a_friend', :name => name
308 redirect_to params[:referer]
310 redirect_to :controller => 'user', :action => 'view'
316 if params[:display_name]
317 name = params[:display_name]
318 friend = User.find_by_display_name(name, :conditions => {:visible => true})
319 if @user.is_friends_with?(friend)
320 Friend.delete_all "user_id = #{@user.id} AND friend_user_id = #{friend.id}"
321 flash[:notice] = t 'user.remove_friend.success', :name => friend.display_name
323 flash[:error] = t 'user.remove_friend.not_a_friend', :name => friend.display_name
327 redirect_to params[:referer]
329 redirect_to :controller => 'user', :action => 'view'
335 # activate a user, allowing them to log in
337 @this_user.update_attributes(:active => true)
338 redirect_to :controller => 'user', :action => 'view', :display_name => params[:display_name]
342 # deactivate a user, preventing them from logging in
344 @this_user.update_attributes(:active => false)
345 redirect_to :controller => 'user', :action => 'view', :display_name => params[:display_name]
349 # hide a user, marking them as logically deleted
351 @this_user.update_attributes(:visible => false)
352 redirect_to :controller => 'user', :action => 'view', :display_name => params[:display_name]
356 # unhide a user, clearing the logically deleted flag
358 @this_user.update_attributes(:visible => true)
359 redirect_to :controller => 'user', :action => 'view', :display_name => params[:display_name]
363 # delete a user, marking them as deleted and removing personal data
366 redirect_to :controller => 'user', :action => 'view', :display_name => params[:display_name]
370 # require that the user is a administrator, or fill out a helpful error message
371 # and return them to the user page.
372 def require_administrator
373 unless @user.administrator?
374 flash[:error] = t('user.filter.not_an_administrator')
375 redirect_to :controller => 'user', :action => 'view', :display_name => params[:display_name]
380 # ensure that there is a "this_user" instance variable
382 @this_user = User.find_by_display_name(params[:display_name])
383 rescue ActiveRecord::RecordNotFound
384 redirect_to :controller => 'user', :action => 'view', :display_name => params[:display_name] unless @this_user