Always login in Nextcloud for unified search to work #96

This commit is contained in:
the-djmaze 2022-10-13 22:38:55 +02:00
parent 313d1efb14
commit dfc1058839
4 changed files with 80 additions and 76 deletions

View file

@ -87,14 +87,13 @@ class Application extends App implements IBootstrap
$sEmail = $config->getUserValue($user->getUID(), 'settings', 'email', ''); $sEmail = $config->getUserValue($user->getUID(), 'settings', 'email', '');
} }
if ($sEmail) { if ($sEmail) {
SnappyMailHelper::startApp(true); \OC::$server->getSession()['snappymail-password'] = SnappyMailHelper::encodePassword($password, \md5($sEmail));
\OC::$server->getSession()['snappymail-sso-hash'] = \RainLoop\Api::CreateUserSsoHash($sEmail, $password/*, array $aAdditionalOptions = array(), bool $bUseTimeout = true*/);
} }
}); });
$userSession->listen('\OC\User', 'logout', function($user) { $userSession->listen('\OC\User', 'logout', function($user) {
\OC::$server->getSession()['snappymail-sso-hash'] = ''; $oSession = \OC::$server->getSession()['snappymail-password'] = '';
SnappyMailHelper::startApp(true); SnappyMailHelper::startApp();
\RainLoop\Api::LogoutCurrentLogginedUser(); \RainLoop\Api::LogoutCurrentLogginedUser();
}); });
} }

View file

