我正在将选民实施到我的系统中,我不知道如何为选民创建/扩展/实施更通用的方式。
我有以下投票人(最小):
class EventVoter extends Voter {
private $roleBaseName = 'ROLE_EVENT';
private $classname = Event::class;
private $ownershipMethod = 'getCreatedBy';
protected function supports($attribute, $subject) {
// Only vote on {$this->classname} objects
if (!$subject instanceof $this->classname) {
return false;
}
return true;
}
protected function voteOnAttribute($attribute, $subject, TokenInterface $token) {
// Check if you own this specific entry:
return $subject->{$this->ownershipMethod} === $user;
}
}
此功能可以正常使用。但是,我也想要一个用于新闻,页面,技能,Foo和Bar的选项。
我可以复制该投票者并更改顶部变量。但是由于这是仅有的三个更改,因此我需要某种通用选民,可以使用例如在构造上进行编码,从而产生了更多的SOLID代码,并且真正的逻辑集中在一个地方。
但是,如果我用Voter扩展泛型类,它将自动加载它。我希望它忽略通用类,并以类似以下内容结束:
class EventVoter extends GenericVoter
{
private $roleBaseName = 'ROLE_EVENT';
private $classname = Event::class;
private $ownershipMethod = 'getCreatedBy';
// Possibly, if required at all:
protected function voteOnAttribute($attribute, $subject, TokenInterface $token){
return parent::voteOnAttribute($attribute, $subject,$token)
}
}
有人能指出我正确的方向吗?
答案 0 :(得分:2)
我可以想到两种可能性:
将params0 = {
'TableName': 'FHCRoomCounts',
'Key': {
'timeSlot': {
'S': '0830'
},
'room': {
'S': 'chapel'
}
},
'ExpressionAttributeValues': {
':n': {'N': '1'},
},
'UpdateExpression':'ADD registrants :n',
'ReturnValues': 'ALL_NEW',
};
逻辑移至接口并在您的实体中实现,然后在 [HttpPost("submited")]
public async Task<IActionResult> SubmitedPayment(PaymentDTO paymentDTO)
{
paymentDTO.Description = "Invoice Description";
paymentDTO.VendorTxCode = Guid.NewGuid().ToString().ToUpper();
paymentDTO.NotificationURL = $"{_configuration["AppUrl"]}/Payment/RedirectURL";
paymentDTO.Vendor = _configuration["Vendor"];
var client = new HttpClient();
var data = PostData(paymentDTO);
var result = await client.PostAsync(_configuration["SagePayUrl"], new FormUrlEncodedContent(data));
var contentResponse = await result.Content.ReadAsStringAsync();
if (contentResponse.Contains("Status=OK"))
return Redirect(await SaveSuccessResponseData(paymentDTO, contentResponse));
ViewBag.StatusDetail = contentResponse;
return View("Error");
}
private Dictionary<string, string> PostData(PaymentDTO paymentDTO)
{
return new Dictionary<string, string>
{
{ "VPSProtocol", "3.00" },
{ "TxType", "PAYMENT" },
{ "Vendor", _configuration["Vendor"] },
{ "Currency", paymentDTO.Currency },
{ "Amount", paymentDTO.Amount.ToString() },
{ "Description", paymentDTO.Description },
{ "VendorTxCode", paymentDTO.VendorTxCode },
{ "NotificationURL", paymentDTO.NotificationURL},
{ "BillingFirstnames", paymentDTO.BillingFirstnames },
{ "BillingSurname", paymentDTO.BillingSurname },
{ "BillingAddress1", paymentDTO.BillingAddress1 },
{ "BillingAddress2", paymentDTO.BillingAddress2 },
{ "BillingCity", paymentDTO.BillingCity },
{ "BillingPostCode", paymentDTO.BillingPostCode },
{ "BillingCountry", paymentDTO.BillingCountry },
{ "DeliveryFirstnames", paymentDTO.DeliveryFirstnames ?? paymentDTO.BillingFirstnames},
{ "DeliverySurname", paymentDTO.DeliverySurname ?? paymentDTO.BillingSurname},
{ "DeliveryAddress1", paymentDTO.DeliveryAddress1 ?? paymentDTO.BillingAddress1},
{ "DeliveryAddress2", paymentDTO.DeliveryAddress2 ?? paymentDTO.BillingAddress2},
{ "DeliveryCity", paymentDTO.DeliveryCity ?? paymentDTO.BillingCity},
{ "DeliveryPostCode", paymentDTO.DeliveryPostCode ?? paymentDTO.BillingPostCode},
{ "DeliveryCountry", paymentDTO.DeliveryCountry ?? paymentDTO.BillingCountry},
{ "BillingState", paymentDTO.BillingState },
{ "DeliveryState", paymentDTO.DeliveryState ?? paymentDTO.BillingState},
{ "CustomerEMail", paymentDTO.CustomerEMail}
};
}
中检查该接口。您仍然需要一些逻辑来计算角色。
ownershipMethod
另一种方法是将您的supports
声明为class GenericVoter extends Voter {
protected function supports($attribute, $subject) {
if (!$subject instanceof OwnershipInterface) {
return false;
}
return true;
}
protected function voteOnAttribute($attribute, $subject, TokenInterface $token) {
return $subject->getOwnership() === $user;
}
}
interface OwnershipInterface {
public function getOwnership();
}
/**
* @Entity
*/
class Event implements OwnershipInterface {
public function getOwnership() {
return $this->getCreatedBy();
}
}
,这样它就不会自动加载:
GenericVoter