From 4bbabe3c0b90cd5f286fe6ab94eb1d535fea5aa4 Mon Sep 17 00:00:00 2001
From: Valentin Tolmer <valentin@tolmer.fr>
Date: Tue, 9 Aug 2022 09:43:55 +0200
Subject: [PATCH] migration-tool: Import users' avatars

---
 Cargo.lock                  |  1 +
 migration-tool/Cargo.toml   |  1 +
 migration-tool/src/ldap.rs  | 19 ++++++++++++++-----
 migration-tool/src/lldap.rs | 17 ++++-------------
 4 files changed, 20 insertions(+), 18 deletions(-)

diff --git a/Cargo.lock b/Cargo.lock
index 31524a1..4910602 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -2278,6 +2278,7 @@ name = "migration-tool"
 version = "0.3.0-alpha.1"
 dependencies = [
  "anyhow",
+ "base64",
  "graphql_client 0.11.0",
  "ldap3",
  "lldap_auth",
diff --git a/migration-tool/Cargo.toml b/migration-tool/Cargo.toml
index 848507c..852002f 100644
--- a/migration-tool/Cargo.toml
+++ b/migration-tool/Cargo.toml
@@ -6,6 +6,7 @@ authors = ["Valentin Tolmer <valentin@tolmer.fr>"]
 
 [dependencies]
 anyhow = "*"
+base64 = "0.13"
 rand = "0.8"
 requestty = "0.4.1"
 serde = "1"
diff --git a/migration-tool/src/ldap.rs b/migration-tool/src/ldap.rs
index 1f86d4c..8beec7a 100644
--- a/migration-tool/src/ldap.rs
+++ b/migration-tool/src/ldap.rs
@@ -184,14 +184,23 @@ impl TryFrom<ResultEntry> for User {
             .or_else(|| get_optional_attribute("name"))
             .or_else(|| get_optional_attribute("displayName"));
         let first_name = get_optional_attribute("givenName");
+        let avatar = entry
+            .attrs
+            .get("jpegPhoto")
+            .map(|v| v.iter().map(|s| s.as_bytes().to_vec()).collect::<Vec<_>>())
+            .or_else(|| entry.bin_attrs.get("jpegPhoto").map(Clone::clone))
+            .and_then(|v| v.into_iter().next().filter(|s| !s.is_empty()));
         let password =
             get_optional_attribute("userPassword").or_else(|| get_optional_attribute("password"));
         Ok(User::new(
-            id,
-            email,
-            display_name,
-            first_name,
-            last_name,
+            crate::lldap::CreateUserInput {
+                id,
+                email,
+                display_name,
+                first_name,
+                last_name,
+                avatar: avatar.map(base64::encode),
+            },
             password,
             entry.dn,
         ))
diff --git a/migration-tool/src/lldap.rs b/migration-tool/src/lldap.rs
index 564f125..7f61c57 100644
--- a/migration-tool/src/lldap.rs
+++ b/migration-tool/src/lldap.rs
@@ -70,23 +70,12 @@ pub struct User {
 impl User {
     // https://github.com/graphql-rust/graphql-client/issues/386
     pub fn new(
-        id: String,
-        email: String,
-        display_name: Option<String>,
-        first_name: Option<String>,
-        last_name: Option<String>,
+        user_input: create_user::CreateUserInput,
         password: Option<String>,
         dn: String,
     ) -> User {
         User {
-            user_input: create_user::CreateUserInput {
-                id,
-                email,
-                display_name,
-                first_name,
-                last_name,
-                avatar: None,
-            },
+            user_input,
             password,
             dn,
         }
@@ -103,6 +92,8 @@ impl User {
 )]
 struct CreateUser;
 
+pub type CreateUserInput = create_user::CreateUserInput;
+
 #[derive(GraphQLQuery)]
 #[graphql(
     schema_path = "../schema.graphql",