<commit_before>require 'rest_client'

class SessionsController < ApplicationController
  
  def create
    session[:form_username] = params[:username]
    begin
      @session = Session.create({ :username => params[:username], 
                                  :password => params[:password] })
                                                          
    rescue RestClient::Unauthorized => e
      flash[:error] = :login_failed
      redirect_to login_path and return
    end

    session[:form_username] = nil
    
    if @session.person_id  # if not app-only-session and person found in cos
      unless  @current_user = Person.find_by_id(@session.person_id)
        # The user has succesfully logged in, but is not found in Kassi DB
        # Existing Sizzle user's first login in Kassi
        session[:temp_cookie] = @session.cookie
        session[:temp_person_id] = @session.person_id
        redirect_to terms_path and return
      end
    end
    
    session[:cookie] = @session.cookie
    session[:person_id] = @session.person_id
      
    flash[:notice] = [:login_successful, (@current_user.given_name + "!").to_s, person_path(@current_user)]
    if session[:return_to]
      redirect_to session[:return_to]
      session[:return_to] = nil
    else
      redirect_to root
    end
  end
  
  def destroy
    Session.destroy(session[:cookie]) if session[:cookie]
    session[:cookie] = nil
    session[:person_id] = nil
    flash[:notice] = :logout_successful
    redirect_to root
  end
  
  def request_new_password
    begin
      RestHelper.make_request(:post, "#{APP_CONFIG.asi_url}/people/recover_password", {:email => params[:email]} ,{:cookies => Session.kassi_cookie})
      # RestClient.post("#{APP_CONFIG.asi_url}/people/recover_password", {:email => params[:email]} ,{:cookies => Session.kassi_cookie})
      flash[:notice] = :password_recovery_sent<commit_msg>Add index action to sessions_controller to avoid too many reported errors when bots try that URL.<commit_after>require 'rest_client'

class SessionsController < ApplicationController
  
  def create
    session[:form_username] = params[:username]
    begin
      @session = Session.create({ :username => params[:username], 
                                  :password => params[:password] })
                                                          
    rescue RestClient::Unauthorized => e
      flash[:error] = :login_failed
      redirect_to login_path and return
    end

    session[:form_username] = nil
    
    if @session.person_id  # if not app-only-session and person found in cos
      unless  @current_user = Person.find_by_id(@session.person_id)
        # The user has succesfully logged in, but is not found in Kassi DB
        # Existing Sizzle user's first login in Kassi
        session[:temp_cookie] = @session.cookie
        session[:temp_person_id] = @session.person_id
        redirect_to terms_path and return
      end
    end
    
    session[:cookie] = @session.cookie
    session[:person_id] = @session.person_id
      
    flash[:notice] = [:login_successful, (@current_user.given_name + "!").to_s, person_path(@current_user)]
    if session[:return_to]
      redirect_to session[:return_to]
      session[:return_to] = nil
    else
      redirect_to root
    end
  end
  
  def destroy
    Session.destroy(session[:cookie]) if session[:cookie]
    session[:cookie] = nil
    session[:person_id] = nil
    flash[:notice] = :logout_successful
    redirect_to root
  end
  
  def index
    # this is not in use in Kassi, but bots seem to try the url so implementing this to avoid errors
   render :file => "#{RAILS_ROOT}/public/404.html", :layout => false, :status => 404
  end
  
  def request_new_password
    begin
      RestHelper.make_request(:post, "#{APP_CONFIG.asi_url}/people/recover_password", {:email => params[:email]} ,{:cookies => Session.kassi_cookie})
      # RestClient.post("#{APP_CONFIG.asi_url}/people/recover_password", {:email => params[:email]} ,{:cookies => Session.kassi_cookie})
      flash[:notice] = :password_recovery_sent
{'fraction_non_alphanumeric': 0.10430504305043051, 'fraction_numerical': 0.0014760147601476014, 'mean_word_length': 2.8946360153256707, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 20, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7644268', 'n_tokens_mistral': 1187, 'n_tokens_neox': 1165, 'n_words': 286}