mirror of
https://github.com/YunoHost-Apps/rainloop_ynh.git
synced 2024-09-03 20:16:18 +02:00
new plugins:
- add mail suggesstion from ldap
This commit is contained in:
parent
aa209f8f04
commit
f564778115
8 changed files with 199 additions and 35 deletions
|
@ -138,7 +138,7 @@ sign_me_auto = "DefaultOff"
|
||||||
enable = On
|
enable = On
|
||||||
|
|
||||||
; List of enabled plugins
|
; List of enabled plugins
|
||||||
enabled_list = "ynh-login-mapping"
|
enabled_list = "ynh-login-mapping,ynh-ldap-addressbooks"
|
||||||
|
|
||||||
[defaults]
|
[defaults]
|
||||||
; Editor mode used by default (Plain, Html, HtmlForced or PlainForced)
|
; Editor mode used by default (Plain, Html, HtmlForced or PlainForced)
|
||||||
|
|
|
@ -82,8 +82,8 @@ sudo cp ../conf/data/domains/disabled $rainloop_path/data/_data_/_default_/domai
|
||||||
|
|
||||||
# now install ynh plugins:
|
# now install ynh plugins:
|
||||||
sudo mkdir -p $rainloop_path/data/_data_/_default_/plugins
|
sudo mkdir -p $rainloop_path/data/_data_/_default_/plugins
|
||||||
sudo cp -rf ../sources/plugins/ynh-login-mapping $rainloop_path/data/_data_/_default_/plugins/.
|
sudo cp -rf ../sources/plugins/ynh-login-mapping $rainloop_path/data/_data_/_default_/plugins/.
|
||||||
|
sudo cp -rf ../sources/plugins/ynh-ldap-addressbooks $rainloop_path/data/_data_/_default_/plugins/.
|
||||||
|
|
||||||
# Hooks for domains are not implemented yet, so new domains will not be added automatically
|
# Hooks for domains are not implemented yet, so new domains will not be added automatically
|
||||||
|
|
||||||
|
|
|
@ -32,7 +32,8 @@ sudo cp ../sources/patch/index_auto_version.php $rainloop_path/index.php
|
||||||
|
|
||||||
# update ynh plugins:
|
# update ynh plugins:
|
||||||
sudo mkdir -p $rainloop_path/data/_data_/_default_/plugins
|
sudo mkdir -p $rainloop_path/data/_data_/_default_/plugins
|
||||||
sudo cp -rf ../sources/plugins/ynh-login-mapping $rainloop_path/data/_data_/_default_/plugins/.
|
sudo cp -rf ../sources/plugins/ynh-login-mapping $rainloop_path/data/_data_/_default_/plugins/.
|
||||||
|
sudo cp -rf ../sources/plugins/ynh-ldap-addressbooks $rainloop_path/data/_data_/_default_/plugins/.
|
||||||
|
|
||||||
# update SSO and auto version
|
# update SSO and auto version
|
||||||
if [ $final_path == $rainloop_path ]
|
if [ $final_path == $rainloop_path ]
|
||||||
|
|
1
sources/plugins/ynh-ldap-addressbooks/README
Normal file
1
sources/plugins/ynh-ldap-addressbooks/README
Normal file
|
@ -0,0 +1 @@
|
||||||
|
Plugin which allows to suggess email from ynh ldap
|
1
sources/plugins/ynh-ldap-addressbooks/VERSION
Normal file
1
sources/plugins/ynh-ldap-addressbooks/VERSION
Normal file
|
@ -0,0 +1 @@
|
||||||
|
0.1
|
150
sources/plugins/ynh-ldap-addressbooks/YnhLdapAddressbooks.php
Normal file
150
sources/plugins/ynh-ldap-addressbooks/YnhLdapAddressbooks.php
Normal file
|
@ -0,0 +1,150 @@
|
||||||
|
<?php
|
||||||
|
class YnhLdapAddressbooks implements \RainLoop\Providers\Suggestions\ISuggestions {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var \MailSo\Log\Logger
|
||||||
|
*/
|
||||||
|
private $oLogger = null;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param \RainLoop\Model\Account $oAccount
|
||||||
|
* @param string $sQuery
|
||||||
|
* @param int $iLimit = 20
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function Process($oAccount, $sQuery, $iLimit = 20) {
|
||||||
|
$sQuery = \trim($sQuery);
|
||||||
|
|
||||||
|
if (''===$sQuery || !$oAccount) {
|
||||||
|
return array ();
|
||||||
|
}
|
||||||
|
|
||||||
|
$aResult = $this->ldapSearch ( $oAccount, $sQuery, $iLimit );
|
||||||
|
|
||||||
|
$aResult = \RainLoop\Utils::RemoveSuggestionDuplicates($aResult);
|
||||||
|
if ($iLimit < \count($aResult)) {
|
||||||
|
$aResult = \array_slice($aResult, 0, $iLimit);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $aResult;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* OK search in ldap.
|
||||||
|
* match email or displayName...
|
||||||
|
*
|
||||||
|
* @param \RainLoop\Model\Account $oAccount
|
||||||
|
* @param string $sQuery
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
private function ldapSearch($oAccount, $sQuery, $iLimit) {
|
||||||
|
$sSearchEscaped = $this->escape($sQuery);
|
||||||
|
|
||||||
|
$aResult = array();
|
||||||
|
$oCon = @\ldap_connect();
|
||||||
|
if (! $oCon) {
|
||||||
|
$this->oLogger->Write('YnhLdapAddressbooks: Could not connect to LDAP server', \MailSo\Log\Enumerations\Type::ERROR );
|
||||||
|
return $aResult;
|
||||||
|
}
|
||||||
|
|
||||||
|
@\ldap_set_option($oCon, LDAP_OPT_PROTOCOL_VERSION, 3 );
|
||||||
|
|
||||||
|
if (!ldap_bind($oCon)) {
|
||||||
|
// bizard... ca renvoie false.... mais ca marche....
|
||||||
|
// $this->logLdapError ( $oCon, 'ldap_bind' );
|
||||||
|
// $this->Manager()->Actions()->Logger()->Write('YnhLdapAdressbooks: Could not bind to LDAP server', \MailSo\Log\Enumerations\Type::ERROR);
|
||||||
|
// return $aResult;
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->oLogger->Write('YnhLdapAddressbooks: connected to LDAP', \MailSo\Log\Enumerations\Type::INFO, 'LDAP' );
|
||||||
|
|
||||||
|
$sSearchDn='dc=yunohost,dc=org';
|
||||||
|
|
||||||
|
// on veut chercher parmis mail et nom utilisateur...
|
||||||
|
$sFilter = '(&(objectClass=inetOrgPerson)';
|
||||||
|
$sFilter .= '(|';
|
||||||
|
$sFilter .= '(mail=*' . $sSearchEscaped . '*)';
|
||||||
|
$sFilter .= '(displayName=*' . $sSearchEscaped . '*)';
|
||||||
|
$sFilter .= '))';
|
||||||
|
|
||||||
|
$aItems = array ( 'mail', 'displayName');
|
||||||
|
|
||||||
|
$this->oLogger->Write('YnhLdapAddressbooks: ldap_search : '.$sSearchDn.' / '.$sFilter, \MailSo\Log\Enumerations\Type::INFO, 'LDAP' );
|
||||||
|
|
||||||
|
$oS = @\ldap_search($oCon, $sSearchDn, $sFilter, $aItems, 0, $iLimit, $iLimit);
|
||||||
|
if ($oS) {
|
||||||
|
$aEntries = @\ldap_get_entries($oCon, $oS);
|
||||||
|
$this->oLogger->Write('YnhLdapAddressbooks: ldap_search => '.var_export($aEntries), \MailSo\Log\Enumerations\Type::INFO, 'LDAP');
|
||||||
|
if (is_array($aEntries)) {
|
||||||
|
if (isset ( $aEntries ['count'] )) { unset ( $aEntries ['count'] ); }
|
||||||
|
|
||||||
|
foreach ( $aEntries as $aItem ) {
|
||||||
|
if ($aItem) {
|
||||||
|
$sEmail = \trim($aItem['mail'] [0] );
|
||||||
|
$sName = \trim($aItem['displayname'] [0] );
|
||||||
|
|
||||||
|
if (!empty($sEmail)) {
|
||||||
|
$aResult[] = array($sEmail, $sName);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
$this->logLdapError($oCon, 'ldap_get_entries');
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
$this->logLdapError($oCon, 'ldap_search');
|
||||||
|
}
|
||||||
|
|
||||||
|
return $aResult;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param string $sStr
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
private function escape($sStr) {
|
||||||
|
$aNewChars = array ();
|
||||||
|
$aChars = array ('\\', '*',' (', ')', \chr(0));
|
||||||
|
|
||||||
|
foreach ( $aChars as $iIndex => $sValue ) {
|
||||||
|
$aNewChars [$iIndex] = '\\' . \str_pad ( \dechex ( \ord ( $sValue ) ), 2, '0' );
|
||||||
|
}
|
||||||
|
return \str_replace($aChars, $aNewChars, $sStr);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param mixed $oCon
|
||||||
|
* @param string $sCmd
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
private function logLdapError($oCon, $sCmd) {
|
||||||
|
if ($this->oLogger) {
|
||||||
|
$sError = $oCon ? @\ldap_error($oCon) : '';
|
||||||
|
$iErrno = $oCon ? @\ldap_errno($oCon) : 0;
|
||||||
|
|
||||||
|
$this->oLogger->Write($sCmd.' error: '.$sError.' ('. $iErrno.')', \MailSo\Log\Enumerations\Type::WARNING, 'LDAP' );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param \MailSo\Log\Logger $oLogger
|
||||||
|
*
|
||||||
|
* @return \LdapContactsSuggestions
|
||||||
|
*/
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param \MailSo\Log\Logger $oLogger
|
||||||
|
*/
|
||||||
|
public function SetLogger($oLogger) {
|
||||||
|
$this->oLogger = $oLogger instanceof \MailSo\Log\Logger ? $oLogger : null;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
36
sources/plugins/ynh-ldap-addressbooks/index.php
Normal file
36
sources/plugins/ynh-ldap-addressbooks/index.php
Normal file
|
@ -0,0 +1,36 @@
|
||||||
|
<?php
|
||||||
|
class YnhLdapAddressbooksPlugin extends \RainLoop\Plugins\AbstractPlugin {
|
||||||
|
|
||||||
|
public function Init() {
|
||||||
|
$this->addHook ( 'main.fabrica', 'MainFabrica' );
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* test if ldap is supported (but with ynh it nead ;) )
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function Supported() {
|
||||||
|
if (! \function_exists ( 'ldap_connect' )) {
|
||||||
|
return 'The LDAP PHP exention must be installed to use this plugin';
|
||||||
|
}
|
||||||
|
return '';
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $sName
|
||||||
|
* @param mixed $mResult
|
||||||
|
*/
|
||||||
|
public function MainFabrica($sName, &$mResult) {
|
||||||
|
if ($sName === 'suggestions') {
|
||||||
|
include_once __DIR__ . '/YnhLdapAddressbooks.php';
|
||||||
|
|
||||||
|
if (! \is_array ( $mResult )) {
|
||||||
|
$mResult = array ();
|
||||||
|
}
|
||||||
|
|
||||||
|
$oProvider = new YnhLdapAddressbooks ();
|
||||||
|
$mResult [] = $oProvider;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -1,23 +1,10 @@
|
||||||
<?php
|
<?php
|
||||||
class YnhLoginMappingPlugin extends \RainLoop\Plugins\AbstractPlugin {
|
class YnhLoginMappingPlugin extends \RainLoop\Plugins\AbstractPlugin {
|
||||||
|
|
||||||
public function Init() {
|
public function Init() {
|
||||||
// $this->addHook('filter.login-credentials.step-1', 'FilterLoginСredentials1');
|
|
||||||
// $this->addHook('filter.login-credentials.step-2', 'FilterLoginСredentials2');
|
|
||||||
$this->addHook ( 'filter.login-credentials', 'FilterLoginСredentials' );
|
$this->addHook ( 'filter.login-credentials', 'FilterLoginСredentials' );
|
||||||
}
|
}
|
||||||
|
|
||||||
/* // ca ne semble pas marcher...
|
|
||||||
* public function FilterLoginCredentials1(&$sEmail, &$sPassword) {
|
|
||||||
* $this->Manager()->Actions()->Logger()->Write('LdapLoginMappingPlugin::FilterLoginСredentials_S1:');
|
|
||||||
* }
|
|
||||||
*/
|
|
||||||
|
|
||||||
/* // ca non plus...
|
|
||||||
* public function FilterLoginCredentials2(&$sEmail, &$sPassword) {
|
|
||||||
* $this->Manager()->Actions()->Logger()->Write('LdapLoginMappingPlugin::FilterLoginСredentials_S2:');
|
|
||||||
* }
|
|
||||||
*/
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @param string $sEmail
|
* @param string $sEmail
|
||||||
|
@ -27,13 +14,13 @@ class YnhLoginMappingPlugin extends \RainLoop\Plugins\AbstractPlugin {
|
||||||
* @throws \RainLoop\Exceptions\ClientException
|
* @throws \RainLoop\Exceptions\ClientException
|
||||||
*/
|
*/
|
||||||
public function FilterLoginСredentials(&$sEmail, &$sLogin, &$sPassword) {
|
public function FilterLoginСredentials(&$sEmail, &$sLogin, &$sPassword) {
|
||||||
$this->Manager()->Actions()->Logger()->Write('LdapLoginMappingPlugin::FilterLoginСredentials IN => '.$sEmail.'/'.$sLogin, \MailSo\Log\Enumerations\Type::INFO);
|
$this->Manager()->Actions()->Logger()->Write('YnhLoginMappingPlugin::FilterLoginСredentials IN => '.$sEmail.'/'.$sLogin, \MailSo\Log\Enumerations\Type::INFO);
|
||||||
|
|
||||||
// connection au ldap ynh... en local
|
// connection au ldap ynh... en local
|
||||||
$cnx = ldap_connect (); // single connection
|
$cnx = ldap_connect (); // single connection
|
||||||
|
|
||||||
if (! $cnx) {
|
if (! $cnx) {
|
||||||
$this->Manager()->Actions()->Logger()->Write('LdapLoginMappingPlugin: Could not connect to LDAP server', \MailSo\Log\Enumerations\Type::ERROR );
|
$this->Manager()->Actions()->Logger()->Write('YnhLoginMappingPlugin: Could not connect to LDAP server', \MailSo\Log\Enumerations\Type::ERROR );
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -52,7 +39,7 @@ class YnhLoginMappingPlugin extends \RainLoop\Plugins\AbstractPlugin {
|
||||||
// OK un petit recherche
|
// OK un petit recherche
|
||||||
$sr = ldap_search ( $cnx, $dn, $filter, $justthese );
|
$sr = ldap_search ( $cnx, $dn, $filter, $justthese );
|
||||||
if (!$sr) {
|
if (!$sr) {
|
||||||
$this->Manager()->Actions()->Logger()->Write('LdapLoginMappingPlugin: search on LDAP server', \MailSo\Log\Enumerations\Type::ERROR );
|
$this->Manager()->Actions()->Logger()->Write('YnhLoginMappingPlugin: search on LDAP server', \MailSo\Log\Enumerations\Type::ERROR );
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
$result = ldap_get_entries ( $cnx, $sr );
|
$result = ldap_get_entries ( $cnx, $sr );
|
||||||
|
@ -61,22 +48,10 @@ class YnhLoginMappingPlugin extends \RainLoop\Plugins\AbstractPlugin {
|
||||||
if (($result['count'] > 0) && ($result[0]['uid']['count'] > 0)) {
|
if (($result['count'] > 0) && ($result[0]['uid']['count'] > 0)) {
|
||||||
$sLogin = $result[0]['uid'][0];
|
$sLogin = $result[0]['uid'][0];
|
||||||
} else {
|
} else {
|
||||||
$this->Manager()->Actions()->Logger()->Write('LdapLoginMappingPlugin: user not found', \MailSo\Log\Enumerations\Type::ERROR );
|
$this->Manager()->Actions()->Logger()->Write('YnhLoginMappingPlugin: user not found', \MailSo\Log\Enumerations\Type::ERROR );
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->Manager()->Actions()->Logger()->Write('LdapLoginMappingPlugin::FilterLoginСredentials OUT => '. $sEmail.'/'.$sLogin, \MailSo\Log\Enumerations\Type::INFO );
|
$this->Manager()->Actions()->Logger()->Write('YnhLoginMappingPlugin::FilterLoginСredentials OUT => '. $sEmail.'/'.$sLogin, \MailSo\Log\Enumerations\Type::INFO );
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
* // pas encore de configuration...
|
|
||||||
* public function configMapping()
|
|
||||||
* {
|
|
||||||
* return array(
|
|
||||||
* \RainLoop\Plugins\Property::NewInstance('mapping')->SetLabel('Mapping')
|
|
||||||
* ->SetType(\RainLoop\Enumerations\PluginPropertyType::STRING_TEXT)
|
|
||||||
* ->SetDescription('email:login mapping')
|
|
||||||
* ->SetDefaultValue("user@domain.com:user.bob\nadmin@domain.com:user.john2")
|
|
||||||
* );
|
|
||||||
* }
|
|
||||||
*/
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue