"virtualmin-change-password" code fixes abd refzctoring

This commit is contained in:
RainLoop Team 2015-05-04 21:11:59 +04:00
parent aed686bcdf
commit 86bb1d0dcf
4 changed files with 131 additions and 206 deletions

View file

@ -1 +1 @@
This plugin utilizes Virtualmin's remote API to change user passwords. The plugin requires the Admin user name and password to succesfully execute the password change. The host and port where Virtualmin listens on is also needed. See https://www.virtualmin.com/documentation/developer/http for more information. This plugin utilizes Virtualmin's remote API to change user passwords. The plugin requires the Admin user name and password to succesfully execute the password change. The host and port where Virtualmin listens on is also needed. See https://www.virtualmin.com/documentation/developer/http for more information.

View file

@ -1 +1 @@
1.0 1.0

View file

@ -1,35 +1,43 @@
<?php <?php
/* /*
This Virtualmin Password Change Plugin was developed by Icedman21 * This Virtualmin Password Change Plugin was developed by Icedman21
http://icedman21.com * http://icedman21.com
*/ */
class VirtualminChangePasswordDriver implements \RainLoop\Providers\ChangePassword\ChangePasswordInterface class VirtualminChangePasswordDriver implements \RainLoop\Providers\ChangePassword\ChangePasswordInterface
{ {
/** /**
* @var string * @var string
*/ */
private $sAllowedEmails = ''; private $sAllowedEmails = '';
/** /**
* @var string * @var string
*/ */
private $sHost = ''; private $sHost = '';
/** /**
* @var string * @var string
*/ */
private $sAdminUser = ''; private $sAdminUser = '';
/** /**
* @var string * @var string
*/ */
private $sAdminPassword = ''; private $sAdminPassword = '';
/** /**
* @param string $sUser * @var \MailSo\Log\Logger
* @param string $sPassword */
* private $oLogger = null;
* @return \DirectAdminChangePasswordDriver
*/ /**
* @param string $sHost
* @param string $sAdminUser
* @param string $sAdminPassword
*
* @return \VirtualminChangePasswordDriver
*/
public function SetConfig($sHost, $sAdminUser, $sAdminPassword) public function SetConfig($sHost, $sAdminUser, $sAdminPassword)
{ {
$this->sHost = $sHost; $this->sHost = $sHost;
@ -38,21 +46,24 @@ class VirtualminChangePasswordDriver implements \RainLoop\Providers\ChangePasswo
return $this; return $this;
} }
/** /**
* @param string $sAllowedEmails * @param string $sAllowedEmails
* *
* @return \ChangePasswordExampleDriver * @return \VirtualminChangePasswordDriver
*/ */
public function SetAllowedEmails($sAllowedEmails) public function SetAllowedEmails($sAllowedEmails)
{ {
$this->sAllowedEmails = $sAllowedEmails; $this->sAllowedEmails = $sAllowedEmails;
return $this; return $this;
} }
/** /**
* @param \MailSo\Log\Logger $oLogger * @param \MailSo\Log\Logger $oLogger
* *
* @return \HmailserverChangePasswordDriver * @return \VirtualminChangePasswordDriver
*/ */
public function SetLogger($oLogger) public function SetLogger($oLogger)
{ {
if ($oLogger instanceof \MailSo\Log\Logger) if ($oLogger instanceof \MailSo\Log\Logger)
@ -62,226 +73,138 @@ class VirtualminChangePasswordDriver implements \RainLoop\Providers\ChangePasswo
return $this; return $this;
} }
/**
* @param \RainLoop\Model\Account $oAccount
*
* @return bool
*/
public function PasswordChangePossibility($oAccount)
{
return $oAccount && $oAccount->Email() &&
\RainLoop\Plugins\Helper::ValidateWildcardValues($oAccount->Email(), $this->sAllowedEmails);
}
/** /**
* @param string $sUrl * @param string $sDesc
* @param array $aPost = array() * @param int $iType = \MailSo\Log\Enumerations\Type::INFO
* @param string $sCustomUserAgent = 'MailSo Http User Agent (v1)' *
* @param int $iCode = 0 * @return \VirtualminChangePasswordDriver
* @param \MailSo\Log\Logger $oLogger = null */
* @param int $iTimeout = 20 public function WriteLog($sDesc, $iType = \MailSo\Log\Enumerations\Type::INFO)
* @param string $sProxy = ''
* @param string $sProxyAuth = ''
*
* @return string|bool
*
* Had to costumize use this as the builtin SendPostRequest in \MailSo\Base\Http had no way of setting CURLOPT_USERPWD
*/
public function PostRequest($sUrl, $aPost = array(), $sAdminUser, $sAdminPassword, $sCustomUserAgent = 'Rainloop Http User Agent (v1)', &$iCode = 0,
$oLogger = null, $iTimeout = 20, $sProxy = '', $sProxyAuth = '')
{
$oLogger->Write('Virtualmin: Inside function: ');
$aOptions = array(
CURLOPT_URL => $sUrl,
CURLOPT_HEADER => false,
CURLOPT_FAILONERROR => true,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => \http_build_query($aPost, '', '&'),
CURLOPT_TIMEOUT => (int) $iTimeout
);
if ($oLogger)
{
$oLogger->Write('Virtualmin: Inside function2: ');
}
if (0 < \strlen($sAdminUser) && 0 < \strlen($sAdminPassword))
{
$aOptions[CURLOPT_USERPWD] = $sAdminUser.':'.$sAdminPassword;
}
if (0 < \strlen($sCustomUserAgent))
{
$aOptions[CURLOPT_USERAGENT] = $sCustomUserAgent;
}
if (0 < \strlen($sProxy))
{
$aOptions[CURLOPT_PROXY] = $sProxy;
if (0 < \strlen($sProxyAuth))
{
$aOptions[CURLOPT_PROXYUSERPWD] = $sProxyAuth;
}
}
if ($oLogger)
{
$oLogger->Write('Virtualmin: before init: ');
}
$oCurl = \curl_init();
\curl_setopt_array($oCurl, $aOptions);
if ($oLogger)
{
$oLogger->Write('cURL: Send post request: '.$sUrl);
}
$mResult = \curl_exec($oCurl);
$iCode = (int) \curl_getinfo($oCurl, CURLINFO_HTTP_CODE);
$sContentType = (string) \curl_getinfo($oCurl, CURLINFO_CONTENT_TYPE);
if ($oLogger)
{
$oLogger->Write('cURL: Post request result: (Status: '.$iCode.', ContentType: '.$sContentType.')');
if (false === $mResult || 200 !== $iCode)
{
$oLogger->Write('cURL: Error: '.\curl_error($oCurl), \MailSo\Log\Enumerations\Type::WARNING);
}
}
if (\is_resource($oCurl))
{
\curl_close($oCurl);
}
return $mResult;
}
/**
* @param \RainLoop\Model\Account $oAccount
* @param string $sPrevPassword
* @param string $sNewPassword
*
* @return bool
*/
public function ChangePassword(\RainLoop\Account $oAccount, $sPrevPassword, $sNewPassword)
{ {
if ($this->oLogger) if ($this->oLogger)
{ {
$this->oLogger->Write('Virtualmin: Try to change password for '.$oAccount->Email()); $this->oLogger->Write($sDesc, $iType);
} }
return $this;
}
/**
* @param \RainLoop\Model\Account $oAccount
*
* @return bool
*/
public function PasswordChangePossibility($oAccount)
{
return $oAccount && $oAccount->Email() &&
\RainLoop\Plugins\Helper::ValidateWildcardValues($oAccount->Email(), $this->sAllowedEmails);
}
/**
* @param \RainLoop\Model\Account $oAccount
* @param string $sPrevPassword
* @param string $sNewPassword
*
* @return bool
*/
public function ChangePassword(\RainLoop\Account $oAccount, $sPrevPassword, $sNewPassword)
{
$this->WriteLog('Virtualmin: Try to change password for '.$oAccount->Email());
$bResult = false; $bResult = false;
if (!empty($this->sHost) && !empty($this->sAdminUser) && !empty($this->sAdminPassword) && $oAccount) if (!empty($this->sHost) && !empty($this->sAdminUser) && !empty($this->sAdminPassword) && $oAccount)
{ {
if ($this->oLogger) $this->WriteLog('Virtualmin:[Check] Required Fields Present');
{
$this->oLogger->Write('Virtualmin:[Check] Required Fields Present');
}
$sEmail = \trim(\strtolower($oAccount->Email())); $sEmail = \trim(\strtolower($oAccount->Email()));
$sEmailUser = \MailSo\Base\Utils::GetAccountNameFromEmail($sEmail); $sEmailUser = \MailSo\Base\Utils::GetAccountNameFromEmail($sEmail);
$sEmailDomain = \MailSo\Base\Utils::GetDomainFromEmail($sEmail); $sEmailDomain = \MailSo\Base\Utils::GetDomainFromEmail($sEmail);
$sHost = \trim($this->sHost);
$sHost = \rtrim(\trim($this->sHost), '/');
$sUrl = $sHost.'/virtual-server/remote.cgi'; $sUrl = $sHost.'/virtual-server/remote.cgi';
$sAdminUser = $this->sAdminUser; $sAdminUser = $this->sAdminUser;
$sAdminPassword=$this->sAdminPassword; $sAdminPassword = $this->sAdminPassword;
$iCode = 0; $iCode = 0;
$aPost = array( $aPost = array(
'user' => $sEmailUser, 'user' => $sEmailUser,
'pass' => $sNewPassword, 'pass' => $sNewPassword,
'domain' => $sEmailDomain, 'domain' => $sEmailDomain,
'program' => 'modify-user' 'program' => 'modify-user'
); );
$aOptions = array( $aOptions = array(
CURLOPT_URL => $sUrl, CURLOPT_URL => $sUrl,
CURLOPT_HEADER => false, CURLOPT_HEADER => false,
CURLOPT_FAILONERROR => true, CURLOPT_FAILONERROR => true,
CURLOPT_SSL_VERIFYPEER => false, CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_RETURNTRANSFER => true, CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true, CURLOPT_POST => true,
CURLOPT_POSTFIELDS => http_build_query($aPost, '', '&'), CURLOPT_POSTFIELDS => \http_build_query($aPost, '', '&'),
CURLOPT_TIMEOUT => 20, CURLOPT_TIMEOUT => 20,
CURLOPT_SSL_VERIFYHOST => false, CURLOPT_SSL_VERIFYHOST => false,
CURLOPT_USERPWD => $sAdminUser.':'.$sAdminPassword CURLOPT_USERPWD => $sAdminUser.':'.$sAdminPassword
); );
$oCurl = \curl_init(); $oCurl = \curl_init();
\curl_setopt_array($oCurl, $aOptions); \curl_setopt_array($oCurl, $aOptions);
if ($this->oLogger) $this->WriteLog('Virtualmin: Send post request: '.$sUrl);
{
$this->oLogger->Write('Virtualmin: Send post request: '.$sUrl);
}
$mResult = \curl_exec($oCurl); $mResult = \curl_exec($oCurl);
$iCode = (int) \curl_getinfo($oCurl, CURLINFO_HTTP_CODE); $iCode = (int) \curl_getinfo($oCurl, CURLINFO_HTTP_CODE);
$sContentType = (string) \curl_getinfo($oCurl, CURLINFO_CONTENT_TYPE); $sContentType = (string) \curl_getinfo($oCurl, CURLINFO_CONTENT_TYPE);
if ($this->oLogger) $this->WriteLog('Virtualmin: Post request result: (Status: '.$iCode.', ContentType: '.$sContentType.')');
if (false === $mResult || 200 !== $iCode)
{ {
$this->oLogger->Write('Virtualmin: Post request result: (Status: '.$iCode.', ContentType: '.$sContentType.')'); $this->WriteLog('Virtualmin: Error: '.\curl_error($oCurl), \MailSo\Log\Enumerations\Type::WARNING);
if (false === $mResult || 200 !== $iCode)
{
$this->oLogger->Write('Virtualmin: Error: '.\curl_error($oCurl), \MailSo\Log\Enumerations\Type::WARNING);
}
} }
if (\is_resource($oCurl)) if (\is_resource($oCurl))
{ {
\curl_close($oCurl); \curl_close($oCurl);
} }
if (false !== $mResult && 200 === $iCode) if (false !== $mResult && 200 === $iCode)
{ {
$aRes = null; $aRes = null;
@\parse_str($mResult, $aRes); @\parse_str($mResult, $aRes);
if (is_array($aRes) && (!isset($aRes['error']) || (int) $aRes['error'] !== 1)) if (\is_array($aRes) && (!isset($aRes['error']) || (int) $aRes['error'] !== 1))
{ {
$iPos = strpos($mResult, 'Exit status: '); $iPos = \strpos($mResult, 'Exit status: ');
if ($iPos !== false) {
$sStatus = explode(' ', $mResult); if ($iPos !== false)
$sStatus=\trim(array_pop($sStatus)); {
$aStatus = \explode(' ', $mResult);
if($sStatus=='0'){ $sStatus = \trim(\array_pop($aStatus));
if ($this->oLogger)
{ if ('0' === $sStatus)
$this->oLogger->Write('Virtualmin: Password Change Status: Success'); {
} $this->WriteLog('Virtualmin: Password Change Status: Success');
$bResult = true; $bResult = true;
} }
else else
{ {
if ($this->oLogger) $this->WriteLog('Virtualmin[Error]: Response: '.$mResult);
{
$this->oLogger->Write('Virtualmin[Error]: Response: '.$mResult);
}
} }
} }
} }
else else
{ {
if ($this->oLogger) $this->WriteLog('Virtualmin[Error]: Response: '.$mResult);
{
$this->oLogger->Write('Virtualmin[Error]: Response: '.$mResult);
}
} }
} }
else else
{ {
if ($this->oLogger) $this->WriteLog('Virtualmin[Error]: Empty Response: Code: '.$iCode);
{
$this->oLogger->Write('Virtualmin[Error]: Empty Response: Code:'.$iCode);
}
} }
} }
return $bResult; return $bResult;
} }
} }

