From 77342f6e8a328888b6cf1c6556a14380bc6eefd6 Mon Sep 17 00:00:00 2001 From: elena Date: Mon, 1 Aug 2022 09:12:55 +0200 Subject: [PATCH] FIX: role changed on admin and on moodle --- dd-sso/admin/src/admin/lib/admin.py | 33 ++++++++++++++++++++++++++-- dd-sso/admin/src/admin/lib/moodle.py | 4 ++++ dd-sso/admin/src/admin/lib/postup.py | 4 +++- 3 files changed, 38 insertions(+), 3 deletions(-) diff --git a/dd-sso/admin/src/admin/lib/admin.py b/dd-sso/admin/src/admin/lib/admin.py index da0ab80..7b79da4 100644 --- a/dd-sso/admin/src/admin/lib/admin.py +++ b/dd-sso/admin/src/admin/lib/admin.py @@ -1,6 +1,7 @@ # # Copyright © 2021,2022 IsardVDI S.L. # Copyright © 2022 Evilham +# Copyright © 2022 Elena Barrios Galán @elena61 # # This file is part of DD # @@ -1576,6 +1577,9 @@ class Admin: ev.update_text("Syncing data from applications...") self.resync_data() + ev.update_text("Removing user role in moodle") + self.unassign_moodle_user_role(internaluser["moodle_id"], mdelete) + ev.update_text("Updating user in moodle") self.update_moodle_user(internaluser["id"], user, mdelete, madd) @@ -1587,6 +1591,31 @@ class Admin: ev.update_text("User updated") return True + + def unassign_moodle_user_role(self, user_id, role_name): + role_id = 0 + if not role_name: + return True + + if role_name[0] =='teacher': + role_id = 2 + elif role_name[0] =='manager': + role_id = 1 + + if role_id == 0: + log.warning("MOODLE: User is student. no need to be unassigned in moodle.") + return True + + log.warning("MOODLE: lets unassign user role") + try: + self.moodle.unassign_user_rol( + user_id, role_id + ) + except: + log.error( + "MOODLE: User has not been able to unassign from role" + ) + return True def update_keycloak_user(self, user_id : str, user : DDUser, kdelete : List[Any], kadd : List[Any]) -> bool: # pprint(self.keycloak.get_user_realm_roles(user_id)) @@ -1652,8 +1681,8 @@ class Admin: ) for group in madd: - cohort = [c for c in cohorts if c["name"] == group][0] - self.moodle.add_user_to_cohort(internaluser["moodle_id"], cohort["id"]) + cohort = [c for c in cohorts if c["name"] == group] + self.moodle.add_user_to_cohort(internaluser["moodle_id"], cohort[0]["id"]) return True diff --git a/dd-sso/admin/src/admin/lib/moodle.py b/dd-sso/admin/src/admin/lib/moodle.py index 972b563..20bb1a0 100644 --- a/dd-sso/admin/src/admin/lib/moodle.py +++ b/dd-sso/admin/src/admin/lib/moodle.py @@ -1,6 +1,7 @@ # # Copyright © 2021,2022 IsardVDI S.L. # Copyright © 2022 Evilham +# Copyright © 2022 Elena Barrios Galán @elena61 # # This file is part of DD # @@ -264,6 +265,9 @@ class Moodle: log.warning( "MOODLE:ADDING THE USER TO ADMINS: This needs a purge cache in moodle!" ) + def unassign_user_rol(self, user_id, role_id): + unassignments = [{"roleid": role_id, "userid": user_id, "contextlevel": 'system', "instanceid": 0}] + return self.call("core_role_unassign_roles", unassignments=unassignments) # def add_role_to_user(self, user_id, role='admin', context='missing'): # if role=='admin': diff --git a/dd-sso/admin/src/admin/lib/postup.py b/dd-sso/admin/src/admin/lib/postup.py index 0e0107f..03e0894 100644 --- a/dd-sso/admin/src/admin/lib/postup.py +++ b/dd-sso/admin/src/admin/lib/postup.py @@ -1,6 +1,7 @@ # # Copyright © 2021,2022 IsardVDI S.L. # Copyright © 2022 Evilham +# Copyright © 2022 Elena Barrios Galán @elena61 # # This file is part of DD # @@ -46,7 +47,7 @@ class Postup: while not ready: try: self.pg = Postgres( - "dd-apps-postgresql", + "isard-apps-postgresql", "moodle", app.config["MOODLE_POSTGRES_USER"], app.config["MOODLE_POSTGRES_PASSWORD"], @@ -190,6 +191,7 @@ class Postup: (3, 'core_cohort_search_cohorts'), (3, 'core_cohort_update_cohorts'), (3, 'core_role_assign_roles'), + (3, 'core_role_unassign_roles'), (3, 'core_cohort_get_cohorts');""" )