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