(most) all functions now take SessionAuth in addition to PasswordAuth
[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.Keys import Key, Keys
5 from PLC.Auth import Auth
6
7 class GetKeys(Method):
8     """
9     Return an array of structs containing details about keys. If
10     key_id_list is specified, only the specified keys will be queried.
11
12     Admin may query all keys. Non-admins may only query their own
13     keys.
14     """
15
16     roles = ['admin', 'pi', 'user', 'tech']
17
18     accepts = [
19         Auth(),
20         [Key.fields['key_id']]
21         ]
22
23     returns = [Key.fields]
24
25     def call(self, auth, key_ids = None):
26         # If we are not admin, make sure to only return our own keys       
27         if 'admin' not in self.caller['roles']:
28             key_ids = set(key_ids).intersection(self.caller['key_ids'])
29             if not key_ids:
30                 return []
31
32         return Keys(self.api, key_ids).values()