Small improvements for #727

This commit is contained in:
the-djmaze 2022-11-30 11:54:55 +01:00
parent e92910d4a0
commit a4ea8a46a8
3 changed files with 56 additions and 52 deletions

View file

@ -3,7 +3,7 @@
use RainLoop\Enumerations\Capa; use RainLoop\Enumerations\Capa;
use MailSo\Log\Logger; use MailSo\Log\Logger;
use RainLoop\Actions; use RainLoop\Actions;
use RainLoop\Model\Account; use RainLoop\Model\MainAccount;
class LdapMailAccounts class LdapMailAccounts
{ {
@ -48,15 +48,15 @@ class LdapMailAccounts
/** /**
* @inheritDoc * @inheritDoc
* *
* Add additional mail accounts to the given primary account by looking up the ldap directory * Add additional mail accounts to the given primary account by looking up the ldap directory
* *
* The ldap lookup has to be configured in the plugin configuration of the extension (in the SnappyMail Admin Panel) * The ldap lookup has to be configured in the plugin configuration of the extension (in the SnappyMail Admin Panel)
* *
* @param Account $oAccount * @param MainAccount $oAccount
* @return bool true if additional accounts have been added or no additional accounts where found in . false if an error occured * @return bool true if additional accounts have been added or no additional accounts where found in . false if an error occured
*/ */
public function AddLdapMailAccounts(Account $oAccount): bool public function AddLdapMailAccounts(MainAccount $oAccount): bool
{ {
try { try {
$this->EnsureBound(); $this->EnsureBound();
@ -64,7 +64,7 @@ class LdapMailAccounts
return false; // exceptions are only thrown from the handleerror function that does logging already return false; // exceptions are only thrown from the handleerror function that does logging already
} }
// Try to get account information. Login() returns the username of the user // Try to get account information. Login() returns the username of the user
// and removes the domainname if this was configured inside the domain config. // and removes the domainname if this was configured inside the domain config.
$username = @ldap_escape($oAccount->Login(), "", LDAP_ESCAPE_FILTER); $username = @ldap_escape($oAccount->Login(), "", LDAP_ESCAPE_FILTER);
@ -74,7 +74,7 @@ class LdapMailAccounts
$searchString = str_replace("#USERNAME#", $username, $searchString); $searchString = str_replace("#USERNAME#", $username, $searchString);
$searchString = str_replace("#BASE_DN#", $this->config->base, $searchString); $searchString = str_replace("#BASE_DN#", $this->config->base, $searchString);
$this->logger->Write("ldap search string after replacement of placeholders: $searchString", \LOG_NOTICE, self::LOG_KEY); $this->logger->Write("ldap search string after replacement of placeholders: $searchString", \LOG_NOTICE, self::LOG_KEY);
try { try {
$mailAddressResults = $this->FindLdapResults( $mailAddressResults = $this->FindLdapResults(
@ -86,7 +86,7 @@ class LdapMailAccounts
$this->config->field_username, $this->config->field_username,
$this->config->field_domain $this->config->field_domain
); );
} }
catch (LdapMailAccountsException $e) { catch (LdapMailAccountsException $e) {
return false; // exceptions are only thrown from the handleerror function that does logging already return false; // exceptions are only thrown from the handleerror function that does logging already
} }
@ -101,7 +101,7 @@ class LdapMailAccounts
//Basing on https://github.com/the-djmaze/snappymail/issues/616 //Basing on https://github.com/the-djmaze/snappymail/issues/616
$oActions = \RainLoop\Api::Actions(); $oActions = \RainLoop\Api::Actions();
//Check if SnappyMail is configured to allow additional accounts //Check if SnappyMail is configured to allow additional accounts
if (!$oActions->GetCapa(Capa::ADDITIONAL_ACCOUNTS)) { if (!$oActions->GetCapa(Capa::ADDITIONAL_ACCOUNTS)) {
return $oActions->FalseResponse(__FUNCTION__); return $oActions->FalseResponse(__FUNCTION__);
@ -136,9 +136,9 @@ class LdapMailAccounts
//Try to login the user with the same password as the primary account has //Try to login the user with the same password as the primary account has
//if this fails the user will see the new mail addresses but will be asked for the correct password //if this fails the user will see the new mail addresses but will be asked for the correct password
$sPass = $oAccount->Password(); $sPass = $oAccount->Password();
$oNewAccount = RainLoop\Model\AdditionalAccount::NewInstanceFromCredentials($oActions, "$sUsername@$sDomain", $sUsername, $sPass); $oNewAccount = RainLoop\Model\AdditionalAccount::NewInstanceFromCredentials($oActions, "$sUsername@$sDomain", $sUsername, $sPass);
$aAccounts["$sUsername@$sDomain"] = $oNewAccount->asTokenArray($oAccount); $aAccounts["$sUsername@$sDomain"] = $oNewAccount->asTokenArray($oAccount);
} }
@ -150,7 +150,7 @@ class LdapMailAccounts
} }
else { else {
$this->logger->Write("Domain $sDomain is not part of configured domains in SnappyMail Admin Panel - mail address $sUsername@$sDomain will not be added.", \LOG_NOTICE, self::LOG_KEY); $this->logger->Write("Domain $sDomain is not part of configured domains in SnappyMail Admin Panel - mail address $sUsername@$sDomain will not be added.", \LOG_NOTICE, self::LOG_KEY);
} }
} }
if ($aAccounts) if ($aAccounts)
@ -164,9 +164,9 @@ class LdapMailAccounts
/** /**
* Checks if a connection to the LDAP was possible * Checks if a connection to the LDAP was possible
* *
* @throws LdapMailAccountsException * @throws LdapMailAccountsException
* *
* */ * */
private function EnsureConnected(): void private function EnsureConnected(): void
{ {
@ -201,11 +201,11 @@ class LdapMailAccounts
return true; return true;
} }
/** /**
* Ensures the plugin has been authenticated at the LDAP * Ensures the plugin has been authenticated at the LDAP
* *
* @throws LdapMailAccountsException * @throws LdapMailAccountsException
* *
* */ * */
private function EnsureBound(): void private function EnsureBound(): void
{ {
@ -219,7 +219,7 @@ class LdapMailAccounts
/** /**
* Authenticates the plugin at the LDAP using the username and password defined inside the configuration of the plugin * Authenticates the plugin at the LDAP using the username and password defined inside the configuration of the plugin
* *
* @return bool true if authentication was successful * @return bool true if authentication was successful
*/ */
private function Bind(): bool private function Bind(): bool
@ -237,7 +237,7 @@ class LdapMailAccounts
/** /**
* Handles and logs an eventual LDAP error * Handles and logs an eventual LDAP error
* *
* @param string $op * @param string $op
* @throws LdapMailAccountsException * @throws LdapMailAccountsException
*/ */
@ -254,9 +254,9 @@ class LdapMailAccounts
/** /**
* Looks up the LDAP for additional mail accounts * Looks up the LDAP for additional mail accounts
* *
* The search for additional mail accounts is done by a ldap search using the defined fields inside the configuration of the plugin (SnappyMail Admin Panel) * The search for additional mail accounts is done by a ldap search using the defined fields inside the configuration of the plugin (SnappyMail Admin Panel)
* *
* @param string $searchField * @param string $searchField
* @param string $searchString * @param string $searchString
* @param string $searchBase * @param string $searchBase
@ -268,15 +268,15 @@ class LdapMailAccounts
* @throws LdapMailAccountsException * @throws LdapMailAccountsException
*/ */
private function FindLdapResults( private function FindLdapResults(
string $searchField, string $searchField,
string $searchString, string $searchString,
string $searchBase, string $searchBase,
string $objectClass, string $objectClass,
string $nameField, string $nameField,
string $usernameField, string $usernameField,
string $domainField): array string $domainField): array
{ {
$this->EnsureBound(); $this->EnsureBound();
$nameField = strtolower($nameField); $nameField = strtolower($nameField);
$usernameField = strtolower($usernameField); $usernameField = strtolower($usernameField);
$domainField = strtolower($domainField); $domainField = strtolower($domainField);
@ -304,7 +304,7 @@ class LdapMailAccounts
$result = new LdapMailAccountResult(); $result = new LdapMailAccountResult();
$result->dn = $entry["dn"]; $result->dn = $entry["dn"];
$result->name = $this->LdapGetAttribute($entry, $nameField, true, true); $result->name = $this->LdapGetAttribute($entry, $nameField, true, true);
$result->username = $this->LdapGetAttribute($entry, $usernameField, true, true); $result->username = $this->LdapGetAttribute($entry, $usernameField, true, true);
$result->username = $this->RemoveEventualDomainPart($result->username); $result->username = $this->RemoveEventualDomainPart($result->username);
@ -319,10 +319,10 @@ class LdapMailAccounts
/** /**
* Removes an eventually found domain-part of an email address * Removes an eventually found domain-part of an email address
* *
* If the input string contains an '@' character the function returns the local-part before the '@'\ * If the input string contains an '@' character the function returns the local-part before the '@'\
* If no '@' character can be found the input string is returned. * If no '@' character can be found the input string is returned.
* *
* @param string $sInput * @param string $sInput
* @return string * @return string
*/ */
@ -338,15 +338,15 @@ class LdapMailAccounts
} }
return $sResult; return $sResult;
} }
/** /**
* Removes an eventually found local-part of an email address * Removes an eventually found local-part of an email address
* *
* If the input string contains an '@' character the function returns the domain-part behind the '@'\ * If the input string contains an '@' character the function returns the domain-part behind the '@'\
* If no '@' character can be found the input string is returned. * If no '@' character can be found the input string is returned.
* *
* @param string $sInput * @param string $sInput
* @return string * @return string
*/ */
@ -360,12 +360,12 @@ class LdapMailAccounts
} }
return $sResult; return $sResult;
} }
/** /**
* Gets LDAP attributes out of the input array * Gets LDAP attributes out of the input array
* *
* @param array $entry Array containing the result of a ldap search * @param array $entry Array containing the result of a ldap search
* @param string $attribute The name of the attribute to return * @param string $attribute The name of the attribute to return
* @param bool $single If true the function checks if exact one value for this attribute is inside the input array. If false an array is returned. Default true. * @param bool $single If true the function checks if exact one value for this attribute is inside the input array. If false an array is returned. Default true.

View file

@ -5,6 +5,7 @@ use RainLoop\Enumerations\PluginPropertyType;
use RainLoop\Plugins\AbstractPlugin; use RainLoop\Plugins\AbstractPlugin;
use RainLoop\Plugins\Property; use RainLoop\Plugins\Property;
use RainLoop\Model\Account; use RainLoop\Model\Account;
use RainLoop\Model\MainAccount;
use RainLoop\Actions; use RainLoop\Actions;
@ -37,17 +38,19 @@ class LdapMailAccountsPlugin extends AbstractPlugin
// Function gets called by RainLoop/Actions/User.php // Function gets called by RainLoop/Actions/User.php
/** /**
* Add additional mail accounts to the webinterface of the user by looking up the ldap directory * Add additional mail accounts to the webinterface of the user by looking up the ldap directory
* *
* @param Account $oAccount * @param Account $oAccount
*/ */
public function AddAdditionalLdapMailAccounts(Account $oAccount) public function AddAdditionalLdapMailAccounts(Account $oAccount)
{ {
// Set up config if ($oAccount instanceof MainAccount) {
$config = LdapMailAccountsConfig::MakeConfig($this->Config()); // Set up config
$config = LdapMailAccountsConfig::MakeConfig($this->Config());
$oldapMailAccounts = new LdapMailAccounts($config, $this->Manager()->Actions()->Logger()); $oldapMailAccounts = new LdapMailAccounts($config, $this->Manager()->Actions()->Logger());
$oldapMailAccounts->AddLdapMailAccounts($oAccount); $oldapMailAccounts->AddLdapMailAccounts($oAccount);
}
} }
/** /**
@ -91,7 +94,7 @@ class LdapMailAccountsPlugin extends AbstractPlugin
->SetLabel("Search field") ->SetLabel("Search field")
->SetType(PluginPropertyType::STRING) ->SetType(PluginPropertyType::STRING)
->SetDescription("The name of the ldap attribute that has to contain the here defined 'LDAP search string'.") ->SetDescription("The name of the ldap attribute that has to contain the here defined 'LDAP search string'.")
->SetDefaultValue("member"), ->SetDefaultValue("member"),
Property::NewInstance(LdapMailAccountsConfig::CONFIG_SEARCH_STRING) Property::NewInstance(LdapMailAccountsConfig::CONFIG_SEARCH_STRING)
->SetLabel("LDAP search string") ->SetLabel("LDAP search string")
@ -99,12 +102,12 @@ class LdapMailAccountsPlugin extends AbstractPlugin
->SetDescription("The search string used to find ldap objects of mail accounts the user has access to. ->SetDescription("The search string used to find ldap objects of mail accounts the user has access to.
\nPossible placeholers:\n#USERNAME# - replaced with the username of the actual SnappyMail user \nPossible placeholers:\n#USERNAME# - replaced with the username of the actual SnappyMail user
\n#BASE_DN# - replaced with the value inside the field 'User base DN'.") \n#BASE_DN# - replaced with the value inside the field 'User base DN'.")
->SetDefaultValue("uid=#USERNAME#"), ->SetDefaultValue("uid=#USERNAME#"),
Property::NewInstance(LdapMailAccountsConfig::CONFIG_FIELD_USERNAME) Property::NewInstance(LdapMailAccountsConfig::CONFIG_FIELD_USERNAME)
->SetLabel("Username field of additional account") ->SetLabel("Username field of additional account")
->SetType(PluginPropertyType::STRING) ->SetType(PluginPropertyType::STRING)
->SetDescription("The field containing the username of the found additional mail account. ->SetDescription("The field containing the username of the found additional mail account.
\nThis username gets used by SnappyMail to login to the additional mail account. \nThis username gets used by SnappyMail to login to the additional mail account.
\nIf this field contains an email address, only the local-part before the @ is used.") \nIf this field contains an email address, only the local-part before the @ is used.")
->SetDefaultValue("uid"), ->SetDefaultValue("uid"),
@ -112,10 +115,10 @@ class LdapMailAccountsPlugin extends AbstractPlugin
Property::NewInstance(LdapMailAccountsConfig::CONFIG_FIELD_MAIL_DOMAIN) Property::NewInstance(LdapMailAccountsConfig::CONFIG_FIELD_MAIL_DOMAIN)
->SetLabel("Domain name field of additional account") ->SetLabel("Domain name field of additional account")
->SetType(PluginPropertyType::STRING) ->SetType(PluginPropertyType::STRING)
->SetDescription("The field containing the domain name of the found additional mail account. ->SetDescription("The field containing the domain name of the found additional mail account.
\nThis domain gets looked up by SnappyMail to choose the right connection parameters at logging in to the additional mail account. \nThis domain gets looked up by SnappyMail to choose the right connection parameters at logging in to the additional mail account.
\nIf this field contains an email address, only the domain-part after the @ is used.") \nIf this field contains an email address, only the domain-part after the @ is used.")
->SetDefaultValue("mail"), ->SetDefaultValue("mail"),
Property::NewInstance(LdapMailAccountsConfig::CONFIG_FIELD_NAME) Property::NewInstance(LdapMailAccountsConfig::CONFIG_FIELD_NAME)
->SetLabel("Additional account name field") ->SetLabel("Additional account name field")

View file

@ -45,10 +45,11 @@ trait User
$this->Logger()->AddSecret($sPassword); $this->Logger()->AddSecret($sPassword);
$oAccount = $this->LoginProcess($sEmail, $sPassword, $bSignMe); $oAccount = $this->LoginProcess($sEmail, $sPassword, $bSignMe);
$this->Plugins()->RunHook('login.success', array($oAccount));
$this->SetAuthToken($oAccount); $this->SetAuthToken($oAccount);
$this->Plugins()->RunHook('login.success', array($oAccount));
$sLanguage = $this->GetActionParam('Language', ''); $sLanguage = $this->GetActionParam('Language', '');
if ($oAccount && $sLanguage) if ($oAccount && $sLanguage)
{ {