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