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 country_code = params[:legale] || OSM.IPToCountry(request.remote_ip)
26 @text = OSM.legal_text_for_country(country_code)
29 render :action => 'new'
34 @title = t 'user.new.title'
36 if Acl.find_by_address(request.remote_ip, :conditions => {:k => "no_account_creation"})
37 render :action => 'new'
39 @user = User.new(params[:user])
42 @user.data_public = true
43 @user.description = "" if @user.description.nil?
44 @user.creation_ip = request.remote_ip
45 @user.languages = request.user_preferred_languages
46 @user.terms_agreed = Time.now.getutc
49 flash[:notice] = t 'user.new.flash create success message'
50 Notifier.deliver_signup_confirm(@user, @user.tokens.create(:referer => params[:referer]))
51 redirect_to :action => 'login'
53 render :action => 'new'
59 @title = t 'user.account.title'
60 @tokens = @user.oauth_tokens.find :all, :conditions => 'oauth_tokens.invalidated_at is null and oauth_tokens.authorized_at is not null'
62 if params[:user] and params[:user][:display_name] and params[:user][:description]
63 @user.display_name = params[:user][:display_name]
64 @user.new_email = params[:user][:new_email]
66 if params[:user][:pass_crypt].length > 0 or params[:user][:pass_crypt_confirmation].length > 0
67 @user.pass_crypt = params[:user][:pass_crypt]
68 @user.pass_crypt_confirmation = params[:user][:pass_crypt_confirmation]
71 @user.description = params[:user][:description]
72 @user.languages = params[:user][:languages].split(",")
74 case params[:image_action]
75 when "new" then @user.image = params[:user][:image]
76 when "delete" then @user.image = nil
79 @user.home_lat = params[:user][:home_lat]
80 @user.home_lon = params[:user][:home_lon]
85 if @user.new_email.nil? or @user.new_email.empty?
86 flash.now[:notice] = t 'user.account.flash update success'
88 flash.now[:notice] = t 'user.account.flash update success confirm needed'
91 Notifier.deliver_email_confirm(@user, @user.tokens.create)
93 # Ignore errors sending email
99 flash[:errors].each do |attr,msg|
100 attr = "new_email" if attr == "email"
101 @user.errors.add(attr,msg)
108 @user.data_public = true
110 flash[:notice] = t 'user.go_public.flash success'
111 redirect_to :controller => 'user', :action => 'account', :display_name => @user.display_name
115 @title = t 'user.lost_password.title'
117 if params[:user] and params[:user][:email]
118 user = User.find_by_email(params[:user][:email], :conditions => {:visible => true})
121 token = user.tokens.create
122 Notifier.deliver_lost_password(user, token)
123 flash[:notice] = t 'user.lost_password.notice email on way'
124 redirect_to :action => 'login'
126 flash.now[:error] = t 'user.lost_password.notice email cannot find'
132 @title = t 'user.reset_password.title'
135 token = UserToken.find_by_token(params[:token])
141 @user.pass_crypt = params[:user][:pass_crypt]
142 @user.pass_crypt_confirmation = params[:user][:pass_crypt_confirmation]
144 @user.email_valid = true
148 flash[:notice] = t 'user.reset_password.flash changed'
149 redirect_to :action => 'login'
153 flash[:error] = t 'user.reset_password.flash token bad'
154 redirect_to :action => 'lost_password'
160 @title = t 'user.new.title'
162 # The user is logged in already, so don't show them the signup
163 # page, instead send them to the home page
164 redirect_to :controller => 'site', :action => 'index' if session[:user]
168 @title = t 'user.login.title'
171 email_or_display_name = params[:user][:email]
172 pass = params[:user][:password]
173 user = User.authenticate(:username => email_or_display_name, :password => pass)
176 session[:user] = user.id
177 session_expires_after 1.month if params[:remember_me]
179 # The user is logged in, if the referer param exists, redirect
180 # them to that unless they've also got a block on them, in
181 # which case redirect them to the block so they can clear it.
182 if user.blocked_on_view
183 redirect_to user.blocked_on_view, :referrer => params[:referrer]
184 elsif params[:referer]
185 redirect_to params[:referer]
187 redirect_to :controller => 'site', :action => 'index'
189 elsif User.authenticate(:username => email_or_display_name, :password => pass, :inactive => true)
190 flash.now[:error] = t 'user.login.account not active'
192 flash.now[:error] = t 'user.login.auth failure'
198 @title = t 'user.logout.title'
200 if params[:session] == request.session_options[:id]
202 token = UserToken.find_by_token(session[:token])
206 session[:token] = nil
209 session_expires_automatically
211 redirect_to params[:referer]
213 redirect_to :controller => 'site', :action => 'index'
219 if params[:confirm_action]
220 token = UserToken.find_by_token(params[:confirm_string])
221 if token and !token.user.active?
224 @user.email_valid = true
226 referer = token.referer
228 flash[:notice] = t 'user.confirm.success'
229 session[:user] = @user.id
233 redirect_to :action => 'account', :display_name => @user.display_name
236 flash.now[:error] = t 'user.confirm.failure'
242 if params[:confirm_action]
243 token = UserToken.find_by_token(params[:confirm_string])
244 if token and token.user.new_email?
246 @user.email = @user.new_email
247 @user.new_email = nil
249 @user.email_valid = true
251 flash[:notice] = t 'user.confirm_email.success'
253 flash[:errors] = @user.errors
256 session[:user] = @user.id
257 redirect_to :action => 'account', :display_name => @user.display_name
259 flash.now[:error] = t 'user.confirm_email.failure'
265 doc = OSM::API.new.get_xml_doc
266 @user.traces.each do |trace|
267 doc.root << trace.to_xml_node() if trace.public? or trace.user == @user
269 render :text => doc.to_s, :content_type => "text/xml"
273 @this_user = User.find_by_display_name(params[:display_name])
276 (@this_user.visible? or (@user and @user.administrator?))
277 @title = @this_user.display_name
279 @title = t 'user.no_such_user.title'
280 @not_found_user = params[:display_name]
281 render :action => 'no_such_user', :status => :not_found
286 if params[:display_name]
287 name = params[:display_name]
288 new_friend = User.find_by_display_name(name, :conditions => {:visible => true})
290 friend.user_id = @user.id
291 friend.friend_user_id = new_friend.id
292 unless @user.is_friends_with?(new_friend)
294 flash[:notice] = t 'user.make_friend.success', :name => name
295 Notifier.deliver_friend_notification(friend)
297 friend.add_error(t('user.make_friend.failed', :name => name))
300 flash[:warning] = t 'user.make_friend.already_a_friend', :name => name
304 redirect_to params[:referer]
306 redirect_to :controller => 'user', :action => 'view'
312 if params[:display_name]
313 name = params[:display_name]
314 friend = User.find_by_display_name(name, :conditions => {:visible => true})
315 if @user.is_friends_with?(friend)
316 Friend.delete_all "user_id = #{@user.id} AND friend_user_id = #{friend.id}"
317 flash[:notice] = t 'user.remove_friend.success', :name => friend.display_name
319 flash[:error] = t 'user.remove_friend.not_a_friend', :name => friend.display_name
323 redirect_to params[:referer]
325 redirect_to :controller => 'user', :action => 'view'
331 # activate a user, allowing them to log in
333 @this_user.update_attributes(:active => true)
334 redirect_to :controller => 'user', :action => 'view', :display_name => params[:display_name]
338 # deactivate a user, preventing them from logging in
340 @this_user.update_attributes(:active => false)
341 redirect_to :controller => 'user', :action => 'view', :display_name => params[:display_name]
345 # hide a user, marking them as logically deleted
347 @this_user.update_attributes(:visible => false)
348 redirect_to :controller => 'user', :action => 'view', :display_name => params[:display_name]
352 # unhide a user, clearing the logically deleted flag
354 @this_user.update_attributes(:visible => true)
355 redirect_to :controller => 'user', :action => 'view', :display_name => params[:display_name]
359 # delete a user, marking them as deleted and removing personal data
362 redirect_to :controller => 'user', :action => 'view', :display_name => params[:display_name]
366 # require that the user is a administrator, or fill out a helpful error message
367 # and return them to the user page.
368 def require_administrator
369 unless @user.administrator?
370 flash[:error] = t('user.filter.not_an_administrator')
371 redirect_to :controller => 'user', :action => 'view', :display_name => params[:display_name]
376 # ensure that there is a "this_user" instance variable
378 @this_user = User.find_by_display_name(params[:display_name])
379 rescue ActiveRecord::RecordNotFound
380 redirect_to :controller => 'user', :action => 'view', :display_name => params[:display_name] unless @this_user