82c7ffcd1e13b33ddfa6e52c2802ef28ad1ce461
[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 PasswordAuth
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         PasswordAuth(),
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         keys = Keys(self.api, key_ids).values()
33         
34         # Turn each key into a real dict
35         keys = [dict(key) for key in keys]
36                 
37         return keys