1 class UserController < ApplicationController
 
   4   before_filter :authorize, :only => [:api_details, :api_gpx_files]
 
   5   before_filter :authorize_web, :only => [:account, :go_public, :view, :diary, :make_friend, :remove_friend, :upload_image]
 
   6   before_filter :require_user, :only => [:set_home, :account, :go_public, :make_friend, :remove_friend, :upload_image]
 
   8   filter_parameter_logging :password, :pass_crypt, :pass_crypt_confirmation
 
  11     @title = 'create account'
 
  12     @user = User.new(params[:user])
 
  15       token = @user.tokens.create
 
  16       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."
 
  17       Notifier::deliver_signup_confirm(@user, token)
 
  18       redirect_to :action => 'login'
 
  20       render :action => 'new'
 
  25     @title = 'edit account'
 
  26     if params[:user] and params[:user][:display_name] and params[:user][:description]
 
  27       home_lat =  params[:user][:home_lat]
 
  28       home_lon =  params[:user][:home_lon]
 
  30       @user.display_name = params[:user][:display_name]
 
  31       if params[:user][:pass_crypt].length > 0 or params[:user][:pass_crypt_confirmation].length > 0
 
  32         @user.pass_crypt = params[:user][:pass_crypt]
 
  33         @user.pass_crypt_confirmation = params[:user][:pass_crypt_confirmation]
 
  35       @user.description = params[:user][:description]
 
  36       @user.home_lat = home_lat
 
  37       @user.home_lon = home_lon
 
  39         flash[:notice] = "User information updated successfully."
 
  47     if params[:user][:home_lat] and params[:user][:home_lon]
 
  48       @user.home_lat = params[:user][:home_lat].to_f
 
  49       @user.home_lon = params[:user][:home_lon].to_f
 
  51         flash[:notice] = "Home location saved successfully."
 
  52         redirect_to :controller => 'user', :action => 'account'
 
  58     @user.data_public = true
 
  60     flash[:notice] = 'All your edits are now public.'
 
  61     redirect_to :controller => 'user', :action => 'account', :display_name => @user.display_name
 
  65     @title = 'lost password'
 
  66     if params[:user] and params[:user][:email]
 
  67       user = User.find_by_email(params[:user][:email])
 
  69         token = user.tokens.create
 
  70         Notifier::deliver_lost_password(user, token)
 
  71         flash[:notice] = "Sorry you lost it :-( but an email is on its way so you can reset it soon."
 
  73         flash[:notice] = "Couldn't find that email address, sorry."
 
  76       render :action => 'lost_password'
 
  81     @title = 'reset password'
 
  83       token = UserToken.find_by_token(params[:token])
 
  85         pass = OSM::make_token(8)
 
  87         user.pass_crypt = pass
 
  88         user.pass_crypt_confirmation = pass
 
  92         Notifier::deliver_reset_password(user, pass)
 
  93         flash[:notice] = "Your password has been changed and is on its way to your mailbox :-)"
 
  95         flash[:notice] = "Didn't find that token, check the URL maybe?"
 
  98     redirect_to :action => 'login'
 
 102     @title = 'create account'
 
 108       email_or_display_name = params[:user][:email]
 
 109       pass = params[:user][:password]
 
 110       user = User.authenticate(:username => email_or_display_name, :password => pass)
 
 112         session[:user] = user.id
 
 114           redirect_to params[:referer]
 
 116           redirect_to :controller => 'site', :action => 'index'
 
 119       elsif User.authenticate(:username => email_or_display_name, :password => pass, :invalid => true)
 
 120         flash[:notice] = "Sorry, your account is not active yet.<br>Please click on the link in the account confirmation email to activate your account."
 
 122         flash[:notice] = "Sorry, couldn't log in with those details."
 
 129       token = UserToken.find_by_token(session[:token])
 
 133       session[:token] = nil
 
 137       redirect_to params[:referer]
 
 139       redirect_to :controller => 'site', :action => 'index'
 
 144     token = UserToken.find_by_token(params[:confirm_string])
 
 145     if token and !token.user.active?
 
 150       flash[:notice] = 'Confirmed your account, thanks for signing up!'
 
 151       session[:user] = @user.id
 
 152       redirect_to :action => 'account', :display_name => @user.display_name
 
 154       flash[:notice] = 'Something went wrong confirming that user.'
 
 159     @user.image = params[:user][:image]
 
 161     redirect_to :controller => 'user', :action => 'view', :display_name => @user.display_name
 
 165     render :text => @user.to_xml.to_s, :content_type => "text/xml"
 
 169     doc = OSM::API.new.get_xml_doc
 
 170     @user.traces.each do |trace|
 
 171       doc.root << trace.to_xml_node() if trace.public? or trace.user == @user
 
 173     render :text => doc.to_s, :content_type => "text/xml"
 
 177     @this_user = User.find_by_display_name(params[:display_name])
 
 180       @title = @this_user.display_name
 
 182       render :nothing => true, :status => :not_found
 
 187     if params[:display_name]     
 
 188       name = params[:display_name]
 
 189       new_friend = User.find_by_display_name(name)
 
 191       friend.user_id = @user.id
 
 192       friend.friend_user_id = new_friend.id
 
 193       unless @user.is_friends_with?(new_friend)
 
 195           flash[:notice] = "#{name} is now your friend."
 
 196           Notifier::deliver_friend_notification(friend)
 
 198           friend.add_error("Sorry, failed to add #{name} as a friend.")
 
 201         flash[:notice] = "You are already friends with #{name}."  
 
 203       redirect_to :controller => 'user', :action => 'view'
 
 208     if params[:display_name]     
 
 209       name = params[:display_name]
 
 210       friend = User.find_by_display_name(name)
 
 211       if @user.is_friends_with?(friend)
 
 212         Friend.delete_all "user_id = #{@user.id} AND friend_user_id = #{friend.id}"
 
 213         flash[:notice] = "#{friend.display_name} was removed from your friends."
 
 215         flash[:notice] = "#{friend.display_name} was not already one of your friends."
 
 217       redirect_to :controller => 'user', :action => 'view'