Skip to content

Commit

Permalink
Merge pull request #570 from magento-api/develop
Browse files Browse the repository at this point in the history
[API] Sprint 56
  • Loading branch information
Korshenko, Olexii(okorshenko) committed Sep 8, 2015
2 parents 7c59753 + 30a2135 commit 7575159
Show file tree
Hide file tree
Showing 108 changed files with 10,360 additions and 360 deletions.
4 changes: 3 additions & 1 deletion app/code/Magento/Captcha/Model/Observer.php
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,9 @@ public function checkUserForgotPasswordBackend($observer)
*/
public function resetAttemptForFrontend($observer)
{
return $this->_getResourceModel()->deleteUserAttempts($observer->getModel()->getEmail());
/** @var \Magento\Customer\Model\Customer $model */
$model = $observer->getModel();
return $this->_getResourceModel()->deleteUserAttempts($model->getEmail());
}

/**
Expand Down
18 changes: 0 additions & 18 deletions app/code/Magento/Cms/Setup/InstallData.php
Original file line number Diff line number Diff line change
Expand Up @@ -377,24 +377,6 @@ public function install(ModuleDataSetupInterface $setup, ModuleContextInterface
$installer->doUpdateClassAliases();

$setup->endSetup();

$cookieRestriction = $this->createPage()->load('privacy-policy-cookie-restriction-mode', 'identifier');

if ($cookieRestriction->getId()) {
$content = $cookieRestriction->getContent();
$replacment = '{{config path="general/store_information/street_line1"}} ' .
'{{config path="general/store_information/street_line2"}} ' .
'{{config path="general/store_information/city"}} ' .
'{{config path="general/store_information/postcode"}} ' .
'{{config path="general/store_information/region_id"}} ' .
'{{config path="general/store_information/country_id"}}';
$content = preg_replace(
'/{{config path="general\\/store_information\\/address"}}/ims',
$replacment,
$content
);
$cookieRestriction->setContent($content)->save();
}
}

/**
Expand Down
2 changes: 2 additions & 0 deletions app/code/Magento/Customer/Controller/Adminhtml/Index/Save.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ protected function _extractCustomerData()
CustomerInterface::DEFAULT_BILLING,
CustomerInterface::DEFAULT_SHIPPING,
'confirmation',
'sendemail_store_id',
];

$customerData = $this->_extractData(
Expand Down Expand Up @@ -226,6 +227,7 @@ public function execute()
['customer' => $customer, 'request' => $request]
);
$customer->setAddresses($addresses);
$customer->setStoreId($customerData['sendemail_store_id']);

// Save customer
if ($isExistingCustomer) {
Expand Down
3 changes: 2 additions & 1 deletion app/code/Magento/Customer/Model/AccountManagement.php
Original file line number Diff line number Diff line change
Expand Up @@ -378,9 +378,10 @@ public function authenticate($username, $password)
throw new EmailNotConfirmedException(__('This account is not confirmed.'));
}

$customerModel = $this->customerFactory->create()->updateData($customer);
$this->eventManager->dispatch(
'customer_customer_authenticated',
['model' => $this->getFullCustomerObject($customer), 'password' => $password]
['model' => $customerModel, 'password' => $password]
);

$this->eventManager->dispatch('customer_data_object_login', ['customer' => $customer]);
Expand Down
43 changes: 38 additions & 5 deletions app/code/Magento/Customer/Model/Observer.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
* See COPYING.txt for license details.
*/

// @codingStandardsIgnoreFile

namespace Magento\Customer\Model;

use Magento\Customer\Api\GroupManagementInterface;
Expand All @@ -14,12 +12,16 @@
use Magento\Framework\App\Area;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\App\State as AppState;
use Magento\Framework\DataObject;
use Magento\Framework\Encryption\Encryptor;
use Magento\Framework\Encryption\EncryptorInterface;
use Magento\Framework\Escaper;
use Magento\Framework\Message\ManagerInterface;
use Magento\Framework\Registry;
use Magento\Store\Model\ScopeInterface;
use Magento\Framework\Message\ManagerInterface;

/**
* Customer Observer Model
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class Observer
Expand Down Expand Up @@ -74,6 +76,13 @@ class Observer
*/
protected $appState;

/**
* Encryption model
*
* @var EncryptorInterface
*/
protected $encryptor;

