From 465305e5d5738d3fde3121a8d92d31db6c77871b Mon Sep 17 00:00:00 2001
From: Mohamed Larabi <mohamed.larabi@inria.fr>
Date: Thu, 14 Nov 2013 17:46:10 +0100
Subject: [PATCH] Update Person HRN when Person's email is updated

---
 PLC/Methods/UpdatePerson.py | 9 +++++++++
 1 file changed, 9 insertions(+)

diff --git a/PLC/Methods/UpdatePerson.py b/PLC/Methods/UpdatePerson.py
index b85cc640..1874f134 100644
--- a/PLC/Methods/UpdatePerson.py
+++ b/PLC/Methods/UpdatePerson.py
@@ -7,6 +7,7 @@ from PLC.Persons import Person, Persons
 from PLC.sendmail import sendmail
 from PLC.TagTypes import TagTypes
 from PLC.PersonTags import PersonTags, PersonTag
+from PLC.Namespace import email_to_hrn
 
 related_fields = Person.related_fields.keys()
 can_update = ['first_name', 'last_name', 'title', 'email',
@@ -85,6 +86,14 @@ class UpdatePerson(Method):
                 Body = "Your %s account has been disabled. Please contact your PI or PlanetLab support for more information" % (self.api.config.PLC_NAME)
             sendmail(self.api, To = To, Cc = Cc, Subject = Subject, Body = Body)
 
+        # if email was modifed make sure to update the hrn tag
+        if 'email' in native:
+            old_hrn=PersonTags(self.api,{'tagname':'hrn','person_id':person['person_id']})[0]['value']
+            if old_hrn :
+                root_auth = self.api.config.PLC_HRN_ROOT
+                login_base = old_hrn.split('.')[-2]
+                hrn=email_to_hrn("%s.%s"%(root_auth,login_base),person['email'])
+                tags['hrn'] = hrn
 
         for (tagname,value) in tags.iteritems():
             # the tagtype instance is assumed to exist, just check that
-- 
2.47.0