From f78e95d0919bc0ae01e178d828a9db7f5300ca02 Mon Sep 17 00:00:00 2001
From: Jordan Bieder <jordan.bieder@uliege.be>
Date: Wed, 2 Oct 2019 15:44:07 +0200
Subject: [PATCH 1/3] Fix issue with autologin with LDAP and user email
 differente than NC user@domain. See issue #11 and #88

---
 rainloop/admin.php                 |  1 +
 rainloop/ajax/admin.php            |  2 ++
 rainloop/app.php                   | 11 ++++++++++-
 rainloop/templates/admin-local.php | 16 ++++++++++++++--
 4 files changed, 27 insertions(+), 3 deletions(-)

diff --git a/rainloop/admin.php b/rainloop/admin.php
index ff67530a..57cc3043 100755
--- a/rainloop/admin.php
+++ b/rainloop/admin.php
@@ -15,4 +15,5 @@
 $oTemplate = new OCP\Template('rainloop', 'admin-local');
 $oTemplate->assign('rainloop-admin-panel-link', OC_RainLoop_Helper::getAppUrl().'?admin');
 $oTemplate->assign('rainloop-autologin', \OC::$server->getConfig()->getAppValue('rainloop', 'rainloop-autologin', false));
+$oTemplate->assign('rainloop-autologin-with-email', \OC::$server->getConfig()->getAppValue('rainloop', 'rainloop-autologin-with-email', false));
 return $oTemplate->fetchPage();
diff --git a/rainloop/ajax/admin.php b/rainloop/ajax/admin.php
index 588136de..70e90b43 100755
--- a/rainloop/ajax/admin.php
+++ b/rainloop/ajax/admin.php
@@ -20,6 +20,8 @@
 {
 	\OC::$server->getConfig()->setAppValue('rainloop', 'rainloop-autologin', isset($_POST['rainloop-autologin']) ?
 		'1' === $_POST['rainloop-autologin'] : false);
+	\OC::$server->getConfig()->setAppValue('rainloop', 'rainloop-autologin-with-email', isset($_POST['rainloop-autologin']) ?
+		'2' === $_POST['rainloop-autologin'] : false);
 
 	$bAutologin = \OC::$server->getConfig()->getAppValue('rainloop', 'rainloop-autologin', false);
 }
diff --git a/rainloop/app.php b/rainloop/app.php
index 31390325..a66074ce 100755
--- a/rainloop/app.php
+++ b/rainloop/app.php
@@ -36,19 +36,28 @@
 		$sEncodedPassword = '';
 
 		$sUser = OCP\User::getUser();
+    $sPasswordSalt = '';
 
 		if (\OC::$server->getConfig()->getAppValue('rainloop', 'rainloop-autologin', false))
 		{
 			$sEmail = $sUser;
+      $sPasswordSalt = $sUser;
+			$sEncodedPassword = \OC::$server->getConfig()->getUserValue($sUser, 'rainloop', 'rainloop-autologin-password', '');
+		}
+		else if (\OC::$server->getConfig()->getAppValue('rainloop', 'rainloop-autologin-with-email', false))
+		{
+			$sEmail = \OC::$server->getConfig()->getUserValue($sUser, 'settings', 'email','');
+      $sPasswordSalt = $sUser;
 			$sEncodedPassword = \OC::$server->getConfig()->getUserValue($sUser, 'rainloop', 'rainloop-autologin-password', '');
 		}
 		else
 		{
 			$sEmail = \OC::$server->getConfig()->getUserValue($sUser, 'rainloop', 'rainloop-email', '');
+      $sPasswordSalt = $sEmail
 			$sEncodedPassword = \OC::$server->getConfig()->getUserValue($sUser, 'rainloop', 'rainloop-password', '');
 		}
 
-		$sDecodedPassword = OC_RainLoop_Helper::decodePassword($sEncodedPassword, md5($sEmail));
+		$sDecodedPassword = OC_RainLoop_Helper::decodePassword($sEncodedPassword, md5($sPasswordSalt));
 
 		$_ENV['___rainloop_owncloud_email'] = $sEmail;
 		$_ENV['___rainloop_owncloud_password'] = $sDecodedPassword;
diff --git a/rainloop/templates/admin-local.php b/rainloop/templates/admin-local.php
index 67bba5ec..1bbf1f7a 100755
--- a/rainloop/templates/admin-local.php
+++ b/rainloop/templates/admin-local.php
@@ -14,13 +14,25 @@
 			</p>
 			<br />
 			<?php endif; ?>
-			<p>      
+			<p>
+				<div style="display: flex;">
+					<input type="radio" id="rainloop-noautologin" name="rainloop-autologin" value="0" <?php if (!$_['rainloop-autologin']&&!$_['rainloop-autologin-with-email']): ?>checked="checked"<?php endif; ?> />
+					<label style="margin: auto 5px;" for="rainloop-noautologin">
+						<?php p($l->t('Do not automatically login')); ?>
+					</label>
+				</div>
 				<div style="display: flex;">
