]> git.openstreetmap.org Git - rails.git/blob - app/controllers/user_controller.rb
First half of a password reset function
[rails.git] / app / controllers / user_controller.rb
1 class UserController < ApplicationController
2   layout 'site'
3   
4   def save
5     @user = User.new(params[:user])
6     @user.set_defaults
7
8     if @user.save
9       flash[:notice] = 'User was successfully created. Check your email for a confirmation note, and you\'ll be mapping in no time :-)'
10       Notifier::deliver_signup_confirm(@user)
11       redirect_to :action => 'login'
12     else
13       render :action => 'new'
14     end
15   end
16
17   def lost_password
18     if params['user']['email']
19       user = User.find_by_email(params['user']['email'])
20       if user
21         user.token = User.make_token
22         user.save
23         Notifier::deliver_lost_password(user)
24         flash[:notice] = "Sorry you lost it :-( but an email is on it's way so you can reset it soon."
25       else
26         flash[:notice] = "Couldn't find that email address, sorry."
27       end
28     end
29   end
30
31   def new
32   end
33
34   def login
35     if params[:user]
36       email = params[:user][:email]
37       pass = params[:user][:password]
38       u = User.authenticate(email, pass)
39       if u
40         u.token = User.make_token
41         u.timeout = 1.day.from_now
42         u.save
43         session[:token] = u.token
44         redirect_to :controller => 'site', :action => 'index'
45         return
46       else
47         flash[:notice] = "Couldn't log in with those details"
48       end
49     end
50   end
51
52   def logout
53     if session[:token]
54       u = User.find_by_token(session[:token])
55       if u
56         u.token = User.make_token
57         u.timeout = Time.now
58         u.save
59       end
60     end
61     session[:token] = nil
62     redirect_to :controller => 'site', :action => 'index'
63   end
64
65   def confirm
66     @user = User.find_by_token(params[:confirm_string])
67     if @user && @user.active == 0
68       @user.active = true
69       @user.save
70       flash[:notice] = 'Confirmed your account, thanks for signing up!'
71
72       #FIXME: login the person magically
73
74       redirect_to :action => 'login'
75     else
76       flash[:notice] = 'Something went wrong confirming that user'
77     end
78   end
79
80 end