]> git.openstreetmap.org Git - rails.git/blob - app/controllers/user_controller.rb
Link to a user's details from their diary page.
[rails.git] / app / controllers / user_controller.rb
1 class UserController < ApplicationController
2   layout 'site'
3
4   before_filter :authorize, :only => [:preferences, :api_details, :api_gpx_files]
5   before_filter :authorize_web, :only => [:account, :go_public, :view, :diary, :make_friend]
6   before_filter :require_user, :only => [:set_home, :account, :go_public, :make_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     @user.set_defaults
14
15     if @user.save
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)
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.to_f
37       @user.home_lon = home_lon.to_f
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         user.token = User.make_token
70         user.save
71         Notifier::deliver_lost_password(user)
72         flash[:notice] = "Sorry you lost it :-( but an email is on its way so you can reset it soon."
73       else
74         flash[:notice] = "Couldn't find that email address, sorry."
75       end
76     else
77       render :action => 'lost_password'
78     end
79   end
80
81   def reset_password
82     @title = 'reset password'
83     if params['token']
84       user = User.find_by_token(params['token'])
85       if user
86         pass = User.make_token(8)
87         user.pass_crypt = pass
88         user.pass_crypt_confirmation = pass
89         user.active = true
90         user.save
91         Notifier::deliver_reset_password(user, pass)
92         flash[:notice] = "Your password has been changed and is on its way to your mailbox :-)"
93       else
94         flash[:notice] = "Didn't find that token, check the URL maybe?"
95       end
96     end
97     redirect_to :action => 'login'
98   end
99
100   def new
101     @title = 'create account'
102   end
103
104   def login
105     @title = 'login'
106     if params[:user]
107       email = params[:user][:email]
108       pass = params[:user][:password]
109       u = User.authenticate(email, pass)
110       if u
111         u.token = User.make_token
112         u.timeout = 1.day.from_now
113         u.save
114         session[:token] = u.token
115         if params[:referer]
116           redirect_to params[:referer]
117         else
118           redirect_to :controller => 'site', :action => 'index'
119         end
120         return
121       elsif User.authenticate(email, pass, false)
122         flash[:notice] = "Sorry, your account is not active yet.<br>Please click on the link in the account confirmation email to activate your account."
123       else
124         flash[:notice] = "Sorry, couldn't log in with those details."
125       end
126     end
127   end
128
129   def logout
130     if session[:token]
131       u = User.find_by_token(session[:token])
132       if u
133         u.token = User.make_token
134         u.timeout = Time.now
135         u.save
136       end
137     end
138     session[:token] = nil
139     if params[:referer]
140       redirect_to params[:referer]
141     else
142       redirect_to :controller => 'site', :action => 'index'
143     end
144   end
145
146   def confirm
147     @user = User.find_by_token(params[:confirm_string])
148     if @user && @user.active == 0
149       @user.active = true
150       @user.token = User.make_token
151       @user.timeout = 1.day.from_now
152       @user.save
153       flash[:notice] = 'Confirmed your account, thanks for signing up!'
154       session[:token] = @user.token
155       redirect_to :action => 'account', :display_name => @user.display_name
156     else
157       flash[:notice] = 'Something went wrong confirming that user.'
158     end
159   end
160
161   def preferences
162     @title = 'preferences'
163     if request.get?
164       render_text @user.preferences
165     elsif request.post? or request.put?
166       @user.preferences = request.raw_post
167       @user.save!
168       render :nothing => true
169     else
170       render :nothing => true, :status => :method_not_allowed
171     end
172   end
173
174   def api_details
175     render :text => @user.to_xml.to_s, :content_type => "text/xml"
176   end
177
178   def api_gpx_files
179     doc = OSM::API.new.get_xml_doc
180     @user.traces.each do |trace|
181       doc.root << trace.to_xml_node() if trace.public? or trace.user == @user
182     end
183     render :text => doc.to_s, :content_type => "text/xml"
184   end
185
186   def view
187     @this_user = User.find_by_display_name(params[:display_name])
188     @title = @this_user.display_name
189   end
190
191   def make_friend
192
193     if params[:display_name]     
194       name = params[:display_name]
195       friend = Friend.new
196       friend.user_id = @user.id
197       friend.friend_user_id = User.find_by_display_name(name).id 
198       unless @user.is_friends_with?(friend)
199         if friend.save
200           flash[:notice] = "#{name} is now your friend."
201         else
202           friend.add_error("Sorry, failed to add #{name} as a friend.")
203         end
204       else
205         flash[:notice] = "You are already friends with #{name}."  
206       end
207       redirect_to :controller => 'user', :action => 'view'
208     end
209   end
210
211 end
212