]> git.openstreetmap.org Git - rails.git/blob - app/controllers/user_controller.rb
Avoid warnings about spaces in method calls.
[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 => [:edit, :account, :go_public, :view, :diary, :make_friend]
6   before_filter :require_user, :only => [:edit, :set_home, :account, :go_public, :make_friend]
7
8   def save
9     @user = User.new(params[:user])
10     @user.set_defaults
11
12     if @user.save
13       flash[:notice] = 'User was successfully created. Check your email for a confirmation note, and you\'ll be mapping in no time :-)'
14       Notifier::deliver_signup_confirm(@user)
15       redirect_to :action => 'login'
16     else
17       render :action => 'new'
18     end
19   end
20
21   def edit
22     if params[:user] and params[:user][:display_name] and params[:user][:description]
23       home_lat =  params[:user][:home_lat]
24       home_lon =  params[:user][:home_lon]
25
26       @user.display_name = params[:user][:display_name]
27       @user.description = params[:user][:description]
28       @user.home_lat = home_lat.to_f
29       @user.home_lon = home_lon.to_f
30       if @user.save
31         flash[:notice] = "User information updated successfully."
32         redirect_to :controller => 'user', :action => 'account'
33       end
34     end
35   end
36
37   def set_home
38     if params[:user][:home_lat] and params[:user][:home_lon]
39       @user.home_lat = params[:user][:home_lat].to_f
40       @user.home_lon = params[:user][:home_lon].to_f
41       if @user.save
42         flash[:notice] = "Home location saved successfully."
43         redirect_to :controller => 'user', :action => 'account'
44       end
45     end
46   end
47
48   def go_public
49     @user.data_public = true
50     @user.save
51     flash[:notice] = 'All your edits are now public.'
52     redirect_to :controller => 'user', :action => 'account', :display_name => @user.display_name
53   end
54
55   def lost_password
56     if params[:user] and params[:user][:email]
57       user = User.find_by_email(params['user']['email'])
58       if user
59         user.token = User.make_token
60         user.save
61         Notifier::deliver_lost_password(user)
62         flash[:notice] = "Sorry you lost it :-( but an email is on its way so you can reset it soon."
63       else
64         flash[:notice] = "Couldn't find that email address, sorry."
65       end
66     else
67       render :action => 'lost_password'
68     end
69   end
70
71   def reset_password
72     if params['token']
73       user = User.find_by_token(params['token'])
74       if user
75         pass = User.make_token(8)
76         user.pass_crypt = pass
77         user.pass_crypt_confirmation = pass
78         user.active = true
79         user.save
80         Notifier::deliver_reset_password(user, pass)
81         flash[:notice] = "Your password has been changed and is on its way to your mailbox :-)"
82       else
83         flash[:notice] = "Didn't find that token, check the URL maybe?"
84       end
85     end
86     redirect_to :action => 'login'
87   end
88
89   def new
90   end
91
92   def login
93     if params[:user]
94       email = params[:user][:email]
95       pass = params[:user][:password]
96       u = User.authenticate(email, pass)
97       if u
98         u.token = User.make_token
99         u.timeout = 1.day.from_now
100         u.save
101         session[:token] = u.token
102         if params[:next_controller] and params[:next_action]
103           redirect_to :controller => params[:next_controller], :action => params[:next_action]
104         else
105           redirect_to :controller => 'site', :action => 'index'
106         end
107         return
108       else
109         flash[:notice] = "Sorry, couldn't log in with those details."
110       end
111     end
112   end
113
114   def logout
115     if session[:token]
116       u = User.find_by_token(session[:token])
117       if u
118         u.token = User.make_token
119         u.timeout = Time.now
120         u.save
121       end
122     end
123     session[:token] = nil
124     if params[:next_controller] and params[:next_action]
125       redirect_to :controller => params[:next_controller], :action => params[:next_action]
126     else
127       redirect_to :controller => 'site', :action => 'index'
128     end
129   end
130
131   def confirm
132     @user = User.find_by_token(params[:confirm_string])
133     if @user && @user.active == 0
134       @user.active = true
135       @user.save
136       flash[:notice] = 'Confirmed your account, thanks for signing up!'
137
138       #FIXME: login the person magically
139
140       redirect_to :action => 'login'
141     else
142       flash[:notice] = 'Something went wrong confirming that user.'
143     end
144   end
145
146   def preferences
147     if request.get?
148       render_text @user.preferences
149     elsif request.post? or request.put?
150       @user.preferences = request.raw_post
151       @user.save!
152       render :nothing => true
153     else
154       render :status => 400, :nothing => true
155     end
156   end
157
158   def api_details
159     render :text => @user.to_xml.to_s
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
168   end
169
170   def view
171     @this_user = User.find_by_display_name(params[:display_name])
172   end
173
174   def diary
175     @this_user = User.find_by_display_name(params[:display_name])
176   end
177
178   def make_friend
179
180     if params[:display_name]     
181       name = params[:display_name]
182       friend = Friend.new
183       friend.user_id = @user.id
184       friend.friend_user_id = User.find_by_display_name(name).id 
185       unless @user.is_friends_with?(friend)
186         if friend.save
187           flash[:notice] = "#{name} is now your friend."
188         else
189           friend.add_error("Sorry, failed to add #{name} as a friend.")
190         end
191       else
192         flash[:notice] = "You are already friends with #{name}."  
193       end
194         redirect_to :controller => 'user', :action => 'view'
195     end
196   end
197
198 end
199