fix(admin): allow more than 2 group levels

darta 2022-02-07 01:59:47 +01:00
parent a091abee64
commit 49cd533a13
3 changed files with 38 additions and 73 deletions

View File

@ -32,7 +32,7 @@ import secrets
from .events import Events
from .exceptions import UserExists, UserNotFound
from .helpers import count_repeated, rand_password, kpath2gids
from .helpers import count_repeated, rand_password, kpath2gids, kpath2kpaths
MANAGER = os.environ["CUSTOM_ROLE_MANAGER"]
TEACHER = os.environ["CUSTOM_ROLE_TEACHER"]
@ -811,27 +811,15 @@ class Admin:
gid = self.keycloak.get_group_by_path("/" + u["roles"][0])["id"]
# self.keycloak.group_user_add(uid,gid)
# Add user to groups
for g in u["groups"]:
parts = g.split("/")
sub = ""
if len(parts) == 0:
log.warning(
" KEYCLOAK USERS: Skip assign user "
+ u["username"]
+ " to any group as does not have one"
)
continue # NO GROUP
for i in range(1, len(parts)):
sub = sub + "/" + parts[i]
if sub == "/":
continue # User with no path
for group in u["groups"]:
for g in kpath2kpaths(group):
log.warning(
" KEYCLOAK USERS: Assign user "
+ u["username"]
+ " to group "
+ str(sub)
+ str(g)
)
kuser = self.keycloak.get_group_by_path(path=sub)
kuser = self.keycloak.get_group_by_path(path=g)
gid = kuser["id"]
self.keycloak.group_user_add(uid, gid)
# We add it as it is needed for moodle and nextcloud
@ -841,7 +829,7 @@ class Admin:
def add_moodle_groups(self,groups):
### Create all groups. Skip / in system groups
total = len(groups)
log.error(groups)
log.warning(groups)
ev = Events("Syncing groups from external to moodle", total=len(groups))
i=1
for g in groups:
@ -858,7 +846,7 @@ class Admin:
)
self.moodle.add_system_cohort(mg)
except Exception as e:
log.error(
log.warning(
" MOODLE GROUPS: Group " + mg + " probably already exists"
)
i=i+1
@ -881,7 +869,7 @@ class Admin:
"Syncing users from external to moodle", total=len(self.internal["users"])
)
for u in self.external["users"]:
log.warning("MOODLE: Creating moodle user: " + u["username"])
log.warning(" MOODLE: Creating moodle user: " + u["username"])
ev.increment({"name": u["username"]})
if u["first"] == "":
u["first"] = "-"
@ -896,7 +884,7 @@ class Admin:
u["last"],
)[0]
except UserExists:
log.warning("MOODLE:The user: " + u["username"] + " already exsits.")
log.warning(" MOODLE: The user: " + u["username"] + " already exsits.")
except:
log.error(" -->> Error creating on moodle the user: " + u["username"])
log.error(traceback.format_exc())
@ -930,7 +918,7 @@ class Admin:
def add_nextcloud_groups(self,groups):
### Create all groups. Skip / in system groups
total = len(groups)
log.error(groups)
log.warning(groups)
ev = Events("Syncing groups from external to nextcloud", total=len(groups))
i=1
for g in groups:
@ -947,7 +935,7 @@ class Admin:
)
self.nextcloud.add_group(ng)
except Exception as e:
log.error(
log.warning(
" NEXTCLOUD GROUPS: Group " + ng + " probably already exists"
)
i=i+1
@ -966,7 +954,7 @@ class Admin:
)
for u in self.external["users"]:
log.warning(
" NEXTCLOUD USERS: Creating nextcloud user: "
" NEXTCLOUD USERS: Creating nextcloud user: "
+ u["username"]
+ " with quota "
+ str(u["quota"])
@ -984,7 +972,7 @@ class Admin:
u["first"] + " " + u["last"],
)
except ProviderItemExists:
log.warning("User " + u["username"] + " already exists. Skipping...")
log.warning(" NEXTCLOUD USERS: User " + u["username"] + " already exists. Skipping...")
continue
except:
log.error(traceback.format_exc())
@ -1012,7 +1000,7 @@ class Admin:
self.moodle.add_system_cohort(pathpart)
except:
# print(traceback.format_exc())
log.error("MOODLE: Group " + pathpart + " probably already exists.")
log.warning("MOODLE: Group " + pathpart + " probably already exists.")
### Get all existing moodle cohorts
cohorts = self.moodle.get_cohorts()
@ -1067,8 +1055,8 @@ class Admin:
try:
self.moodle.add_user_to_cohort(u["moodle_id"], cohort["id"])
except:
log.error(traceback.format_exc())
log.error(
# log.error(traceback.format_exc())
log.warning(
" MOODLE USER GROUPS: User "
+ u["username"]
+ " already exists in cohort "
@ -1110,7 +1098,7 @@ class Admin:
subpath = subpath + "/" + parts[i]
self.nextcloud.add_group(subpath)
except:
log.error("probably exists")
log.warning("NEXTCLOUD GROUPS: "+subpath+" probably already exists")
i = i + 1
ev = Events(
@ -1633,7 +1621,7 @@ class Admin:
password="*12" + secrets.token_urlsafe(16),
):
log.warning(
" NEXTCLOUD USERS: Creating nextcloud user: "
" NEXTCLOUD USERS: Creating nextcloud user: "
+ username
+ " in groups "
+ str(groups)
@ -1646,7 +1634,7 @@ class Admin:
)
ev.increment({"name": "Added to nextcloud", "data": []})
except ProviderItemExists:
log.warning("User " + username + " already exists. Skipping...")
log.warning(" NEXTCLOUD USERS: User " + username + " already exists. Skipping...")
except:
log.error(traceback.format_exc())

View File

@ -44,6 +44,12 @@ def kpath2gids(path):
l.append(".".join(path.split(".")[: i + 1]))
return l
def kpath2kpaths(path):
l = []
for i in range(len(path.split("/"))):
l.append("/".join(path.split("/")[: i + 1]))
return l[1:]
def gid2kpath(gid):
return "/" + gid.replace(".", "/")

View File

@ -11,6 +11,7 @@ from pprint import pprint
import yaml
from jinja2 import Environment, FileSystemLoader
from keycloak import KeycloakAdmin
from .helpers import kpath2kpaths
# from admin import app
@ -345,48 +346,18 @@ class KeycloakClient:
return self.keycloak_admin.group_user_add(user_id, group_id)
def add_group_tree(self, path):
parts = path.split("/")
parent_path = "/"
for i in range(1, len(parts)):
if i == 1:
try:
self.add_group(parts[i], None, skip_exists=True)
except:
log.warning("KEYCLOAK: Group :" + parts[i] + " already exists.")
parent_path = parent_path + parts[i]
else:
try:
self.add_group(parts[i], parent_path, skip_exists=True)
except:
log.warning("KEYCLOAK: Group :" + parts[i] + " already exists.")
parent_path = parent_path + parts[i]
# parts=path.split('/')
# parent_path=None
# for i in range(1,len(parts)):
# # print('Adding group name '+parts[i]+' with parent path '+str(parent_path))
# try:
# self.add_group(parts[i],parent_path,skip_exists=True)
# except:
# if parent_path==None:
# parent_path='/'+parts[i]
# else:
# parent_path=self.get_group_by_path(parent_path)['path']
# parent_path=parent_path+'/'+parts[i]
# continue
# if parent_path==None:
# parent_path='/'+parts[i]
# else:
# parent_path=parent_path+'/'+parts[i]
# try:
# if i == 1: parent_id=self.add_group(parts[i])
# except:
# # Main already exists?? What a fail!
# parent_id=self.get_group(parent_id)['id']
# continue
# self.add_group(parts[i],parent_id)
paths = kpath2kpaths(path)
parent="/"
for path in paths:
try:
parent_path=None if parent=="/" else parent
# print("parent: "+str(parent_path)+" path: "+path.split("/")[-1])
self.add_group(path.split("/")[-1], parent_path, skip_exists=True)
parent=path
except:
# print(traceback.format_exc())
log.warning("KEYCLOAK: Group :" + path + " already exists.")
parent=path
def add_user_with_groups_and_role(
self, username, first, last, email, password, role, groups