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
9 hidden_fields = ['password', 'verification_key', 'verification_expires']
11 class GetPersons(Method):
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.
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.
24 roles = ['admin', 'pi', 'user', 'tech', 'node']
28 Mixed([Mixed(Person.fields['person_id'],
29 Person.fields['email'])],
30 Filter(Person.fields)),
31 Parameter([str], "List of fields to return", nullok = True)
34 # Filter out password field
35 return_fields = dict(filter(lambda (field, value): field not in hidden_fields,
36 Person.fields.items()))
37 returns = [return_fields]
39 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 isinstance(self.caller, Person) and \
42 'admin' not in self.caller['roles']:
43 # Get accounts that we are able to view
44 valid_person_ids = [self.caller['person_id']]
45 if 'pi' in self.caller['roles'] and self.caller['site_ids']:
46 sites = Sites(self.api, self.caller['site_ids'])
48 valid_person_ids += site['person_ids']
50 if not valid_person_ids:
53 if person_filter is None:
54 person_filter = valid_person_ids
56 # Filter out password field
58 return_fields = filter(lambda field: field not in hidden_fields,
61 return_fields = self.return_fields.keys()
63 persons = Persons(self.api, person_filter, return_fields)
65 # Filter out accounts that are not viewable
66 if isinstance(self.caller, Person) and \
67 'admin' not in self.caller['roles']:
68 persons = filter(self.caller.can_view, persons)