From 9b4b12157e70f0a4264269aa5c910595fc378152 Mon Sep 17 00:00:00 2001
From: Guilhem Saurel <guilhem.saurel@laas.fr>
Date: Wed, 26 Jun 2024 13:42:55 +0200
Subject: [PATCH] onboarding: offboarding too

---
 onboarding/onboarding.py | 55 ++++++++++++++++++++++++++++++++++++----
 1 file changed, 50 insertions(+), 5 deletions(-)

diff --git a/onboarding/onboarding.py b/onboarding/onboarding.py
index 4c7d5bc..2e55678 100755
--- a/onboarding/onboarding.py
+++ b/onboarding/onboarding.py
@@ -1,6 +1,8 @@
 #!/usr/bin/env python3
 
+import logging
 import shelve
+from datetime import date
 from email.mime.text import MIMEText
 from getpass import getuser
 from os import environ
@@ -72,16 +74,52 @@ def greet(to, sender):
     s.quit()
 
 
+def offboard(member, cn, sender, responsable):
+    """
+    Send a notification to responsable and team lead about required offboarding
+    """
+    if "@" not in sender:
+        sender = "%s@laas.fr" % sender
+
+    msg = MIMEText(f"{cn} finit son contrat dans 90 jours.")
+
+    msg["Subject"] = f"[gepetto-utils] Notification fin de contrat {member}"
+    msg["From"] = sender
+    msg["To"] = f"responsable-gepetto@laas.fr, {responsable}"
+    msg["Bcc"] = sender
+    s = SMTP("mail.laas.fr")
+    s.send_message(msg)
+    s.quit()
+
+
 def get_gepetto_ldap():
     """
     Get a new list of Gepetto members in the LDAP of the LAAS
     """
     conn = Connection("ldap.laas.fr", auto_bind=True)
-    conn.search("dc=laas,dc=fr", "(o=gepetto)", attributes=["uid"])
-    return [str(entry.uid) for entry in conn.entries]
+    conn.search(
+        "dc=laas,dc=fr",
+        "(o=gepetto)",
+        attributes=["uid", "cn", "laas-responsable", "st"],
+    )
+    return [
+        (
+            str(e["uid"]),
+            str(e["cn"]),
+            str(e["laas-responsable"]),
+            (
+                date(*reversed([int(i) for i in str(e["st"]).split("/")]))
+                if "/" in str(e["st"])
+                else None
+            ),
+        )
+        for e in conn.entries
+    ]
 
 
 if __name__ == "__main__":
+    logging.basicConfig(level=logging.INFO)
+
     # Get old and new list of members
     gepetto = get_gepetto()
     gepetto_ldap = get_gepetto_ldap()
@@ -89,13 +127,20 @@ if __name__ == "__main__":
     # On the first run, old list is empty, so we just set it to the new one
     if not gepetto:
         gepetto = gepetto_ldap
+    gepetto_uids = [m[0] for m in gepetto]
 
     # Retrieve the login of the current user, who must already be a member
-    me = whoami(gepetto)
+    me = whoami(gepetto_uids)
 
-    for member in gepetto_ldap:
-        if member not in gepetto:
+    for member, cn, responsable, st in gepetto_ldap:
+        # Onboard newcommers
+        if member not in gepetto_uids:
+            logging.info(f"onboarding {cn}")
             greet(member, me)
+        # prepare offboarding
+        if st is not None and (st - date.today()).days == 90:
+            logging.info(f"offboarding {cn}")
+            offboard(member, cn, me, responsable)
 
     # Save the new list
     with shelve.open(SHELF) as shelf:
-- 
GitLab