91006912752f02e68b51c83b3066130d43c71a2e
[plcapi.git] / PLC / Methods / SliceUsersList.py
1 from PLC.Method import Method
2 from PLC.Parameter import Parameter, Mixed
3 from PLC.Filter import Filter
4 from PLC.Auth import Auth
5 from PLC.Slices import Slice, Slices
6 from PLC.Persons import Person, Persons
7
8 class SliceUsersList(Method):
9     """
10     Deprecated. Can be implemented with GetSlices.
11
12     List users that are members of the named slice.
13
14     Users may only query slices of which they are members. PIs may
15     query any of the slices at their sites. Admins may query any
16     slice. If a slice that cannot be queried is specified details 
17     about that slice will not be returned.
18     """
19
20     roles = ['admin', 'pi', 'user']
21
22     accepts = [
23         Auth(),
24         Slice.fields['name']
25         ]
26
27     returns = [Person.fields['email']]
28     
29
30     def call(self, auth, slice_name):
31         # If we are not admin, make sure to return only viewable
32         # slices.
33         slice_filter = [slice_name]
34         slices = Slices(self.api, slice_filter)
35         if not slices:
36             raise PLCInvalidArgument, "No such slice"
37         slice = slices[0]
38      
39         if 'admin' not in self.caller['roles']:
40             # Get slices that we are able to view
41             valid_slice_ids = self.caller['slice_ids']
42             if 'pi' in self.caller['roles'] and self.caller['site_ids']:
43                 sites = Sites(self.api, self.caller['site_ids'])
44                 for site in sites:
45                     valid_slice_ids += site['slice_ids']
46
47             if not valid_slice_ids:
48                 return []
49
50             if slice['slice_id'] not in valid_slice_ids:
51                 return []
52         
53         persons = Persons(self.api, slice['person_ids'])
54         person_names = [person['email'] for person in persons]
55
56         return person_names