group db-related stuff in sfa/storage
[sfa.git] / sfa / methods / GetCredential.py
1 from sfa.util.xrn import urn_to_hrn
2 from sfa.util.method import Method
3
4 from sfa.trust.credential import Credential
5
6 from sfa.storage.parameter import Parameter, Mixed
7
8 class GetCredential(Method):
9     """
10     Retrive a credential for an object
11     If cred == None then the behavior reverts to GetSelfCredential
12
13     @param hrn human readable name of object (hrn or urn)
14     @param cred credential object specifying rights of the caller
15     @param type type of object (user | slice | node | authority )
16
17     @return the string representation of a credential object  
18     """
19
20     interfaces = ['registry']
21     
22     accepts = [
23         Mixed(Parameter(str, "Credential string"),
24               Parameter(type([str]), "List of credentials")), 
25         Parameter(str, "Human readable name (hrn or urn)"),
26         Mixed(Parameter(str, "Record type"),
27               Parameter(None, "Type not specified")),
28         ]
29
30     returns = Parameter(str, "String representation of a credential object")
31
32     def call(self, creds, xrn, type):
33     
34         if type:
35             hrn = urn_to_hrn(xrn)[0]
36         else:
37             hrn, type = urn_to_hrn(xrn)
38
39         # check creds
40         valid_creds = self.api.auth.checkCredentials(creds, 'getcredential')
41         self.api.auth.verify_object_belongs_to_me(hrn)
42
43         #log the call
44         origin_hrn = Credential(string=valid_creds[0]).get_gid_caller().get_hrn()
45         self.api.logger.info("interface: %s\tcaller-hrn: %s\ttarget-hrn: %s\tmethod-name: %s"%(self.api.interface, origin_hrn, hrn, self.name)) 
46
47         return self.api.manager.GetCredential(self.api, xrn, type)
48