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 => [:set_home, :account, :go_public, :make_friend, :remove_friend, :upload_image, :delete_image]
8 before_filter :check_database_readable, :except => [:api_details, :api_gpx_files]
9 before_filter :check_database_writable, :only => [:login, :new, :set_home, :account, :go_public, :make_friend, :remove_friend, :upload_image, :delete_image]
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'
24 if Acl.find_by_address(request.remote_ip, :conditions => {:k => "no_account_creation"})
25 render :action => 'new'
27 @user = User.new(params[:user])
30 @user.data_public = true
31 @user.description = "" if @user.description.nil?
32 @user.creation_ip = request.remote_ip
33 @user.languages = request.user_preferred_languages
36 flash[:notice] = t 'user.new.flash create success message'
37 Notifier.deliver_signup_confirm(@user, @user.tokens.create(:referer => params[:referer]))
38 redirect_to :action => 'login'
40 render :action => 'new'
46 @title = t 'user.account.title'
47 @tokens = @user.oauth_tokens.find :all, :conditions => 'oauth_tokens.invalidated_at is null and oauth_tokens.authorized_at is not null'
49 if params[:user] and params[:user][:display_name] and params[:user][:description]
50 if params[:user][:email] != @user.email
51 @user.new_email = params[:user][:email]
54 @user.display_name = params[:user][:display_name]
56 if params[:user][:pass_crypt].length > 0 or params[:user][:pass_crypt_confirmation].length > 0
57 @user.pass_crypt = params[:user][:pass_crypt]
58 @user.pass_crypt_confirmation = params[:user][:pass_crypt_confirmation]
61 @user.description = params[:user][:description]
62 @user.languages = params[:user][:languages].split(",")
63 @user.home_lat = params[:user][:home_lat]
64 @user.home_lon = params[:user][:home_lon]
69 if params[:user][:email] == @user.new_email
70 flash.now[:notice] = t 'user.account.flash update success confirm needed'
71 Notifier.deliver_email_confirm(@user, @user.tokens.create)
73 flash.now[:notice] = t 'user.account.flash update success'
78 flash[:errors].each do |attr,msg|
79 @user.errors.add(attr,msg)
83 @user.email = @user.new_email if @user.new_email
88 if params[:user][:home_lat] and params[:user][:home_lon]
89 @user.home_lat = params[:user][:home_lat].to_f
90 @user.home_lon = params[:user][:home_lon].to_f
92 flash[:notice] = t 'user.set_home.flash success'
93 redirect_to :controller => 'user', :action => 'account'
99 @user.data_public = true
101 flash[:notice] = t 'user.go_public.flash success'
102 redirect_to :controller => 'user', :action => 'account', :display_name => @user.display_name
106 @title = t 'user.lost_password.title'
108 if params[:user] and params[:user][:email]
109 user = User.find_by_email(params[:user][:email], :conditions => {:visible => true})
112 token = user.tokens.create
113 Notifier.deliver_lost_password(user, token)
114 flash.now[:notice] = t 'user.lost_password.notice email on way'
116 flash.now[:error] = t 'user.lost_password.notice email cannot find'
122 @title = t 'user.reset_password.title'
125 token = UserToken.find_by_token(params[:token])
131 @user.pass_crypt = params[:user][:pass_crypt]
132 @user.pass_crypt_confirmation = params[:user][:pass_crypt_confirmation]
134 @user.email_valid = true
138 flash[:notice] = t 'user.reset_password.flash changed'
139 redirect_to :action => 'login'
143 flash[:error] = t 'user.reset_password.flash token bad'
144 redirect_to :action => 'lost_password'
150 @title = t 'user.new.title'
152 # The user is logged in already, so don't show them the signup page, instead
153 # send them to the home page
154 redirect_to :controller => 'site', :action => 'index' if session[:user]
158 if params[:user] and session[:user].nil?
159 email_or_display_name = params[:user][:email]
160 pass = params[:user][:password]
161 user = User.authenticate(:username => email_or_display_name, :password => pass)
163 session[:user] = user.id
164 elsif User.authenticate(:username => email_or_display_name, :password => pass, :inactive => true)
165 flash.now[:error] = t 'user.login.account not active'
167 flash.now[:error] = t 'user.login.auth failure'
172 # The user is logged in, if the referer param exists, redirect them to that
173 # unless they've also got a block on them, in which case redirect them to
174 # the block so they can clear it.
175 user = User.find(session[:user])
176 block = user.blocked_on_view
178 redirect_to block, :referrer => params[:referrer]
179 elsif params[:referer]
180 redirect_to params[:referer]
182 redirect_to :controller => 'site', :action => 'index'
187 @title = t 'user.login.title'
192 token = UserToken.find_by_token(session[:token])
196 session[:token] = nil
200 redirect_to params[:referer]
202 redirect_to :controller => 'site', :action => 'index'
207 if params[:confirm_action]
208 token = UserToken.find_by_token(params[:confirm_string])
209 if token and !token.user.active?
212 @user.email_valid = true
214 referer = token.referer
216 flash[:notice] = t 'user.confirm.success'
217 session[:user] = @user.id
221 redirect_to :action => 'account', :display_name => @user.display_name
224 flash.now[:error] = t 'user.confirm.failure'
230 if params[:confirm_action]
231 token = UserToken.find_by_token(params[:confirm_string])
232 if token and token.user.new_email?
234 @user.email = @user.new_email
235 @user.new_email = nil
237 @user.email_valid = true
239 flash[:notice] = t 'user.confirm_email.success'
241 flash[:errors] = @user.errors
244 session[:user] = @user.id
245 redirect_to :action => 'account', :display_name => @user.display_name
247 flash.now[:error] = t 'user.confirm_email.failure'
253 @user.image = params[:user][:image]
255 redirect_to :controller => 'user', :action => 'view', :display_name => @user.display_name
261 redirect_to :controller => 'user', :action => 'view', :display_name => @user.display_name
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
303 redirect_to :controller => 'user', :action => 'view'
308 if params[:display_name]
309 name = params[:display_name]
310 friend = User.find_by_display_name(name, :conditions => {:visible => true})
311 if @user.is_friends_with?(friend)
312 Friend.delete_all "user_id = #{@user.id} AND friend_user_id = #{friend.id}"
313 flash[:notice] = t 'user.remove_friend.success', :name => friend.display_name
315 flash[:error] = t 'user.remove_friend.not_a_friend', :name => friend.display_name
318 redirect_to :controller => 'user', :action => 'view'
323 # activate a user, allowing them to log in
325 @this_user.update_attributes(:active => true)
326 redirect_to :controller => 'user', :action => 'view', :display_name => params[:display_name]
330 # deactivate a user, preventing them from logging in
332 @this_user.update_attributes(:active => false)
333 redirect_to :controller => 'user', :action => 'view', :display_name => params[:display_name]
337 # hide a user, marking them as logically deleted
339 @this_user.update_attributes(:visible => false)
340 redirect_to :controller => 'user', :action => 'view', :display_name => params[:display_name]
344 # unhide a user, clearing the logically deleted flag
346 @this_user.update_attributes(:visible => true)
347 redirect_to :controller => 'user', :action => 'view', :display_name => params[:display_name]
351 # delete a user, marking them as deleted and removing personal data
354 redirect_to :controller => 'user', :action => 'view', :display_name => params[:display_name]
358 # require that the user is a administrator, or fill out a helpful error message
359 # and return them to the user page.
360 def require_administrator
361 unless @user.administrator?
362 flash[:error] = t('user.filter.not_an_administrator')
363 redirect_to :controller => 'user', :action => 'view', :display_name => params[:display_name]
368 # ensure that there is a "this_user" instance variable
370 @this_user = User.find_by_display_name(params[:display_name])
371 rescue ActiveRecord::RecordNotFound
372 redirect_to :controller => 'user', :action => 'view', :display_name => params[:display_name] unless @this_user