[evolution-data-server] I#356 - LDAP: Consume departmentNumber



commit 79f5ffda310b32830dd563cb83022ca0d449f9ec
Author: Milan Crha <mcrha redhat com>
Date:   Fri Nov 19 12:57:51 2021 +0100

    I#356 - LDAP: Consume departmentNumber
    
    Closes https://gitlab.gnome.org/GNOME/evolution-data-server/-/issues/356

 .../backends/ldap/e-book-backend-ldap.c            | 128 ++++++++++++++++++---
 1 file changed, 112 insertions(+), 16 deletions(-)
---
diff --git a/src/addressbook/backends/ldap/e-book-backend-ldap.c 
b/src/addressbook/backends/ldap/e-book-backend-ldap.c
index 17fee26f0..818d94267 100644
--- a/src/addressbook/backends/ldap/e-book-backend-ldap.c
+++ b/src/addressbook/backends/ldap/e-book-backend-ldap.c
@@ -189,43 +189,43 @@ static gboolean poll_ldap (gpointer user_data);
 static EContact *build_contact_from_entry (EBookBackendLDAP *bl, LDAPMessage *e, GList 
**existing_objectclasses, gchar **ldap_uid);
 
 static void email_populate (EContact *contact, gchar **values);
-static struct berval ** email_ber (EContact *contact, GError **error);
+static struct berval ** email_ber (EContact *contact, const gchar *ldap_attr, GError **error);
 static gboolean email_compare (EContact *contact1, EContact *contact2);
 
 static void member_populate (EContact *contact, gchar **values);
-static struct berval ** member_ber (EContact *contact, GError **error);
+static struct berval ** member_ber (EContact *contact, const gchar *ldap_attr, GError **error);
 static gboolean member_compare (EContact *contact1, EContact *contact2);
 
 static void homephone_populate (EContact *contact, gchar **values);
-static struct berval ** homephone_ber (EContact *contact, GError **error);
+static struct berval ** homephone_ber (EContact *contact, const gchar *ldap_attr, GError **error);
 static gboolean homephone_compare (EContact *contact1, EContact *contact2);
 
 static void business_populate (EContact *contact, gchar **values);
-static struct berval ** business_ber (EContact *contact, GError **error);
+static struct berval ** business_ber (EContact *contact, const gchar *ldap_attr, GError **error);
 static gboolean business_compare (EContact *contact1, EContact *contact2);
 
 static void anniversary_populate (EContact *contact, gchar **values);
-static struct berval ** anniversary_ber (EContact *contact, GError **error);
+static struct berval ** anniversary_ber (EContact *contact, const gchar *ldap_attr, GError **error);
 static gboolean anniversary_compare (EContact *contact1, EContact *contact2);
 
 static void birthday_populate (EContact *contact, gchar **values);
-static struct berval ** birthday_ber (EContact *contact, GError **error);
+static struct berval ** birthday_ber (EContact *contact, const gchar *ldap_attr, GError **error);
 static gboolean birthday_compare (EContact *contact1, EContact *contact2);
 
 static void category_populate (EContact *contact, gchar **values);
-static struct berval ** category_ber (EContact *contact, GError **error);
+static struct berval ** category_ber (EContact *contact, const gchar *ldap_attr, GError **error);
 static gboolean category_compare (EContact *contact1, EContact *contact2);
 
 static void home_address_populate (EContact * card, gchar **values);
-static struct berval **home_address_ber (EContact * card, GError **error);
+static struct berval **home_address_ber (EContact * card, const gchar *ldap_attr, GError **error);
 static gboolean home_address_compare (EContact * ecard1, EContact * ecard2);
 
 static void work_address_populate (EContact * card, gchar **values);
-static struct berval **work_address_ber (EContact * card, GError **error);
+static struct berval **work_address_ber (EContact * card, const gchar *ldap_attr, GError **error);
 static gboolean work_address_compare (EContact * ecard1, EContact * ecard2);
 
 static void other_address_populate (EContact * card, gchar **values);
-static struct berval **other_address_ber (EContact * card, GError **error);
+static struct berval **other_address_ber (EContact * card, const gchar *ldap_attr, GError **error);
 static gboolean other_address_compare (EContact * ecard1, EContact * ecard2);
 
 static void work_city_populate (EContact * card, gchar **values);
@@ -239,13 +239,17 @@ static void home_zip_populate (EContact * card, gchar **values);
 static void home_country_populate (EContact * card, gchar **values);
 
 static void photo_populate (EContact *contact, struct berval **ber_values);
