Class: Mysqlman::User
- Inherits:
-
Object
- Object
- Mysqlman::User
- Defined in:
- lib/mysqlman/user.rb
Constant Summary collapse
- PASSWORD_LENGTH =
8
Instance Attribute Summary collapse
-
#host ⇒ Object
readonly
Returns the value of attribute host.
-
#privs ⇒ Object
readonly
Returns the value of attribute privs.
-
#role ⇒ Object
readonly
Returns the value of attribute role.
-
#user ⇒ Object
readonly
Returns the value of attribute user.
Class Method Summary collapse
Instance Method Summary collapse
- #create(debug = false) ⇒ Object
- #create_user_query(password) ⇒ Object
- #drop(debug = false) ⇒ Object
- #exists? ⇒ Boolean
-
#initialize(user:, host: HOST_ALL, role: nil) ⇒ User
constructor
A new instance of User.
- #name_with_host ⇒ Object
Constructor Details
#initialize(user:, host: HOST_ALL, role: nil) ⇒ User
Returns a new instance of User.
29 30 31 32 33 34 35 |
# 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 |
Instance Attribute Details
#host ⇒ Object (readonly)
Returns the value of attribute host.
27 28 29 |
# File 'lib/mysqlman/user.rb', line 27 def host @host end |
#privs ⇒ Object (readonly)
Returns the value of attribute privs.
27 28 29 |
# File 'lib/mysqlman/user.rb', line 27 def privs @privs end |
#role ⇒ Object (readonly)
Returns the value of attribute role.
27 28 29 |
# File 'lib/mysqlman/user.rb', line 27 def role @role end |
#user ⇒ Object (readonly)
Returns the value of attribute user.
27 28 29 |
# File 'lib/mysqlman/user.rb', line 27 def user @user end |
Class Method Details
.all ⇒ Object
8 9 10 11 12 13 |
# 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) ⇒ Object
15 16 17 18 19 20 21 22 23 24 |
# 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 |
Instance Method Details
#create(debug = false) ⇒ Object
51 52 53 54 55 56 57 58 |
# 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) ⇒ Object
60 61 62 |
# File 'lib/mysqlman/user.rb', line 60 def create_user_query(password) "CREATE USER '#{@user}'@'#{@host}' IDENTIFIED BY '#{password}'" end |
#drop(debug = false) ⇒ Object
64 65 66 67 |
# 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? ⇒ Boolean
41 42 43 44 45 46 47 48 49 |
# 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 ⇒ Object
37 38 39 |
# File 'lib/mysqlman/user.rb', line 37 def name_with_host { 'user' => @user, 'host' => @host } end |