This commit was manufactured by cvs2svn to create branch
[plcapi.git] / PLC / Methods / DeletePersonFromSlice.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 DeletePersonFromSlice(Method):
9     """
10     Deletes the specified person from the specified slice. If the person is
11     not 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     object_type = 'Slice'
29
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         person = persons[0]
37
38         # Get slice information
39         slices = Slices(self.api, [slice_id_or_name])
40         if not slices:
41             raise PLCInvalidArgument, "No such slice"
42         slice = slices[0]
43
44         # N.B. Allow foreign users to be added to local slices and
45         # local users to be added to foreign slices (and, of course,
46         # local users to be added to local slices).
47         if person['peer_id'] is not None and slice['peer_id'] is not None:
48             raise PLCInvalidArgument, "Cannot delete foreign users from foreign slices"
49
50         # If we are not admin, make sure the caller is a pi
51         # of the site associated with the slice
52         if 'admin' not in self.caller['roles']:
53                 if slice['site_id'] not in self.caller['site_ids']:
54                         raise PLCPermissionDenied, "Not allowed to delete users from this slice"
55
56         if slice['slice_id'] in person['slice_ids']:
57             slice.remove_person(person)
58         
59         self.object_ids = [slice['slice_id']]
60
61         return 1