1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Oro\Bundle\MagentoBundle\Datagrid; |
4
|
|
|
|
5
|
|
|
use Oro\Bundle\DataGridBundle\Datasource\ResultRecordInterface; |
6
|
|
|
use Oro\Bundle\SecurityBundle\SecurityFacade; |
7
|
|
|
use Oro\Bundle\MagentoBundle\Entity\NewsletterSubscriber; |
8
|
|
|
use Oro\Bundle\MagentoBundle\Entity\Customer; |
9
|
|
|
|
10
|
|
|
class NewsletterSubscriberPermissionProvider extends AbstractTwoWaySyncActionPermissionProvider |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var SecurityFacade |
14
|
|
|
*/ |
15
|
|
|
protected $securityFacade; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var bool|null |
19
|
|
|
*/ |
20
|
|
|
protected $subscribeGranted; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var bool|null |
24
|
|
|
*/ |
25
|
|
|
protected $unsubscribeGranted; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param ResultRecordInterface $record |
29
|
|
|
* @param array $actions |
30
|
|
|
* |
31
|
|
|
* @return array |
32
|
|
|
*/ |
33
|
|
|
public function getActionsPermissions(ResultRecordInterface $record, array $actions) |
34
|
|
|
{ |
35
|
|
|
$actions = array_keys($actions); |
36
|
|
|
$permissions = []; |
37
|
|
|
foreach ($actions as $action) { |
38
|
|
|
$permissions[$action] = true; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
$isChannelApplicable = $this->isChannelApplicable($record); |
42
|
|
|
$customerId = $record->getValue(self::CUSTOMER_ID); |
43
|
|
|
$customerOriginId = $record->getValue(self::CUSTOMER_ORIGIN_ID); |
44
|
|
|
|
45
|
|
|
$isActionAllowed = $isChannelApplicable && (($customerOriginId && $customerId) || !$customerId); |
46
|
|
|
|
47
|
|
|
$statusId = (int)$record->getValue('newsletterSubscriberStatusId'); |
48
|
|
|
$isSubscribed = $statusId === NewsletterSubscriber::STATUS_SUBSCRIBED; |
49
|
|
|
|
50
|
|
|
if (array_key_exists('subscribe', $permissions)) { |
51
|
|
|
$permissions['subscribe'] = $this->isSubscribeGranted() && $isActionAllowed && !$isSubscribed; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
if (array_key_exists('unsubscribe', $permissions)) { |
55
|
|
|
$permissions['unsubscribe'] = $this->isUnsubscribeGranted() && $isActionAllowed && $isSubscribed; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
return $permissions; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param SecurityFacade $securityFacade |
63
|
|
|
* @return NewsletterSubscriberPermissionProvider |
64
|
|
|
*/ |
65
|
|
|
public function setSecurityFacade(SecurityFacade $securityFacade) |
66
|
|
|
{ |
67
|
|
|
$this->securityFacade = $securityFacade; |
68
|
|
|
|
69
|
|
|
return $this; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Check if channel integration is applicable for magento customer. |
74
|
|
|
* Get channel integration id from customer channel if it's does not exist in grid record |
75
|
|
|
* |
76
|
|
|
* @param ResultRecordInterface $record |
77
|
|
|
* @param bool $checkExtension |
78
|
|
|
* |
79
|
|
|
* @return bool |
80
|
|
|
*/ |
81
|
|
View Code Duplication |
protected function isChannelApplicable(ResultRecordInterface $record, $checkExtension = true) |
|
|
|
|
82
|
|
|
{ |
83
|
|
|
$channelId = $this->getChannelId($record); |
84
|
|
|
|
85
|
|
|
if (!$channelId) { |
|
|
|
|
86
|
|
|
return false; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
return $this->channelSettingsProvider->isChannelApplicable($channelId, $checkExtension); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param ResultRecordInterface $record |
94
|
|
|
* |
95
|
|
|
* @return int|null |
96
|
|
|
*/ |
97
|
|
|
protected function getChannelId(ResultRecordInterface $record) |
98
|
|
|
{ |
99
|
|
|
$channelId = $record->getValue(self::CHANNEL_KEY); |
100
|
|
|
if (!$channelId) { |
101
|
|
|
$customer = $record->getValue(self::CUSTOMER); |
102
|
|
|
if ($customer instanceof Customer || $customer instanceof NewsletterSubscriber) { |
103
|
|
|
$channel = $customer->getChannel(); |
104
|
|
|
if ($channel) { |
105
|
|
|
$channelId = $customer->getChannel()->getId(); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
return $channelId; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @return bool |
115
|
|
|
*/ |
116
|
|
|
protected function isSubscribeGranted() |
117
|
|
|
{ |
118
|
|
|
if ($this->subscribeGranted === null) { |
119
|
|
|
$this->subscribeGranted = $this->securityFacade |
120
|
|
|
->isGranted('oro_magento_newsletter_subscriber_subscribe_customer'); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
return $this->subscribeGranted; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @return bool |
128
|
|
|
*/ |
129
|
|
|
protected function isUnsubscribeGranted() |
130
|
|
|
{ |
131
|
|
|
if ($this->unsubscribeGranted === null) { |
132
|
|
|
$this->unsubscribeGranted = $this->securityFacade |
133
|
|
|
->isGranted('oro_magento_newsletter_subscriber_unsubscribe_customer'); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
return $this->unsubscribeGranted; |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.