source: subversion/sites/rails_port/app/controllers/user_controller.rb @ 4898

Last change on this file since 4898 was 4898, checked in by dankarran, 10 years ago

Improving friend capabilities so you can now add and remove friends as you wish. Friends will be notified when you add them. You can view a list of friends on your profile page (which has been reorganised a little).

File size: 6.7 KB
Line 
1class UserController < ApplicationController
2  layout 'site'
3
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]
6  before_filter :require_user, :only => [:set_home, :account, :go_public, :make_friend, :remove_friend]
7
8  filter_parameter_logging :password, :pass_crypt, :pass_crypt_confirmation
9
10  def save
11    @title = 'create account'
12    @user = User.new(params[:user])
13
14    if @user.save
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'
19    else
20      render :action => 'new'
21    end
22  end
23
24  def account
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]
29
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]
34      end
35      @user.description = params[:user][:description]
36      @user.home_lat = home_lat
37      @user.home_lon = home_lon
38      if @user.save
39        flash[:notice] = "User information updated successfully."
40      else
41        flash.delete(:notice)
42      end
43    end
44  end
45
46  def set_home
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
50      if @user.save
51        flash[:notice] = "Home location saved successfully."
52        redirect_to :controller => 'user', :action => 'account'
53      end
54    end
55  end
56
57  def go_public
58    @user.data_public = true
59    @user.save
60    flash[:notice] = 'All your edits are now public.'
61    redirect_to :controller => 'user', :action => 'account', :display_name => @user.display_name
62  end
63
64  def lost_password
65    @title = 'lost password'
66    if params[:user] and params[:user][:email]
67      user = User.find_by_email(params[:user][:email])
68      if user
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."
72      else
73        flash[:notice] = "Couldn't find that email address, sorry."
74      end
75    else
76      render :action => 'lost_password'
77    end
78  end
79
80  def reset_password
81    @title = 'reset password'
82    if params['token']
83      token = UserToken.find_by_token(params[:token])
84      if token
85        pass = OSM::make_token(8)
86        user = token.user
87        user.pass_crypt = pass
88        user.pass_crypt_confirmation = pass
89        user.active = true
90        user.save!
91        token.destroy
92        Notifier::deliver_reset_password(user, pass)
93        flash[:notice] = "Your password has been changed and is on its way to your mailbox :-)"
94      else
95        flash[:notice] = "Didn't find that token, check the URL maybe?"
96      end
97    end
98    redirect_to :action => 'login'
99  end
100
101  def new
102    @title = 'create account'
103  end
104
105  def login
106    @title = 'login'
107    if params[:user]
108      email = params[:user][:email]
109      pass = params[:user][:password]
110      user = User.authenticate(:username => email, :password => pass)
111      if user
112        session[:user] = user.id
113        if params[:referer]
114          redirect_to params[:referer]
115        else
116          redirect_to :controller => 'site', :action => 'index'
117        end
118        return
119      elsif User.authenticate(:username => email, :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."
121      else
122        flash[:notice] = "Sorry, couldn't log in with those details."
123      end
124    end
125  end
126
127  def logout
128    if session[:token]
129      token = UserToken.find_by_token(session[:token])
130      if token
131        token.destroy
132      end
133      session[:token] = nil
134    end
135    session[:user] = nil
136    if params[:referer]
137      redirect_to params[:referer]
138    else
139      redirect_to :controller => 'site', :action => 'index'
140    end
141  end
142
143  def confirm
144    token = UserToken.find_by_token(params[:confirm_string])
145    if token and !token.user.active?
146      @user = token.user
147      @user.active = true
148      @user.save!
149      token.destroy
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
153    else
154      flash[:notice] = 'Something went wrong confirming that user.'
155    end
156  end
157
158  def api_details
159    render :text => @user.to_xml.to_s, :content_type => "text/xml"
160  end
161
162  def api_gpx_files
163    doc = OSM::API.new.get_xml_doc
164    @user.traces.each do |trace|
165      doc.root << trace.to_xml_node() if trace.public? or trace.user == @user
166    end
167    render :text => doc.to_s, :content_type => "text/xml"
168  end
169
170  def view
171    @this_user = User.find_by_display_name(params[:display_name])
172
173    if @this_user
174      @title = @this_user.display_name
175    else
176      render :nothing => true, :status => :not_found
177    end
178  end
179
180  def make_friend
181    if params[:display_name]     
182      name = params[:display_name]
183      new_friend = User.find_by_display_name(name)
184      friend = Friend.new
185      friend.user_id = @user.id
186      friend.friend_user_id = new_friend.id
187      unless @user.is_friends_with?(new_friend)
188        if friend.save
189          flash[:notice] = "#{name} is now your friend."
190          Notifier::deliver_friend_notification(friend)
191        else
192          friend.add_error("Sorry, failed to add #{name} as a friend.")
193        end
194      else
195        flash[:notice] = "You are already friends with #{name}." 
196      end
197      redirect_to :controller => 'user', :action => 'view'
198    end
199  end
200
201  def remove_friend
202    if params[:display_name]     
203      name = params[:display_name]
204      friend = User.find_by_display_name(name)
205      if @user.is_friends_with?(friend)
206        Friend.delete_all "user_id = #{@user.id} AND friend_user_id = #{friend.id}"
207        flash[:notice] = "#{friend.display_name} was removed from your friends."
208      else
209        flash[:notice] = "#{friend.display_name} was not already one of your friends."
210      end
211      redirect_to :controller => 'user', :action => 'view'
212    end
213  end
214
215end
216
Note: See TracBrowser for help on using the repository browser.