-					<input style="cursor: pointer;" type="checkbox" id="rainloop-autologin" name="rainloop-autologin" value="1" <?php if ($_['rainloop-autologin']): ?>checked="checked"<?php endif; ?> />
+					<input type="radio" id="rainloop-autologin" name="rainloop-autologin" value="1" <?php if ($_['rainloop-autologin']): ?>checked="checked"<?php endif; ?> />
 					<label style="margin: auto 5px;" for="rainloop-autologin">
 						<?php p($l->t('Automatically login with Nextcloud user credentials')); ?>
 					</label>
 				</div>
+				<div style="display: flex;">
+					<input type="radio" id="rainloop-autologin-with-email" name="rainloop-autologin" value="2" <?php if ($_['rainloop-autologin-with-email']): ?>checked="checked"<?php endif; ?> />
+					<label style="margin: auto 5px;" for="rainloop-autologin-with-email">
+						<?php p($l->t('Automatically login with Nextcloud user email credentials')); ?>
+					</label>
+				</div>
 				<br />
 				<br />
 				<input type="button" id="rainloop-save-button" name="rainloop-save-button" value="<?php p($l->t('Save')); ?>" />

From 6c7931e234c8bf5f2e77c0921c6e3f99889185c2 Mon Sep 17 00:00:00 2001
From: Jordan Bieder <jordan.bieder@uliege.be>
Date: Thu, 24 Oct 2019 20:03:59 +0200
Subject: [PATCH 2/3] Fix missing coma. Beautification for tabs alignment Fix
 autologin with autologin with email which did not work ...

---
 rainloop/app.php         | 10 +++++-----
 rainloop/appinfo/app.php |  2 +-
 2 files changed, 6 insertions(+), 6 deletions(-)
 mode change 100755 => 100644 rainloop/appinfo/app.php

diff --git a/rainloop/app.php b/rainloop/app.php
index a66074ce..7fe66c75 100755
--- a/rainloop/app.php
+++ b/rainloop/app.php
@@ -1,4 +1,4 @@
-<?php
+appinfo/app.php<?php
 
 /**
  * Nextcloud - RainLoop mail plugin
@@ -36,24 +36,24 @@
 		$sEncodedPassword = '';
 
 		$sUser = OCP\User::getUser();
-    $sPasswordSalt = '';
+		$sPasswordSalt = '';
 
 		if (\OC::$server->getConfig()->getAppValue('rainloop', 'rainloop-autologin', false))
 		{
 			$sEmail = $sUser;
-      $sPasswordSalt = $sUser;
+			$sPasswordSalt = $sUser;
 			$sEncodedPassword = \OC::$server->getConfig()->getUserValue($sUser, 'rainloop', 'rainloop-autologin-password', '');
 		}
 		else if (\OC::$server->getConfig()->getAppValue('rainloop', 'rainloop-autologin-with-email', false))
 		{
 			$sEmail = \OC::$server->getConfig()->getUserValue($sUser, 'settings', 'email','');
-      $sPasswordSalt = $sUser;
+			$sPasswordSalt = $sUser;
 			$sEncodedPassword = \OC::$server->getConfig()->getUserValue($sUser, 'rainloop', 'rainloop-autologin-password', '');
 		}
 		else
 		{
 			$sEmail = \OC::$server->getConfig()->getUserValue($sUser, 'rainloop', 'rainloop-email', '');
-      $sPasswordSalt = $sEmail
+			$sPasswordSalt = $sEmail;
 			$sEncodedPassword = \OC::$server->getConfig()->getUserValue($sUser, 'rainloop', 'rainloop-password', '');
 		}
 
diff --git a/rainloop/appinfo/app.php b/rainloop/appinfo/app.php
old mode 100755
new mode 100644
index cd21ed0d..496c3a68
--- a/rainloop/appinfo/app.php
+++ b/rainloop/appinfo/app.php
@@ -13,7 +13,7 @@
 OCP\App::registerAdmin('rainloop', 'admin');
 OCP\App::registerPersonal('rainloop', 'personal');
 
-if (\OC::$server->getConfig()->getAppValue('rainloop', 'rainloop-autologin', false))
+if (\OC::$server->getConfig()->getAppValue('rainloop', 'rainloop-autologin', false) || \OC::$server->getConfig()->getAppValue('rainloop', 'rainloop-autologin-with-email', false))
 {
 	OCP\Util::connectHook('OC_User', 'post_login', 'OC_RainLoop_Helper', 'login');
 	OCP\Util::connectHook('OC_User', 'post_setPassword', 'OC_RainLoop_Helper', 'changePassword');

From d10ccf47e1291684863c0fbc5ddbe010f475c3a9 Mon Sep 17 00:00:00 2001
From: Jordan Bieder <jordan.bieder@uliege.be>
Date: Fri, 25 Oct 2019 15:25:06 +0200
Subject: [PATCH 3/3] Fix typo again ..

---
 rainloop/app.php | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/rainloop/app.php b/rainloop/app.php
index 7fe66c75..4ab70ee8 100755
--- a/rainloop/app.php
+++ b/rainloop/app.php
@@ -1,4 +1,4 @@
-appinfo/app.php<?php
+<?php
 
 /**
  * Nextcloud - RainLoop mail plugin