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'
72 Notifier.deliver_email_confirm(@user, @user.tokens.create)
74 # Ignore errors sending email
80 flash[:errors].each do |attr,msg|
81 attr = "new_email" if attr == "email"
82 @user.errors.add(attr,msg)
89 if params[:user][:home_lat] and params[:user][:home_lon]
90 @user.home_lat = params[:user][:home_lat].to_f
91 @user.home_lon = params[:user][:home_lon].to_f
93 flash[:notice] = t 'user.set_home.flash success'
94 redirect_to :controller => 'user', :action => 'account'
100 @user.data_public = true
102 flash[:notice] = t 'user.go_public.flash success'
103 redirect_to :controller => 'user', :action => 'account', :display_name => @user.display_name
107 @title = t 'user.lost_password.title'
109 if params[:user] and params[:user][:email]
110 user = User.find_by_email(params[:user][:email], :conditions => {:visible => true})
113 token = user.tokens.create
114 Notifier.deliver_lost_password(user, token)
115 flash.now[:notice] = t 'user.lost_password.notice email on way'
117 flash.now[:error] = t 'user.lost_password.notice email cannot find'
123 @title = t 'user.reset_password.title'
126 token = UserToken.find_by_token(params[:token])
132 @user.pass_crypt = params[:user][:pass_crypt]
133 @user.pass_crypt_confirmation = params[:user][:pass_crypt_confirmation]
135 @user.email_valid = true
139 flash[:notice] = t 'user.reset_password.flash changed'
140 redirect_to :action => 'login'
144 flash[:error] = t 'user.reset_password.flash token bad'
145 redirect_to :action => 'lost_password'
151 @title = t 'user.new.title'
153 # The user is logged in already, so don't show them the signup page, instead
154 # send them to the home page
155 redirect_to :controller => 'site', :action => 'index' if session[:user]
159 if params[:user] and session[:user].nil?
160 email_or_display_name = params[:user][:email]
161 pass = params[:user][:password]
162 user = User.authenticate(:username => email_or_display_name, :password => pass)
164 session[:user] = user.id
165 session_expires_after 1.month if params[:remember_me]
166 elsif User.authenticate(:username => email_or_display_name, :password => pass, :inactive => true)
167 flash.now[:error] = t 'user.login.account not active'
169 flash.now[:error] = t 'user.login.auth failure'
174 # The user is logged in, if the referer param exists, redirect them to that
175 # unless they've also got a block on them, in which case redirect them to
176 # the block so they can clear it.
177 user = User.find(session[:user])
178 block = user.blocked_on_view
180 redirect_to block, :referrer => params[:referrer]
181 elsif params[:referer]
182 redirect_to params[:referer]
184 redirect_to :controller => 'site', :action => 'index'
189 @title = t 'user.login.title'
194 token = UserToken.find_by_token(session[:token])
198 session[:token] = nil
201 session_expires_automatically
203 redirect_to params[:referer]
205 redirect_to :controller => 'site', :action => 'index'
210 if params[:confirm_action]
211 token = UserToken.find_by_token(params[:confirm_string])
212 if token and !token.user.active?
215 @user.email_valid = true
217 referer = token.referer
219 flash[:notice] = t 'user.confirm.success'
220 session[:user] = @user.id
224 redirect_to :action => 'account', :display_name => @user.display_name
227 flash.now[:error] = t 'user.confirm.failure'
233 if params[:confirm_action]
234 token = UserToken.find_by_token(params[:confirm_string])
235 if token and token.user.new_email?
237 @user.email = @user.new_email
238 @user.new_email = nil
240 @user.email_valid = true
242 flash[:notice] = t 'user.confirm_email.success'
244 flash[:errors] = @user.errors
247 session[:user] = @user.id
248 redirect_to :action => 'account', :display_name => @user.display_name
250 flash.now[:error] = t 'user.confirm_email.failure'
256 @user.image = params[:user][:image]
258 redirect_to :controller => 'user', :action => 'view', :display_name => @user.display_name
264 redirect_to :controller => 'user', :action => 'view', :display_name => @user.display_name
268 doc = OSM::API.new.get_xml_doc
269 @user.traces.each do |trace|
270 doc.root << trace.to_xml_node() if trace.public? or trace.user == @user
272 render :text => doc.to_s, :content_type => "text/xml"
276 @this_user = User.find_by_display_name(params[:display_name])
279 (@this_user.visible? or (@user and @user.administrator?))
280 @title = @this_user.display_name
282 @title = t 'user.no_such_user.title'
283 @not_found_user = params[:display_name]
284 render :action => 'no_such_user', :status => :not_found
289 if params[:display_name]
290 name = params[:display_name]
291 new_friend = User.find_by_display_name(name, :conditions => {:visible => true})
293 friend.user_id = @user.id
294 friend.friend_user_id = new_friend.id
295 unless @user.is_friends_with?(new_friend)
297 flash[:notice] = t 'user.make_friend.success', :name => name
298 Notifier.deliver_friend_notification(friend)
300 friend.add_error(t('user.make_friend.failed', :name => name))
303 flash[:warning] = t 'user.make_friend.already_a_friend', :name => name
306 redirect_to :controller => 'user', :action => 'view'
311 if params[:display_name]
312 name = params[:display_name]
313 friend = User.find_by_display_name(name, :conditions => {:visible => true})
314 if @user.is_friends_with?(friend)
315 Friend.delete_all "user_id = #{@user.id} AND friend_user_id = #{friend.id}"
316 flash[:notice] = t 'user.remove_friend.success', :name => friend.display_name
318 flash[:error] = t 'user.remove_friend.not_a_friend', :name => friend.display_name
321 redirect_to :controller => 'user', :action => 'view'
326 # activate a user, allowing them to log in
328 @this_user.update_attributes(:active => true)
329 redirect_to :controller => 'user', :action => 'view', :display_name => params[:display_name]
333 # deactivate a user, preventing them from logging in
335 @this_user.update_attributes(:active => false)
336 redirect_to :controller => 'user', :action => 'view', :display_name => params[:display_name]
340 # hide a user, marking them as logically deleted
342 @this_user.update_attributes(:visible => false)
343 redirect_to :controller => 'user', :action => 'view', :display_name => params[:display_name]
347 # unhide a user, clearing the logically deleted flag
349 @this_user.update_attributes(:visible => true)
350 redirect_to :controller => 'user', :action => 'view', :display_name => params[:display_name]
354 # delete a user, marking them as deleted and removing personal data
357 redirect_to :controller => 'user', :action => 'view', :display_name => params[:display_name]
361 # require that the user is a administrator, or fill out a helpful error message
362 # and return them to the user page.
363 def require_administrator
364 unless @user.administrator?
365 flash[:error] = t('user.filter.not_an_administrator')
366 redirect_to :controller => 'user', :action => 'view', :display_name => params[:display_name]
371 # ensure that there is a "this_user" instance variable
373 @this_user = User.find_by_display_name(params[:display_name])
374 rescue ActiveRecord::RecordNotFound
375 redirect_to :controller => 'user', :action => 'view', :display_name => params[:display_name] unless @this_user