reviewing the tags permission system
[plcapi.git] / PLC / Methods / UpdatePersonTag.py
1 #
2 # Thierry Parmentelat - INRIA
3 #
4 from PLC.Faults import *
5 from PLC.Method import Method
6 from PLC.Parameter import Parameter, Mixed
7 from PLC.Auth import Auth
8
9 from PLC.PersonTags import PersonTag, PersonTags
10 from PLC.Persons import Person, Persons
11
12 from PLC.AuthorizeHelpers import AuthorizeHelpers
13
14 class UpdatePersonTag(Method):
15     """
16     Updates the value of an existing person setting
17
18     Admins have full access.  Non-admins can change their own tags.
19
20     Returns 1 if successful, faults otherwise.
21     """
22
23     roles = ['admin', 'pi', 'tech', 'user']
24
25     accepts = [
26         Auth(),
27         PersonTag.fields['person_tag_id'],
28         PersonTag.fields['value']
29         ]
30
31     returns = Parameter(int, '1 if successful')
32
33     def call(self, auth, person_tag_id, value):
34         person_tags = PersonTags(self.api, [person_tag_id])
35         if not person_tags:
36             raise PLCInvalidArgument, "No such person setting %r"%person_tag_id
37         person_tag = person_tags[0]
38
39         person = Persons (self.api, person_tag['person_id'])[0]
40
41         # check authorizations
42         if 'admin' in self.caller['roles']:
43             pass
44         # user can change tags on self
45         elif AuthorizeHelpers.person_access_person (self.api, self.caller, person):
46             pass
47         else:
48             raise PLCPermissionDenied, "%s: you can only change your own tags"%self.name
49
50         person_tag['value'] = value
51         person_tag.sync()
52
53         self.object_ids = [person_tag['person_tag_id']]
54         return 1