fix(admin): jwt api checks if new users groups exist

darta 2022-05-16 10:12:11 +02:00
parent b82cf79224
commit 5d9231d724
4 changed files with 42 additions and 19 deletions

View File

@ -162,7 +162,7 @@ class Admin:
ddmail, ddmail,
ddpassword, ddpassword,
group="admin", group="admin",
temporary=False, password_temporary=False,
) )
self.keycloak.assign_realm_roles(uid, "admin") self.keycloak.assign_realm_roles(uid, "admin")
log.warning("KEYCLOAK: OK") log.warning("KEYCLOAK: OK")
@ -632,7 +632,7 @@ class Admin:
"gids": pathslist, "gids": pathslist,
"quota": u["quota"], "quota": u["quota"],
"roles": [u["role"].strip()], "roles": [u["role"].strip()],
"temporary": True "password_temporary": True
if u["password_temporal"].lower() == "yes" if u["password_temporal"].lower() == "yes"
else False, else False,
"password": self.get_dice_pwd() "password": self.get_dice_pwd()
@ -803,7 +803,7 @@ class Admin:
u["last"], u["last"],
u["email"], u["email"],
u["password"], u["password"],
temporary=u["temporary"], password_temporary=u["password_temporary"],
) )
self.av.add_user_default_avatar(uid, u["roles"][0]) self.av.add_user_default_avatar(uid, u["roles"][0])
# Add user to role and group rolename # Add user to role and group rolename
@ -1296,8 +1296,8 @@ class Admin:
externaluser["gids"].append(data["action"]) externaluser["gids"].append(data["action"])
return True return True
def user_update_password(self, userid, password, temporary): def user_update_password(self, userid, password, password_temporary):
return self.keycloak.update_user_pwd(userid, password, temporary) return self.keycloak.update_user_pwd(userid, password, password_temporary)
def update_users_from_keycloak(self): def update_users_from_keycloak(self):
kgroups = self.keycloak.get_groups() kgroups = self.keycloak.get_groups()
@ -1700,6 +1700,22 @@ class Admin:
pathpart = pathpart + "." + part pathpart = pathpart + "." + part
pathslist.append(pathpart) pathslist.append(pathpart)
for path in pathslist:
path = "/" + path.replace(".", "/")
log.warning(
" KEYCLOAK USERS: Assign user " + u["username"] + " to group " + path
)
try:
gid = self.keycloak.get_group_by_path(path=path)["id"]
except:
return False
# gid = self.keycloak.add_group_tree(path)
# log.warning("THE PATH "+str(path)+" HAS GID "+str(gid))
# self.moodle.add_system_cohort(path)
# self.nextcloud.add_group(path)
# self.resync_data()
# gid = self.keycloak.get_group_by_path(path=path)["id"]
### KEYCLOAK ### KEYCLOAK
####################### #######################
ev = Events("Add user", u["username"], total=5) ev = Events("Add user", u["username"], total=5)
@ -1711,18 +1727,14 @@ class Admin:
u["email"], u["email"],
u["password"], u["password"],
enabled=u["enabled"], enabled=u["enabled"],
password_temporary=u.get("password_temporary", True),
) )
self.av.add_user_default_avatar(uid, u["role"]) self.av.add_user_default_avatar(uid, u["role"])
# Add user to role and group rolename # Add user to role and group rolename
log.warning( log.warning(
" KEYCLOAK USERS: Assign user " " KEYCLOAK USERS: Assign user " + u["username"] + " to role " + u["role"]
+ u["username"]
+ " with initial pwd "
+ u["password"]
+ " to role "
+ u["role"]
) )
self.keycloak.assign_realm_roles(uid, u["role"]) self.keycloak.assign_realm_roles(uid, u["role"])
gid = self.keycloak.get_group_by_path(path="/" + u["role"])["id"] gid = self.keycloak.get_group_by_path(path="/" + u["role"])["id"]
@ -1731,9 +1743,6 @@ class Admin:
# Add user to groups # Add user to groups
for path in pathslist: for path in pathslist:
path = "/" + path.replace(".", "/") path = "/" + path.replace(".", "/")
log.warning(
" KEYCLOAK USERS: Assign user " + u["username"] + " to group " + path
)
gid = self.keycloak.get_group_by_path(path=path)["id"] gid = self.keycloak.get_group_by_path(path=path)["id"]
self.keycloak.group_user_add(uid, gid) self.keycloak.group_user_add(uid, gid)
ev.increment({"name": "Added to system groups", "data": []}) ev.increment({"name": "Added to system groups", "data": []})

View File

@ -152,7 +152,7 @@ class KeycloakClient:
email, email,
password, password,
group=False, group=False,
temporary=True, password_temporary=True,
enabled=True, enabled=True,
): ):
# RETURNS string with keycloak user id (the main id in this app) # RETURNS string with keycloak user id (the main id in this app)
@ -167,7 +167,11 @@ class KeycloakClient:
"firstName": first, "firstName": first,
"lastName": last, "lastName": last,
"credentials": [ "credentials": [
{"type": "password", "value": password, "temporary": temporary} {
"type": "password",
"value": password,
"temporary": password_temporary,
}
], ],
} }
) )
@ -186,11 +190,11 @@ class KeycloakClient:
self.keycloak_admin.group_user_add(uid, gid) self.keycloak_admin.group_user_add(uid, gid)
return uid return uid
def update_user_pwd(self, user_id, password, temporary=True): def update_user_pwd(self, user_id, password, password_temporary=True):
# Updates # Updates
payload = { payload = {
"credentials": [ "credentials": [
{"type": "password", "value": password, "temporary": temporary} {"type": "password", "value": password, "temporary": password_temporary}
] ]
} }
self.connect() self.connect()

View File

@ -13,6 +13,10 @@ email:
password: password:
required: true required: true
type: string type: string
password_temporary:
required: false
type: boolean
default: true
quota: quota:
required: true required: true
type: string type: string

View File

@ -172,7 +172,13 @@ def ddapi_user(user_ddid=None):
if app.admin.get_user_username(data["username"]): if app.admin.get_user_username(data["username"]):
raise Error("conflict", "User id already exists") raise Error("conflict", "User id already exists")
data = app.validators["user"].normalized(data)
keycloak_id = app.admin.add_user(data) keycloak_id = app.admin.add_user(data)
if not keycloak_id:
raise Error(
"precondition_required",
"Not all user groups already in system. Please create user groups before adding user.",
)
return ( return (
json.dumps({"keycloak_id": keycloak_id}), json.dumps({"keycloak_id": keycloak_id}),
200, 200,