- added class variables: event_type, object_type, object_ids (used by event logger)
[plcapi.git] / PLC / Methods / AddPersonToSlice.py
1 from PLC.Faults import *
2 from PLC.Method import Method
3 from PLC.Parameter import Parameter, Mixed
4 from PLC.Persons import Person, Persons
5 from PLC.Slices import Slice, Slices
6 from PLC.Auth import PasswordAuth
7
8 class AddPersonToSlice(Method):
9     """
10     Adds the specified person to the specified slice. If the person is
11     already a member of the slice, no errors are returned. 
12
13     Returns 1 if successful, faults otherwise.
14     """
15
16     roles = ['admin', 'pi']
17
18     accepts = [
19         PasswordAuth(),
20         Mixed(Person.fields['person_id'],
21               Person.fields['email']),
22         Mixed(Slice.fields['slice_id'],
23               Slice.fields['name'])
24         ]
25
26     returns = Parameter(int, '1 if successful')
27     event_type = 'AddTo'
28     object_type = 'Slice'
29     object_ids = []
30
31     def call(self, auth, person_id_or_email, slice_id_or_name):
32         # Get account information
33         persons = Persons(self.api, [person_id_or_email])
34         if not persons:
35             raise PLCInvalidArgument, "No such account"
36
37         person = persons.values()[0]
38
39         # Get slice information
40         slices = Slices(self.api, [slice_id_or_name])
41         if not slices:
42             raise PLCInvalidArgument, "No such slice"
43
44         slice = slices.values()[0]
45
46         # If we are not admin, make sure the caller is a PI
47         # of the site associated with the slice
48         if 'admin' not in self.caller['roles']:
49             if slice['site_id'] not in self.caller['site_ids']:
50                 raise PLCPermissionDenied, "Not allowed to add users to this slice"
51
52         if slice['slice_id'] not in person['slice_ids']:
53             slice.add_person(person)
54         self.object_ids = [slice['slice_id']]
55
56         return 1