/**
* @param Vat $customerVat
* @param HelperAddress $customerAddress
Expand All @@ -83,6 +92,7 @@ class Observer
* @param ManagerInterface $messageManager
* @param Escaper $escaper
* @param AppState $appState
* @param EncryptorInterface $encryptor
*/
public function __construct(
Vat $customerVat,
Expand All @@ -92,7 +102,8 @@ public function __construct(
ScopeConfigInterface $scopeConfig,
ManagerInterface $messageManager,
Escaper $escaper,
AppState $appState
AppState $appState,
EncryptorInterface $encryptor
) {
$this->_customerVat = $customerVat;
$this->_customerAddress = $customerAddress;
Expand All @@ -102,6 +113,7 @@ public function __construct(
$this->messageManager = $messageManager;
$this->escaper = $escaper;
$this->appState = $appState;
$this->encryptor = $encryptor;
}

/**
Expand Down Expand Up @@ -251,7 +263,7 @@ public function afterAddressSave($observer)
* Add success message for valid VAT ID
*
* @param Address $customerAddress
* @param $validationResult
* @param DataObject $validationResult
* @return $this
*/
protected function addValidMessage($customerAddress, $validationResult)
Expand Down Expand Up @@ -326,4 +338,25 @@ protected function addErrorMessage($customerAddress)
$this->messageManager->addError(implode(' ', $message));
return $this;
}

/**
* Upgrade customer password hash when customer has logged in
*
* @param EventObserver $observer
* @return void
*/
public function upgradeCustomerPassword($observer)
{
$password = $observer->getEvent()->getPassword();
/** @var \Magento\Customer\Model\Customer $model */
$model = $observer->getEvent()->getModel();
$isValidHash = $this->encryptor->isValidHashByVersion(
$password,
$model->getPasswordHash(),
Encryptor::HASH_VERSION_LATEST
);
if (!$isValidHash) {
$model->changePassword($password);
}
}
}
3 changes: 2 additions & 1 deletion app/code/Magento/Customer/README.md
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
The Magento_Customer module serves to handle the customer data (Customer, Customer Address and Customer Group entities) both in the admin panel and the storefront.
The Magento_Customer module serves to handle the customer data (Customer, Customer Address and Customer Group entities) both in the admin panel and the storefront.
For customer passwords, the module implements upgrading hashes.
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,7 @@ public function testExecuteWithExistentCustomer()
\Magento\Customer\Api\Data\CustomerInterface::DEFAULT_BILLING => $addressId,
\Magento\Customer\Api\Data\CustomerInterface::DEFAULT_SHIPPING => $addressId,
'confirmation' => false,
'sendemail_store_id' => '1',
'id' => $customerId,
];
$mergedAddressData = [
Expand Down
66 changes: 65 additions & 1 deletion app/code/Magento/Customer/Test/Unit/Model/ObserverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,16 @@ class ObserverTest extends \PHPUnit_Framework_TestCase
*/
protected $appState;

/**
* @var \Magento\Framework\Encryption\Encryptor|\PHPUnit_Framework_MockObject_MockObject
*/
protected $encryptorMock;

/**
* @var \Magento\Customer\Model\Customer|\PHPUnit_Framework_MockObject_MockObject
*/
protected $customerMock;

protected function setUp()
{
$this->vat = $this->getMockBuilder('Magento\Customer\Model\Vat')
Expand Down Expand Up @@ -99,6 +109,18 @@ protected function setUp()
->disableOriginalConstructor()
->getMock();

$this->encryptorMock = $this->getMockBuilder('\Magento\Framework\Encryption\Encryptor')
->disableOriginalConstructor()
->getMock();
$this->encryptorMock->expects($this->any())
->method('isValidHashByVersion')
->will(
$this->returnCallback(
function ($arg1, $arg2) {
return $arg1 == $arg2;
}
)
);
$this->model = new Observer(
$this->vat,
$this->helperAddress,
Expand All @@ -107,7 +129,8 @@ protected function setUp()
$this->scopeConfig,
$this->messageManager,
$this->escaper,
$this->appState
$this->appState,
$this->encryptorMock
);
}

Expand Down Expand Up @@ -746,4 +769,45 @@ public function dataProviderAfterAddressSaveNewGroup()
],
];
}

/**
* Test successfully password change if new password doesn't match old one
*/
public function testUpgradeCustomerPassword()
{
$customerMock = $this->getMockBuilder('\Magento\Customer\Model\Customer')
->disableOriginalConstructor()
->setMethods(['getPasswordHash', 'changePassword', '__wakeup'])
->getMock();
$customerMock->expects($this->once())->method('changePassword')->will($this->returnSelf());
$customerMock->expects($this->once())->method('getPasswordHash')->will($this->returnValue('old password'));

$event = new \Magento\Framework\DataObject();
$event->setData(['password' => 'different password', 'model' => $customerMock]);

$observerMock = new \Magento\Framework\DataObject();
$observerMock->setData('event', $event);

$this->model->upgradeCustomerPassword($observerMock);
}

