From 58cacb67b86274fc8bab793e9393bcffbec2956d Mon Sep 17 00:00:00 2001 From: Emmanuel Viennet Date: Tue, 21 Dec 2021 00:04:42 +0100 Subject: [PATCH] commande user-role --- scodoc.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/scodoc.py b/scodoc.py index d85500f3f..482501e0e 100755 --- a/scodoc.py +++ b/scodoc.py @@ -248,6 +248,35 @@ def edit_role(rolename, addpermissionname=None, removepermissionname=None): # e db.session.commit() +@app.cli.command() +@click.argument("username") +@click.option("-d", "--dept", "dept_acronym") +@click.option("-a", "--add", "add_role_name") +@click.option("-r", "--remove", "remove_role_name") +def user_role(username, dept_acronym=None, add_role_name=None, remove_role_name=None): + """Add or remove a role to the given user in the given dept""" + user = User.query.filter_by(user_name=username).first() + if not user: + sys.stderr.write(f"user_role: user {username} does not exists\n") + return 1 + if dept_acronym: + dept = models.Departement.query.filter_by(acronym=dept_acronym).first() + if dept is None: + sys.stderr.write(f"Erreur: le departement {dept} n'existe pas !\n") + return 2 + + if add_role_name: + role = Role.query.filter_by(name=add_role_name).first() + user.add_role(role, dept_acronym) + if remove_role_name: + role = Role.query.filter_by(name=remove_role_name).first() + user_role = UserRole.query.filter( + UserRole.role == role, UserRole.user == user, UserRole.dept == dept_acronym + ).first() + db.session.delete(user_role) + db.session.commit() + + @app.cli.command() @click.argument("dept") def delete_dept(dept): # delete-dept