Small Cleanups + Merchandise Search
[cs356-p2-videostore.git] / app / controllers / login_controller.rb
1 class LoginController < ApplicationController
2   layout "admin"
3
4   # Make sure that a user logs in before doing any action here
5   before_filter :authorize, :only => :index
6
7   # Only managers can do the following actions
8   before_filter :manager, :only => [:add_user, :delete_user, :list_users, :maintenance]
9
10   def maintenance
11     render :action => 'maintenance'
12   end
13
14   def limits
15     render :action => 'limits'
16   end
17
18   def add_user
19     @user = User.new(params[:user])
20     if request.post? and @user.save
21       flash.now[:notice] = "User #{@user.name} created"
22       @user = User.new
23     end
24   end
25
26   def login
27     session[:user_id] = nil
28     if request.post?
29       user = User.authenticate(params[:name], params[:password])
30       if user
31         session[:user_id] = user.id
32         redirect_to :action => 'index'
33       else
34         flash[:notice] = "Invalid user/password combination"
35       end
36     end
37   end
38
39   def logout
40     session[:user_id] = nil
41     flash[:notice] = "Logged Out"
42     redirect_to :action => :login
43   end
44
45   def index
46     # No code needed
47   end
48
49   def delete_user
50     if request.post?
51       user = User.find(params[:id])
52       begin
53         user.destroy
54         flash[:notice] = "User #{user.name} deleted"
55       rescue Exception => e
56         flash[:notice] = e.message
57       end
58     end
59     redirect_to(:action => :list_users)
60   end
61
62   def list_users
63     @all_users = User.find(:all)
64   end
65
66 end