<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 '#{<PASSWORD>}';")
        pub_con.execute("alter user #{user.username} nologin;")
        return true
      rescue => e
        user.errors.add(:base, e.message)
        return false
      end
    end

    def can_create_user_account?(user)
      if user_account_exists?(user.username)
        user.errors.add(:Username, "Database account already exists for '#{user.username}'")
        return false
      else
        return true
      end
    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<commit_msg>aact-598: prevent user maintenance if database is ‘locked’<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;")
        return true
      rescue => e
        user.errors.add(:base, e.message)
        return false
      end
    end

    def can_create_user_account?(user)
      can_flag=true

      if user_account_exists?(user.username)
        user.errors.add(:Username, "Database account already exists for '#{user.username}'")
        can_flag=false
      end

      if privs_revoked?
        user.errors.add(:Sorry, "AACT database is temporarily unavailable.  Please try later.")
        can_flag=false
      end

      return can_flag
    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
{'fraction_non_alphanumeric': 0.10012919896640828, 'fraction_numerical': 0.001614987080103359, 'mean_word_length': 2.726835138387485, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, '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': '3848447', 'n_tokens_mistral': 907, 'n_tokens_neox': 896, 'n_words': 232}