Class: Gratan::Driver
- Inherits:
-
Object
- Object
- Gratan::Driver
- Includes:
- Logger::Helper
- Defined in:
- lib/gratan/driver.rb
Instance Method Summary collapse
- #create_user(user, host, options = {}) ⇒ Object
- #drop_user(user, host) ⇒ Object
- #each_user ⇒ Object
- #grant(user, host, object, options) ⇒ Object
- #identify(user, host, identifier) ⇒ Object
-
#initialize(client, options = {}) ⇒ Driver
constructor
A new instance of Driver.
- #revoke(user, host, object, options = {}) ⇒ Object
- #revoke0(user, host, object, privs) ⇒ Object
- #set_require(user, host, required) ⇒ Object
- #show_grants(user, host) ⇒ Object
- #update_with_option(user, host, object, with_option) ⇒ Object
Methods included from Logger::Helper
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, = {}) @client = client @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, = {}) objects = [:objects] = [:options] objects.each do |object, | grant(user, host, object, .merge()) 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_user ⇒ Object
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, ) privs = .fetch(:privs) identified = [:identified] required = [:required] with_option = [: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, = {}) privs = .fetch(:privs) with_option = [: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) = [] if with_option =~ /\bGRANT\s+OPTION\b/i << '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 << [name, count].join(' ') end unless .empty? grant(user, host, object, :privs => ['USAGE'], :with => .join(' ')) end end |