View file

@ -18,12 +18,14 @@ class VirtualminChangePasswordPlugin extends \RainLoop\Plugins\AbstractPlugin
case 'change-password': case 'change-password':
include_once __DIR__.'/VirtualminChangePasswordDriver.php'; include_once __DIR__.'/VirtualminChangePasswordDriver.php';
$sHost = \trim($this->Config()->Get('plugin', 'host', '')); $sHost = \trim($this->Config()->Get('plugin', 'host', ''));
$sAdminUser = (string) $this->Config()->Get('plugin', 'admin_user', ''); $sAdminUser = (string) $this->Config()->Get('plugin', 'admin_user', '');
$sAdminPassword = (string) $this->Config()->Get('plugin', 'admin_password', ''); $sAdminPassword = (string) $this->Config()->Get('plugin', 'admin_password', '');
$oProvider = new VirtualminChangePasswordDriver();
$oProvider = new \VirtualminChangePasswordDriver();
$oProvider->SetLogger($this->Manager()->Actions()->Logger()); $oProvider->SetLogger($this->Manager()->Actions()->Logger());
$oProvider->SetConfig($sHost,$sAdminUser, $sAdminPassword); $oProvider->SetConfig($sHost, $sAdminUser, $sAdminPassword);
$oProvider->SetAllowedEmails(\strtolower(\trim($this->Config()->Get('plugin', 'allowed_emails', '')))); $oProvider->SetAllowedEmails(\strtolower(\trim($this->Config()->Get('plugin', 'allowed_emails', ''))));
break; break;
@ -36,13 +38,13 @@ class VirtualminChangePasswordPlugin extends \RainLoop\Plugins\AbstractPlugin
public function configMapping() public function configMapping()
{ {
return array( return array(
\RainLoop\Plugins\Property::NewInstance('host')->SetLabel('Virtualmin Host') \RainLoop\Plugins\Property::NewInstance('host')->SetLabel('Virtualmin Host')
->SetDefaultValue('https://localhost:10000') ->SetDefaultValue('https://localhost:10000')
->SetDescription('Virtualmin host URL. Example: https://example.com:10000'), ->SetDescription('Virtualmin host URL. Example: https://example.com:10000'),
\RainLoop\Plugins\Property::NewInstance('admin_user')->SetLabel('Admin User') \RainLoop\Plugins\Property::NewInstance('admin_user')->SetLabel('Admin User')
->SetDefaultValue(''), ->SetDefaultValue(''),
\RainLoop\Plugins\Property::NewInstance('admin_password')->SetLabel('Admin Password') \RainLoop\Plugins\Property::NewInstance('admin_password')->SetLabel('Admin Password')
->SetDefaultValue(''), ->SetDefaultValue(''),
\RainLoop\Plugins\Property::NewInstance('allowed_emails')->SetLabel('Allowed emails') \RainLoop\Plugins\Property::NewInstance('allowed_emails')->SetLabel('Allowed emails')
->SetType(\RainLoop\Enumerations\PluginPropertyType::STRING_TEXT) ->SetType(\RainLoop\Enumerations\PluginPropertyType::STRING_TEXT)
->SetDescription('Allowed emails, space as delimiter, wildcard supported. Example: user1@domain1.net user2@domain1.net *@domain2.net') ->SetDescription('Allowed emails, space as delimiter, wildcard supported. Example: user1@domain1.net user2@domain1.net *@domain2.net')