class Mysqlman::User

Constants

PASSWORD_LENGTH

Attributes

host[R]
privs[R]
role[R]
user[R]

Public Class Methods

all() click to toggle source
# File lib/mysqlman/user.rb, line 8
def all
  conn = Connection.instance
  conn.query('SELECT Host, User FROM mysql.user').map do |row|
    new(host: row['Host'], user: row['User'])
  end
end
find(user, host = HOST_ALL) click to toggle source
# File lib/mysqlman/user.rb, line 15
      def find(user, host = HOST_ALL)
        conn = Connection.instance
        user = conn.query(<<-QUERY
          SELECT Host, User
          FROM mysql.user
          WHERE Host = '#{host}' AND User = '#{user}'
        QUERY
                         ).first
        new(host: user['Host'], user: user['User']) unless user.nil?
      end
new(user:, host: HOST_ALL, role: nil) click to toggle source
# File lib/mysqlman/user.rb, line 29
def initialize(user:, host: HOST_ALL, role: nil)
  @host = host
  @user = user
  @role = Role.find(role) unless role.nil?
  @privs = Privs.new(self)
  @conn = Connection.instance
end

Public Instance Methods

create(debug = false) click to toggle source
# File lib/mysqlman/user.rb, line 51
def create(debug = false)
  password = debug ? '******' : SecureRandom.urlsafe_base64(PASSWORD_LENGTH)
  @conn.query(create_user_query(password)) unless debug
  Logger.new(STDOUT).info(
    "Create user: '#{@user}'@'#{@host}', password is '#{password}'"
  )
  self
end
create_user_query(password) click to toggle source
# File lib/mysqlman/user.rb, line 60
def create_user_query(password)
  "CREATE USER '#{@user}'@'#{@host}' IDENTIFIED BY '#{password}'"
end
drop(debug = false) click to toggle source
# File lib/mysqlman/user.rb, line 64
def drop(debug = false)
  @conn.query("DROP USER '#{@user}'@'#{@host}'") unless debug
  Logger.new(STDOUT).info("Delete user: '#{@user}'@'#{@host}'")
end
exists?() click to toggle source
# File lib/mysqlman/user.rb, line 41
    def exists?
      user = @conn.query(<<-QUERY
        SELECT Host, User
        FROM mysql.user
        WHERE Host = '#{@host}' AND User = '#{@user}'
      QUERY
                        ).first
      !user.nil?
    end
name_with_host() click to toggle source
# File lib/mysqlman/user.rb, line 37
def name_with_host
  { 'user' => @user, 'host' => @host }
end