06da9f5b5006a995ff2af3e7a53832e0a5f92ebf
[plcapi.git] / PLC / Methods / GetPersons.py
1 from PLC.Faults import *
2 from PLC.Method import Method
3 from PLC.Parameter import Parameter, Mixed
4 from PLC.Filter import Filter
5 from PLC.Persons import Person, Persons
6 from PLC.Sites import Site, Sites
7 from PLC.Auth import Auth
8
9 hidden_fields = ['password', 'verification_key', 'verification_expires']
10
11 class GetPersons(Method):
12     """
13     Returns an array of structs containing details about users. If
14     person_filter is specified and is an array of user identifiers or
15     usernames, or a struct of user attributes, only users matching the
16     filter will be returned. If return_fields is specified, only the
17     specified details will be returned.
18
19     Users and techs may only retrieve details about themselves. PIs
20     may retrieve details about themselves and others at their
21     sites. Admins and nodes may retrieve details about all accounts.
22     """
23
24     roles = ['admin', 'pi', 'user', 'tech', 'node']
25
26     accepts = [
27         Auth(),
28         Mixed([Mixed(Person.fields['person_id'],
29                      Person.fields['email'])],
30               Parameter(str,"email"),
31               Parameter(int,"person_id"),
32               Filter(Person.fields)),
33         Parameter([str], "List of fields to return", nullok = True)
34         ]
35
36     # Filter out password field
37     return_fields = dict(filter(lambda (field, value): field not in hidden_fields,
38                                 Person.fields.items()))
39     returns = [return_fields]
40
41     def call(self, auth, person_filter = None, return_fields = None):
42         # If we are not admin, make sure to only return viewable accounts
43         if isinstance(self.caller, Person) and \
44            'admin' not in self.caller['roles']:
45             # Get accounts that we are able to view
46             valid_person_ids = [self.caller['person_id']]
47             if ('pi' in self.caller['roles'] or 'tech' in self.caller['roles']) \
48                and self.caller['site_ids']:
49                 sites = Sites(self.api, self.caller['site_ids'])
50                 for site in sites:
51                     valid_person_ids += site['person_ids']
52             if not valid_person_ids:
53                 return []
54
55             # this may look suspicious; what if person_filter is not None ?
56             # turns out the results are getting filtered again below, so we're safe
57             # although this part of the code does not always trigger, it's probably 
58             # a sensible performance enhancement for all the times 
59             # when GetPersons() gets called without an argument
60             if person_filter is None:
61                 person_filter = valid_person_ids
62
63         # Filter out password field
64         if return_fields:
65             return_fields = filter(lambda field: field not in hidden_fields,
66                                    return_fields)
67         else:
68             return_fields = self.return_fields.keys()
69
70         # Must query at least person_id, site_ids, and role_ids (see
71         # Person.can_view() and below).
72         if return_fields is not None:
73             added_fields = set(['person_id', 'site_ids', 'role_ids','roles']).difference(return_fields)
74             return_fields += added_fields
75         else:
76             added_fields = []
77
78         persons = Persons(self.api, person_filter, return_fields)
79
80         # Filter out accounts that are not viewable
81         if isinstance(self.caller, Person) and \
82            'admin' not in self.caller['roles']:
83             persons = filter(self.caller.can_view, persons)
84
85         # Remove added fields if not specified
86         if added_fields:
87             for person in persons:
88                 for field in added_fields:
89                     if field in person:
90                         del person[field]
91
92         return persons