add enable_hmac slice tag accessor
[plcapi.git] / PLC / Methods / AddPersonTag.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.Persons import Person, Persons
10 from PLC.TagTypes import TagType, TagTypes
11 from PLC.PersonTags import PersonTag, PersonTags
12
13 from PLC.AuthorizeHelpers import AuthorizeHelpers
14
15 class AddPersonTag(Method):
16     """
17     Sets the specified setting for the specified person
18     to the specified value.
19
20     Admins have full access.  Non-admins can change their own tags.
21
22     Returns the new person_tag_id (> 0) if successful, faults
23     otherwise.
24     """
25
26     roles = ['admin', 'pi', 'tech', 'user']
27
28     accepts = [
29         Auth(),
30         # no other way to refer to a person
31         PersonTag.fields['person_id'],
32         Mixed(TagType.fields['tag_type_id'],
33               TagType.fields['tagname']),
34         PersonTag.fields['value'],
35         ]
36
37     returns = Parameter(int, 'New person_tag_id (> 0) if successful')
38
39     def call(self, auth, person_id, tag_type_id_or_name, value):
40         persons = Persons(self.api, [person_id])
41         if not persons:
42             raise PLCInvalidArgument, "No such person %r"%person_id
43         person = persons[0]
44
45         tag_types = TagTypes(self.api, [tag_type_id_or_name])
46         if not tag_types:
47             raise PLCInvalidArgument, "No such tag type %r"%tag_type_id_or_name
48         tag_type = tag_types[0]
49
50         # checks for existence - does not allow several different settings
51         conflicts = PersonTags(self.api, {'person_id':person['person_id'],
52                                           'tag_type_id':tag_type['tag_type_id']})
53
54         if len(conflicts) :
55             raise PLCInvalidArgument, "Person %d (%s) already has setting %d"% \
56                 (person['person_id'],person['email'], tag_type['tag_type_id'])
57
58         # check authorizations
59         if 'admin' in self.caller['roles']:
60             pass
61         # user can change tags on self
62         elif AuthorizeHelpers.person_access_person (self.api, self.caller, person):
63             pass
64         else:
65             raise PLCPermissionDenied, "%s: you can only change your own tags"%self.name
66
67
68         person_tag = PersonTag(self.api)
69         person_tag['person_id'] = person['person_id']
70         person_tag['tag_type_id'] = tag_type['tag_type_id']
71         person_tag['value'] = value
72
73         person_tag.sync()
74         self.object_ids = [person_tag['person_tag_id']]
75
76         return person_tag['person_tag_id']