main.py: finish app group syncing

This commit is contained in:
Antoine Martin 2024-03-05 16:16:50 -05:00
parent 3bd5f39d9c
commit 9f4d911a5d
Signed by: forge
GPG key ID: D62A472A4AA7D541

36
main.py
View file

@ -25,12 +25,12 @@ def authentik_get_users():
json_object = json.loads(resp._content) json_object = json.loads(resp._content)
return(json_object['results']) return(json_object['results'])
def authentik_get_app_used_by(slug): def authentik_get_user_consent(id):
url = authentik_api_url + '/core/applications/' + slug + '/used_by/' url = authentik_api_url + '/core/user_consent/?user=' + str(id)
headers = {'accept': 'application/json', 'Authorization': "Bearer " + authentik_api_key} headers = {'accept': 'application/json', 'Authorization': "Bearer " + authentik_api_key}
resp = requests.get(url, headers=headers) resp = requests.get(url, headers=headers)
json_object = json.loads(resp._content) json_object = json.loads(resp._content)
return(json_object) return(json_object['results'])
def authentik_get_groups(): def authentik_get_groups():
url = authentik_api_url + '/core/groups/' url = authentik_api_url + '/core/groups/'
@ -109,6 +109,18 @@ for group_info in authentik_get_groups():
print(create_resp) print(create_resp)
continue continue
# sync that apps exists as lists
for app_info in authentik_get_apps():
print(app_info)
try:
list_info = listmonk_get_list('app-' + app_info['slug'])
except Exception as e:
print('> Failed to find app as list, creating app-' + app_info['slug'])
create_resp = listmonk_create_list('app-' + app_info['slug'])
print(create_resp)
continue
# ensure that users are synced # ensure that users are synced
for user_info in authentik_get_users(): for user_info in authentik_get_users():
print('>>> Checking user ' + user_info['username']) print('>>> Checking user ' + user_info['username'])
@ -154,6 +166,12 @@ for user_info in authentik_get_users():
list_info = listmonk_get_list('group-' + group_info['name']) list_info = listmonk_get_list('group-' + group_info['name'])
groups_id.append(list_info['id']) groups_id.append(list_info['id'])
# matches apps to list_id
for consent_info in authentik_get_user_consent(user_info['pk']):
print(consent_info['application']['slug'])
list_info = listmonk_get_list('app-' + consent_info['application']['slug'])
groups_id.append(list_info['id'])
# checks if groups_id is in list of actual lists_id # checks if groups_id is in list of actual lists_id
# this will fail if user is in a group, but subscriber is not in the respective list # this will fail if user is in a group, but subscriber is not in the respective list
for id in groups_id: for id in groups_id:
@ -170,16 +188,4 @@ for user_info in authentik_get_users():
print('> User groups check failed, updating') print('> User groups check failed, updating')
set_resp = listmonk_set_list([subscriber_info['id']], "remove", [id], status="") set_resp = listmonk_set_list([subscriber_info['id']], "remove", [id], status="")
# sync used_by with users
for app_info in authentik_get_apps():
try:
list_info = listmonk_get_list('app-' + app_info['slug'])
except Exception as e:
print('> Failed to find app as list, creating app-' + app_info['slug'])
create_resp = listmonk_create_list('app-' + app_info['slug'])
print(create_resp)
continue
for used_by in authentik_get_app_used_by(app_info['slug']):
print(used_by)