use self.get_auth_info
[sfa.git] / sfa / methods / update.py
1 ### $Id$
2 ### $URL$
3
4 from sfa.util.faults import *
5 from sfa.util.method import Method
6 from sfa.util.parameter import Parameter, Mixed
7 from sfa.util.auth import Auth
8 from sfa.util.record import GeniRecord
9 from sfa.util.debug import log
10
11 class update(Method):
12     """
13     Update an object in the registry. Currently, this only updates the
14     PLC information associated with the record. The Geni fields (name, type,
15     GID) are fixed.
16     
17     @param cred credential string specifying rights of the caller
18     @param record a record dictionary to be updated
19
20     @return 1 if successful, faults otherwise 
21     """
22
23     interfaces = ['registry']
24     
25     accepts = [
26         Parameter(str, "Credential string"),
27         Parameter(dict, "Record dictionary to be updated")
28         ]
29
30     returns = Parameter(int, "1 if successful")
31     
32     def call(self, cred, record_dict):
33         self.api.auth.check(cred, "update")
34         record = GeniRecord(dict = record_dict)
35         type = record.get_type()
36         self.api.auth.verify_object_permission(record.get_name())
37         auth_name = self.api.auth.get_authority(record.get_name())
38         if not auth_name:
39             auth_name = record.get_name()
40         table = self.api.auth.get_auth_table(auth_name)
41
42         # make sure the record exists
43         existing_record_list = table.resolve(type, record.get_name())
44         if not existing_record_list:
45             raise RecordNotFound(record.get_name())
46         existing_record = existing_record_list[0]
47
48         # Update_membership needs the membership lists in the existing record
49         # filled in, so it can see if members were added or removed
50         self.api.fill_record_info(existing_record)
51
52          # Use the pointer from the existing record, not the one that the user
53         # gave us. This prevents the user from inserting a forged pointer
54         pointer = existing_record.get_pointer()
55
56         # update the PLC information that was specified with the record
57
58         if (type == "authority"):
59             self.api.plshell.UpdateSite(self.api.plauth, pointer, record)
60
61         elif type == "slice":
62             hrn=record.get_name()
63             pl_record=self.api.geni_fields_to_pl_fields(type, hrn, record)
64             self.api.plshell.UpdateSlice(self.api.plauth, pointer, pl_record)
65
66         elif type == "user":
67             # SMBAKER: UpdatePerson only allows a limited set of fields to be
68             #    updated. Ideally we should have a more generic way of doing
69             #    this. I copied the field names from UpdatePerson.py...
70             update_fields = {}
71             all_fields = record
72             for key in all_fields.keys():
73                 if key in ['first_name', 'last_name', 'title', 'email',
74                            'password', 'phone', 'url', 'bio', 'accepted_aup',
75                            'enabled']:
76                     update_fields[key] = all_fields[key]
77             self.api.plshell.UpdatePerson(self.api.plauth, pointer, update_fields)
78
79         elif type == "node":
80             self.api.plshell.UpdateNode(self.api.plauth, pointer, record)
81
82         else:
83             raise UnknownGeniType(type)
84
85         # update membership for researchers, pis, owners, operators
86         self.api.update_membership(existing_record, record)
87
88         return 1