b85cc6401cde3962f96c118b1c2adf2feca8031f
[plcapi.git] / PLC / Methods / UpdatePerson.py
1 from PLC.Faults import *
2 from PLC.Method import Method
3 from PLC.Parameter import Parameter, Mixed
4 from PLC.Auth import Auth
5 from PLC.Table import Row
6 from PLC.Persons import Person, Persons
7 from PLC.sendmail import sendmail
8 from PLC.TagTypes import TagTypes
9 from PLC.PersonTags import PersonTags, PersonTag
10
11 related_fields = Person.related_fields.keys()
12 can_update = ['first_name', 'last_name', 'title', 'email',
13               'password', 'phone', 'url', 'bio', 'accepted_aup',
14               'enabled'] + related_fields
15
16 class UpdatePerson(Method):
17     """
18     Updates a person. Only the fields specified in person_fields are
19     updated, all other fields are left untouched.
20
21     Users and techs can only update themselves. PIs can only update
22     themselves and other non-PIs at their sites.
23
24     Returns 1 if successful, faults otherwise.
25     """
26
27     roles = ['admin', 'pi', 'user', 'tech']
28
29     accepted_fields = Row.accepted_fields(can_update,Person.fields)
30     # xxx check the related_fields feature
31     accepted_fields.update(Person.related_fields)
32     accepted_fields.update(Person.tags)
33
34     accepts = [
35         Auth(),
36         Mixed(Person.fields['person_id'],
37               Person.fields['email']),
38         accepted_fields
39         ]
40
41     returns = Parameter(int, '1 if successful')
42
43     def call(self, auth, person_id_or_email, person_fields):
44         # split provided fields
45         [native,related,tags,rejected] = Row.split_fields(person_fields,[Person.fields,Person.related_fields,Person.tags])
46
47         # type checking
48         native = Row.check_fields (native, self.accepted_fields)
49         if rejected:
50             raise PLCInvalidArgument, "Cannot update Person column(s) %r"%rejected
51
52         # Authenticated function
53         assert self.caller is not None
54
55         # Get account information
56         persons = Persons(self.api, [person_id_or_email])
57         if not persons:
58             raise PLCInvalidArgument, "No such account %s"%person_id_or_email
59         person = persons[0]
60
61         if person['peer_id'] is not None:
62             raise PLCInvalidArgument, "Not a local account %s"%person_id_or_email
63
64         # Check if we can update this account
65         if not self.caller.can_update(person):
66             raise PLCPermissionDenied, "Not allowed to update specified account"
67
68         # Make requested associations
69         for k,v in related.iteritems():
70             person.associate (auth, k, v)
71
72         person.update(native)
73         person.update_last_updated(False)
74         person.sync(commit=True)
75         
76         # send a mail
77         if 'enabled' in person_fields:
78             To = [("%s %s" % (person['first_name'], person['last_name']), person['email'])]
79             Cc = []
80             if person['enabled']:
81                 Subject = "%s account enabled" % (self.api.config.PLC_NAME)
82                 Body = "Your %s account has been enabled. Please visit %s to access your account." % (self.api.config.PLC_NAME, self.api.config.PLC_WWW_HOST)
83             else:
84                 Subject = "%s account disabled" % (self.api.config.PLC_NAME)
85                 Body = "Your %s account has been disabled. Please contact your PI or PlanetLab support for more information" % (self.api.config.PLC_NAME)
86             sendmail(self.api, To = To, Cc = Cc, Subject = Subject, Body = Body)
87
88
89         for (tagname,value) in tags.iteritems():
90             # the tagtype instance is assumed to exist, just check that
91             tag_types = TagTypes(self.api,{'tagname':tagname})
92             if not tag_types:
93                 raise PLCInvalidArgument,"No such TagType %s"%tagname
94             tag_type = tag_types[0]
95             person_tags=PersonTags(self.api,{'tagname':tagname,'person_id':person['person_id']})
96             if not person_tags:
97                 person_tag = PersonTag(self.api)
98                 person_tag['person_id'] = person['person_id']
99                 person_tag['tag_type_id'] = tag_type['tag_type_id']
100                 person_tag['tagname']  = tagname
101                 person_tag['value'] = value
102                 person_tag.sync()
103             else:
104                 person_tag = person_tags[0]
105                 person_tag['value'] = value
106                 person_tag.sync()
107
108         # Logging variables
109         self.event_objects = {'Person': [person['person_id']]}
110
111         # Redact password
112         if 'password' in person_fields:
113             person_fields['password'] = "Removed by API"
114         self.message = 'Person %d updated: %s.' % \
115                        (person['person_id'], person_fields.keys())
116         if 'enabled' in person_fields:
117             self.message += ' Person enabled'
118
119         return 1