Add PersonTags as well. Performed a simple test but not extensive regression
[plcapi.git] / PLC / Methods / DeletePersonTag.py
1 # $Id: DeletePersonTag.py 14587 2009-07-19 13:18:50Z thierry $
2 # $URL: http://svn.planet-lab.org/svn/PLCAPI/tags/PLCAPI-4.3-27/PLC/Methods/DeletePersonTag.py $
3 #
4 # Thierry Parmentelat - INRIA
5 #
6 # $Revision: 14587 $
7 #
8
9 from PLC.Faults import *
10 from PLC.Method import Method
11 from PLC.Parameter import Parameter, Mixed
12 from PLC.Auth import Auth
13
14 from PLC.PersonTags import PersonTag, PersonTags
15 from PLC.Persons import Person, Persons
16
17 from PLC.Nodes import Node, Nodes
18 from PLC.Persons import Person, Persons
19
20 class DeletePersonTag(Method):
21     """
22     Deletes the specified person setting
23
24     Attributes may require the caller to have a particular role in order
25     to be deleted, depending on the related tag type.
26     Admins may delete attributes of any slice or sliver.
27
28     Returns 1 if successful, faults otherwise.
29     """
30
31     roles = ['admin', 'pi', 'user']
32
33     accepts = [
34         Auth(),
35         PersonTag.fields['person_tag_id']
36         ]
37
38     returns = Parameter(int, '1 if successful')
39
40     object_type = 'Person'
41
42
43     def call(self, auth, person_tag_id):
44         person_tags = PersonTags(self.api, [person_tag_id])
45         if not person_tags:
46             raise PLCInvalidArgument, "No such person tag %r"%person_tag_id
47         person_tag = person_tags[0]
48
49         ### reproducing a check from UpdateSliceTag, looks dumb though
50         persons = Persons(self.api, [person_tag['person_id']])
51         if not persons:
52             raise PLCInvalidArgument, "No such person %r"%person_tag['person_id']
53         person = persons[0]
54
55         assert person_tag['person_tag_id'] in person['person_tag_ids']
56
57         # check permission : it not admin, is the user affiliated with the right person
58         if 'admin' not in self.caller['roles']:
59             # check caller is affiliated with this person's site
60             if len(set(person['site_ids']) & set(self.caller['site_ids'])) == 0:
61                 raise PLCPermissionDenied, "Not a member of the person's sites: %s"%person['site_ids']
62             
63             required_min_role = tag_type ['min_role_id']
64             if required_min_role is not None and \
65                     min(self.caller['role_ids']) > required_min_role:
66                 raise PLCPermissionDenied, "Not allowed to modify the specified person setting, requires role %d",required_min_role
67
68         person_tag.delete()
69         self.object_ids = [person_tag['person_tag_id']]
70
71         return 1