Merge pull request #1845 from erseco/rest-change-password-plugin

Added generic REST change password plugin
This commit is contained in:
RainLoop Team 2019-03-28 01:08:25 +03:00 committed by GitHub
commit b7ad459a16
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 272 additions and 0 deletions

View file

@ -0,0 +1,20 @@
The MIT License (MIT)
Copyright (c) 2013 RainLoop Team
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

View file

@ -0,0 +1 @@
Plugin that adds functionality to change the email account password (Generic REST).

View file

@ -0,0 +1,172 @@
<?php
class RestChangePasswordDriver implements \RainLoop\Providers\ChangePassword\ChangePasswordInterface
{
/**
* @var string
*/
private $sUrl = '';
/**
* @var string
*/
private $sKey = '';
/**
* @var string
*/
private $sFieldEmail = '';
/**
* @var string
*/
private $sFieldOldpassword = '';
/**
* @var string
*/
private $sFieldNewpassword = '';
/**
* @var string
*/
private $sAllowedEmails = '';
/**
* @var \MailSo\Log\Logger
*/
private $oLogger = null;
/**
* @param string $sHost
* @param int $iPort
*
* @return \RestChangePasswordDriver
*/
public function SetConfig($sUrl, $sKey)
{
$this->sUrl = $sUrl;
$this->sKey = $sKey;
return $this;
}
$oProvider->SetFieldNames($sFieldEmail, $sFieldOldpassword, $sFieldNewpassword);
/**
* @param string $sFieldEmail
* @param string $sFieldOldpassword
* @param string $sFieldNewpassword
*
* @return \RestChangePasswordDriver
*/
public function SetFieldNames($sFieldEmail, $sFieldOldpassword, $sFieldNewpassword)
{
$this->sFieldEmail = $sFieldEmail;
$this->sFieldOldpassword = $sFieldOldpassword;
$this->sFieldNewpassword = $sFieldNewpassword;
return $this;
}
/**
* @param string $sAllowedEmails
*
* @return \RestChangePasswordDriver
*/
public function SetAllowedEmails($sAllowedEmails)
{
$this->sAllowedEmails = $sAllowedEmails;
return $this;
}
/**
* @param \MailSo\Log\Logger $oLogger
*
* @return \RestChangePasswordDriver
*/
public function SetLogger($oLogger)
{
if ($oLogger instanceof \MailSo\Log\Logger)
{
$this->oLogger = $oLogger;
}
return $this;
}
/**
* @param \RainLoop\Account $oAccount
*
* @return bool
*/
public function PasswordChangePossibility($oAccount)
{
return $oAccount && $oAccount->Email() &&
\RainLoop\Plugins\Helper::ValidateWildcardValues($oAccount->Email(), $this->sAllowedEmails);
}
/**
* @param \RainLoop\Account $oAccount
* @param string $sPrevPassword
* @param string $sNewPassword
*
* @return bool
*/
public function ChangePassword(\RainLoop\Account $oAccount, $sPrevPassword, $sNewPassword)
{
if ($this->oLogger)
{
$this->oLogger->Write('Rest: Try to change password for '.$oAccount->Email());
}
$bResult = false;
if (!empty($this->sHost) && 0 < $this->iPort && $oAccount)
{
$sEmail = \trim(\strtolower($oAccount->Email()));
# Adding the REST Api key to the url, try to use always https
$sUrl = str_replace('://', '://'+$this->sKey+"@", $this->sUrl);
$iCode = 0;
$oHttp = \MailSo\Base\Http::SingletonInstance();
if ($this->oLogger)
{
$this->oLogger->Write('Rest[Api Request]:'.$sUrl);
}
$mResult = $oHttp->SendPostRequest($sUrl,
array(
$this->sFieldEmail => $sEmail,
$this->sFieldOldpassword => $sPrevPassword,
$this->sFieldNewpassword => $sNewPassword,
), 'MailSo Http User Agent (v1)', $iCode, $this->oLogger);
if (false !== $mResult && 200 === $iCode)
{
$aRes = null;
@\parse_str($mResult, $aRes);
if (is_array($aRes) && (!isset($aRes['error']) || (int) $aRes['error'] !== 1))
{
$bResult = true;
}
else
{
if ($this->oLogger)
{
$this->oLogger->Write('Rest[Error]: Response: '.$mResult);
}
}
}
else
{
if ($this->oLogger)
{
$this->oLogger->Write('Rest[Error]: Empty Response: Code:'.$iCode);
}
}
}
return $bResult;
}
}