-static struct berval **photo_ber (EContact * contact, GError **error);
+static struct berval **photo_ber (EContact * contact, const gchar *ldap_attr, GError **error);
 static gboolean photo_compare (EContact * ecard1, EContact * ecard2);
 
 static void cert_populate (EContact *contact, struct berval **ber_values);
-static struct berval **cert_ber (EContact *contact, GError **error);
+static struct berval **cert_ber (EContact *contact, const gchar *ldap_attr, GError **error);
 static gboolean cert_compare (EContact *ecard1, EContact *ecard2);
 
+static void org_unit_populate (EContact *contact, gchar **values);
+static struct berval ** org_unit_ber (EContact *contact, const gchar *ldap_attr, GError **error);
+static gboolean org_unit_compare (EContact *contact1, EContact *contact2);
+
 static struct prop_info {
        EContactField field_id;
        const gchar *ldap_attr;
@@ -265,7 +269,7 @@ static struct prop_info {
        /* used when reading from the ldap server populates EContact with the values in **values. */
        void (*populate_contact_func)(EContact *contact, gchar **values);
        /* used when writing to an ldap server.  returns a NULL terminated array of berval*'s */
-       struct berval ** (*ber_func)(EContact *contact, GError **error);
+       struct berval ** (*ber_func)(EContact *contact, const gchar *ldap_attr, GError **error);
        /* used to compare list attributes */
        gboolean (*compare_func)(EContact *contact1, EContact *contact2);
 
@@ -316,7 +320,8 @@ static struct prop_info {
 
        /* org information */
        CONTACT_STRING_PROP   (E_CONTACT_ORG,       "o"),
-       CONTACT_STRING_PROP   (E_CONTACT_ORG_UNIT,  "ou"),
+       COMPLEX_PROP  (E_CONTACT_ORG_UNIT,          "ou", org_unit_populate, org_unit_ber, org_unit_compare),
+       COMPLEX_PROP  (E_CONTACT_ORG_UNIT,          "departmentNumber", org_unit_populate, org_unit_ber, 
org_unit_compare), /* Keep this after "ou" */
        CONTACT_STRING_PROP   (E_CONTACT_OFFICE,    "roomNumber"),
        CONTACT_STRING_PROP   (E_CONTACT_TITLE,     "title"),
        E_STRING_PROP (E_CONTACT_ROLE,      "businessRole"),
@@ -1311,7 +1316,7 @@ build_mods_from_contacts (EBookBackendLDAP *bl,
                        new_prop_present = (new_prop != NULL);
                }
                else {
-                       new_prop_bers = prop_info[i].ber_func ? prop_info[i].ber_func (new, &local_error) : 
NULL;
+                       new_prop_bers = prop_info[i].ber_func ? prop_info[i].ber_func (new, 
prop_info[i].ldap_attr, &local_error) : NULL;
                        new_prop_present = (new_prop_bers != NULL);
                }
 
@@ -1342,7 +1347,7 @@ build_mods_from_contacts (EBookBackendLDAP *bl,
                        }
                        else {
                                gint j;
-                               struct berval **current_prop_bers = prop_info[i].ber_func ? 
prop_info[i].ber_func (current, &local_error) : NULL;
+                               struct berval **current_prop_bers = prop_info[i].ber_func ? 
prop_info[i].ber_func (current, prop_info[i].ldap_attr, &local_error) : NULL;
 
                                current_prop_present = (current_prop_bers != NULL);
 
@@ -2516,6 +2521,7 @@ email_populate (EContact *contact,
 
 static struct berval **
 email_ber (EContact *contact,
+          const gchar *ldap_attr,
           GError **error)
 {
        struct berval ** result;
@@ -2617,6 +2623,7 @@ member_populate (EContact *contact,
 
 static struct berval **
 member_ber (EContact *contact,
+           const gchar *ldap_attr,
            GError **error)
 {
        struct berval ** result;
@@ -2787,6 +2794,7 @@ homephone_populate (EContact *contact,
 
 static struct berval **
 homephone_ber (EContact *contact,
+              const gchar *ldap_attr,
               GError **error)
 {
        struct berval ** result;
@@ -2858,6 +2866,7 @@ business_populate (EContact *contact,
 
 static struct berval **
 business_ber (EContact *contact,
+             const gchar *ldap_attr,
              GError **error)
 {
        struct berval ** result;
@@ -2929,6 +2938,7 @@ anniversary_populate (EContact *contact,
 
 static struct berval **
 anniversary_ber (EContact *contact,
+                const gchar *ldap_attr,
                 GError **error)
 {
        EContactDate *dt;
@@ -2985,6 +2995,7 @@ birthday_populate (EContact *contact,
 
 static struct berval **
 birthday_ber (EContact *contact,
+             const gchar *ldap_attr,
              GError **error)
 {
        EContactDate *dt;
@@ -3045,6 +3056,7 @@ category_populate (EContact *contact,
 
 static struct berval **
 category_ber (EContact *contact,
+             const gchar *ldap_attr,
              GError **error)
 {
        struct berval ** result = NULL;
@@ -3278,6 +3290,7 @@ address_ber (EContact *card,
 
 static struct berval **
 home_address_ber (EContact *card,
+                 const gchar *ldap_attr,
                  GError **error)
 {
        return address_ber (card, E_CONTACT_ADDRESS_LABEL_HOME, error);
@@ -3285,6 +3298,7 @@ home_address_ber (EContact *card,
 
 static struct berval **
 work_address_ber (EContact *card,
+                 const gchar *ldap_attr,
                  GError **error)
 {
        return address_ber (card, E_CONTACT_ADDRESS_LABEL_WORK, error);
@@ -3292,6 +3306,7 @@ work_address_ber (EContact *card,
 
 static struct berval **
 other_address_ber (EContact *card,
+                  const gchar *ldap_attr,
                   GError **error)
 {
        return address_ber (card, E_CONTACT_ADDRESS_LABEL_OTHER, error);
@@ -3354,6 +3369,7 @@ photo_populate (EContact *contact,
 
 static struct berval **
 photo_ber (EContact *contact,
+          const gchar *ldap_attr,
           GError **error)
 {
        struct berval **result = NULL;
@@ -3425,6 +3441,7 @@ cert_populate (EContact *contact,
 
 static struct berval **
 cert_ber (EContact *contact,
+         const gchar *ldap_attr,
          GError **error)
 {
        struct berval **result = NULL;
@@ -3471,6 +3488,85 @@ cert_compare (EContact *ecard1,
        return equal;
 }
 
+static void
+org_unit_populate (EContact *contact,
+                  gchar **values)
+{
+       if (values[0] && *values[0]) {
+               gchar *org_unit = e_contact_get (contact, E_CONTACT_ORG_UNIT);
+               if (org_unit && *org_unit) {
+                       gchar *tmp;
+
+                       tmp = g_strconcat (org_unit, ";", values[0], NULL);
+                       e_contact_set (contact, E_CONTACT_ORG_UNIT, tmp);
+                       g_free (tmp);
+               } else {
+                       e_contact_set (contact, E_CONTACT_ORG_UNIT, values[0]);
+               }
+
+               g_free (org_unit);
+       }
+}
+
+static struct berval **
+org_unit_ber (EContact *contact,
+             const gchar *ldap_attr,
+             GError **error)
+{
+       struct berval ** result;
+       gchar *ptr;
+       gchar *org_unit;
+
+       org_unit = e_contact_get (contact, E_CONTACT_ORG_UNIT);
+       if (!org_unit || !*org_unit) {
+               g_free (org_unit);
+               return NULL;
+       }
+
+       ptr = strchr (org_unit, ';');
+
+       if (g_strcmp0 (ldap_attr, "departmentNumber") == 0) {
+               gchar *tmp;
+
+               if (!ptr || !ptr[1]) {
+                       g_free (org_unit);
+                       return NULL;
+               }
+
+               tmp = g_strdup (ptr + 1);
+               g_free (org_unit);
+               org_unit = tmp;
+       } else if (ptr) {
+               *ptr = '\0';
+       }
+
+       result = g_new (struct berval *, 2);
+       result[0] = g_new (struct berval, 1);
+       result[0]->bv_val = org_unit;
+       result[0]->bv_len = strlen (org_unit);
+       result[1] = NULL;
+
+       return result;
+}
+
+static gboolean
+org_unit_compare (EContact *contact1,
+                 EContact *contact2)
+{
+       gchar *org_unit1, *org_unit2;
+       gboolean equal;
+
+       org_unit1 = e_contact_get (contact1, E_CONTACT_ORG_UNIT);
+       org_unit2 = e_contact_get (contact1, E_CONTACT_ORG_UNIT);
+
+       equal = g_strcmp0 (org_unit1, org_unit2);
+
+       g_free (org_unit1);
+       g_free (org_unit2);
+
+       return equal;
+}
+
 typedef struct {
        EBookBackendLDAP *bl;
 } EBookBackendLDAPSExpData;


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]