1 | #Generated by LoginGenerator: |
---|
2 | # http://wiki.rubyonrails.org/rails/pages/LoginGenerator |
---|
3 | class AccountController < ApplicationController |
---|
4 | # Be sure to include AuthenticationSystem in Application Controller instead |
---|
5 | include AuthenticatedSystem |
---|
6 | # If you want "remember me" functionality, add this before_filter to Application Controller |
---|
7 | before_filter :login_from_cookie |
---|
8 | before_filter :login_required, :only => [:manage, :change_email, :modify] |
---|
9 | |
---|
10 | def index |
---|
11 | return unless request.post? |
---|
12 | self.current_user = User.authenticate(params[:login], params[:password]) |
---|
13 | if logged_in? |
---|
14 | if params[:remember_me] == "1" |
---|
15 | self.current_user.remember_me |
---|
16 | cookies[:auth_token] = { :value => self.current_user.remember_token , :expires => self.current_user.remember_token_expires_at } |
---|
17 | end |
---|
18 | redirect_back_or_default( '/' ) |
---|
19 | flash[:notice] = "Logged in successfully" |
---|
20 | end |
---|
21 | end |
---|
22 | |
---|
23 | def login |
---|
24 | return unless request.post? |
---|
25 | self.current_user = User.authenticate(params[:login], params[:password]) |
---|
26 | if logged_in? |
---|
27 | if params[:remember_me] == "1" |
---|
28 | self.current_user.remember_me |
---|
29 | cookies[:auth_token] = { :value => self.current_user.remember_token , :expires => self.current_user.remember_token_expires_at } |
---|
30 | end |
---|
31 | redirect_back_or_default(:controller => '/account', :action => 'index') |
---|
32 | flash[:notice] = "Logged in successfully" |
---|
33 | end |
---|
34 | end |
---|
35 | |
---|
36 | def signup |
---|
37 | usercount = User.count |
---|
38 | |
---|
39 | if ((usercount != 0) && !(logged_in?)) |
---|
40 | redirect_to :action => 'login' |
---|
41 | else |
---|
42 | @user = User.new(params[:user]) |
---|
43 | return unless request.post? |
---|
44 | @user.save! |
---|
45 | self.current_user = @user |
---|
46 | redirect_back_or_default(:controller => '/account', :action => 'index') |
---|
47 | flash[:notice] = "Thanks for signing up!" |
---|
48 | end |
---|
49 | rescue ActiveRecord::RecordInvalid |
---|
50 | render :action => 'signup' |
---|
51 | end |
---|
52 | |
---|
53 | def logout |
---|
54 | self.current_user.forget_me if logged_in? |
---|
55 | cookies.delete :auth_token |
---|
56 | reset_session |
---|
57 | flash[:notice] = "You have been logged out." |
---|
58 | redirect_back_or_default(:controller => '/account', :action => 'index') |
---|
59 | end |
---|
60 | |
---|
61 | def manage |
---|
62 | if User.find_by_id(session[:user]).roles.include?("admin") |
---|
63 | @users = User.find(:all) |
---|
64 | else |
---|
65 | redirect_to :action => 'overview' |
---|
66 | end |
---|
67 | end |
---|
68 | |
---|
69 | def overview |
---|
70 | @user = User.find_by_id(session[:user]) |
---|
71 | end |
---|
72 | |
---|
73 | def modify |
---|
74 | @user = User.find(params[:id]) |
---|
75 | render :partial => 'form' |
---|
76 | end |
---|
77 | |
---|
78 | def reset_password |
---|
79 | @user = User.find(params[:id]) |
---|
80 | if ( (User.find_by_id(session[:user]).login == @user.login) || ( |
---|
81 | User.find_by_id(session[:user]).roles.include?("admin")) ) |
---|
82 | render :partial => 'change_password' |
---|
83 | else |
---|
84 | render :inline => '<h2 style="background-color: #333; color: white;">No Access</h2>' |
---|
85 | end |
---|
86 | end |
---|
87 | |
---|
88 | def change_email |
---|
89 | @user = User.find(params[:id]) |
---|
90 | render :partial => 'change_email' |
---|
91 | end |
---|
92 | |
---|
93 | def update |
---|
94 | @user = User.find(params[:id]) |
---|
95 | |
---|
96 | respond_to do |format| |
---|
97 | if @user.update_attributes(params[:user]) |
---|
98 | #Regular Display |
---|
99 | flash[:notice] = 'User was successfully updated.' |
---|
100 | format.html { redirect_to(:action => 'manage') } |
---|
101 | format.xml { head :ok } |
---|
102 | end |
---|
103 | end |
---|
104 | end |
---|
105 | |
---|
106 | def destroy |
---|
107 | if !permission_required :admin |
---|
108 | return |
---|
109 | end |
---|
110 | |
---|
111 | @user = User.find(params[:id]) |
---|
112 | @user.destroy |
---|
113 | |
---|
114 | respond_to do |format| |
---|
115 | format.html { redirect_to(:action => 'manage') } |
---|
116 | format.xml { head :ok } |
---|
117 | end |
---|
118 | end |
---|
119 | end |
---|