]> git.openstreetmap.org Git - rails.git/blob - app/controllers/user_controller.rb
API 0.4 Reverted minor errors in changeset 2324
[rails.git] / app / controllers / user_controller.rb
1 class UserController < ApplicationController
2   layout 'site'
3
4   before_filter :authorize, :only => :preferences
5   
6   def save
7     @user = User.new(params[:user])
8     @user.set_defaults
9
10     if @user.save
11       flash[:notice] = 'User was successfully created. Check your email for a confirmation note, and you\'ll be mapping in no time :-)'
12       Notifier::deliver_signup_confirm(@user)
13       redirect_to :action => 'login'
14     else
15       render :action => 'new'
16     end
17   end
18
19   def lost_password
20     if params['user']['email']
21       user = User.find_by_email(params['user']['email'])
22       if user
23         user.token = User.make_token
24         user.save
25         Notifier::deliver_lost_password(user)
26         flash[:notice] = "Sorry you lost it :-( but an email is on it's way so you can reset it soon."
27       else
28         flash[:notice] = "Couldn't find that email address, sorry."
29       end
30     end
31   end
32
33   def reset_password
34     if params['token']
35       user = User.find_by_token(params['token'])
36       if user
37         pass = User.make_token(8)
38         user.pass_crypt = pass
39         user.save
40         Notifier::deliver_reset_password(user, pass)
41         flash[:notice] = "You're password has been changed and is on the way to your mailbox :-)"
42       else
43         flash[:notice] = "Didn't find that token, check the URL maybe?"
44       end
45     end
46     redirect_to :action => 'login'
47   end
48
49   def new
50   end
51
52   def login
53     if params[:user]
54       email = params[:user][:email]
55       pass = params[:user][:password]
56       u = User.authenticate(email, pass)
57       if u
58         u.token = User.make_token
59         u.timeout = 1.day.from_now
60         u.save
61         session[:token] = u.token
62         redirect_to :controller => 'site', :action => 'index'
63         return
64       else
65         flash[:notice] = "Couldn't log in with those details"
66       end
67     end
68   end
69
70   def logout
71     if session[:token]
72       u = User.find_by_token(session[:token])
73       if u
74         u.token = User.make_token
75         u.timeout = Time.now
76         u.save
77       end
78     end
79     session[:token] = nil
80     redirect_to :controller => 'site', :action => 'index'
81   end
82
83   def confirm
84     @user = User.find_by_token(params[:confirm_string])
85     if @user && @user.active == 0
86       @user.active = true
87       @user.save
88       flash[:notice] = 'Confirmed your account, thanks for signing up!'
89
90       #FIXME: login the person magically
91
92       redirect_to :action => 'login'
93     else
94       flash[:notice] = 'Something went wrong confirming that user'
95     end
96   end
97
98   def preferences
99     if request.get?
100       render_text @user.preferences
101     elsif request.post? or request.put?
102       @user.preferences = request.raw_post
103       @user.save!
104       render :nothing => true
105     else
106       render :status => 400, :nothing => true
107     end
108   end
109 end
110