1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Customer\Model\Config;
/**
* Customer sharing config model
*
* @author Magento Core Team <core@magentocommerce.com>
*/
class Share extends \Magento\Framework\App\Config\Value implements \Magento\Framework\Option\ArrayInterface
{
/**
* Xml config path to customers sharing scope value
*
*/
const XML_PATH_CUSTOMER_ACCOUNT_SHARE = 'customer/account_share/scope';
/**
* Possible customer sharing scopes
*
*/
const SHARE_GLOBAL = 0;
const SHARE_WEBSITE = 1;
/**
* @var \Magento\Customer\Model\ResourceModel\Customer
*/
protected $_customerResource;
/**
* @var \Magento\Store\Model\StoreManagerInterface
*/
protected $_storeManager;
/**
* Constructor
*
* @param \Magento\Framework\Model\Context $context
* @param \Magento\Framework\Registry $registry
* @param \Magento\Framework\App\Config\ScopeConfigInterface $config
* @param \Magento\Framework\App\Cache\TypeListInterface $cacheTypeList
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
* @param \Magento\Customer\Model\ResourceModel\Customer $customerResource
* @param \Magento\Framework\Model\ResourceModel\AbstractResource $resource
* @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection
* @param array $data
*/
public function __construct(
\Magento\Framework\Model\Context $context,
\Magento\Framework\Registry $registry,
\Magento\Framework\App\Config\ScopeConfigInterface $config,
\Magento\Framework\App\Cache\TypeListInterface $cacheTypeList,
\Magento\Store\Model\StoreManagerInterface $storeManager,
\Magento\Customer\Model\ResourceModel\Customer $customerResource,
\Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
\Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
array $data = []
) {
$this->_storeManager = $storeManager;
$this->_customerResource = $customerResource;
parent::__construct($context, $registry, $config, $cacheTypeList, $resource, $resourceCollection, $data);
}
/**
* Check whether current customers sharing scope is global
*
* @return bool
*/
public function isGlobalScope()
{
return !$this->isWebsiteScope();
}
/**
* Check whether current customers sharing scope is website
*
* @return bool
*/
public function isWebsiteScope()
{
return $this->_config->getValue(
self::XML_PATH_CUSTOMER_ACCOUNT_SHARE,
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
) == self::SHARE_WEBSITE;
}
/**
* Get possible sharing configuration options
*
* @return array
*/
public function toOptionArray()
{
return [self::SHARE_GLOBAL => __('Global'), self::SHARE_WEBSITE => __('Per Website')];
}
/**
* Check for email duplicates before saving customers sharing options
*
* @return $this
* @throws \Magento\Framework\Exception\LocalizedException
*/
public function beforeSave()
{
$value = $this->getValue();
if ($value == self::SHARE_GLOBAL) {
if ($this->_customerResource->findEmailDuplicates()) {
//@codingStandardsIgnoreStart
throw new \Magento\Framework\Exception\LocalizedException(
__(
'We can\'t share customer accounts globally when the accounts share identical email addresses on more than one website.'
)
);
//@codingStandardsIgnoreEnd
}
}
return $this;
}
/**
* Returns shared website Ids.
*
* @param int $websiteId the ID to use if website scope is on
* @return int[]
*/
public function getSharedWebsiteIds($websiteId)
{
$ids = [];
if ($this->isWebsiteScope()) {
$ids[] = $websiteId;
} else {
foreach ($this->_storeManager->getWebsites() as $website) {
$ids[] = $website->getId();
}
}
return $ids;
}
}