-
Notifications
You must be signed in to change notification settings - Fork 0
/
db_sql.py
35 lines (32 loc) · 927 Bytes
/
db_sql.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import psycopg2 as psy
db = psy.connect(dbname='gkdb', host='gkdb.org')
def get_groups(uid):
cur = db.cursor()
cur.execute("""
SELECT * FROM
pg_group
WHERE
%s = ANY (grolist)
;""", (uid, ))
return cur
def get_sql_usergroups():
cur = db.cursor()
cur.execute("""
SELECT usename, STRING_AGG(groname, ', ')
FROM pg_catalog.pg_user AS u
LEFT JOIN pg_catalog.pg_group AS g ON u.usesysid = ANY (g.grolist)
GROUP BY usename
""")
return cur
def create_user(username, password=None, groups=[]):
cur = db.cursor()
cur.execute('SET ROLE developer') # Needed to create new roles
groups = ', '.join(groups)
print(groups)
if password is None:
password = "something.."
print(username, password, groups)
cur.execute("CREATE USER " + username +
" IN ROLE " + groups
)
db.commit()