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
20 @title = t 'user.new.title'
22 if Acl.find_by_address(request.remote_ip, :conditions => {:k => "no_account_creation"})
23 render :action => 'new'
25 @user = User.new(params[:user])
28 @user.data_public = true
29 @user.description = "" if @user.description.nil?
30 @user.creation_ip = request.remote_ip
31 @user.languages = request.user_preferred_languages
34 flash[:notice] = t 'user.new.flash create success message'
35 Notifier.deliver_signup_confirm(@user, @user.tokens.create(:referer => params[:referer]))
36 redirect_to :action => 'login'
38 render :action => 'new'
44 @title = t 'user.account.title'
45 @tokens = @user.oauth_tokens.find :all, :conditions => 'oauth_tokens.invalidated_at is null and oauth_tokens.authorized_at is not null'
47 if params[:user] and params[:user][:display_name] and params[:user][:description]
48 if params[:user][:email] != @user.email
49 @user.new_email = params[:user][:email]
52 @user.display_name = params[:user][:display_name]
54 if params[:user][:pass_crypt].length > 0 or params[:user][:pass_crypt_confirmation].length > 0
55 @user.pass_crypt = params[:user][:pass_crypt]
56 @user.pass_crypt_confirmation = params[:user][:pass_crypt_confirmation]
59 @user.description = params[:user][:description]
60 @user.languages = params[:user][:languages].split(",")
61 @user.home_lat = params[:user][:home_lat]
62 @user.home_lon = params[:user][:home_lon]
67 if params[:user][:email] == @user.new_email
68 flash.now[:notice] = t 'user.account.flash update success confirm needed'
69 Notifier.deliver_email_confirm(@user, @user.tokens.create)
71 flash.now[:notice] = t 'user.account.flash update success'
78 if params[:user][:home_lat] and params[:user][:home_lon]
79 @user.home_lat = params[:user][:home_lat].to_f
80 @user.home_lon = params[:user][:home_lon].to_f
82 flash[:notice] = t 'user.set_home.flash success'
83 redirect_to :controller => 'user', :action => 'account'
89 @user.data_public = true
91 flash[:notice] = t 'user.go_public.flash success'
92 redirect_to :controller => 'user', :action => 'account', :display_name => @user.display_name
96 @title = t 'user.lost_password.title'
98 if params[:user] and params[:user][:email]
99 user = User.find_by_email(params[:user][:email], :conditions => {:visible => true})
102 token = user.tokens.create
103 Notifier.deliver_lost_password(user, token)
104 flash.now[:notice] = t 'user.lost_password.notice email on way'
106 flash.now[:error] = t 'user.lost_password.notice email cannot find'
112 @title = t 'user.reset_password.title'
115 token = UserToken.find_by_token(params[:token])
121 @user.pass_crypt = params[:user][:pass_crypt]
122 @user.pass_crypt_confirmation = params[:user][:pass_crypt_confirmation]
124 @user.email_valid = true
128 flash[:notice] = t 'user.reset_password.flash changed'
129 redirect_to :action => 'login'
133 flash[:error] = t 'user.reset_password.flash token bad'
134 redirect_to :action => 'lost_password'
140 @title = t 'user.new.title'
142 # The user is logged in already, so don't show them the signup page, instead
143 # send them to the home page
144 redirect_to :controller => 'site', :action => 'index' if session[:user]
148 if params[:user] and session[:user].nil?
149 email_or_display_name = params[:user][:email]
150 pass = params[:user][:password]
151 user = User.authenticate(:username => email_or_display_name, :password => pass)
153 session[:user] = user.id
154 elsif User.authenticate(:username => email_or_display_name, :password => pass, :inactive => true)
155 flash.now[:error] = t 'user.login.account not active'
157 flash.now[:error] = t 'user.login.auth failure'
162 # The user is logged in, if the referer param exists, redirect them to that
163 # unless they've also got a block on them, in which case redirect them to
164 # the block so they can clear it.
165 user = User.find(session[:user])
166 block = user.blocked_on_view
168 redirect_to block, :referrer => params[:referrer]
169 elsif params[:referer]
170 redirect_to params[:referer]
172 redirect_to :controller => 'site', :action => 'index'
177 @title = t 'user.login.title'
182 token = UserToken.find_by_token(session[:token])
186 session[:token] = nil
190 redirect_to params[:referer]
192 redirect_to :controller => 'site', :action => 'index'
197 if params[:confirm_action]
198 token = UserToken.find_by_token(params[:confirm_string])
199 if token and !token.user.active?
202 @user.email_valid = true
204 referer = token.referer
206 flash[:notice] = t 'user.confirm.success'
207 session[:user] = @user.id
211 redirect_to :action => 'account', :display_name => @user.display_name
214 flash.now[:error] = t 'user.confirm.failure'
220 if params[:confirm_action]
221 token = UserToken.find_by_token(params[:confirm_string])
222 if token and token.user.new_email?
224 @user.email = @user.new_email
225 @user.new_email = nil
227 @user.email_valid = true
230 flash[:notice] = t 'user.confirm_email.success'
231 session[:user] = @user.id
232 redirect_to :action => 'account', :display_name => @user.display_name
234 flash.now[:error] = t 'user.confirm_email.failure'
240 @user.image = params[:user][:image]
242 redirect_to :controller => 'user', :action => 'view', :display_name => @user.display_name
248 redirect_to :controller => 'user', :action => 'view', :display_name => @user.display_name
252 doc = OSM::API.new.get_xml_doc
253 @user.traces.each do |trace|
254 doc.root << trace.to_xml_node() if trace.public? or trace.user == @user
256 render :text => doc.to_s, :content_type => "text/xml"
260 @this_user = User.find_by_display_name(params[:display_name])
263 (@this_user.visible? or (@user and @user.administrator?))
264 @title = @this_user.display_name
266 @title = t 'user.no_such_user.title'
267 @not_found_user = params[:display_name]
268 render :action => 'no_such_user', :status => :not_found
273 if params[:display_name]
274 name = params[:display_name]
275 new_friend = User.find_by_display_name(name, :conditions => {:visible => true})
277 friend.user_id = @user.id
278 friend.friend_user_id = new_friend.id
279 unless @user.is_friends_with?(new_friend)
281 flash[:notice] = t 'user.make_friend.success', :name => name
282 Notifier.deliver_friend_notification(friend)
284 friend.add_error(t('user.make_friend.failed', :name => name))
287 flash[:warning] = t 'user.make_friend.already_a_friend', :name => name
290 redirect_to :controller => 'user', :action => 'view'
295 if params[:display_name]
296 name = params[:display_name]
297 friend = User.find_by_display_name(name, :conditions => {:visible => true})
298 if @user.is_friends_with?(friend)
299 Friend.delete_all "user_id = #{@user.id} AND friend_user_id = #{friend.id}"
300 flash[:notice] = t 'user.remove_friend.success', :name => friend.display_name
302 flash[:error] = t 'user.remove_friend.not_a_friend', :name => friend.display_name
305 redirect_to :controller => 'user', :action => 'view'
310 # activate a user, allowing them to log in
312 @this_user.update_attributes(:active => true)
313 redirect_to :controller => 'user', :action => 'view', :display_name => params[:display_name]
317 # deactivate a user, preventing them from logging in
319 @this_user.update_attributes(:active => false)
320 redirect_to :controller => 'user', :action => 'view', :display_name => params[:display_name]
324 # hide a user, marking them as logically deleted
326 @this_user.update_attributes(:visible => false)
327 redirect_to :controller => 'user', :action => 'view', :display_name => params[:display_name]
331 # unhide a user, clearing the logically deleted flag
333 @this_user.update_attributes(:visible => true)
334 redirect_to :controller => 'user', :action => 'view', :display_name => params[:display_name]
338 # delete a user, marking them as deleted and removing personal data
341 redirect_to :controller => 'user', :action => 'view', :display_name => params[:display_name]
345 # require that the user is a administrator, or fill out a helpful error message
346 # and return them to the user page.
347 def require_administrator
348 unless @user.administrator?
349 flash[:error] = t('user.filter.not_an_administrator')
350 redirect_to :controller => 'user', :action => 'view', :display_name => params[:display_name]
355 # ensure that there is a "this_user" instance variable
357 @this_user = User.find_by_display_name(params[:display_name])
358 rescue ActiveRecord::RecordNotFound
359 redirect_to :controller => 'user', :action => 'view', :display_name => params[:display_name] unless @this_user