6725f33ad6db46b2c89fa8bbe3e8686143a2687c
[plcapi.git] / PLC / Methods / GetPersons.py
1 from PLC.Faults import *
2 from PLC.Method import Method
3 from PLC.Filter import Filter
4 from PLC.Parameter import Parameter, Mixed
5 from PLC.Persons import Person, Persons
6 from PLC.Auth import Auth
7
8 hidden_fields = ['password', 'verification_key', 'verification_expires']
9
10 class GetPersons(Method):
11     """
12     Returns an array of structs containing details about users. If
13     person_filter is specified and is an array of user identifiers or
14     usernames, or a struct of user attributes, only users matching the
15     filter will be returned. If return_fields is specified, only the
16     specified details will be returned.
17
18     Users and techs may only retrieve details about themselves. PIs
19     may retrieve details about themselves and others at their
20     sites. Admins and nodes may retrieve details about all accounts.
21     """
22
23     roles = ['admin', 'pi', 'user', 'tech']
24
25     accepts = [
26         Auth(),
27         Mixed(Person.fields['person_id'],
28               Parameter(str,"email"),
29               Filter(Person.fields)),
30         Parameter([str], "List of fields to return", nullok = True)
31         ]
32
33     # Filter out password field
34     return_fields = dict(filter(lambda (field, value): field not in hidden_fields,
35                                 Person.fields.items()))
36     returns = [return_fields]
37
38     def call(self, auth, person_filter = None, return_fields = None):
39         persons = Persons(self.api, person_filter, return_fields)
40
41         # Filter out accounts that are not viewable
42         #if isinstance(self.caller, Person) and \
43         #   'admin' not in self.caller['roles']:
44         #    persons = filter(self.caller.can_view, persons)
45         return persons.dicts()