Class: Gratan::Driver

Inherits:
Object
  • Object
show all
Includes:
Logger::Helper
Defined in:
lib/gratan/driver.rb

Instance Method Summary collapse

Methods included from Logger::Helper

#log

Constructor Details

#initialize(client, options = {}) ⇒ Driver

Returns a new instance of Driver.



4
5
6
7
# File 'lib/gratan/driver.rb', line 4

def initialize(client, options = {})
  @client = client
  @options = options
end

Instance Method Details

#create_user(user, host, options = {}) ⇒ Object



21
22
23
24
25
26
27
28
# File 'lib/gratan/driver.rb', line 21

def create_user(user, host, options = {})
  objects = options[:objects]
  grant_options = options[:options]

  objects.each do |object, object_options|
    grant(user, host, object, grant_options.merge(object_options))
  end
end

#drop_user(user, host) ⇒ Object



30
31
32
33
# File 'lib/gratan/driver.rb', line 30

def drop_user(user, host)
  sql = "DROP USER #{quote_user(user, host)}"
  delete(sql)
end

#each_userObject



9
10
11
12
13
# File 'lib/gratan/driver.rb', line 9

def each_user
  read('SELECT user, host FROM mysql.user').each do |row|
    yield(row['user'], row['host'])
  end
end

#grant(user, host, object, options) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/gratan/driver.rb', line 35

def grant(user, host, object, options)
  privs = options.fetch(:privs)
  identified = options[:identified]
  required = options[:required]
  with_option = options[:with]

  sql = 'GRANT %s ON %s TO %s' % [
    privs.join(', '),
    quote_object(object),
    quote_user(user, host),
  ]

  sql << " IDENTIFIED BY #{quote_identifier(identified)}" if identified
  sql << " REQUIRE #{required}" if required
  sql << " WITH #{with_option}" if with_option

  update(sql)
end

#identify(user, host, identifier) ⇒ Object



54
55
56
57
58
59
60
61
# File 'lib/gratan/driver.rb', line 54

def identify(user, host, identifier)
  sql = 'GRANT USAGE ON *.* TO %s IDENTIFIED BY %s' % [
    quote_user(user, host),
    quote_identifier(identifier),
  ]

  update(sql)
end

#revoke(user, host, object, options = {}) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/gratan/driver.rb', line 74

def revoke(user, host, object, options = {})
  privs = options.fetch(:privs)
  with_option = options[:with]

  if with_option =~ /\bGRANT\s+OPTION\b/i
    revoke0(user, host, object, ['GRANT OPTION'])

    if privs.length == 1 and privs[0] =~ /\AUSAGE\z/i
      return
    end
  end

  revoke0(user, host, object, privs)
end

#revoke0(user, host, object, privs) ⇒ Object



89
90
91
92
93
94
95
96
97
# File 'lib/gratan/driver.rb', line 89

def revoke0(user, host, object, privs)
  sql = 'REVOKE %s ON %s FROM %s' % [
    privs.join(', '),
    quote_object(object),
    quote_user(user, host),
  ]

  delete(sql)
end

#set_require(user, host, required) ⇒ Object



63
64
65
66
67
68
69
70
71
72
# File 'lib/gratan/driver.rb', line 63

def set_require(user, host, required)
  required ||= 'NONE'

  sql = 'GRANT USAGE ON *.* TO %s REQUIRE %s' % [
    quote_user(user, host),
    required
  ]

  update(sql)
end

#show_grants(user, host) ⇒ Object



15
16
17
18
19
# File 'lib/gratan/driver.rb', line 15

def show_grants(user, host)
  read("SHOW GRANTS FOR #{quote_user(user, host)}").each do |row|
    yield(row.values.first)
  end
end

#update_with_option(user, host, object, with_option) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/gratan/driver.rb', line 99

def update_with_option(user, host, object, with_option)
  options = []

  if with_option =~ /\bGRANT\s+OPTION\b/i
    options << 'GRANT OPTION'
  else
    revoke(user, host, object, :privs => ['GRANT OPTION'])
  end

  %w(
    MAX_QUERIES_PER_HOUR
    MAX_UPDATES_PER_HOUR
    MAX_CONNECTIONS_PER_HOUR
    MAX_USER_CONNECTIONS
  ).each do |name|
    count = 0

    if with_option =~ /\b#{name}\s+(\d+)\b/i
      count = $1
    end

    options << [name, count].join(' ')
  end

  unless options.empty?
    grant(user, host, object, :privs => ['USAGE'], :with => options.join(' '))
  end
end