2 from PLC.Faults import *
3 from PLC.Method import Method
4 from PLC.Parameter import Parameter, Mixed
5 from PLC.Filter import Filter
6 from PLC.Persons import Person, Persons
7 from PLC.Sites import Site, Sites
8 from PLC.Auth import Auth
10 hidden_fields = ['password', 'verification_key', 'verification_expires']
12 class GetPersons(Method):
14 Returns an array of structs containing details about users. If
15 person_filter is specified and is an array of user identifiers or
16 usernames, or a struct of user attributes, only users matching the
17 filter will be returned. If return_fields is specified, only the
18 specified details will be returned.
20 Users and techs may only retrieve details about themselves. PIs
21 may retrieve details about themselves and others at their
22 sites. Admins and nodes may retrieve details about all accounts.
25 roles = ['admin', 'pi', 'user', 'tech', 'node']
29 Mixed([Mixed(Person.fields['person_id'],
30 Person.fields['email'])],
31 Parameter(str,"email"),
32 Parameter(int,"person_id"),
33 Filter(Person.fields)),
34 Parameter([str], "List of fields to return", nullok = True)
37 # Filter out password field
38 return_fields = dict(filter(lambda (field, value): field not in hidden_fields,
39 Person.fields.items()))
40 returns = [return_fields]
42 def call(self, auth, person_filter = None, return_fields = None):
43 # If we are not admin, make sure to only return viewable accounts
44 if isinstance(self.caller, Person) and \
45 'admin' not in self.caller['roles']:
46 # Get accounts that we are able to view
47 valid_person_ids = [self.caller['person_id']]
48 if 'pi' in self.caller['roles'] and self.caller['site_ids']:
49 sites = Sites(self.api, self.caller['site_ids'])
51 valid_person_ids += site['person_ids']
53 if not valid_person_ids:
56 if person_filter is None:
57 person_filter = valid_person_ids
59 # Filter out password field
61 return_fields = filter(lambda field: field not in hidden_fields,
64 return_fields = self.return_fields.keys()
66 # Must query at least person_id, site_ids, and role_ids (see
67 # Person.can_view() and below).
68 if return_fields is not None:
69 added_fields = set(['person_id', 'site_ids', 'role_ids']).difference(return_fields)
70 return_fields += added_fields
74 persons = Persons(self.api, person_filter, return_fields)
76 # Filter out accounts that are not viewable
77 if isinstance(self.caller, Person) and \
78 'admin' not in self.caller['roles']:
79 persons = filter(self.caller.can_view, persons)
81 # Remove added fields if not specified
83 for person in persons:
84 for field in added_fields: