Class: PgConn::RoleMethods

Inherits:
Object
  • Object
show all
Defined in:
lib/pg_conn/role_methods.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(conn) ⇒ RoleMethods

Returns a new instance of RoleMethods.



5
6
7
8
# File 'lib/pg_conn/role_methods.rb', line 5

def initialize(conn)
  @conn = conn
  # TODO: Check if conn is a superuser connection
end

Instance Attribute Details

#connObject (readonly)

Returns the value of attribute conn.



3
4
5
# File 'lib/pg_conn/role_methods.rb', line 3

def conn
  @conn
end

Instance Method Details

#can_login?(username, superuser: nil) ⇒ Boolean Also known as: user?

Return true if the user can login

Returns:

  • (Boolean)


33
34
35
# File 'lib/pg_conn/role_methods.rb', line 33

def can_login?(username, superuser: nil)
  exist?(username, superuser: superuser, can_login: true)
end

#clean(rolename) ⇒ Object

Remove all privileges from the given role. TODO #demote!, strip! ?



56
57
# File 'lib/pg_conn/role_methods.rb', line 56

def clean(rolename)
end

#create(rolename, superuser: false, create_database: false, can_login: false, create_role: false) ⇒ Object

Create a new role



45
46
47
48
49
50
51
52
53
# File 'lib/pg_conn/role_methods.rb', line 45

def create(rolename, superuser: false, create_database: false, can_login: false, create_role: false)
  user_decl = "create role \"#{rolename}\""
  superuser_decl = superuser ? "superuser" : "nosuperuser"
  create_database_decl = create_database ? "createdb" : "nocreatedb"
   =  ? "login" : "nologin"
  create_role_decl = create_role ? "createrole" : "nocreaterole"
  stmt = [user_decl, superuser_decl, , create_role_decl].compact.join(" ")
  conn.exec stmt
end

#drop(*rolenames, cascade: false, fail: true, silent: false) ⇒ Object

Drop existing users. Return true if any role was dropped. Drop depending privileges and objects too if :cascade is true. Returns true if the user(s) was deleted and false if :fail is true and one or more user counldn’t be deleted

Note that cascade only works if connected to the database where the privileges exist.

TODO The :silent option is used in tests - fix it somehow!



68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/pg_conn/role_methods.rb', line 68

def drop(*rolenames, cascade: false, fail: true, silent: false)
  rolenames = Array(rolenames).flatten.compact.select { |role| exist?(role) }
  return false if rolenames.empty?
  rolenames_sql = PgConn.sql_idents(rolenames)
#     begin
  conn.exec("drop owned by #{rolenames_sql} cascade", fail: false, silent: silent) if cascade
  conn.exec("drop role #{rolenames_sql}", fail: fail, silent: silent) && true
#     rescue PG::Error
#       raise if fail
#       conn.cancel_transaction
#       return false
#     end
end

#exist?(*rolenames, superuser: nil, can_login: nil) ⇒ Boolean

Return true if role(s) exists

Returns:

  • (Boolean)


11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/pg_conn/role_methods.rb', line 11

def exist?(*rolenames, superuser: nil, can_login: nil)
  rolenames = Array(rolenames).flatten.compact
  rolename_clause = "rolname in (#{PgConn.sql_values(rolenames)})"
  superuser_clause =
      case superuser
        when true; "rolsuper"
        when false; "not rolsuper"
      else
        nil
      end
   =
      case 
        when true; "rolcanlogin"
        when false; "not rolcanlogin"
      else
        nil
      end
  where_clause = [rolename_clause, superuser_clause, ].compact.join(" and ")
  conn.value("select count(*) from pg_roles where #{where_clause}") == rolenames.size
end

#list(database: nil, owner: false, superuser: nil, can_login: nil) ⇒ Object

List users. TODO Use RE. Also doc this shit



83
84
85
86
87
88
89
90
91
92
93
# File 'lib/pg_conn/role_methods.rb', line 83

def list(database: nil, owner: false, superuser: nil, can_login: nil)
  database_clause = database && "rolname like '#{database}\\_\\_%'"
  database_clause = database && "(#{database_clause} or rolname = '#{database}')" if owner
  superuser_clause = superuser.nil? ? nil : "rolsuper = #{superuser}"
   = .nil? ? nil : "rolcanlogin = #{}"
  query = [
      "select rolname from pg_roles where true",
      database_clause, superuser_clause, 
  ].compact.join(" and ")
  conn.values(query)
end

#superuser?(username) ⇒ Boolean

Return true if the user is a superuser

Returns:

  • (Boolean)


40
41
42
# File 'lib/pg_conn/role_methods.rb', line 40

def superuser?(username)
  exist?(username, superuser: true)
end