<commit_before>class LoginController < ApplicationController
  require 'uri'

  def login
    cookies.delete :query_string
    cookies.signed[:query_string] = request.query_string
    referer = request.env["HTTP_REFERER"]
    cookies.signed[:referer] = referer
    logger.info "the query_string is #{cookies.signed[:query_string]}"
    logger.info "the referer is #{cookies.signed[:referer]}"
    redirect_to signin_path :cas
  end

  def create
    uri = URI.parse(cookies.signed[:referer])
    username = request.env["omniauth.auth"][:uid]
    email = request.env["omniauth.auth"][:extra][configatron.cas.email_attribute]
    if request.env["omniauth.auth"][:extra][configatron.cas.name_attribute]
      name = request.env["omniauth.auth"][:extra][configatron.cas.name_attribute].split(',').reverse.each { |x| x.strip }.join(' ')
    else
      name = username
    end

    sso = SingleSignOn.parse(cookies.signed[:query_string], configatron.sso.secret)
    sso.email = email
    sso.name = name
    sso.username = username
    sso.external_id = username # unique to your application
    sso.sso_secret = configatron.sso.secret



    #if there are groups in the data returned by CAS see if we need
    #filter through the allow and deny groups
    allowed_groups = true
    denied_groups = false

    if configatron.filter_by_groups
      #if there are groups in the data returned by CAS see if we need
      #filter through the allow and deny groups
      if request.env["omniauth.auth"][:extra][configatron.sso.groups.name]
        logger.info request.env["omniauth.auth"][:extra][configatron.sso.groups.name]
        users_groups = request.env["omniauth.auth"][:extra][configatron.sso.groups.name].tr('][','').split(', ')
        allowed_groups = configatron.sso.groups.allow ? allowed_group(users_groups) : true
        denied_groups = configatron.sso.groups.deny ? denied_group(users_groups) : false
      end
    end
    logger.info "allowed_groups #{allowed_groups} && !denied_groups #{!denied_groups}"
    if allowed_groups && !denied_groups
      redirect_to sso.to_url("#{uri.scheme}://#{uri.host}#{configatron.sso.login.path}")
    else
      render template: 'login/failure'
    end
  end


  def failure
  end

  protected


  def allowed_group(users_groups)
    allowed_set = Set.new(configatron.sso.groups.allow_list.split('|'))
    users_set = Set.new(users_groups)
    #is there and intersection in the groups
    logger.info "allow_groups #{(allowed_set & users_set).to_a.to_s} empty? #{(allowed_set & users_set).empty?} "
    !(allowed_set & users_set).empty?
  end

  def denied_group(users_groups)
    denied_set = Set.new(configatron.sso.groups.deny_list.split('|'))
    users_set = Set.new(users_groups)
    #is there and intersection in the groups
    logger.info "denied_group #{(allowed_set & users_set).to_a.to_s}  empty? #{(allowed_set & users_set).empty?} "
    (denied_set & users_set).empty?
  end

  def after_create_account(user, auth)
    user.update_attribute(:approved, SiteSetting.cas_sso_user_approved)
    ::PluginStore.set("cas", "cas_uid_#{auth[:username]}", {user_id: user.id})
  end


  def auth_hash
    request.env['omniauth.auth']
  end

end<commit_msg>remove some logging<commit_after>class LoginController < ApplicationController
  require 'uri'

  def login
    cookies.delete :query_string
    cookies.signed[:query_string] = request.query_string
    referer = request.env["HTTP_REFERER"]
    cookies.signed[:referer] = referer
    logger.error "the query_string is #{cookies.signed[:query_string]}"
    logger.error "the referer is #{cookies.signed[:referer]}"
    redirect_to signin_path :cas
  end

  def create
    uri = URI.parse(cookies.signed[:referer])
    username = request.env["omniauth.auth"][:uid]
    email = request.env["omniauth.auth"][:extra][configatron.cas.email_attribute]
    if request.env["omniauth.auth"][:extra][configatron.cas.name_attribute]
      name = request.env["omniauth.auth"][:extra][configatron.cas.name_attribute].split(',').reverse.each { |x| x.strip }.join(' ')
    else
      name = username
    end

    sso = SingleSignOn.parse(cookies.signed[:query_string], configatron.sso.secret)
    sso.email = email
    sso.name = name
    sso.username = username
    sso.external_id = username # unique to your application
    sso.sso_secret = configatron.sso.secret



    #if there are groups in the data returned by CAS see if we need
    #filter through the allow and deny groups
    allowed_groups = true
    denied_groups = false

    if configatron.filter_by_groups
      #if there are groups in the data returned by CAS see if we need
      #filter through the allow and deny groups
      if request.env["omniauth.auth"][:extra][configatron.sso.groups.name]
        logger.error request.env["omniauth.auth"][:extra][configatron.sso.groups.name]
        users_groups = request.env["omniauth.auth"][:extra][configatron.sso.groups.name].tr('][','').split(', ')
        allowed_groups = configatron.sso.groups.allow ? allowed_group(users_groups) : true
        denied_groups = configatron.sso.groups.deny ? denied_group(users_groups) : false
      end
    end
    logger.error "allowed_groups #{allowed_groups} && !denied_groups #{!denied_groups}"
    if allowed_groups && !denied_groups
      redirect_to sso.to_url("#{uri.scheme}://#{uri.host}#{configatron.sso.login.path}")
    else
      render template: 'login/failure'
    end
  end


  def failure
  end

  protected


  def allowed_group(users_groups)
    allowed_set = Set.new(configatron.sso.groups.allow_list.split('|'))
    users_set = Set.new(users_groups)
    #is there and intersection in the groups
    logger.error "allow_groups #{(allowed_set & users_set).to_a.to_s} empty? #{(allowed_set & users_set).empty?} "
    !(allowed_set & users_set).empty?
  end

  def denied_group(users_groups)
    denied_set = Set.new(configatron.sso.groups.deny_list.split('|'))
    users_set = Set.new(users_groups)
    #is there and intersection in the groups
    logger.error "denied_group #{(allowed_set & users_set).to_a.to_s}  empty? #{(allowed_set & users_set).empty?} "
    (denied_set & users_set).empty?
  end

  def after_create_account(user, auth)
    user.update_attribute(:approved, SiteSetting.cas_sso_user_approved)
    ::PluginStore.set("cas", "cas_uid_#{auth[:username]}", {user_id: user.id})
  end


  def auth_hash
    request.env['omniauth.auth']
  end

end
{'fraction_non_alphanumeric': 0.12468827930174564, 'fraction_numerical': 0.0, 'mean_word_length': 3.902215431627196, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1865600', 'n_tokens_mistral': 2127, 'n_tokens_neox': 2114, 'n_words': 421}