Révision 421c0bc8
Ajouté par Julien Enselme il y a plus de 10 ans
scripts_divers/fix-cas.py | ||
---|---|---|
1 |
#!/usr/bin/env python3 |
|
2 |
|
|
3 |
# This script is intended to fix the cas_user table. Just give the database name. |
|
4 |
|
|
5 |
import argparse |
|
6 |
|
|
7 |
def fix_cas(db, host, user, password, prefix, roles): |
|
8 |
import pymysql as mysql |
|
9 |
|
|
10 |
while not password: |
|
11 |
password = input('Please enter the password: ') |
|
12 |
|
|
13 |
conn = mysql.connect(host=host, user=user, passwd=password, db=db, charset='utf8') |
|
14 |
with conn: |
|
15 |
cur = conn.cursor() |
|
16 |
|
|
17 |
# We format the name of the tables according to prefix |
|
18 |
tables = {'users': '{}users', 'users_roles': '{}users_roles',\ |
|
19 |
'role': '{}role', 'cas_user': '{}cas_user'} |
|
20 |
for key, elt in tables.items(): |
|
21 |
tables[key] = elt.format(prefix) |
|
22 |
|
|
23 |
# We take into account the roles to modify |
|
24 |
format_dict = {'roles_to_modify': ', '.join(roles)} |
|
25 |
|
|
26 |
# We are ready to format queries |
|
27 |
format_dict.update(tables) |
|
28 |
|
|
29 |
select = """SELECT {users}.name, {users}.uid FROM {users} |
|
30 |
JOIN {users_roles} ON {users_roles}.uid = {users}.uid |
|
31 |
JOIN {role} ON {role}.rid = {users_roles}.rid |
|
32 |
WHERE {role}.name IN ({roles_to_modify}) AND {users}.uid NOT IN |
|
33 |
(SELECT uid FROM {cas_user}) |
|
34 |
""".format(**format_dict) |
|
35 |
|
|
36 |
insert = """INSERT INTO {cas_user} (uid, cas_name) |
|
37 |
VALUES (%s, %s) |
|
38 |
""".format(**format_dict) |
|
39 |
|
|
40 |
cur.execute(select) |
|
41 |
rows = cur.fetchall() |
|
42 |
|
|
43 |
for name, uid in rows: |
|
44 |
cur.execute(insert, (uid, name)) |
|
45 |
|
|
46 |
conn.commit() |
|
47 |
|
|
48 |
|
|
49 |
|
|
50 |
###### Main |
|
51 |
|
|
52 |
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.') |
|
53 |
parser.add_argument('database', metavar='database', help='name of the database to fix') |
|
54 |
parser.add_argument('--host', dest='host', default='localhost') |
|
55 |
parser.add_argument('--user', '-u', dest='user', default='root') |
|
56 |
parser.add_argument('--password', '--passwd', '-p', dest='password') |
|
57 |
parser.add_argument('--prefix', dest='prefix') |
|
58 |
parser.add_argument('--roles', '-r', dest='roles', nargs='+', default=['authenticated user']) |
|
59 |
|
|
60 |
args = parser.parse_args() |
|
61 |
fix_cas(args.database, args.host, args.user, args.passwd, args.prefix, args.roles) |
Formats disponibles : Unified diff
fix-cas.py: creation.
Ce script permet de corriger l’import LDAP des utilisateurs CAS.
La table cas_user n'étant pas remplie.