1#
2# Copyright 2008 Google Inc. All Rights Reserved.
3
4"""
5The user module contains the objects and methods used to
6manage users in Autotest.
7
8The valid action is:
9list:    lists user(s)
10
11The common options are:
12--ulist / -U: file containing a list of USERs
13
14See topic_common.py for a High Level Design and Algorithm.
15"""
16
17import os, sys
18from autotest_lib.cli import topic_common, action_common
19
20
21class user(topic_common.atest):
22    """User class
23    atest user list <options>"""
24    usage_action = 'list'
25    topic = msg_topic = 'user'
26    msg_items = '<users>'
27
28    def __init__(self):
29        """Add to the parser the options common to all the
30        user actions"""
31        super(user, self).__init__()
32
33        self.parser.add_option('-U', '--ulist',
34                               help='File listing the users',
35                               type='string',
36                               default=None,
37                               metavar='USER_FLIST')
38
39        self.topic_parse_info = topic_common.item_parse_info(
40            attribute_name='users',
41            filename_option='ulist',
42            use_leftover=True)
43
44
45    def get_items(self):
46        return self.users
47
48
49class user_help(user):
50    """Just here to get the atest logic working.
51    Usage is set by its parent"""
52    pass
53
54
55class user_list(action_common.atest_list, user):
56    """atest user list <user>|--ulist <file>
57    [--acl <ACL>|--access_level <n>]"""
58    def __init__(self):
59        super(user_list, self).__init__()
60
61        self.parser.add_option('-a', '--acl',
62                               help='Only list users within this ACL')
63
64        self.parser.add_option('-l', '--access_level',
65                               help='Only list users at this access level')
66
67
68    def parse(self):
69        (options, leftover) = super(user_list, self).parse()
70        self.acl = options.acl
71        self.access_level = options.access_level
72        return (options, leftover)
73
74
75    def execute(self):
76        filters = {}
77        check_results = {}
78        if self.acl:
79            filters['aclgroup__name__in'] = [self.acl]
80            check_results['aclgroup__name__in'] = None
81
82        if self.access_level:
83            filters['access_level__in'] = [self.access_level]
84            check_results['access_level__in'] = None
85
86        if self.users:
87            filters['login__in'] = self.users
88            check_results['login__in'] = 'login'
89
90        return super(user_list, self).execute(op='get_users',
91                                              filters=filters,
92                                              check_results=check_results)
93
94
95    def output(self, results):
96        if self.verbose:
97            keys = ['id', 'login', 'access_level']
98        else:
99            keys = ['login']
100
101        super(user_list, self).output(results, keys)
102