<commit_before>class KeyPairsController < ApplicationController
  # GET /key_pairs
  # GET /key_pairs.json
  def index
    # Grab the own user's keys only
    @key_pairs = KeyPair.where("user_id = ?", @current_user.id)

    # todo: Need to make an array with KeyPair -> String values to be displayed in view
    @key_strings = Array.new

    @key_pairs.each do |keypair|
      # Read the key
      key = File.open(KEY_STORAGE + '/' + keypair.file_name)
      @key_strings.push key.read
    end

    @key_pairs = @key_pairs.zip @key_strings

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json => {:key_pairs => @key_pairs}}
    end
  end



  # GET /key_pairs/1
  # GET /key_pairs/1.json
  def show
    @key_pair = KeyPair.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @key_pair }
    end
  end

  # GET /key_pairs/new
  # GET /key_pairs/new.json
  def new
    @key_pair = KeyPair.new
    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @key_pair }
    end
  end

  # GET /key_pairs/1/edit
  def edit
    @key_pair = KeyPair.find(params[:id])
  end

  # Given a File object to upload, save it on the file system with
  # association to the user_name given.
  # If a String is supplied as the first argument then it's content
  # is used to create the public key
  # Creates the KEY_STORAGE directory if it does not yet exist
  def upload_key_file(_file, user_name, time_stamp)
    Dir.mkdir(KEY_STORAGE) unless File.exists?(KEY_STORAGE)

    public_key_content = ''

    if _file.is_a? String
      # Dump the string into the file
      public_key_content = _file
    else
      public_key_content = _file.read
    end

    File.open(Rails.root.join(KEY_STORAGE, user_name + "@" + time_stamp + '.pub'), 'wb') do |f|
      f.write(public_key_content)
    end

    # todo: check current user to see if they have permission to
    # use a different user_name then their current one (admins)


    # Perform student key update in isolation from admins

    #$stderr.puts Membership.all(:joins => 'INNER JOIN groupings ON memberships.grouping_id = groupings.group_id AND memberships.user_id = ' + String(@current_user.id))
    #$stderr.puts Membership.all(:joins => 'INNER JOIN groupings ON memberships.grouping_id = groupings.group_id AND memberships.user_id = ' + String(@current_user.id) + ' INNER JOIN groups ON group_id = groups.id')
    # todo: get rid of all - optimize
    #@groups = Membership.where("membership_status != 'pending'").all(:joins => 'INNER JOIN groupings ON memberships.grouping_id = groupings.group_id AND memberships.user_id = ' + String(@current_user.id) + ' INNER JOIN groups ON group_id = groups.id')

    #$stderr.puts @temp.all(:joins => 'INNER JOIN groups ON group_id = groups.id')

    # uniqueness
    #@groups = @groups.uniq{|x| x.grouping.group.group_name}

    add_key(KEY_STORAGE + '/' + user_name + "@" + time_stamp + '.pub')

  end

  # Adds a specific public key to a specific user.
  def add_key(_path)

    #gitolite admin repo - these keys are for the repo-admin user - aka git on my machine
    settings = { :public_key => '/home/git/.ssh/id_rsa.pub', :private_key => '/home/git/.ssh/id_rsa' }
    ga_repo = Gitolite::GitoliteAdmin.new(REPOSITORY_STORAGE + '/gitolite-admin', settings)
    # The admin repo is loaded into memory
    conf = ga_repo.config

    # Check to see if an individual repo exists for this user
    #if conf.has_repo?(_username)
    key = Gitolite::SSHKey.from_file(_path)

    ga_repo.add_key(key)

    # todo: make a constant for the admin key - readd admin key
    adminKey = Gitolite::SSHKey.from_file("/home/git/git.pub")
    ga_repo.add_key(adminKey)

    # update Gitolite repo
    #ga_repo.save_and_apply

    ga_repo.save
    ga_repo.apply

  end

  # Deletes a specific public key from a specific user.
  def remove_key(_path)

    #gitolite admin repo - these keys are for the repo-admin user - aka git on my machine
    settings = { :public_key => '/home/git/.ssh/id_rsa.pub', :private_key => '/home/git/.ssh/id_rsa' }
    ga_repo = Gitolite::GitoliteAdmin.new(REPOSITORY_STORAGE + '/gitolite-admin', settings)
    # The admin repo is loaded into memory
    conf = ga_repo.config

    # Check to see if an individual repo exists for this user
    #if conf.has_repo?(_username)
    key = Gitolite::SSHKey.from_file(_path)

    ga_repo.rm_key(key)

    # todo: make a constant for the admin key - readd admin key
    adminKey = Gitolite::SSHKey.from_file("/home/git/git.pub")
    ga_repo.add_key(adminKey)

    # update Gitolite repo
    #ga_repo.save_and_apply

    ga_repo.save
    ga_repo.apply

  end

  # POST /key_pairs
  # POST /key_pairs.json
  def create
