1
+ HOST = "tolgee.marticliment.com"
2
+ TOKEN = ""
3
+ import requests , json , getpass
4
+
5
+ def Get_JWTToken ():
6
+ url = f"https://{ HOST } /api/public/generatetoken"
7
+ payload = json .dumps ({
8
+ "username" : input ("Username: " ),
9
+ "password" : getpass .getpass (),
10
+ "otp" : "string"
11
+ })
12
+ headers = {
13
+ 'Content-Type' : 'application/json' ,
14
+ 'Accept' : 'application/json'
15
+ }
16
+ response = requests .request ("POST" , url , headers = headers , data = payload )
17
+ return (json .loads (response .text )["accessToken" ])
18
+
19
+
20
+ def Get_AllUsers (page : int = 0 ) -> list [dict ]:
21
+ url = f"https://{ HOST } /v2/administration/users"
22
+
23
+ payload = {}
24
+ query = {
25
+ 'page' : str (page )
26
+ }
27
+ headers = {
28
+ 'Accept' : 'application/json' ,
29
+ 'Authorization' : f'Bearer { TOKEN } ' ,
30
+ }
31
+
32
+ response = requests .request ("GET" , url , headers = headers , data = payload , params = query )
33
+
34
+ if (response .status_code != 200 ):
35
+ print (response .text )
36
+ return []
37
+
38
+
39
+ res_content = json .loads (response .text )
40
+ MAX_PAGE = int (res_content ["page" ]["totalPages" ])
41
+
42
+ if page + 1 == MAX_PAGE :
43
+ print (f"USER: Loaded page { page + 1 } out of { MAX_PAGE } " )
44
+ return list (res_content ["_embedded" ]["users" ])
45
+ else :
46
+ print (f"USER: Loaded page { page + 1 } out of { MAX_PAGE } " )
47
+ return list (res_content ["_embedded" ]["users" ]) + Get_AllUsers (page + 1 )
48
+
49
+ def Get_AllUsersOnProject (project : int ) -> list [dict ]:
50
+ url = f"https://{ HOST } /v2/projects/{ project } /users"
51
+
52
+ payload = {}
53
+ query = {
54
+ 'page' : "0" ,
55
+ 'size' : "300" ,
56
+ }
57
+ headers = {
58
+ 'Accept' : 'application/json' ,
59
+ 'Authorization' : f'Bearer { TOKEN } ' ,
60
+ }
61
+
62
+ response = requests .request ("GET" , url , headers = headers , data = payload , params = query )
63
+
64
+ if (response .status_code != 200 ):
65
+ print (response .text )
66
+ return []
67
+
68
+ res_content = json .loads (response .text )
69
+ print (f"PROJ: Loaded active users for project" )
70
+ return list (res_content ["_embedded" ]["users" ])
71
+
72
+
73
+ def Delete_User (user_id : int ) -> bool :
74
+ url = f"https://{ HOST } /v2/administration/users/{ user_id } "
75
+
76
+ payload = {}
77
+ headers = {
78
+ 'Authorization' : f'Bearer { TOKEN } '
79
+ }
80
+
81
+ response = requests .request ("DELETE" , url , headers = headers , data = payload )
82
+
83
+ if (response .status_code != 200 ):
84
+ print (response .text )
85
+
86
+ return response .status_code == 200
87
+
88
+
89
+ print (f"Running for host { HOST } " )
90
+
91
+ TOKEN = Get_JWTToken ()
92
+
93
+ active_ids = []
94
+ for user in Get_AllUsersOnProject (1 ):
95
+ active_ids .append (int (user ["id" ]))
96
+ active_ids .sort ()
97
+
98
+ ids = []
99
+ for user in Get_AllUsers ():
100
+ ids .append (int (user ["id" ]))
101
+ ids .sort ()
102
+
103
+ INACTIVEs = []
104
+ for user in ids :
105
+ if user not in active_ids :
106
+ INACTIVEs .append (user )
107
+ INACTIVEs .sort ()
108
+
109
+ total = len (INACTIVEs )
110
+ SKIP_VERIFICATIION = ""
111
+ input (f"Press <ENTER> to confirm deletion of { total } users (users in server are { len (ids )} , active users are { len (active_ids )} ): " )
112
+
113
+ for i in range (total ):
114
+ user = INACTIVEs [i ]
115
+
116
+ if (user == "1" ):
117
+ continue
118
+
119
+ if SKIP_VERIFICATIION != "12" :
120
+ SKIP_VERIFICATIION = input ("About to delete User id=" + str (user ) + ". Press intro to proceed, type 12 to run all: " )
121
+
122
+ print (f"Deleting user id={ user } , ({ i + 1 } out of { total } )... " , end = "" , flush = True )
123
+ print ("SUCCESS" if Delete_User (user ) else "FAILED" )
0 commit comments