/**
* Test failure password change if new password matches old one
*/
public function testUpgradeCustomerPasswordNotChanged()
{
$customerMock = $this->getMockBuilder('\Magento\Customer\Model\Customer')
->disableOriginalConstructor()
->setMethods(['getPasswordHash', 'changePassword', '__wakeup'])
->getMock();
$customerMock->expects($this->never())->method('changePassword');
$customerMock->expects($this->once())->method('getPasswordHash')->will($this->returnValue('same password'));

$event = new \Magento\Framework\DataObject();
$event->setData(['password' => 'same password', 'model' => $customerMock]);

$observerMock = new \Magento\Framework\DataObject();
$observerMock->setData('event', $event);
$this->model->upgradeCustomerPassword($observerMock);
}
}
3 changes: 3 additions & 0 deletions app/code/Magento/Customer/etc/frontend/events.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,7 @@
<event name="customer_login">
<observer name="customer_log_login" instance="Magento\Customer\Model\Observer\Log" method="logLastLoginAt" />
</event>
<event name="customer_customer_authenticated">
<observer name="customer_password" instance="Magento\Customer\Model\Observer" method="upgradeCustomerPassword" />
</event>
</config>
10 changes: 10 additions & 0 deletions app/code/Magento/Customer/view/base/ui_component/customer_form.xml
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,16 @@
</item>
</argument>
</field>
<field name="sendemail_store_id">
<argument name="data" xsi:type="array">
<item name="options" xsi:type="object">Magento\Store\Model\System\Store</item>
<item name="config" xsi:type="array">
<item name="label" xsi:type="string">Send Welcome Email From</item>
<item name="dataType" xsi:type="string">number</item>
<item name="formElement" xsi:type="string">select</item>
</item>
</argument>
</field>
</fieldset>
<fieldset name="address">
<argument name="data" xsi:type="array">
Expand Down
11 changes: 11 additions & 0 deletions app/code/Magento/Email/Model/Source/Variables.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,15 @@ public function toOptionArray($withGroup = false)
}
return $optionArray;
}

/**
* Return available config variables
*
* @return array
* @codeCoverageIgnore
*/
public function getData()
{
return $this->_configVariables;
}
}
28 changes: 25 additions & 3 deletions app/code/Magento/Email/Model/Template/Filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,11 @@ class Filter extends \Magento\Framework\Filter\Template
*/
protected $emogrifier;

/**
* @var \Magento\Email\Model\Source\Variables
*/
protected $configVariables;

/**
* @param \Magento\Framework\Stdlib\StringUtils $string
* @param \Psr\Log\LoggerInterface $logger
Expand All @@ -158,6 +163,7 @@ class Filter extends \Magento\Framework\Filter\Template
* @param \Magento\Framework\App\State $appState
* @param \Magento\Framework\UrlInterface $urlModel
* @param \Pelago\Emogrifier $emogrifier
* @param \Magento\Email\Model\Source\Variables $configVariables
* @param array $variables
*
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
Expand All @@ -175,6 +181,7 @@ public function __construct(
\Magento\Framework\App\State $appState,
\Magento\Framework\UrlInterface $urlModel,
\Pelago\Emogrifier $emogrifier,
\Magento\Email\Model\Source\Variables $configVariables,
$variables = []
) {
$this->_escaper = $escaper;
Expand All @@ -189,6 +196,7 @@ public function __construct(
$this->_appState = $appState;
$this->urlModel = $urlModel;
$this->emogrifier = $emogrifier;
$this->configVariables = $configVariables;
parent::__construct($string, $variables);
}

Expand Down Expand Up @@ -225,7 +233,7 @@ public function setUseSessionInUrl($flag)
*/
public function setPlainTemplateMode($plainTemplateMode)
{
$this->plainTemplateMode = (bool) $plainTemplateMode;
$this->plainTemplateMode = (bool)$plainTemplateMode;
return $this;
}

Expand All @@ -247,7 +255,7 @@ public function isPlainTemplateMode()
*/
public function setIsChildTemplate($isChildTemplate)
{
$this->isChildTemplate = (bool) $isChildTemplate;
$this->isChildTemplate = (bool)$isChildTemplate;
return $this;
}

Expand Down Expand Up @@ -703,7 +711,7 @@ public function configDirective($construction)
$configValue = '';
$params = $this->getParameters($construction[2]);
$storeId = $this->getStoreId();
if (isset($params['path'])) {
if (isset($params['path']) && $this->isAvailableConfigVariable($params['path'])) {
$configValue = $this->_scopeConfig->getValue(
$params['path'],
\Magento\Store\Model\ScopeInterface::SCOPE_STORE,
Expand All @@ -713,6 +721,20 @@ public function configDirective($construction)
return $configValue;
}

/**
* Check if given variable is available for directive "Config"
*
* @param string $variable
* @return bool
*/
private function isAvailableConfigVariable($variable)
{
return in_array(
$variable,
array_column($this->configVariables->getData(), 'value')
);
}

/**
* Custom Variable directive
*
Expand Down
Loading

0 comments on commit 7575159

Please sign in to comment.