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.Auth import Auth
8 class GetPersons(Method):
10 Returns an array of structs containing details about users. If
11 person_filter is specified and is an array of user identifiers or
12 usernames, or a struct of user attributes, only users matching the
13 filter will be returned. If return_fields is specified, only the
14 specified details will be returned.
16 Users and techs may only retrieve details about themselves. PIs
17 may retrieve details about themselves and others at their
18 sites. Admins may retrieve details about all accounts.
21 roles = ['admin', 'pi', 'user', 'tech']
25 Mixed([Mixed(Person.fields['person_id'],
26 Person.fields['email'])],
27 Filter(Person.fields)),
28 Parameter([str], "List of fields to return", nullok = True)
31 # Filter out password field
32 can_return = lambda (field, value): field not in \
33 ['password', 'verification_key', 'verification_expires']
34 return_fields = dict(filter(can_return, Person.fields.items()))
35 returns = [return_fields]
38 def call(self, auth, person_filter = None, return_fields = None):
40 # If we are not admin, make sure to only return viewable accounts
41 if 'admin' not in self.caller['roles']:
42 # Get accounts that we are able to view
43 valid_person_ids = [self.caller['person_id']]
44 if 'pi' in self.caller['roles'] and self.caller['site_ids']:
45 sites = Sites(self.api, self.caller['site_ids'])
47 valid_person_ids += site['person_ids']
49 if not valid_person_ids:
52 if person_filter is None:
53 person_filter = valid_person_ids
55 # Filter out password field
57 return_fields = filter(self.can_return, return_fields)
59 return_fields = self.return_fields.keys()
61 persons = Persons(self.api, person_filter, return_fields)
63 # Filter out accounts that are not viewable
64 if 'admin' not in self.caller['roles']:
65 persons = filter(self.caller.can_view, persons)