本文整理汇总了PHP中Magento\Customer\Model\Session::setCustomerDataAsLoggedIn方法的典型用法代码示例。如果您正苦于以下问题:PHP Session::setCustomerDataAsLoggedIn方法的具体用法?PHP Session::setCustomerDataAsLoggedIn怎么用?PHP Session::setCustomerDataAsLoggedIn使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Magento\Customer\Model\Session
的用法示例。
在下文中一共展示了Session::setCustomerDataAsLoggedIn方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: execute
/**
* Login post action
*
* @return \Magento\Framework\Controller\Result\Redirect
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
*/
public function execute()
{
if ($this->session->isLoggedIn() || !$this->formKeyValidator->validate($this->getRequest())) {
/** @var \Magento\Framework\Controller\Result\Redirect $resultRedirect */
$resultRedirect = $this->resultRedirectFactory->create();
$resultRedirect->setPath('*/*/');
return $resultRedirect;
}
if ($this->getRequest()->isPost()) {
$login = $this->getRequest()->getPost('login');
if (!empty($login['username']) && !empty($login['password'])) {
try {
$customer = $this->customerAccountManagement->authenticate($login['username'], $login['password']);
$this->session->setCustomerDataAsLoggedIn($customer);
$this->session->regenerateId();
} catch (EmailNotConfirmedException $e) {
$value = $this->customerUrl->getEmailConfirmationUrl($login['username']);
$message = __('This account is not confirmed.' . ' <a href="%1">Click here</a> to resend confirmation email.', $value);
$this->messageManager->addError($message);
$this->session->setUsername($login['username']);
} catch (AuthenticationException $e) {
$message = __('Invalid login or password.');
$this->messageManager->addError($message);
$this->session->setUsername($login['username']);
} catch (\Exception $e) {
$this->messageManager->addError(__('Invalid login or password.'));
}
} else {
$this->messageManager->addError(__('A login and a password are required.'));
}
}
return $this->accountRedirect->getRedirect();
}
示例2: setUp
public function setUp()
{
$this->_objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
$this->_customerSession = $this->_objectManager->get('Magento\\Customer\\Model\\Session');
$service = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create('Magento\\Customer\\Api\\AccountManagementInterface');
$customer = $service->authenticate('customer@example.com', 'password');
$this->_customerSession->setCustomerDataAsLoggedIn($customer);
$this->_customerViewHelper = $this->_objectManager->create('Magento\\Customer\\Helper\\View');
}
示例3: testSetCustomerDataAsLoggedIn
public function testSetCustomerDataAsLoggedIn()
{
$customer = $this->getMock('Magento\\Customer\\Model\\Customer', [], [], '', false);
$customerDto = $this->getMock('Magento\\Customer\\Api\\Data\\CustomerInterface', [], [], '', false);
$this->customerFactoryMock->expects($this->once())->method('create')->will($this->returnValue($customer));
$customer->expects($this->once())->method('updateData')->with($customerDto)->will($this->returnSelf());
$this->_eventManagerMock->expects($this->at(0))->method('dispatch')->with('customer_login', ['customer' => $customer]);
$this->_eventManagerMock->expects($this->at(1))->method('dispatch')->with('customer_data_object_login', ['customer' => $customerDto]);
$this->_model->setCustomerDataAsLoggedIn($customerDto);
$this->assertSame($customer, $this->_model->getCustomer());
}
示例4: setUp
protected function setUp()
{
parent::setUp();
$logger = $this->getMock('Magento\\Framework\\Logger', array(), array(), '', false);
$this->_customerSession = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get('Magento\\Customer\\Model\\Session', array($logger));
$service = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create('Magento\\Customer\\Service\\V1\\CustomerAccountService');
$customer = $service->authenticate('customer@example.com', 'password');
$this->_customerSession->setCustomerDataAsLoggedIn($customer);
$this->_customerViewHelper = $this->_objectManager->create('Magento\\Customer\\Helper\\View');
$this->_messages = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get('Magento\\Framework\\Message\\ManagerInterface');
}
示例5: setUp
protected function setUp()
{
parent::setUp();
$logger = $this->getMock('Psr\\Log\\LoggerInterface', [], [], '', false);
$this->_customerSession = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get('Magento\\Customer\\Model\\Session', [$logger]);
/** @var \Magento\Customer\Api\AccountManagementInterface $service */
$service = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create('Magento\\Customer\\Api\\AccountManagementInterface');
$customer = $service->authenticate('customer@example.com', 'password');
$this->_customerSession->setCustomerDataAsLoggedIn($customer);
$this->_customerViewHelper = $this->_objectManager->create('Magento\\Customer\\Helper\\View');
$this->_messages = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get('Magento\\Framework\\Message\\ManagerInterface');
}
示例6: execute
/**
* Confirm customer account by id and confirmation key
*
* @return \Magento\Framework\Controller\Result\Redirect
*/
public function execute()
{
/** @var \Magento\Framework\Controller\Result\Redirect $resultRedirect */
$resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
if ($this->session->isLoggedIn()) {
$resultRedirect->setPath('*/*/');
return $resultRedirect;
}
try {
$customerId = $this->getRequest()->getParam('id', false);
$key = $this->getRequest()->getParam('key', false);
if (empty($customerId) || empty($key)) {
throw new \Exception(__('Bad request.'));
}
// log in and send greeting email
$customerEmail = $this->customerRepository->getById($customerId)->getEmail();
$customer = $this->customerAccountManagement->activate($customerEmail, $key);
$this->session->setCustomerDataAsLoggedIn($customer);
$this->messageManager->addSuccess($this->getSuccessMessage());
$resultRedirect->setUrl($this->getSuccessRedirect());
return $resultRedirect;
} catch (StateException $e) {
$this->messageManager->addException($e, __('This confirmation key is invalid or has expired.'));
} catch (\Exception $e) {
$this->messageManager->addException($e, __('There was an error confirming the account'));
}
$url = $this->urlModel->getUrl('*/*/index', ['_secure' => true]);
return $resultRedirect->setUrl($this->_redirect->error($url));
}
示例7: auth
/**
* @return bool
*/
protected function auth()
{
if (!$this->customerSession->isLoggedIn()) {
list($login, $password) = $this->httpAuthentication->getCredentials();
try {
$customer = $this->customerAccountManagement->authenticate($login, $password);
$this->customerSession->setCustomerDataAsLoggedIn($customer);
$this->customerSession->regenerateId();
} catch (\Exception $e) {
$this->logger->critical($e);
}
}
if (!$this->customerSession->isLoggedIn()) {
$this->httpAuthentication->setAuthenticationFailed('RSS Feeds');
return false;
}
return true;
}
示例8: testSetCustomerDataAsLoggedIn
public function testSetCustomerDataAsLoggedIn()
{
$customer = $this->getMock('Magento\\Customer\\Model\\Customer', [], [], '', false);
$customerDto = $this->getMock('Magento\\Customer\\Service\\V1\\Data\\Customer', [], [], '', false);
$this->_converterMock->expects($this->any())->method('createCustomerModel')->will($this->returnValue($customer));
$this->_eventManagerMock->expects($this->at(0))->method('dispatch')->with('customer_login', ['customer' => $customer]);
$this->_eventManagerMock->expects($this->at(1))->method('dispatch')->with('customer_data_object_login', ['customer' => $customerDto]);
$this->_model->setCustomerDataAsLoggedIn($customerDto);
$this->assertSame($customer, $this->_model->getCustomer());
}
示例9: execute
/**
* Login post action
*
* @return \Magento\Framework\Controller\Result\Redirect
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
*/
public function execute()
{
if ($this->session->isLoggedIn() || !$this->formKeyValidator->validate($this->getRequest())) {
/** @var \Magento\Framework\Controller\Result\Redirect $resultRedirect */
$resultRedirect = $this->resultRedirectFactory->create();
$resultRedirect->setPath('*/*/');
return $resultRedirect;
}
if ($this->getRequest()->isPost()) {
$login = $this->getRequest()->getPost('login');
if (!empty($login['username']) && !empty($login['password'])) {
try {
$customer = $this->customerAccountManagement->authenticate($login['username'], $login['password']);
$this->session->setCustomerDataAsLoggedIn($customer);
$this->session->regenerateId();
if ($this->getCookieManager()->getCookie('mage-cache-sessid')) {
$metadata = $this->getCookieMetadataFactory()->createCookieMetadata();
$metadata->setPath('/');
$this->getCookieManager()->deleteCookie('mage-cache-sessid', $metadata);
}
$redirectUrl = $this->accountRedirect->getRedirectCookie();
if (!$this->getScopeConfig()->getValue('customer/startup/redirect_dashboard') && $redirectUrl) {
$this->accountRedirect->clearRedirectCookie();
$resultRedirect = $this->resultRedirectFactory->create();
// URL is checked to be internal in $this->_redirect->success()
$resultRedirect->setUrl($this->_redirect->success($redirectUrl));
return $resultRedirect;
}
} catch (EmailNotConfirmedException $e) {
$value = $this->customerUrl->getEmailConfirmationUrl($login['username']);
$message = __('This account is not confirmed. <a href="%1">Click here</a> to resend confirmation email.', $value);
$this->messageManager->addError($message);
$this->session->setUsername($login['username']);
} catch (UserLockedException $e) {
$message = __('The account is locked. Please wait and try again or contact %1.', $this->getScopeConfig()->getValue('contact/email/recipient_email'));
$this->messageManager->addError($message);
$this->session->setUsername($login['username']);
} catch (AuthenticationException $e) {
$message = __('Invalid login or password.');
$this->messageManager->addError($message);
$this->session->setUsername($login['username']);
} catch (LocalizedException $e) {
$message = $e->getMessage();
$this->messageManager->addError($message);
$this->session->setUsername($login['username']);
} catch (\Exception $e) {
// PA DSS violation: throwing or logging an exception here can disclose customer password
$this->messageManager->addError(__('An unspecified error occurred. Please contact us for assistance.'));
}
} else {
$this->messageManager->addError(__('A login and a password are required.'));
}
}
return $this->accountRedirect->getRedirect();
}
示例10: connectByFacebookId
public function connectByFacebookId($facebookId, \stdClass $token, $customerId)
{
$customerDetailsObject = $this->_customerAccountService->getCustomerDetails($customerId);
/* @var $customerDetailsObject \Magento\Customer\Service\V1\Data\CustomerDetails */
$customerDataObject = $customerDetailsObject->getCustomer();
/* @var $customerDetailsObject \Magento\Customer\Service\V1\Data\Customer */
// Merge old and new data
$customerDetailsArray = array_merge($customerDataObject->__toArray(), array('custom_attributes' => array(array(\Magento\Framework\Service\Data\AttributeValue::ATTRIBUTE_CODE => 'inchoo_socialconnect_fid', \Magento\Framework\Service\Data\AttributeValue::VALUE => $facebookId), array(\Magento\Framework\Service\Data\AttributeValue::ATTRIBUTE_CODE => 'inchoo_socialconnect_ftoken', \Magento\Framework\Service\Data\AttributeValue::VALUE => serialize($token)))));
// Pass result to customerBuilder
$this->_customerBuilder->populateWithArray($customerDetailsArray);
// Pass result to customerDetailsBuilder
$this->_customerDetailsBuilder->setCustomer($this->_customerBuilder->create());
// Update customer
$this->_customerAccountService->updateCustomer($customerId, $this->_customerDetailsBuilder->create());
// Set customer as logged in
$this->_customerSession->setCustomerDataAsLoggedIn($customerDataObject);
}
示例11: execute
/**
* Create customer account action
*
* @return void
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
*/
public function execute()
{
/** @var \Magento\Framework\Controller\Result\Redirect $resultRedirect */
$resultRedirect = $this->resultRedirectFactory->create();
if ($this->session->isLoggedIn() || !$this->registration->isAllowed()) {
$resultRedirect->setPath('*/*/');
return $resultRedirect;
}
if (!$this->getRequest()->isPost()) {
$url = $this->urlModel->getUrl('*/*/create', ['_secure' => true]);
$resultRedirect->setUrl($this->_redirect->error($url));
return $resultRedirect;
}
$this->session->regenerateId();
try {
$address = $this->extractAddress();
$addresses = $address === null ? [] : [$address];
$customer = $this->customerExtractor->extract('customer_account_create', $this->_request);
$customer->setAddresses($addresses);
$password = $this->getRequest()->getParam('password');
$confirmation = $this->getRequest()->getParam('password_confirmation');
$redirectUrl = $this->session->getBeforeAuthUrl();
$this->checkPasswordConfirmation($password, $confirmation);
$customer = $this->accountManagement->createAccount($customer, $password, $redirectUrl);
if ($this->getRequest()->getParam('is_subscribed', false)) {
$this->subscriberFactory->create()->subscribeCustomerById($customer->getId());
}
$this->_eventManager->dispatch('customer_register_success', ['account_controller' => $this, 'customer' => $customer]);
$confirmationStatus = $this->accountManagement->getConfirmationStatus($customer->getId());
if ($confirmationStatus === AccountManagementInterface::ACCOUNT_CONFIRMATION_REQUIRED) {
$email = $this->customerUrl->getEmailConfirmationUrl($customer->getEmail());
// @codingStandardsIgnoreStart
$this->messageManager->addSuccess(__('You must confirm your account. Please check your email for the confirmation link or <a href="%1">click here</a> for a new link.', $email));
// @codingStandardsIgnoreEnd
$url = $this->urlModel->getUrl('*/*/index', ['_secure' => true]);
$resultRedirect->setUrl($this->_redirect->success($url));
} else {
$this->session->setCustomerDataAsLoggedIn($customer);
$this->messageManager->addSuccess($this->getSuccessMessage());
$resultRedirect = $this->accountRedirect->getRedirect();
}
return $resultRedirect;
} catch (StateException $e) {
$url = $this->urlModel->getUrl('customer/account/forgotpassword');
// @codingStandardsIgnoreStart
$message = __('There is already an account with this email address. If you are sure that it is your email address, <a href="%1">click here</a> to get your password and access your account.', $url);
// @codingStandardsIgnoreEnd
$this->messageManager->addError($message);
} catch (InputException $e) {
$this->messageManager->addError($this->escaper->escapeHtml($e->getMessage()));
foreach ($e->getErrors() as $error) {
$this->messageManager->addError($this->escaper->escapeHtml($error->getMessage()));
}
} catch (\Exception $e) {
$this->messageManager->addException($e, __('We can\'t save the customer.'));
}
$this->session->setCustomerFormData($this->getRequest()->getPostValue());
$defaultUrl = $this->urlModel->getUrl('*/*/create', ['_secure' => true]);
$resultRedirect->setUrl($this->_redirect->error($defaultUrl));
return $resultRedirect;
}