/
mruser.py
executable file
·101 lines (82 loc) · 3.97 KB
/
mruser.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
#!/usr/bin/env python
#
## mruser
##
## This program provides an interface to retrieve information about Moira users.
## It could also provide the writing functions, but the developer of this software does
## not have access to test that functionality nor is insane enough to set up a test Moira
## server.
#
from pymoira import User
import common, ownership
import colorama
def format_user_status(user):
statuses = {
0 : ('Registerable', 'blue', False),
1 : ('Active', 'green', False),
2 : ('Half-registered', 'blue', True ),
3 : ('Deleted', 'red', False),
4 : ('Non-registerable', None, False),
5 : ('Enrolled -- Registerable', 'cyan', False),
6 : ('Enrolled -- Unregisterable', None, False),
7 : ('Half-enrolled', None, False),
8 : ('Registered -- Kerberos-only', 'yellow', False),
9 : ('Active -- Kerberos-only', 'yellow', False),
10 : ('Supsended', 'red', True ),
11 : ('Expired', 'red', True ),
12 : ('Expired -- Kerberos-only', 'red', True ),
}
if user.status not in statuses:
return "Unknown status code (%i)" % user.status
text, color, bold = statuses[user.status]
if color: text = common.color_text(text, color)
if bold : text = common.emph_text(text)
if user.status in {User.Registerable, User.HalfRegistered}:
if user.secure:
text += " (needs secure Account Coupun)"
else:
text += " (no Account Coupon needed)"
return text
def show_info():
"""Handle 'mruser info'."""
user = User(client, args.user)
user.loadInfo()
if user.middle_name:
realname = "%s %s %s" % (user.first_name, user.middle_name, user.last_name)
else:
realname = "%s %s" % (user.first_name, user.last_name)
common.section_header( "Information about user %s" % common.emph_text(user.name) )
common.show_fields(
('Login name', user.name),
('Real name', realname),
('Status', format_user_status(user)),
('User ID', user.uid),
('MIT ID', user.mit_id),
('Class', user.user_class),
('Shell (Unix)', user.shell),
('Shell (Windows)', user.windows_shell),
('Comments', user.comments) if user.comments else None,
('Sponsor', str(user.sponsor) if user.sponsor else 'None'),
('Expires', user.expiration) if user.expiration else None,
('Alternate email', user.alternate_email) if user.alternate_email else None,
('Alternate phone', user.alternate_phone) if user.alternate_phone else None,
('Created', "%s by %s" % (common.last_modified_date(user.created_date), user.created_by)),
('Last modified', "%s by %s using %s" % (common.last_modified_date(user.lastmod_datetime), user.lastmod_by, user.lastmod_with)),
)
def show_ownerships():
"""Handle 'mruser ownerships'."""
user = User(client, args.user)
ownership.show_ownerships(client, args, user)
def setup_subcommands(argparser):
"""Sets up all the subcommands."""
subparsers = argparser.add_subparsers()
parser_info = subparsers.add_parser('info', help = 'Provide the information about the user')
parser_info.add_argument('user', help = 'The user to inspect')
parser_ownerships = subparsers.add_parser('ownerships', help = 'Show items which this user owns')
parser_ownerships.add_argument('user', help = 'The name of the user to show information about')
parser_ownerships.add_argument('-r', '--recursive', action = 'store_true', help = 'Show items which this user own through being in lists')
parser_info.set_defaults(handler = show_info)
parser_ownerships.set_defaults(handler = show_ownerships)
if __name__ == '__main__':
client, args = common.init('mruser', 'Inspect Moira users', setup_subcommands)
common.main()