<commit_before>module Util
  class UserDbManager < DbManager

    def self.change_password(user, pwd)
      new.change_password(user, pwd)
    end

    def create_user_account(user)
      begin
        return false if !can_create_user_account?(user)
        pub_con.execute("create user \"#{user.username}\" password '#{user.password}';")
        pub_con.execute("alter user #{user.username} nologin;")  # can't login until they confirm their email
        return true
      rescue => e
        user.errors.add(:base, e.message)
        return false
      end
    end

    def can_create_user_account?(user)
      return false if user_account_exists?(user.username)
      return false if !public_db_accessible?
      return true
    end

    def user_account_exists?(username)
      return true if username == 'postgres'
      return true if username == 'ctti'
      pub_con.execute("SELECT usename FROM pg_catalog.pg_user where usename = '#{username}' UNION
                       SELECT groname  FROM pg_catalog.pg_group where groname = '#{username}'").count > 0
    end

    def remove_user(username)
      begin
        return false if !user_account_exists?(username)
        revoke_db_privs(username)
        pub_con.execute("reassign owned by #{username} to postgres;")
        pub_con.execute("drop owned by #{username};")
        pub_con.execute("drop user #{username};")
        return true
      rescue => e
        raise e
      end
    end

    def change_password(user,pwd)
      begin
        pub_con.execute("alter user \"#{user.username}\" password '#{pwd}';")
      rescue => e
        user.errors.add(:base, e.message)
      end
    end

    def backup_user_info
      fm=Util::FileManager.new
      file_prefix="#{fm.backup_directory}/#{Time.zone.now.strftime('%Y%m%d')}"

      table_file_name="#{file_prefix}_aact_users_table.sql"
      event_file_name="#{file_prefix}_aact_user_events.sql"
      account_file_name="#{file_prefix}_aact_user_accounts.sql"

      File.delete(table_file_name) if File.exist?(table_file_name)
      File.delete(event_file_name) if File.exist?(event_file_name)
      File.delete(account_file_name) if File.exist?(account_file_name)

      log "dumping Users table..."
      cmd="pg_dump --no-owner --host=localhost -U #{ENV['DB_SUPER_USERNAME']} --table=Users  --data-only aact_admin > #{table_file_name}"
      run_command_line(cmd)

      log "dumping User events..."
      cmd="pg_dump --no-owner --host=localhost -U #{ENV['DB_SUPER_USERNAME']} --table=User_Events  --data-only aact_admin > #{event_file_name}"
      run_command_line(cmd)

      log "dumping User accounts..."
      cmd="pg_dumpall -U  #{ENV['DB_SUPER_USERNAME']} -h #{public_host_name} --globals-only > #{account_file_name}"
      run_command_line(cmd)

      event=Admin::UserEvent.new({:event_type=>'backup', :file_names=>" #{table_file_name}, #{event_file_name}, #{account_file_name}" })<commit_msg>aact-655:  use postgres 9.6 to run pg_dumpall.  It is dumping data from db on DigitalOcean server which is at 9.6. The command won’t work unless the versions match.  We will upgrade everything to 9.6 on the OIT (processing) servers, but for now, just need pg_dumpall to work.<commit_after>module Util
  class UserDbManager < DbManager

    def self.change_password(user, pwd)
      new.change_password(user, pwd)
    end

    def create_user_account(user)
      begin
        return false if !can_create_user_account?(user)
        pub_con.execute("create user \"#{user.username}\" password '#{<PASSWORD>}';")
        pub_con.execute("alter user #{user.username} nologin;")  # can't login until they confirm their email
        return true
      rescue => e
        user.errors.add(:base, e.message)
        return false
      end
    end

    def can_create_user_account?(user)
      return false if user_account_exists?(user.username)
      return false if !public_db_accessible?
      return true
    end

    def user_account_exists?(username)
      return true if username == 'postgres'
      return true if username == 'ctti'
      pub_con.execute("SELECT usename FROM pg_catalog.pg_user where usename = '#{username}' UNION
                       SELECT groname  FROM pg_catalog.pg_group where groname = '#{username}'").count > 0
    end

    def remove_user(username)
      begin
        return false if !user_account_exists?(username)
        revoke_db_privs(username)
        pub_con.execute("reassign owned by #{username} to postgres;")
        pub_con.execute("drop owned by #{username};")
        pub_con.execute("drop user #{username};")
        return true
      rescue => e
        raise e
      end
    end

    def change_password(user,pwd)
      begin
        pub_con.execute("alter user \"#{user.username}\" password '#{<PASSWORD>}';")
      rescue => e
        user.errors.add(:base, e.message)
      end
    end

    def backup_user_info
      fm=Util::FileManager.new
      file_prefix="#{fm.backup_directory}/#{Time.zone.now.strftime('%Y%m%d')}"

      table_file_name="#{file_prefix}_aact_users_table.sql"
      event_file_name="#{file_prefix}_aact_user_events.sql"
      account_file_name="#{file_prefix}_aact_user_accounts.sql"

      File.delete(table_file_name) if File.exist?(table_file_name)
      File.delete(event_file_name) if File.exist?(event_file_name)
      File.delete(account_file_name) if File.exist?(account_file_name)

      log "dumping Users table..."
      cmd="pg_dump --no-owner --host=localhost -U #{ENV['DB_SUPER_USERNAME']} --table=Users  --data-only aact_admin > #{table_file_name}"
      run_command_line(cmd)

      log "dumping User events..."
      cmd="pg_dump --no-owner --host=localhost -U #{ENV['DB_SUPER_USERNAME']} --table=User_Events  --data-only aact_admin > #{event_file_name}"
      run_command_line(cmd)

      log "dumping User accounts..."
      # to backup user accounts on the public server, we need to use the same version of postgres (9.6) - but for some
      # reason, we cannot get 9.2 replaced with 9.6 on the OIT servers, but can get a 9.6 instance installed,
      # We should shift everything on OIT server to 9.6, but for now, just run the pg_dumpall using 9.6, cuz it won't work
      # without it.
      cmd="scl enable rh-postgresql96 bash; /opt/rh/rh-postgresql96/root/bin/pg_dumpall -U  #{ENV['DB_SUPER_USERNAME']} -h #{public_host_name} --globals-only > #{account_file_name}"
      run_command_line(cmd)

      event=Admin::UserEvent.new({:event_type=>'backup', :file_names=>" #{table_file_name}, #{event_file_name}, #{account_file_name}" })
{'fraction_non_alphanumeric': 0.11786259541984732, 'fraction_numerical': 0.004122137404580153, 'mean_word_length': 3.1673027989821882, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 23, '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': '7466976', 'n_tokens_mistral': 2103, 'n_tokens_neox': 2077, 'n_words': 495}