View file

@ -0,0 +1 @@
1.0

View file

@ -0,0 +1,78 @@
<?php
class RestChangePasswordPlugin extends \RainLoop\Plugins\AbstractPlugin
{
public function Init()
{
$this->addHook('main.fabrica', 'MainFabrica');
}
/**
* @param string $sName
* @param mixed $oProvider
*/
public function MainFabrica($sName, &$oProvider)
{
switch ($sName)
{
case 'change-password':
$sUrl = \trim($this->Config()->Get('plugin', 'rest_url', ''));
$sKey = \trim($this->Config()->Get('plugin', 'rest_key', ''));
$sFieldEmail = \trim($this->Config()->Get('plugin', 'rest_field_email', ''));
$sFieldOldpassword = \trim($this->Config()->Get('plugin', 'rest_field_oldpassword', ''));
$sFieldNewpassword = \trim($this->Config()->Get('plugin', 'rest_field_newpassword', ''));
if (!empty($sHost) && (!empty($sKey)))
{
include_once __DIR__.'/RestChangePasswordDriver.php';
$oProvider = new RestChangePasswordDriver();
$oProvider->SetLogger($this->Manager()->Actions()->Logger());
$oProvider->SetConfig($sUrl, $sKey);
$oProvider->SetFieldNames($sFieldEmail, $sFieldOldpassword, $sFieldNewpassword);
$oProvider->SetAllowedEmails(\strtolower(\trim($this->Config()->Get('plugin', 'allowed_emails', ''))));
}
break;
}
}
/**
* @return array
*/
public function configMapping()
{
return array(
\RainLoop\Plugins\Property::NewInstance('rest_url')
->SetLabel('REST API Url')
->SetDefaultValue('')
->SetDescription('Ex: http://localhost:8080/api/change_password or https://domain.com/api/user/passsword_update'),
\RainLoop\Plugins\Property::NewInstance('rest_key')
->SetLabel('REST API key')
->SetType(\RainLoop\Enumerations\PluginPropertyType::PASSWORD)
->SetDescription('REST API Key for authentication, if you have "user" and "passsword" enter it as "user:password"')
->SetDefaultValue(''),
\RainLoop\Plugins\Property::NewInstance('rest_field_email')
->SetLabel('Field "email" name')
->SetType(\RainLoop\Enumerations\PluginPropertyType::STRING_TEXT)
->SetDescription('Enter the name of the REST field name for email')
->SetDefaultValue('email'),
\RainLoop\Plugins\Property::NewInstance('rest_field_oldpassword')
->SetLabel('Field "oldpassword" name')
->SetType(\RainLoop\Enumerations\PluginPropertyType::STRING_TEXT)
->SetDescription('Enter the name of the REST field name for oldpassword')
->SetDefaultValue('oldpassword'),
\RainLoop\Plugins\Property::NewInstance('rest_field_newpassword')
->SetLabel('Field "newpassword" name')
->SetType(\RainLoop\Enumerations\PluginPropertyType::STRING_TEXT)
->SetDescription('Enter the name of the REST field name for newpassword')
->SetDefaultValue('newpassword'),
\RainLoop\Plugins\Property::NewInstance('allowed_emails')->SetLabel('Allowed emails')
->SetType(\RainLoop\Enumerations\PluginPropertyType::STRING_TEXT)
->SetDescription('Allowed emails, space as delimiter, wildcard supported. Example: user1@domain1.net user2@domain1.net *@domain2.net')
->SetDefaultValue('*')
);
}
}