1 class UserController < ApplicationController
4 before_filter :authorize, :only => [:api_details, :api_gpx_files]
5 before_filter :authorize_web, :except => [:api_details, :api_gpx_files]
6 before_filter :require_user, :only => [:set_home, :account, :go_public, :make_friend, :remove_friend, :upload_image, :delete_image]
7 before_filter :check_database_availability, :except => [:api_details, :api_gpx_files]
8 before_filter :check_read_availability, :only => [:api_details, :api_gpx_files]
10 filter_parameter_logging :password, :pass_crypt, :pass_crypt_confirmation
13 @title = 'create account'
15 if Acl.find_by_address(request.remote_ip, :conditions => {:k => "no_account_creation"})
16 render :action => 'new'
18 @user = User.new(params[:user])
21 @user.data_public = true
22 @user.description = "" if @user.description.nil?
23 @user.creation_ip = request.remote_ip
26 flash[:notice] = "User was successfully created. Check your email for a confirmation note, and you\'ll be mapping in no time :-)<br>Please note that you won't be able to login until you've received and confirmed your email address."
27 Notifier.deliver_signup_confirm(@user, @user.tokens.create)
28 redirect_to :action => 'login'
30 render :action => 'new'
36 @title = 'edit account'
37 if params[:user] and params[:user][:display_name] and params[:user][:description]
38 if params[:user][:email] != @user.email
39 @user.new_email = params[:user][:email]
42 @user.display_name = params[:user][:display_name]
44 if params[:user][:pass_crypt].length > 0 or params[:user][:pass_crypt_confirmation].length > 0
45 @user.pass_crypt = params[:user][:pass_crypt]
46 @user.pass_crypt_confirmation = params[:user][:pass_crypt_confirmation]
49 @user.description = params[:user][:description]
50 @user.home_lat = params[:user][:home_lat]
51 @user.home_lon = params[:user][:home_lon]
54 if params[:user][:email] == @user.new_email
55 @notice = "User information updated successfully. Check your email for a note to confirm your new email address."
56 Notifier.deliver_email_confirm(@user, @user.tokens.create)
58 @notice = "User information updated successfully."
65 if params[:user][:home_lat] and params[:user][:home_lon]
66 @user.home_lat = params[:user][:home_lat].to_f
67 @user.home_lon = params[:user][:home_lon].to_f
69 flash[:notice] = "Home location saved successfully."
70 redirect_to :controller => 'user', :action => 'account'
76 @user.data_public = true
78 flash[:notice] = 'All your edits are now public.'
79 redirect_to :controller => 'user', :action => 'account', :display_name => @user.display_name
83 @title = 'lost password'
84 if params[:user] and params[:user][:email]
85 user = User.find_by_email(params[:user][:email], :conditions => {:visible => true})
88 token = user.tokens.create
89 Notifier.deliver_lost_password(user, token)
90 @notice = "Sorry you lost it :-( but an email is on its way so you can reset it soon."
92 @notice = "Couldn't find that email address, sorry."
98 @title = 'reset password'
100 token = UserToken.find_by_token(params[:token])
102 pass = OSM::make_token(8)
104 user.pass_crypt = pass
105 user.pass_crypt_confirmation = pass
107 user.email_valid = true
110 Notifier.deliver_reset_password(user, pass)
111 flash[:notice] = "Your password has been changed and is on its way to your mailbox :-)"
113 flash[:notice] = "Didn't find that token, check the URL maybe?"
117 redirect_to :action => 'login'
121 @title = 'create account'
122 # The user is logged in already, so don't show them the signup page, instead
123 # send them to the home page
124 redirect_to :controller => 'site', :action => 'index' if session[:user]
129 # The user is logged in already, if the referer param exists, redirect them to that
131 redirect_to params[:referer]
133 redirect_to :controller => 'site', :action => 'index'
139 email_or_display_name = params[:user][:email]
140 pass = params[:user][:password]
141 user = User.authenticate(:username => email_or_display_name, :password => pass)
143 session[:user] = user.id
145 redirect_to params[:referer]
147 redirect_to :controller => 'site', :action => 'index'
150 elsif User.authenticate(:username => email_or_display_name, :password => pass, :inactive => true)
151 @notice = "Sorry, your account is not active yet.<br>Please click on the link in the account confirmation email to activate your account."
153 @notice = "Sorry, couldn't log in with those details."
160 token = UserToken.find_by_token(session[:token])
164 session[:token] = nil
168 redirect_to params[:referer]
170 redirect_to :controller => 'site', :action => 'index'
175 if params[:confirm_action]
176 token = UserToken.find_by_token(params[:confirm_string])
177 if token and !token.user.active?
180 @user.email_valid = true
183 flash[:notice] = 'Confirmed your account, thanks for signing up!'
184 session[:user] = @user.id
185 redirect_to :action => 'account', :display_name => @user.display_name
187 @notice = 'Something went wrong confirming that user.'
193 if params[:confirm_action]
194 token = UserToken.find_by_token(params[:confirm_string])
195 if token and token.user.new_email?
197 @user.email = @user.new_email
198 @user.new_email = nil
200 @user.email_valid = true
203 flash[:notice] = 'Confirmed your email address, thanks for signing up!'
204 session[:user] = @user.id
205 redirect_to :action => 'account', :display_name => @user.display_name
207 @notice = 'Something went wrong confirming that email address.'
213 @user.image = params[:user][:image]
215 redirect_to :controller => 'user', :action => 'view', :display_name => @user.display_name
221 redirect_to :controller => 'user', :action => 'view', :display_name => @user.display_name
225 render :text => @user.to_xml.to_s, :content_type => "text/xml"
229 doc = OSM::API.new.get_xml_doc
230 @user.traces.each do |trace|
231 doc.root << trace.to_xml_node() if trace.public? or trace.user == @user
233 render :text => doc.to_s, :content_type => "text/xml"
237 @this_user = User.find_by_display_name(params[:display_name], :conditions => {:visible => true})
240 @title = @this_user.display_name
242 @not_found_user = params[:display_name]
243 render :action => 'no_such_user', :status => :not_found
248 if params[:display_name]
249 name = params[:display_name]
250 new_friend = User.find_by_display_name(name, :conditions => {:visible => true})
252 friend.user_id = @user.id
253 friend.friend_user_id = new_friend.id
254 unless @user.is_friends_with?(new_friend)
256 flash[:notice] = "#{name} is now your friend."
257 Notifier.deliver_friend_notification(friend)
259 friend.add_error("Sorry, failed to add #{name} as a friend.")
262 flash[:notice] = "You are already friends with #{name}."
265 redirect_to :controller => 'user', :action => 'view'
270 if params[:display_name]
271 name = params[:display_name]
272 friend = User.find_by_display_name(name, :conditions => {:visible => true})
273 if @user.is_friends_with?(friend)
274 Friend.delete_all "user_id = #{@user.id} AND friend_user_id = #{friend.id}"
275 flash[:notice] = "#{friend.display_name} was removed from your friends."
277 flash[:notice] = "#{friend.display_name} is not one of your friends."
280 redirect_to :controller => 'user', :action => 'view'