<commit_before>def user_exists?(username, connection)
  connection['admin']['system.users'].find(:user => username).count > 0
end

def add_user(username, password, roles = [], database)
  require 'rubygems'
  require 'mongo'

  connection = retrieve_db
  admin = connection.db('admin')
  db = connection.db(database)

  # Must authenticate as a userAdmin after an admin user has been created
  # this will fail on the first attempt, but user will still be created
  # because of the localhost exception
  begin
    admin.authenticate(node[:mongodb][:admin][:username], node[:mongodb][:admin][:password])
  rescue Mongo::AuthenticationError => e
    Chef::Log.warn("Unable to authenticate as admin user. If this is a fresh install, ignore warning: #{e}")
  end

  # Check if the user exists, if they don't, create them
  # if they do, update them
  if !user_exists?(username, connection)
    db.add_user(username, password, false, :roles => roles)
    Chef::Log.info("Created user #{username} on #{database}")
  else
    db.add_user(username, password, false, :roles => roles)
    Chef::Log.info("Updating user #{username} on #{database}")
  end
end

# Drop a user from the database specified
def delete_user(username, database)
  require 'rubygems'
  require 'mongo'

  connection = retrieve_db
  admin = connection.db('admin')
  db = connection.db(database)

  admin.authenticate(node[:mongodb][:admin][:username], node[:mongodb][:admin][:password])

  if user_exists?(username, connection)
    db.remove_user(username)<commit_msg>removing redundant code on add_user<commit_after>def user_exists?(username, connection)
  connection['admin']['system.users'].find(:user => username).count > 0
end

def add_user(username, password, roles = [], database)
  require 'rubygems'
  require 'mongo'

  connection = retrieve_db
  admin = connection.db('admin')
  db = connection.db(database)

  # Must authenticate as a userAdmin after an admin user has been created
  # this will fail on the first attempt, but user will still be created
  # because of the localhost exception
  begin
    admin.authenticate(node[:mongodb][:admin][:username], node[:mongodb][:admin][:password])
  rescue Mongo::AuthenticationError => e
    Chef::Log.warn("Unable to authenticate as admin user. If this is a fresh install, ignore warning: #{e}")
  end

  # Create the user if they don't exist
  # Update the user if they already exist
  db.add_user(username, password, false, :roles => roles)
  Chef::Log.info("Created or updated user #{username} on #{database}")
end

# Drop a user from the database specified
def delete_user(username, database)
  require 'rubygems'
  require 'mongo'

  connection = retrieve_db
  admin = connection.db('admin')
  db = connection.db(database)

  admin.authenticate(node[:mongodb][:admin][:username], node[:mongodb][:admin][:password])

  if user_exists?(username, connection)
    db.remove_user(username)
{'fraction_non_alphanumeric': 0.12221077926536217, 'fraction_numerical': 0.0006865774116031583, 'mean_word_length': 4.971311475409836, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 12, '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': '7012176', 'n_tokens_mistral': 879, 'n_tokens_neox': 851, 'n_words': 271}