d4a17a4e8d1f6880639c7e93728bf5855fc07698
[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 Auth
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         Auth(),
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
28     def call(self, auth, person_id_or_email, slice_id_or_name):
29         # Get account information
30         persons = Persons(self.api, [person_id_or_email])
31         if not persons:
32             raise PLCInvalidArgument, "No such account"
33         person = persons[0]
34
35         # Get slice information
36         slices = Slices(self.api, [slice_id_or_name])
37         if not slices:
38             raise PLCInvalidArgument, "No such slice"
39         slice = slices[0]
40
41         # N.B. Allow foreign users to be added to local slices and
42         # local users to be added to foreign slices (and, of course,
43         # local users to be added to local slices).
44         if person['peer_id'] is not None and slice['peer_id'] is not None:
45             raise PLCInvalidArgument, "Cannot add foreign users to foreign slices"
46
47         # If we are not admin, make sure the caller is a PI
48         # of the site associated with the slice
49         if 'admin' not in self.caller['roles']:
50             if slice['site_id'] not in self.caller['site_ids']:
51                 raise PLCPermissionDenied, "Not allowed to add users to this slice"
52
53         if slice['slice_id'] not in person['slice_ids']:
54             slice.add_person(person)
55
56         # Logging variables
57         self.event_objects = {'Person': [person['person_id']],
58                               'Slice': [slice['slice_id']]}
59         self.object_ids = [slice['slice_id']]
60
61         return 1