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 @user.display_name = params[:user][:display_name]
51 @user.new_email = params[:user][:new_email]
53 if params[:user][:pass_crypt].length > 0 or params[:user][:pass_crypt_confirmation].length > 0
54 @user.pass_crypt = params[:user][:pass_crypt]
55 @user.pass_crypt_confirmation = params[:user][:pass_crypt_confirmation]
58 @user.description = params[:user][:description]
59 @user.languages = params[:user][:languages].split(",")
60 @user.home_lat = params[:user][:home_lat]
61 @user.home_lon = params[:user][:home_lon]
66 if @user.new_email.nil? or @user.new_email.empty?
67 flash.now[:notice] = t 'user.account.flash update success'
69 flash.now[:notice] = t 'user.account.flash update success confirm needed'
70 Notifier.deliver_email_confirm(@user, @user.tokens.create)
75 flash[:errors].each do |attr,msg|
76 attr = "new_email" if attr == "email"
77 @user.errors.add(attr,msg)
84 if params[:user][:home_lat] and params[:user][:home_lon]
85 @user.home_lat = params[:user][:home_lat].to_f
86 @user.home_lon = params[:user][:home_lon].to_f
88 flash[:notice] = t 'user.set_home.flash success'
89 redirect_to :controller => 'user', :action => 'account'
95 @user.data_public = true
97 flash[:notice] = t 'user.go_public.flash success'
98 redirect_to :controller => 'user', :action => 'account', :display_name => @user.display_name
102 @title = t 'user.lost_password.title'
104 if params[:user] and params[:user][:email]
105 user = User.find_by_email(params[:user][:email], :conditions => {:visible => true})
108 token = user.tokens.create
109 Notifier.deliver_lost_password(user, token)
110 flash.now[:notice] = t 'user.lost_password.notice email on way'
112 flash.now[:error] = t 'user.lost_password.notice email cannot find'
118 @title = t 'user.reset_password.title'
121 token = UserToken.find_by_token(params[:token])
127 @user.pass_crypt = params[:user][:pass_crypt]
128 @user.pass_crypt_confirmation = params[:user][:pass_crypt_confirmation]
130 @user.email_valid = true
134 flash[:notice] = t 'user.reset_password.flash changed'
135 redirect_to :action => 'login'
139 flash[:error] = t 'user.reset_password.flash token bad'
140 redirect_to :action => 'lost_password'
146 @title = t 'user.new.title'
148 # The user is logged in already, so don't show them the signup page, instead
149 # send them to the home page
150 redirect_to :controller => 'site', :action => 'index' if session[:user]
154 if params[:user] and session[:user].nil?
155 email_or_display_name = params[:user][:email]
156 pass = params[:user][:password]
157 user = User.authenticate(:username => email_or_display_name, :password => pass)
159 session[:user] = user.id
160 elsif User.authenticate(:username => email_or_display_name, :password => pass, :inactive => true)
161 flash.now[:error] = t 'user.login.account not active'
163 flash.now[:error] = t 'user.login.auth failure'
168 # The user is logged in, if the referer param exists, redirect them to that
169 # unless they've also got a block on them, in which case redirect them to
170 # the block so they can clear it.
171 user = User.find(session[:user])
172 block = user.blocked_on_view
174 redirect_to block, :referrer => params[:referrer]
175 elsif params[:referer]
176 redirect_to params[:referer]
178 redirect_to :controller => 'site', :action => 'index'
183 @title = t 'user.login.title'
188 token = UserToken.find_by_token(session[:token])
192 session[:token] = nil
196 redirect_to params[:referer]
198 redirect_to :controller => 'site', :action => 'index'
203 if params[:confirm_action]
204 token = UserToken.find_by_token(params[:confirm_string])
205 if token and !token.user.active?
208 @user.email_valid = true
210 referer = token.referer
212 flash[:notice] = t 'user.confirm.success'
213 session[:user] = @user.id
217 redirect_to :action => 'account', :display_name => @user.display_name
220 flash.now[:error] = t 'user.confirm.failure'
226 if params[:confirm_action]
227 token = UserToken.find_by_token(params[:confirm_string])
228 if token and token.user.new_email?
230 @user.email = @user.new_email
231 @user.new_email = nil
233 @user.email_valid = true
235 flash[:notice] = t 'user.confirm_email.success'
237 flash[:errors] = @user.errors
240 session[:user] = @user.id
241 redirect_to :action => 'account', :display_name => @user.display_name
243 flash.now[:error] = t 'user.confirm_email.failure'
249 @user.image = params[:user][:image]
251 redirect_to :controller => 'user', :action => 'view', :display_name => @user.display_name
257 redirect_to :controller => 'user', :action => 'view', :display_name => @user.display_name
261 doc = OSM::API.new.get_xml_doc
262 @user.traces.each do |trace|
263 doc.root << trace.to_xml_node() if trace.public? or trace.user == @user
265 render :text => doc.to_s, :content_type => "text/xml"
269 @this_user = User.find_by_display_name(params[:display_name])
272 (@this_user.visible? or (@user and @user.administrator?))
273 @title = @this_user.display_name
275 @title = t 'user.no_such_user.title'
276 @not_found_user = params[:display_name]
277 render :action => 'no_such_user', :status => :not_found
282 if params[:display_name]
283 name = params[:display_name]
284 new_friend = User.find_by_display_name(name, :conditions => {:visible => true})
286 friend.user_id = @user.id
287 friend.friend_user_id = new_friend.id
288 unless @user.is_friends_with?(new_friend)
290 flash[:notice] = t 'user.make_friend.success', :name => name
291 Notifier.deliver_friend_notification(friend)
293 friend.add_error(t('user.make_friend.failed', :name => name))
296 flash[:warning] = t 'user.make_friend.already_a_friend', :name => name
299 redirect_to :controller => 'user', :action => 'view'
304 if params[:display_name]
305 name = params[:display_name]
306 friend = User.find_by_display_name(name, :conditions => {:visible => true})
307 if @user.is_friends_with?(friend)
308 Friend.delete_all "user_id = #{@user.id} AND friend_user_id = #{friend.id}"
309 flash[:notice] = t 'user.remove_friend.success', :name => friend.display_name
311 flash[:error] = t 'user.remove_friend.not_a_friend', :name => friend.display_name
314 redirect_to :controller => 'user', :action => 'view'
319 # activate a user, allowing them to log in
321 @this_user.update_attributes(:active => true)
322 redirect_to :controller => 'user', :action => 'view', :display_name => params[:display_name]
326 # deactivate a user, preventing them from logging in
328 @this_user.update_attributes(:active => false)
329 redirect_to :controller => 'user', :action => 'view', :display_name => params[:display_name]
333 # hide a user, marking them as logically deleted
335 @this_user.update_attributes(:visible => false)
336 redirect_to :controller => 'user', :action => 'view', :display_name => params[:display_name]
340 # unhide a user, clearing the logically deleted flag
342 @this_user.update_attributes(:visible => true)
343 redirect_to :controller => 'user', :action => 'view', :display_name => params[:display_name]
347 # delete a user, marking them as deleted and removing personal data
350 redirect_to :controller => 'user', :action => 'view', :display_name => params[:display_name]
354 # require that the user is a administrator, or fill out a helpful error message
355 # and return them to the user page.
356 def require_administrator
357 unless @user.administrator?
358 flash[:error] = t('user.filter.not_an_administrator')
359 redirect_to :controller => 'user', :action => 'view', :display_name => params[:display_name]
364 # ensure that there is a "this_user" instance variable
366 @this_user = User.find_by_display_name(params[:display_name])
367 rescue ActiveRecord::RecordNotFound
368 redirect_to :controller => 'user', :action => 'view', :display_name => params[:display_name] unless @this_user