From b714652e1020246d8365d9f9c307370168e5b792 Mon Sep 17 00:00:00 2001
From: Jason Houle <jhoule@connecticutchildrens.org>
Date: Mon, 12 Oct 2020 12:33:55 -0400
Subject: [PATCH 1/2] Import thumbnail photos when LDAP users are created.

---
 app/Auth/Access/Guards/LdapSessionGuard.php | 12 +++++++++++-
 app/Auth/Access/LdapService.php             |  1 +
 app/Config/services.php                     |  2 ++
 3 files changed, 14 insertions(+), 1 deletion(-)

diff --git a/app/Auth/Access/Guards/LdapSessionGuard.php b/app/Auth/Access/Guards/LdapSessionGuard.php
index 652141c0c..f67c04f82 100644
--- a/app/Auth/Access/Guards/LdapSessionGuard.php
+++ b/app/Auth/Access/Guards/LdapSessionGuard.php
@@ -117,7 +117,17 @@ class LdapSessionGuard extends ExternalBaseSessionGuard
             'password' => Str::random(32),
         ];
 
-        return $this->registrationService->registerUser($details, null, false);
+        $user = $this->registrationService->registerUser($details, null, false);
+
+        if (config('services.ldap.import_thumbnail_photos')) {
+            $imageService = app()->make(ImageService::class);
+            $image = $imageService->saveNewFromBase64Uri('data:image/jpg;base64,'.base64_encode($ldapUserDetails['avatar']), $ldapUserDetails['uid'].'.jpg', 'user');
+
+            $user['image_id'] = $image->id;
+            $user->save();
+        }
+
+        return $user;
     }
 
 }
diff --git a/app/Auth/Access/LdapService.php b/app/Auth/Access/LdapService.php
index 92234edcf..47dc24532 100644
--- a/app/Auth/Access/LdapService.php
+++ b/app/Auth/Access/LdapService.php
@@ -89,6 +89,7 @@ class LdapService extends ExternalAuthService
             'name'  => $this->getUserResponseProperty($user, $displayNameAttr, $userCn),
             'dn'    => $user['dn'],
             'email' => $this->getUserResponseProperty($user, $emailAttr, null),
+            'avatar'=> $this->getUserResponseProperty($user, $thumbnailAttr, null),
         ];
 
         if ($this->config['dump_user_details']) {
diff --git a/app/Config/services.php b/app/Config/services.php
index fcde621d2..230234e4c 100644
--- a/app/Config/services.php
+++ b/app/Config/services.php
@@ -132,6 +132,8 @@ return [
         'group_attribute' => env('LDAP_GROUP_ATTRIBUTE', 'memberOf'),
         'remove_from_groups' => env('LDAP_REMOVE_FROM_GROUPS', false),
         'tls_insecure' => env('LDAP_TLS_INSECURE', false),
+        'import_thumbnail_photos' => env('LDAP_IMPORT_THUMBNAIL_PHOTOS', false),
+        'thumbnail_attribute' => env('LDAP_THUMBNAIL_ATTRIBUTE', 'thumbnailPhoto'),
     ],
 
 ];

From a192b600fc818313ef26b139a9a2d11d4d17f0a4 Mon Sep 17 00:00:00 2001
From: Jason Houle <jhoule@connecticutchildrens.org>
Date: Mon, 12 Oct 2020 12:47:36 -0400
Subject: [PATCH 2/2] Missed a variable when updating LdapService.

---
 app/Auth/Access/LdapService.php | 1 +
 1 file changed, 1 insertion(+)

diff --git a/app/Auth/Access/LdapService.php b/app/Auth/Access/LdapService.php
index 47dc24532..a7ee3b374 100644
--- a/app/Auth/Access/LdapService.php
+++ b/app/Auth/Access/LdapService.php
@@ -76,6 +76,7 @@ class LdapService extends ExternalAuthService
         $idAttr = $this->config['id_attribute'];
         $emailAttr = $this->config['email_attribute'];
         $displayNameAttr = $this->config['display_name_attribute'];
+        $thumbnailAttr = $this->config['thumbnail_attribute'];
 
         $user = $this->getUserWithAttributes($userName, ['cn', 'dn', $idAttr, $emailAttr, $displayNameAttr]);