also fix error message
[plcapi.git] / PLC / Methods / UpdatePersonTag.py
1 # $Id: UpdatePersonTag.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/UpdatePersonTag.py $
3 #
4 # $Revision: 14587 $
5 #
6
7 from PLC.Faults import *
8 from PLC.Method import Method
9 from PLC.Parameter import Parameter, Mixed
10 from PLC.Auth import Auth
11
12 from PLC.PersonTags import PersonTag, PersonTags
13 from PLC.Persons import Person, Persons
14
15 from PLC.Nodes import Nodes
16 from PLC.Persons import Persons
17
18 class UpdatePersonTag(Method):
19     """
20     Updates the value of an existing person setting
21
22     Access rights depend on the tag type.
23
24     Returns 1 if successful, faults otherwise.
25     """
26
27     roles = ['admin', 'pi', 'tech', 'user']
28
29     accepts = [
30         Auth(),
31         PersonTag.fields['person_tag_id'],
32         PersonTag.fields['value']
33         ]
34
35     returns = Parameter(int, '1 if successful')
36
37     object_type = 'Person'
38
39     def call(self, auth, person_tag_id, value):
40         person_tags = PersonTags(self.api, [person_tag_id])
41         if not person_tags:
42             raise PLCInvalidArgument, "No such person setting %r"%person_tag_id
43         person_tag = person_tags[0]
44
45         ### reproducing a check from UpdateSliceTag, looks dumb though
46         persons = Persons(self.api, [person_tag['person_id']])
47         if not persons:
48             raise PLCInvalidArgument, "No such person %r"%person_tag['person_id']
49         person = persons[0]
50
51         assert person_tag['person_tag_id'] in person['person_tag_ids']
52
53         # check permission : it not admin, is the user affiliated with the right person
54         if 'admin' not in self.caller['roles']:
55             # check caller is affiliated with this person's person
56             if not self.caller.can_update(person):
57                 raise PLCPermissionDenied, "person_id %s doesn't have access to person_tag_id %s" % (
58                     person['person_id'],
59                     person_tag['person_tag_id'])
60
61             required_min_role = person_tag['min_role_id']
62             if required_min_role is not None and \
63                     min(self.caller['role_ids']) > required_min_role:
64                 raise PLCPermissionDenied, "Not allowed to modify the specified person setting, requires role %d",required_min_role
65
66         person_tag['value'] = value
67         person_tag.sync()
68
69         self.object_ids = [person_tag['person_tag_id']]
70         return 1