<commit_before>module Authentication

protected

  ##
  # Require login checks if the user is logged on Typus, otherwise 
  # is sent to the login page with a :back_to param to return where 
  # she tried to go.
  #
  # Use this for demo!
  #
  #     session[:typus] = Typus.user_class.find(:first)
  #
  def require_login
    if session[:typus]
      set_current_user
    else
      back_to = (request.env['REQUEST_URI'] == '/admin') ? nil : request.env['REQUEST_URI']
      redirect_to admin_sign_in_path(:back_to => back_to)
    end
  end

  ##
  # Return the current user. The important thing here is that if the 
  # roles does not longer exist on the system the user will be logged 
  # off from Typus.
  #
  def set_current_user
    @current_user ||= Typus.user_class.find(session[:typus])
    raise unless Typus::Configuration.roles.keys.include?(@current_user.roles)
  rescue
    flash[:error] = t("Error! Typus User or role doesn't exist.")
    session[:typus] = nil
    redirect_to admin_sign_in_path
  end

  ##
  # Password generation using numbers and letters.
  #
  def generate_password(length = 8)
    chars = ('a'..'z').to_a + ('A'..'Z').to_a + ('0'..'9').to_a
    password = ""
    1.upto(length) { |i| password << chars[rand(chars.size - 1)] }
    return password
  end

  ##
  # Action is available on:
  #
  #     edit, update, toggle and destroy
  #
  def check_if_user_can_perform_action_on_user<commit_msg>Cleaned password generation.<commit_after>module Authentication

protected

  ##
  # Require login checks if the user is logged on Typus, otherwise 
  # is sent to the login page with a :back_to param to return where 
  # she tried to go.
  #
  # Use this for demo!
  #
  #     session[:typus] = Typus.user_class.find(:first)
  #
  def require_login
    if session[:typus]
      set_current_user
    else
      back_to = (request.env['REQUEST_URI'] == '/admin') ? nil : request.env['REQUEST_URI']
      redirect_to admin_sign_in_path(:back_to => back_to)
    end
  end

  ##
  # Return the current user. The important thing here is that if the 
  # roles does not longer exist on the system the user will be logged 
  # off from Typus.
  #
  def set_current_user
    @current_user ||= Typus.user_class.find(session[:typus])
    raise unless Typus::Configuration.roles.keys.include?(@current_user.roles)
  rescue
    flash[:error] = t("Error! Typus User or role doesn't exist.")
    session[:typus] = nil
    redirect_to admin_sign_in_path
  end

  ##
  # Password generation using numbers and letters.
  #
  def generate_password(length = 8)
    chars = ('a'..'z').to_a + ('A'..'Z').to_a + ('0'..'9').to_a
    return Array.new(length) { chars.rand }.join
  end

  ##
  # Action is available on:
  #
  #     edit, update, toggle and destroy
  #
  def check_if_user_can_perform_action_on_user
{'fraction_non_alphanumeric': 0.11201701524282169, 'fraction_numerical': 0.002835873803615739, 'mean_word_length': 3.1561119293078055, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '5973109', 'n_tokens_mistral': 960, 'n_tokens_neox': 932, 'n_words': 267}