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