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