remove simplejson dependency
[plcapi.git] / PLC / Methods / GetKeys.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.Keys import Key, Keys
7 from PLC.Auth import Auth
8
9 class GetKeys(Method):
10     """
11     Returns an array of structs containing details about keys. If
12     key_filter is specified and is an array of key identifiers, or a
13     struct of key attributes, only keys matching the filter will be
14     returned. If return_fields is specified, only the specified
15     details will be returned.
16
17     Admin may query all keys. Non-admins may only query their own
18     keys.
19     """
20
21     roles = ['admin', 'pi', 'user', 'tech', 'node']
22
23     accepts = [
24         Auth(),
25         Mixed([Mixed(Key.fields['key_id'])],
26               Filter(Key.fields)),
27         Parameter([str], "List of fields to return", nullok = True)
28         ]
29
30     returns = [Key.fields]
31
32
33     def call(self, auth, key_filter = None, return_fields = None):
34         keys = Keys(self.api, key_filter, return_fields)
35
36         # If we are not admin, make sure to only return our own keys
37         if isinstance(self.caller, Person) and \
38            'admin' not in self.caller['roles']:
39             keys = filter(lambda key: key['key_id'] in self.caller['key_ids'], keys)
40
41         return keys