forked from wrosenauer/oxcloud-provisioning
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlistuser.py
executable file
·127 lines (107 loc) · 5.5 KB
/
listuser.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#!/usr/bin/python3
# Copyright (C) 2023 OX Software GmbH
# Wolfgang Rosenauer
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import argparse
import requests
import settings
import soapclient
def main():
parser = argparse.ArgumentParser(
description='List users in an OX Cloud context.')
parser.add_argument("-n", dest="context_name", help="Context name.")
parser.add_argument("-c", "--cid", help="Context ID.", type=int)
parser.add_argument(
"-s", "--search", help="Search pattern to limit output.")
parser.add_argument(
"--skip-acn", help="Skip extraction of ACN.", action="store_true")
parser.add_argument("--skip-cos", help="Skip COS.", action="store_true")
parser.add_argument("--skip-spamlevel",
help="Skip spamlevel.", action="store_true")
parser.add_argument(
"-d", "--dump", help="Dump raw object.", action="store_true")
parser.add_argument("--includeguests",
help="Include guests.", action="store_true")
args = parser.parse_args()
if args.context_name is None and args.cid is None:
parser.error("Context must be specified by either -n or -c !")
ctx = {}
if args.cid is not None:
ctx["id"] = args.cid
else:
ctx["name"] = settings.getCreds()["login"] + "_" + args.context_name
contextService = soapclient.getService("OXResellerContextService")
ctx = contextService.getData(ctx, settings.getCreds())
oxaasService = soapclient.getService("OXaaSService")
userService = soapclient.getService("OXResellerUserService")
if args.search is not None:
users = userService.listCaseInsensitive(
ctx, "*"+args.search+"*", settings.getCreds())
else:
users = userService.listAll(
ctx, settings.getCreds(), args.includeguests)
users = userService.getMultipleData(
ctx, users, settings.getCreds())
# code.interact(local=locals())
print("{:<3} {:<40} {:<30} {:<12} {:<12} {:<15} {:<20} {:<15}".format(
'UID', 'Name', 'Primary email', 'File Quota', 'Mail Quota', 'ACN', 'COS', 'Spamlevel'))
for user in users:
cos = '<skipped>'
acn = "<skipped>"
spamlevel = '<skipped>'
# fetching COS via SOAP cannot be trusted therefore use REST below
# for userAttributes in user.userAttributes.entries:
# # find COS in array (currently cloud should only have one entry)
# if userAttributes['key'] == 'cloud':
# cos = userAttributes['value'].entries[0]['value']
if not args.dump:
if user.id != 2:
if not args.skip_cos:
cos = "unset"
r = requests.get(settings.getRestHost()+"oxaas/v1/admin/contexts/"+str(
ctx.id)+"/users/"+str(user.id)+"/classofservice", auth=(settings.getRestCreds()), verify=settings.getVerifyTls())
if r.status_code == 200:
if r.json()['classofservice'] != '':
cos = r.json()['classofservice']
if not args.skip_acn:
acn = userService.getAccessCombinationName(
ctx, user, settings.getCreds())
if not args.skip_spamlevel:
r = requests.get(settings.getRestHost()+"oxaas/v1/admin/contexts/"+str(
ctx.id)+"/users/"+str(user.id)+"/spamlevel", auth=(settings.getRestCreds()), verify=settings.getVerifyTls())
if r.status_code == 200:
if r.json()['spamlevel'] != '':
spamlevel = r.json()['spamlevel']
try:
# fails for Guest users w/o LDAP entry
mailquota = oxaasService.getMailQuota(
ctx.id, user.id, settings.getCreds())
# can error if Dovecot has no mailbox/quotausage yet OPS-13238
mailquotaUsage = oxaasService.getQuotaUsagePerUser(
ctx.id, user.id, settings.getCreds())
mailquotaUsage = str(round(mailquotaUsage.storage/1024))
except:
mailquota = "-"
mailquotaUsage = "-"
print("{:<3} {:<40} {:<30} {:<12} {:<12} {:<15} {:<20} {:<15}".format(
user.id, user.name, user.primaryEmail, str(user.usedQuota) + "/" + str(user.maxQuota), mailquotaUsage + "/" + str(mailquota), str(acn), ','.join(cos), spamlevel))
else:
print("{:<3} {:<40} {:<30} {:<12} {:<12} {:<15} {:<20} {:<15}".format(
user.id, user.name, user.primaryEmail, str(user.usedQuota) + "/" + str(user.maxQuota), str(acn), "n/a", "n/a", "n/a"))
else:
print(user)
permissions = userService.getModuleAccess(ctx, user, settings.getCreds())
print(permissions)
if __name__ == "__main__":
main()