reviewing the tags permission system
[plcapi.git] / PLC / Methods / DeletePersonTag.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 DeletePersonTag(Method):
15     """
16     Deletes the specified 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', 'user']
24
25     accepts = [
26         Auth(),
27         PersonTag.fields['person_tag_id']
28         ]
29
30     returns = Parameter(int, '1 if successful')
31
32     def call(self, auth, person_tag_id):
33         person_tags = PersonTags(self.api, [person_tag_id])
34         if not person_tags:
35             raise PLCInvalidArgument, "No such person tag %r"%person_tag_id
36         person_tag = person_tags[0]
37
38         person = Persons (self.api, person_tag['person_id'])[0]
39
40         # check authorizations
41         if 'admin' in self.caller['roles']:
42             pass
43         # user can change tags on self
44         elif AuthorizeHelpers.person_access_person (self.api, self.caller, person):
45             pass
46         else:
47             raise PLCPermissionDenied, "%s: you can only change your own tags"%self.name
48
49         person_tag.delete()
50         self.object_ids = [person_tag['person_tag_id']]
51
52         return 1