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'
80 if params[:user][:home_lat] and params[:user][:home_lon]
81 @user.home_lat = params[:user][:home_lat].to_f
82 @user.home_lon = params[:user][:home_lon].to_f
84 flash[:notice] = t 'user.set_home.flash success'
85 redirect_to :controller => 'user', :action => 'account'
91 @user.data_public = true
93 flash[:notice] = t 'user.go_public.flash success'
94 redirect_to :controller => 'user', :action => 'account', :display_name => @user.display_name
98 @title = t 'user.lost_password.title'
100 if params[:user] and params[:user][:email]
101 user = User.find_by_email(params[:user][:email], :conditions => {:visible => true})
104 token = user.tokens.create
105 Notifier.deliver_lost_password(user, token)
106 flash.now[:notice] = t 'user.lost_password.notice email on way'
108 flash.now[:error] = t 'user.lost_password.notice email cannot find'
114 @title = t 'user.reset_password.title'
117 token = UserToken.find_by_token(params[:token])
123 @user.pass_crypt = params[:user][:pass_crypt]
124 @user.pass_crypt_confirmation = params[:user][:pass_crypt_confirmation]
126 @user.email_valid = true
130 flash[:notice] = t 'user.reset_password.flash changed'
131 redirect_to :action => 'login'
135 flash[:error] = t 'user.reset_password.flash token bad'
136 redirect_to :action => 'lost_password'
142 @title = t 'user.new.title'
144 # The user is logged in already, so don't show them the signup page, instead
145 # send them to the home page
146 redirect_to :controller => 'site', :action => 'index' if session[:user]
150 if params[:user] and session[:user].nil?
151 email_or_display_name = params[:user][:email]
152 pass = params[:user][:password]
153 user = User.authenticate(:username => email_or_display_name, :password => pass)
155 session[:user] = user.id
156 elsif User.authenticate(:username => email_or_display_name, :password => pass, :inactive => true)
157 flash.now[:error] = t 'user.login.account not active'
159 flash.now[:error] = t 'user.login.auth failure'
164 # The user is logged in, if the referer param exists, redirect them to that
165 # unless they've also got a block on them, in which case redirect them to
166 # the block so they can clear it.
167 user = User.find(session[:user])
168 block = user.blocked_on_view
170 redirect_to block, :referrer => params[:referrer]
171 elsif params[:referer]
172 redirect_to params[:referer]
174 redirect_to :controller => 'site', :action => 'index'
179 @title = t 'user.login.title'
184 token = UserToken.find_by_token(session[:token])
188 session[:token] = nil
192 redirect_to params[:referer]
194 redirect_to :controller => 'site', :action => 'index'
199 if params[:confirm_action]
200 token = UserToken.find_by_token(params[:confirm_string])
201 if token and !token.user.active?
204 @user.email_valid = true
206 referer = token.referer
208 flash[:notice] = t 'user.confirm.success'
209 session[:user] = @user.id
213 redirect_to :action => 'account', :display_name => @user.display_name
216 flash.now[:error] = t 'user.confirm.failure'
222 if params[:confirm_action]
223 token = UserToken.find_by_token(params[:confirm_string])
224 if token and token.user.new_email?
226 @user.email = @user.new_email
227 @user.new_email = nil
229 @user.email_valid = true
232 flash[:notice] = t 'user.confirm_email.success'
233 session[:user] = @user.id
234 redirect_to :action => 'account', :display_name => @user.display_name
236 flash.now[:error] = t 'user.confirm_email.failure'
242 @user.image = params[:user][:image]
244 redirect_to :controller => 'user', :action => 'view', :display_name => @user.display_name
250 redirect_to :controller => 'user', :action => 'view', :display_name => @user.display_name
254 doc = OSM::API.new.get_xml_doc
255 @user.traces.each do |trace|
256 doc.root << trace.to_xml_node() if trace.public? or trace.user == @user
258 render :text => doc.to_s, :content_type => "text/xml"
262 @this_user = User.find_by_display_name(params[:display_name])
265 (@this_user.visible? or (@user and @user.administrator?))
266 @title = @this_user.display_name
268 @title = t 'user.no_such_user.title'
269 @not_found_user = params[:display_name]
270 render :action => 'no_such_user', :status => :not_found
275 if params[:display_name]
276 name = params[:display_name]
277 new_friend = User.find_by_display_name(name, :conditions => {:visible => true})
279 friend.user_id = @user.id
280 friend.friend_user_id = new_friend.id
281 unless @user.is_friends_with?(new_friend)
283 flash[:notice] = t 'user.make_friend.success', :name => name
284 Notifier.deliver_friend_notification(friend)
286 friend.add_error(t('user.make_friend.failed', :name => name))
289 flash[:warning] = t 'user.make_friend.already_a_friend', :name => name
292 redirect_to :controller => 'user', :action => 'view'
297 if params[:display_name]
298 name = params[:display_name]
299 friend = User.find_by_display_name(name, :conditions => {:visible => true})
300 if @user.is_friends_with?(friend)
301 Friend.delete_all "user_id = #{@user.id} AND friend_user_id = #{friend.id}"
302 flash[:notice] = t 'user.remove_friend.success', :name => friend.display_name
304 flash[:error] = t 'user.remove_friend.not_a_friend', :name => friend.display_name
307 redirect_to :controller => 'user', :action => 'view'
312 # activate a user, allowing them to log in
314 @this_user.update_attributes(:active => true)
315 redirect_to :controller => 'user', :action => 'view', :display_name => params[:display_name]
319 # deactivate a user, preventing them from logging in
321 @this_user.update_attributes(:active => false)
322 redirect_to :controller => 'user', :action => 'view', :display_name => params[:display_name]
326 # hide a user, marking them as logically deleted
328 @this_user.update_attributes(:visible => false)
329 redirect_to :controller => 'user', :action => 'view', :display_name => params[:display_name]
333 # unhide a user, clearing the logically deleted flag
335 @this_user.update_attributes(:visible => true)
336 redirect_to :controller => 'user', :action => 'view', :display_name => params[:display_name]
340 # delete a user, marking them as deleted and removing personal data
343 redirect_to :controller => 'user', :action => 'view', :display_name => params[:display_name]
347 # require that the user is a administrator, or fill out a helpful error message
348 # and return them to the user page.
349 def require_administrator
350 unless @user.administrator?
351 flash[:error] = t('user.filter.not_an_administrator')
352 redirect_to :controller => 'user', :action => 'view', :display_name => params[:display_name]
357 # ensure that there is a "this_user" instance variable
359 @this_user = User.find_by_display_name(params[:display_name])
360 rescue ActiveRecord::RecordNotFound
361 redirect_to :controller => 'user', :action => 'view', :display_name => params[:display_name] unless @this_user