Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added script to see the user roles difs from two user lists #133

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions DHIS2/userpermissionsdiff/create_csv_with_roles_diff_from_users.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import json
import csv
import requests
from requests.auth import HTTPBasicAuth

server = 'http://localhost:8080/api/userRoles/{}.json'
serverusername = ''
password = ''
def get_names_by_id(uid, server, user, password):
url = server.format(uid)
payload = {}
response = requests.request("GET", url, data=payload, auth=HTTPBasicAuth(user,password))

if response.status_code == 200:
name = response.json().get('name')
return name
else:
return "name not found"


# Load the JSON files
with open('users1.json', 'r') as file:
data1 = json.load(file)
with open('users2.json', 'r') as file:
data2 = json.load(file)

# Adjusted the variable assignments here according to your later clarification
users1 = {user['username']: user for user in data2}
users2 = {user['username']: user for user in data1}

differences = []


for username, user in users1.items():
if username in users2:
ids1 = {role['id'] for role in user.get('userRoles', [])}
ids2 = {role['id'] for role in users2[username].get('userRoles', [])}

diff_ids = ids1.difference(ids2)

if diff_ids:
names = [get_names_by_id(uid, server, serverusername, password) for uid in diff_ids]
differences.append({
'username': username,
'missing_ids_in_json2': ', '.join(diff_ids),
'names': ', '.join(names)
})

for username, user in users2.items():
if username not in users1:
ids2 = {role['id'] for role in user.get('userRoles', [])}
differences.append({'username': username, 'missing_ids_in_json2': ', '.join(ids2), 'names': 'N/A'})

with open('differences.csv', 'w', newline='') as file:
fieldnames = ['username', 'missing_ids_in_json2', "names"]
writer = csv.DictWriter(file, fieldnames=fieldnames)
writer.writeheader()
for row in differences:
writer.writerow(row)

print("The CSV file with the differences has been created.")