<commit_msg>Cleaned up key_pairs_controller with constants<commit_after>class KeyPairsController < ApplicationController

  # GET /key_pairs
  # GET /key_pairs.json
  def index
    # Grab the own user's keys only
    @key_pairs = KeyPair.where("user_id = ?", @current_user.id)

    @key_strings = Array.new

    @key_pairs.each do |keypair|
      # Read the key
      key = File.open(KEY_STORAGE + '/' + keypair.file_name)
      @key_strings.push key.read
    end

    @key_pairs = @key_pairs.zip @key_strings

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json => {:key_pairs => @key_pairs}}
    end
  end


  # GET /key_pairs/1
  # GET /key_pairs/1.json
  def show
    @key_pair = KeyPair.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @key_pair }
    end
  end

  # GET /key_pairs/new
  # GET /key_pairs/new.json
  def new
    @key_pair = KeyPair.new
    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @key_pair }
    end
  end

  # GET /key_pairs/1/edit
  def edit
    @key_pair = KeyPair.find(params[:id])
  end

  # Given a File object to upload, save it on the file system with
  # association to the user_name given.
  # If a String is supplied as the first argument then it's content
  # is used to create the public key
  # Creates the KEY_STORAGE directory if it does not yet exist
  def upload_key_file(_file, user_name, time_stamp)
    Dir.mkdir(KEY_STORAGE) unless File.exists?(KEY_STORAGE)

    public_key_content = ''

    if _file.is_a? String
      # Dump the string into the file
      public_key_content = _file
    else
      public_key_content = _file.read
    end

    File.open(Rails.root.join(KEY_STORAGE, user_name + "@" + time_stamp + '.pub'), 'wb') do |f|
      f.write(public_key_content)
    end

    add_key(KEY_STORAGE + '/' + user_name + "@" + time_stamp + '.pub')

  end

  # Adds a specific public key to a specific user.
  def add_key(_path)

    #gitolite admin repo - these keys are for the repo-admin user - aka git on my machine
    settings = { :public_key => '/home/git/.ssh/id_rsa.pub', :private_key => '/home/git/.ssh/id_rsa' }
    ga_repo = Gitolite::GitoliteAdmin.new(REPOSITORY_STORAGE + '/gitolite-admin', settings)
    # The admin repo is loaded into memory
    conf = ga_repo.config

    # Check to see if an individual repo exists for this user
    key = Gitolite::SSHKey.from_file(_path)

    ga_repo.add_key(key)

    adminKey = Gitolite::SSHKey.from_file(GITOLITE_SETTINGS[:public_key])
    ga_repo.add_key(adminKey)

    # update Gitolite repo
    ga_repo.save_and_apply

  end

  # Deletes a specific public key from a specific user.
  def remove_key(_path)

    #gitolite admin repo - these keys are for the repo-admin user - aka git on my machine
    settings = { :public_key => '/home/git/.ssh/id_rsa.pub', :private_key => '/home/git/.ssh/id_rsa' }
    ga_repo = Gitolite::GitoliteAdmin.new(REPOSITORY_STORAGE + '/gitolite-admin', settings)
    # The admin repo is loaded into memory
    conf = ga_repo.config

    # Check to see if an individual repo exists for this user
    #if conf.has_repo?(_username)
    key = Gitolite::SSHKey.from_file(_path)

    ga_repo.rm_key(key)

    # todo: make a constant for the admin key - readd admin key
    adminKey = Gitolite::SSHKey.from_file(GITOLITE_SETTINGS[:public_key])
    ga_repo.add_key(adminKey)

    # update Gitolite repo
    ga_repo.save_and_apply
  end

  # POST /key_pairs
  # POST /key_pairs.json
  def create

{'fraction_non_alphanumeric': 0.10093279119827793, 'fraction_numerical': 0.0007175316909830184, 'mean_word_length': 3.057738961669093, '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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5280890', 'n_tokens_mistral': 2972, 'n_tokens_neox': 2858, 'n_words': 782}