class UsersController < ApplicationController
    before_filter :check_access, :set_locale   
    def index
        @users=User.where(:ad_user => nil).all
    end

    def new
    end

    def create
       salt=Digest::SHA1.hexdigest("#{params[:user][:username]}#{DateTime.now}")
       puts params[:user][:new_password]
       password=Digest::SHA1.hexdigest("#{params[:user][:new_password]}#{salt}")
       user=User.create(:username => params[:user][:username], :password => password, :salt => salt, :user_group_id => params[:user][:user_group_id], :from_date => DateTime.now, :to_date => DateTime.now)
       check_global_filter(user.id)
       redirect_to settings_users_path 
    end

    def edit
       @user=User.find(params[:id])
    end

    def update
      puts params[:user][:new_password]
      user=User.find(params[:id]) 
      user.update(:user_group_id => params[:user][:user_group_id])
      if params[:user][:new_password] != nil
         password=Digest::SHA1.hexdigest("#{params[:user][:new_password]}#{user.salt}")
         user.update(:password => password)
      end
      redirect_to settings_users_path
    end

    def destroy
       user=User.find(params[:id])
       user.destroy
       redirect_to settings_users_path 
    end

    def show
    end
    
    def check_username
        if User.where('ad_user is null').where(:username => params[:name]).count == 0
            render :text => 1
        else
            render :text => 0
        end
    end
end
