X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;ds=sidebyside;f=PLC%2FMethods%2FGetKeys.py;h=70b3a4db9a62cdfaba71860e227718a80a48fcb0;hb=bd0cbf4f7f2e4cf7ceda500bfa6f98c0a700018b;hp=de0eb135177c4172a2debb85fce6d82be306594b;hpb=52a5ed7fae1f8dc82d472a12117c71f0dcf3f281;p=plcapi.git diff --git a/PLC/Methods/GetKeys.py b/PLC/Methods/GetKeys.py index de0eb13..70b3a4d 100644 --- a/PLC/Methods/GetKeys.py +++ b/PLC/Methods/GetKeys.py @@ -1,38 +1,41 @@ from PLC.Faults import * from PLC.Method import Method from PLC.Parameter import Parameter, Mixed +from PLC.Filter import Filter +from PLC.Persons import Person, Persons from PLC.Keys import Key, Keys -from PLC.Auth import PasswordAuth +from PLC.Auth import Auth class GetKeys(Method): """ - Return an array of structs containing details about keys. If - key_id_list is specified, only the specified keys will be queried. + Returns an array of structs containing details about keys. If + key_filter is specified and is an array of key identifiers, or a + struct of key attributes, only keys matching the filter will be + returned. If return_fields is specified, only the specified + details will be returned. Admin may query all keys. Non-admins may only query their own keys. """ - roles = ['admin', 'pi', 'user', 'tech'] + roles = ['admin', 'pi', 'user', 'tech', 'node'] accepts = [ - PasswordAuth(), - [Key.fields['key_id']] + Auth(), + Mixed([Mixed(Key.fields['key_id'])], + Filter(Key.fields)), + Parameter([str], "List of fields to return", nullok = True) ] returns = [Key.fields] - def call(self, auth, key_ids = None): - # If we are not admin, make sure to only return our own keys - if 'admin' not in self.caller['roles']: - if not key_ids: - key_ids = self.caller['key_ids'] - else: - key_ids = set(self.caller['key_ids']).intersection(key_ids) - - keys = Keys(self.api, key_ids).values() - - # Turn each key into a real dict - keys = [dict(key) for key in keys] - + + def call(self, auth, key_filter = None, return_fields = None): + keys = Keys(self.api, key_filter, return_fields) + + # If we are not admin, make sure to only return our own keys + if isinstance(self.caller, Person) and \ + 'admin' not in self.caller['roles']: + keys = filter(lambda key: key['key_id'] in self.caller['key_ids'], keys) + return keys