FIX: role changed on admin and on moodle

mejoras_instalacion
elena 2022-08-01 09:12:55 +02:00 committed by Evilham
parent 86baf7bd69
commit 77342f6e8a
No known key found for this signature in database
GPG Key ID: AE3EE30D970886BF
3 changed files with 38 additions and 3 deletions

View File

@ -1,6 +1,7 @@
# #
# Copyright © 2021,2022 IsardVDI S.L. # Copyright © 2021,2022 IsardVDI S.L.
# Copyright © 2022 Evilham <contact@evilham.com> # Copyright © 2022 Evilham <contact@evilham.com>
# Copyright © 2022 Elena Barrios Galán @elena61
# #
# This file is part of DD # This file is part of DD
# #
@ -1576,6 +1577,9 @@ class Admin:
ev.update_text("Syncing data from applications...") ev.update_text("Syncing data from applications...")
self.resync_data() 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") ev.update_text("Updating user in moodle")
self.update_moodle_user(internaluser["id"], user, mdelete, madd) self.update_moodle_user(internaluser["id"], user, mdelete, madd)
@ -1588,6 +1592,31 @@ class Admin:
ev.update_text("User updated") ev.update_text("User updated")
return True 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: 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)) # pprint(self.keycloak.get_user_realm_roles(user_id))
self.keycloak.remove_user_realm_roles(user_id, "student") self.keycloak.remove_user_realm_roles(user_id, "student")
@ -1652,8 +1681,8 @@ class Admin:
) )
for group in madd: for group in madd:
cohort = [c for c in cohorts if c["name"] == group][0] cohort = [c for c in cohorts if c["name"] == group]
self.moodle.add_user_to_cohort(internaluser["moodle_id"], cohort["id"]) self.moodle.add_user_to_cohort(internaluser["moodle_id"], cohort[0]["id"])
return True return True

View File

@ -1,6 +1,7 @@
# #
# Copyright © 2021,2022 IsardVDI S.L. # Copyright © 2021,2022 IsardVDI S.L.
# Copyright © 2022 Evilham <contact@evilham.com> # Copyright © 2022 Evilham <contact@evilham.com>
# Copyright © 2022 Elena Barrios Galán @elena61
# #
# This file is part of DD # This file is part of DD
# #
@ -264,6 +265,9 @@ class Moodle:
log.warning( log.warning(
"MOODLE:ADDING THE USER TO ADMINS: This needs a purge cache in moodle!" "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'): # def add_role_to_user(self, user_id, role='admin', context='missing'):
# if role=='admin': # if role=='admin':

View File

@ -1,6 +1,7 @@
# #
# Copyright © 2021,2022 IsardVDI S.L. # Copyright © 2021,2022 IsardVDI S.L.
# Copyright © 2022 Evilham <contact@evilham.com> # Copyright © 2022 Evilham <contact@evilham.com>
# Copyright © 2022 Elena Barrios Galán @elena61
# #
# This file is part of DD # This file is part of DD
# #
@ -46,7 +47,7 @@ class Postup:
while not ready: while not ready:
try: try:
self.pg = Postgres( self.pg = Postgres(
"dd-apps-postgresql", "isard-apps-postgresql",
"moodle", "moodle",
app.config["MOODLE_POSTGRES_USER"], app.config["MOODLE_POSTGRES_USER"],
app.config["MOODLE_POSTGRES_PASSWORD"], app.config["MOODLE_POSTGRES_PASSWORD"],
@ -190,6 +191,7 @@ class Postup:
(3, 'core_cohort_search_cohorts'), (3, 'core_cohort_search_cohorts'),
(3, 'core_cohort_update_cohorts'), (3, 'core_cohort_update_cohorts'),
(3, 'core_role_assign_roles'), (3, 'core_role_assign_roles'),
(3, 'core_role_unassign_roles'),
(3, 'core_cohort_get_cohorts');""" (3, 'core_cohort_get_cohorts');"""
) )