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
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
16 class UpdatePerson(Method):
18 Updates a person. Only the fields specified in person_fields are
19 updated, all other fields are left untouched.
21 Users and techs can only update themselves. PIs can only update
22 themselves and other non-PIs at their sites.
24 Returns 1 if successful, faults otherwise.
27 roles = ['admin', 'pi', 'user', 'tech']
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)
36 Mixed(Person.fields['person_id'],
37 Person.fields['email']),
41 returns = Parameter(int, '1 if successful')
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])
48 native = Row.check_fields (native, self.accepted_fields)
50 raise PLCInvalidArgument, "Cannot update Person column(s) %r"%rejected
52 # Authenticated function
53 assert self.caller is not None
55 # Get account information
56 persons = Persons(self.api, [person_id_or_email])
58 raise PLCInvalidArgument, "No such account %s"%person_id_or_email
61 if person['peer_id'] is not None:
62 raise PLCInvalidArgument, "Not a local account %s"%person_id_or_email
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"
68 # Make requested associations
69 for k,v in related.iteritems():
70 person.associate (auth, k, v)
73 person.update_last_updated(False)
74 person.sync(commit=True)
77 if 'enabled' in person_fields:
78 To = [("%s %s" % (person['first_name'], person['last_name']), person['email'])]
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)
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)
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})
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']})
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
104 person_tag = person_tags[0]
105 person_tag['value'] = value
109 self.event_objects = {'Person': [person['person_id']]}
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'