@ -28,24 +28,9 @@ class PageController extends Controller
\OCP\Util::addStyle('snappymail', 'style'); \OCP\Util::addStyle('snappymail', 'style');
$query = ''; $query = '';
// If the user has set credentials for SnappyMail in their personal $aCredentials = SnappyMailHelper::getLoginCredentials();
// settings, override everything before and use those instead. if ($aCredentials[0] && $aCredentials[1]) {
$sUID = \OC::$server->getUserSession()->getUser()->getUID(); $query = '?sso&hash=' . \RainLoop\Api::CreateUserSsoHash($aCredentials[0] && $aCredentials[1]);
$sEmail = $config->getUserValue($sUID, 'snappymail', 'snappymail-email', '');
if ($sEmail) {
$password = SnappyMailHelper::decodePassword(
$config->getUserValue($sUID, 'snappymail', 'snappymail-password', ''),
\md5($sEmail)
);
if ($password) {
$query = '?sso&hash=' . \RainLoop\Api::CreateUserSsoHash($sEmail, $password);
}
}
if (!$query) {
$session = \OC::$server->getSession();
if (!empty($session['snappymail-sso-hash'])) {
$query = '?sso&hash=' . $session['snappymail-sso-hash'];
}
} }
$params = [ $params = [
@ -68,6 +53,8 @@ class PageController extends Controller
public function appGet() public function appGet()
{ {
SnappyMailHelper::startApp(); SnappyMailHelper::startApp();
\RainLoop\Service::Handle();
exit;
} }
/** /**
@ -76,7 +63,7 @@ class PageController extends Controller
*/ */
public function appPost() public function appPost()
{ {
SnappyMailHelper::startApp(); SnappyMailHelper::startApp(true);
} }
/** /**
@ -85,7 +72,7 @@ class PageController extends Controller
*/ */
public function indexPost() public function indexPost()
{ {
SnappyMailHelper::startApp(); SnappyMailHelper::startApp(true);
} }
/** /**
@ -94,15 +81,14 @@ class PageController extends Controller
private static function index_embed() private static function index_embed()
{ {
if (!empty($_SERVER['QUERY_STRING'])) { if (!empty($_SERVER['QUERY_STRING'])) {
SnappyMailHelper::startApp(); SnappyMailHelper::startApp(true);
return;
} }
\OC::$server->getNavigationManager()->setActiveEntry('snappymail'); \OC::$server->getNavigationManager()->setActiveEntry('snappymail');
\OCP\Util::addStyle('snappymail', 'embed'); \OCP\Util::addStyle('snappymail', 'embed');
SnappyMailHelper::startApp(true); SnappyMailHelper::startApp();
$oConfig = \RainLoop\Api::Config(); $oConfig = \RainLoop\Api::Config();
$oActions = \RainLoop\Api::Actions(); $oActions = \RainLoop\Api::Actions();
$oHttp = \MailSo\Base\Http::SingletonInstance(); $oHttp = \MailSo\Base\Http::SingletonInstance();

View file

@ -43,9 +43,10 @@ class Provider implements IProvider
public function search(IUser $user, ISearchQuery $query): SearchResult public function search(IUser $user, ISearchQuery $query): SearchResult
{ {
$result = []; $result = [];
SnappyMailHelper::startApp(true); SnappyMailHelper::startApp();
$oActions = \RainLoop\Api::Actions(); $oActions = \RainLoop\Api::Actions();
$oAccount = $oActions->getAccountFromToken(false); $oAccount = $oActions->getMainAccountFromToken(false);
// $oAccount = $oActions->getAccountFromToken(false);
$iCursor = (int) $query->getCursor(); $iCursor = (int) $query->getCursor();
$iLimit = $query->getLimit(); $iLimit = $query->getLimit();
if ($oAccount) { if ($oAccount) {

View file

@ -5,7 +5,7 @@ namespace OCA\SnappyMail\Util;
class SnappyMailHelper class SnappyMailHelper
{ {
public static function startApp(bool $api = false) public static function startApp(bool $handle = false) : void
{ {
if (!\class_exists('RainLoop\\Api')) { if (!\class_exists('RainLoop\\Api')) {
$_ENV['SNAPPYMAIL_NEXTCLOUD'] = true; $_ENV['SNAPPYMAIL_NEXTCLOUD'] = true;
@ -18,47 +18,77 @@ class SnappyMailHelper
} }
}); });
if ($api) { $_ENV['SNAPPYMAIL_INCLUDE_AS_API'] = true;
$_ENV['SNAPPYMAIL_INCLUDE_AS_API'] = true;
}
require_once \dirname(\dirname(__DIR__)) . '/app/index.php'; require_once \dirname(\dirname(__DIR__)) . '/app/index.php';
if ($api) { $oConfig = \RainLoop\Api::Config();
$oConfig = \RainLoop\Api::Config(); $bSave = false;
$bSave = false; if (!$oConfig->Get('webmail', 'app_path')) {
if (!$oConfig->Get('webmail', 'app_path')) { $oConfig->Set('webmail', 'app_path', \OC::$server->getAppManager()->getAppWebPath('snappymail') . '/app/');
$oConfig->Set('webmail', 'app_path', \OC::$server->getAppManager()->getAppWebPath('snappymail') . '/app/'); $bSave = true;
$bSave = true;
}
if (!\is_dir(APP_PLUGINS_PATH . 'nextcloud')) {
\SnappyMail\Repository::installPackage('plugin', 'nextcloud');
$oConfig->Set('plugins', 'enable', true);
$aList = \SnappyMail\Repository::getEnabledPackagesNames();
$aList[] = 'nextcloud';
$oConfig->Set('plugins', 'enabled_list', \implode(',', \array_unique($aList)));
$oConfig->Set('webmail', 'theme', 'Nextcloud@custom');
$bSave = true;
}
$bSave && $oConfig->Save();
} }
if (!\is_dir(APP_PLUGINS_PATH . 'nextcloud')) {
\SnappyMail\Repository::installPackage('plugin', 'nextcloud');
$oConfig->Set('plugins', 'enable', true);
$aList = \SnappyMail\Repository::getEnabledPackagesNames();
$aList[] = 'nextcloud';
$oConfig->Set('plugins', 'enabled_list', \implode(',', \array_unique($aList)));
$oConfig->Set('webmail', 'theme', 'Nextcloud@custom');
$bSave = true;
}
$bSave && $oConfig->Save();
$oActions = \RainLoop\Api::Actions();
if (!$oActions->getMainAccountFromToken(false)) {
$aCredentials = SnappyMailHelper::getLoginCredentials();
if ($aCredentials[0] && $aCredentials[1]) {
$oActions->Logger()->AddSecret($aCredentials[1]);
$oAccount = $oActions->LoginProcess($aCredentials[0], $aCredentials[1], false);
if ($oAccount) {
$oActions->Plugins()->RunHook('login.success', array($oAccount));
$oActions->SetAuthToken($oAccount);
}
}
}
}
if ($handle) {
\RainLoop\Service::Handle();
exit;
} }
} }
/** public static function getLoginCredentials() : array
* @return string {
*/ $config = \OC::$server->getConfig();
public static function getAppUrl() $sEmail = '';
$sPassword = '';
$sUID = \OC::$server->getUserSession()->getUser()->getUID();
// Only store the user's password in the current session if they have
// enabled auto-login using Nextcloud username or email address.
if ($config->getAppValue('snappymail', 'snappymail-autologin', false)) {
$sEmail = $sUID;
$sPassword = \OC::$server->getSession()['snappymail-password'];
} else if ($config->getAppValue('snappymail', 'snappymail-autologin-with-email', false)) {
$sEmail = $config->getUserValue($sUID, 'settings', 'email', '');
$sPassword = \OC::$server->getSession()['snappymail-password'];
}
// If the user has set credentials for SnappyMail in their personal
// settings, override everything before and use those instead.
$sEmail = $config->getUserValue($sUID, 'snappymail', 'snappymail-email', '');
if ($sEmail) {
$sPassword = $config->getUserValue($sUID, 'snappymail', 'snappymail-password', '');
}
return [$sEmail, $sPassword ? SnappyMailHelper::decodePassword($sPassword, \md5($sEmail)) : ''];
}
public static function getAppUrl() : string
{ {
return \OC::$server->getURLGenerator()->linkToRoute('snappymail.page.appGet'); return \OC::$server->getURLGenerator()->linkToRoute('snappymail.page.appGet');
} }
/** public static function normalizeUrl(string $sUrl) : string
* @param string $sUrl
*
* @return string
*/
public static function normalizeUrl($sUrl)
{ {
$sUrl = \rtrim(\trim($sUrl), '/\\'); $sUrl = \rtrim(\trim($sUrl), '/\\');
if ('.php' !== \strtolower(\substr($sUrl, -4))) { if ('.php' !== \strtolower(\substr($sUrl, -4))) {
@ -68,27 +98,15 @@ class SnappyMailHelper
return $sUrl; return $sUrl;
} }
/** public static function encodePassword(string $sPassword, string $sSalt) : string
* @param string $sPassword
* @param string $sSalt
*
* @return string
*/
public static function encodePassword($sPassword, $sSalt)
{ {
static::startApp(true); static::startApp();
return \SnappyMail\Crypt::EncryptUrlSafe($sPassword, $sSalt); return \SnappyMail\Crypt::EncryptUrlSafe($sPassword, $sSalt);
} }
/** public static function decodePassword(string $sPassword, string $sSalt)/* : mixed */
* @param string $sPassword
* @param string $sSalt
*
* @return string
*/
public static function decodePassword($sPassword, $sSalt)
{ {
static::startApp(true); static::startApp();
return \SnappyMail\Crypt::DecryptUrlSafe($sPassword, $sSalt); return \SnappyMail\Crypt::DecryptUrlSafe($sPassword, $sSalt);
} }
} }