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