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'
14 @user = User.new(params[:user])
17 @user.data_public = true
18 @user.description = "" if @user.description.nil?
19 @user.creation_ip = request.remote_ip
22 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."
23 Notifier.deliver_signup_confirm(@user, @user.tokens.create)
24 redirect_to :action => 'login'
26 render :action => 'new'
31 @title = 'edit account'
32 if params[:user] and params[:user][:display_name] and params[:user][:description]
33 if params[:user][:email] != @user.email
34 @user.new_email = params[:user][:email]
37 @user.display_name = params[:user][:display_name]
39 if params[:user][:pass_crypt].length > 0 or params[:user][:pass_crypt_confirmation].length > 0
40 @user.pass_crypt = params[:user][:pass_crypt]
41 @user.pass_crypt_confirmation = params[:user][:pass_crypt_confirmation]
44 @user.description = params[:user][:description]
45 @user.home_lat = params[:user][:home_lat]
46 @user.home_lon = params[:user][:home_lon]
49 if params[:user][:email] == @user.new_email
50 @notice = "User information updated successfully. Check your email for a note to confirm your new email address."
51 Notifier.deliver_email_confirm(@user, @user.tokens.create)
53 @notice = "User information updated successfully."
60 if params[:user][:home_lat] and params[:user][:home_lon]
61 @user.home_lat = params[:user][:home_lat].to_f
62 @user.home_lon = params[:user][:home_lon].to_f
64 flash[:notice] = "Home location saved successfully."
65 redirect_to :controller => 'user', :action => 'account'
71 @user.data_public = true
73 flash[:notice] = 'All your edits are now public.'
74 redirect_to :controller => 'user', :action => 'account', :display_name => @user.display_name
78 @title = 'lost password'
79 if params[:user] and params[:user][:email]
80 user = User.find_by_email(params[:user][:email], :conditions => {:visible => true})
83 token = user.tokens.create
84 Notifier.deliver_lost_password(user, token)
85 @notice = "Sorry you lost it :-( but an email is on its way so you can reset it soon."
87 @notice = "Couldn't find that email address, sorry."
93 @title = 'reset password'
95 token = UserToken.find_by_token(params[:token])
97 pass = OSM::make_token(8)
99 user.pass_crypt = pass
100 user.pass_crypt_confirmation = pass
102 user.email_valid = true
105 Notifier.deliver_reset_password(user, pass)
106 flash[:notice] = "Your password has been changed and is on its way to your mailbox :-)"
108 flash[:notice] = "Didn't find that token, check the URL maybe?"
112 redirect_to :action => 'login'
116 @title = 'create account'
121 # The user is logged in already, if the referer param exists, redirect them to that
123 redirect_to params[:referer]
125 redirect_to :controller => 'site', :action => 'index'
130 email_or_display_name = params[:user][:email]
131 pass = params[:user][:password]
132 user = User.authenticate(:username => email_or_display_name, :password => pass)
134 session[:user] = user.id
136 redirect_to params[:referer]
138 redirect_to :controller => 'site', :action => 'index'
141 elsif User.authenticate(:username => email_or_display_name, :password => pass, :inactive => true)
142 @notice = "Sorry, your account is not active yet.<br>Please click on the link in the account confirmation email to activate your account."
144 @notice = "Sorry, couldn't log in with those details."
151 token = UserToken.find_by_token(session[:token])
155 session[:token] = nil
159 redirect_to params[:referer]
161 redirect_to :controller => 'site', :action => 'index'
166 if params[:confirm_action]
167 token = UserToken.find_by_token(params[:confirm_string])
168 if token and !token.user.active?
171 @user.email_valid = true
174 flash[:notice] = 'Confirmed your account, thanks for signing up!'
175 session[:user] = @user.id
176 redirect_to :action => 'account', :display_name => @user.display_name
178 @notice = 'Something went wrong confirming that user.'
184 if params[:confirm_action]
185 token = UserToken.find_by_token(params[:confirm_string])
186 if token and token.user.new_email?
188 @user.email = @user.new_email
189 @user.new_email = nil
191 @user.email_valid = true
194 flash[:notice] = 'Confirmed your email address, thanks for signing up!'
195 session[:user] = @user.id
196 redirect_to :action => 'account', :display_name => @user.display_name
198 @notice = 'Something went wrong confirming that email address.'
204 @user.image = params[:user][:image]
206 redirect_to :controller => 'user', :action => 'view', :display_name => @user.display_name
212 redirect_to :controller => 'user', :action => 'view', :display_name => @user.display_name
216 render :text => @user.to_xml.to_s, :content_type => "text/xml"
220 doc = OSM::API.new.get_xml_doc
221 @user.traces.each do |trace|
222 doc.root << trace.to_xml_node() if trace.public? or trace.user == @user
224 render :text => doc.to_s, :content_type => "text/xml"
228 @this_user = User.find_by_display_name(params[:display_name], :conditions => {:visible => true})
231 @title = @this_user.display_name
233 @not_found_user = params[:display_name]
234 render :action => 'no_such_user', :status => :not_found
239 if params[:display_name]
240 name = params[:display_name]
241 new_friend = User.find_by_display_name(name, :conditions => {:visible => true})
243 friend.user_id = @user.id
244 friend.friend_user_id = new_friend.id
245 unless @user.is_friends_with?(new_friend)
247 flash[:notice] = "#{name} is now your friend."
248 Notifier.deliver_friend_notification(friend)
250 friend.add_error("Sorry, failed to add #{name} as a friend.")
253 flash[:notice] = "You are already friends with #{name}."
256 redirect_to :controller => 'user', :action => 'view'
261 if params[:display_name]
262 name = params[:display_name]
263 friend = User.find_by_display_name(name, :conditions => {:visible => true})
264 if @user.is_friends_with?(friend)
265 Friend.delete_all "user_id = #{@user.id} AND friend_user_id = #{friend.id}"
266 flash[:notice] = "#{friend.display_name} was removed from your friends."
268 flash[:notice] = "#{friend.display_name} is not one of your friends."
271 redirect_to :controller => 'user', :action => 'view'