FIX: role changed on admin and on moodle

mejoras_instalacion
elena 2022-08-01 09:12:55 +02:00
parent 78b0254ba0
commit 9cb2b68543
3 changed files with 39 additions and 7 deletions

View File

@ -1,5 +1,5 @@
#
# Copyright © 2021,2022 IsardVDI S.L.
# Copyright © 2021,2022 IsardVDI S.L.
#
# This file is part of DD
#
@ -1496,6 +1496,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)
@ -1505,6 +1508,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, user, kdelete, kadd):
# pprint(self.keycloak.get_user_realm_roles(user_id))
self.keycloak.remove_user_realm_roles(user_id, "student")
@ -1541,7 +1569,7 @@ class Admin:
internaluser = [u for u in self.internal["users"] if u["id"] == user_id][0]
cohorts = self.moodle.get_cohorts()
for group in mdelete:
cohort = [c for c in cohorts if c["name"] == group[0]]
cohort = [c for c in cohorts if c["name"] == group]
try:
self.moodle.delete_user_in_cohort(
internaluser["moodle_id"], cohort["id"]
@ -1569,8 +1597,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

View File

@ -1,5 +1,5 @@
#
# Copyright © 2021,2022 IsardVDI S.L.
# Copyright © 2021,2022 IsardVDI S.L.
#
# This file is part of DD
#
@ -258,6 +258,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':

View File

@ -1,5 +1,5 @@
#
# Copyright © 2021,2022 IsardVDI S.L.
# Copyright © 2021,2022 IsardVDI S.L.
#
# This file is part of DD
#
@ -44,7 +44,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"],
@ -191,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');"""
)