1 |
421c0bc8
|
Julien Enselme
|
|
2 |
|
|
|
3 |
|
|
|
4 |
|
|
|
5 |
|
|
import argparse
|
6 |
ef98b98a
|
Julien Enselme
|
from getpass import getpass
|
7 |
421c0bc8
|
Julien Enselme
|
|
8 |
|
|
def fix_cas(db, host, user, password, prefix, roles):
|
9 |
|
|
import pymysql as mysql
|
10 |
|
|
|
11 |
e406049b
|
Julien Enselme
|
if not password:
|
12 |
|
|
conn = mysql.connect(host=host, user=user, db=db, charset='utf8')
|
13 |
|
|
else:
|
14 |
|
|
conn = mysql.connect(host=host, user=user, passwd=password, db=db, charset='utf8')
|
15 |
|
|
|
16 |
421c0bc8
|
Julien Enselme
|
with conn:
|
17 |
|
|
cur = conn.cursor()
|
18 |
|
|
|
19 |
|
|
|
20 |
|
|
tables = {'users': '{}users', 'users_roles': '{}users_roles',\
|
21 |
|
|
'role': '{}role', 'cas_user': '{}cas_user'}
|
22 |
|
|
for key, elt in tables.items():
|
23 |
|
|
tables[key] = elt.format(prefix)
|
24 |
|
|
|
25 |
|
|
|
26 |
140af436
|
Julien Enselme
|
|
27 |
|
|
if 'authenticated user' not in roles:
|
28 |
|
|
for role in roles:
|
29 |
|
|
i = roles.index(role)
|
30 |
|
|
roles[i] = "'" + role + "'"
|
31 |
|
|
format_dict = {'roles_to_modify': ', '.join(roles)}
|
32 |
|
|
|
33 |
|
|
|
34 |
|
|
format_dict.update(tables)
|
35 |
|
|
|
36 |
|
|
select = """SELECT {users}.name, {users}.uid FROM {users}
|
37 |
|
|
JOIN {users_roles} ON {users_roles}.uid = {users}.uid
|
38 |
|
|
JOIN {role} ON {role}.rid = {users_roles}.rid
|
39 |
|
|
WHERE {role}.name IN ({roles_to_modify}) AND {users}.uid NOT IN
|
40 |
|
|
(SELECT uid FROM {cas_user})
|
41 |
|
|
""".format(**format_dict)
|
42 |
|
|
else:
|
43 |
|
|
select = """SELECT {users}.name, {users}.uid FROM {users}
|
44 |
|
|
WHERE {users}.uid NOT IN (SELECT uid FROM {cas_user})""".format(**tables)
|
45 |
421c0bc8
|
Julien Enselme
|
|
46 |
|
|
insert = """INSERT INTO {cas_user} (uid, cas_name)
|
47 |
|
|
VALUES (%s, %s)
|
48 |
140af436
|
Julien Enselme
|
""".format(**tables)
|
49 |
421c0bc8
|
Julien Enselme
|
|
50 |
|
|
cur.execute(select)
|
51 |
|
|
rows = cur.fetchall()
|
52 |
|
|
|
53 |
|
|
for name, uid in rows:
|
54 |
|
|
cur.execute(insert, (uid, name))
|
55 |
|
|
|
56 |
|
|
conn.commit()
|
57 |
|
|
|
58 |
|
|
|
59 |
|
|
|
60 |
|
|
|
61 |
|
|
|
62 |
|
|
parser = argparse.ArgumentParser(description='Fill the cas_user table from the users table for a specific role or all roles. Requires pymysql to query the database.')
|
63 |
|
|
parser.add_argument('database', metavar='database', help='name of the database to fix')
|
64 |
|
|
parser.add_argument('--host', dest='host', default='localhost')
|
65 |
|
|
parser.add_argument('--user', '-u', dest='user', default='root')
|
66 |
|
|
parser.add_argument('--password', '--passwd', '-p', dest='password')
|
67 |
ef98b98a
|
Julien Enselme
|
parser.add_argument('--prefix', dest='prefix', default='')
|
68 |
421c0bc8
|
Julien Enselme
|
parser.add_argument('--roles', '-r', dest='roles', nargs='+', default=['authenticated user'])
|
69 |
|
|
|
70 |
|
|
args = parser.parse_args()
|
71 |
ef98b98a
|
Julien Enselme
|
fix_cas(args.database, args.host, args.user, args.password, args.prefix, args.roles) |