Send user an email when thier account has been disable or enabled. This should help...
[plcapi.git] / PLC / Methods / UpdatePerson.py
index 71c47bf..61b8e26 100644 (file)
@@ -3,11 +3,13 @@ from PLC.Method import Method
 from PLC.Parameter import Parameter, Mixed
 from PLC.Persons import Person, Persons
 from PLC.Auth import Auth
 from PLC.Parameter import Parameter, Mixed
 from PLC.Persons import Person, Persons
 from PLC.Auth import Auth
+from PLC.sendmail import sendmail
 
 
+related_fields = Person.related_fields.keys()
 can_update = lambda (field, value): field in \
              ['first_name', 'last_name', 'title', 'email',
               'password', 'phone', 'url', 'bio', 'accepted_aup',
 can_update = lambda (field, value): field in \
              ['first_name', 'last_name', 'title', 'email',
               'password', 'phone', 'url', 'bio', 'accepted_aup',
-              'enabled']
+              'enabled'] + related_fields
 
 class UpdatePerson(Method):
     """
 
 class UpdatePerson(Method):
     """
@@ -22,7 +24,7 @@ class UpdatePerson(Method):
 
     roles = ['admin', 'pi', 'user', 'tech']
 
 
     roles = ['admin', 'pi', 'user', 'tech']
 
-    person_fields = dict(filter(can_update, Person.fields.items()))
+    person_fields = dict(filter(can_update, Person.fields.items() + Person.related_fields.items()))
 
     accepts = [
         Auth(),
 
     accepts = [
         Auth(),
@@ -40,8 +42,10 @@ class UpdatePerson(Method):
         persons = Persons(self.api, [person_id_or_email])
         if not persons:
             raise PLCInvalidArgument, "No such account"
         persons = Persons(self.api, [person_id_or_email])
         if not persons:
             raise PLCInvalidArgument, "No such account"
+        person = persons[0]
 
 
-        person = persons.values()[0]
+        if person['peer_id'] is not None:
+            raise PLCInvalidArgument, "Not a local account"
 
         # Authenticated function
         assert self.caller is not None
 
         # Authenticated function
         assert self.caller is not None
@@ -49,8 +53,38 @@ class UpdatePerson(Method):
         # Check if we can update this account
         if not self.caller.can_update(person):
             raise PLCPermissionDenied, "Not allowed to update specified account"
         # Check if we can update this account
         if not self.caller.can_update(person):
             raise PLCPermissionDenied, "Not allowed to update specified account"
+       
+       # Make requested associations
+        for field in related_fields:
+            if field in person_fields:
+                person.associate(auth, field, person_fields[field])
+                person_fields.pop(field)
 
         person.update(person_fields)
 
         person.update(person_fields)
+       person.update_last_updated(False)
         person.sync()
 
         person.sync()
 
+       if 'enabled' in person_fields:
+           To = [("%s %s" % (person['first_name'], person['last_name']), person['email'])]
+           Cc = []     
+           if person['enabled']:
+               Subject = "PlanetLab account enabled"
+               Body = "Your PlanetLab account has been enabled. You should now be allowd to access you account"  
+           else:
+               Subject = "PlanetLab account disabled"
+               Body = "Your PlanetLab account has been disabled. Please contact your PI or PlanetLab support for more information"
+           sendmail(self.api, To = To, Cc = Cc, Subject = Subject, Body = Body)                
+
+                               
+       # Logging variables
+       self.event_objects = {'Person': [person['person_id']]}
+
+        # Redact password
+        if 'password' in person_fields:
+            person_fields['password'] = "Removed by API"
+        self.message = 'Person %d updated: %s.' % \
+                       (person['person_id'], person_fields.keys())
+       if 'enabled' in person_fields:
+            self.message += ' Person enabled'  
+
         return 1
         return 1