我有一个应用程序实体,用于存储带有用户 ID 的广告 ID
class User implements UserInterface, Serializable
{
/**
* @ORMOneToMany(targetEntity="AppEntityApplication", mappedBy="author", orphanRemoval=true)
*/
private $applications;
}
应用程序实体
class Application
{
/**
* @ORMManyToOne(targetEntity="AppEntityAdvert", inversedBy="applications")
* @ORMJoinColumn(nullable=false)
*/
private $advert;
/**
* @ORMManyToOne(targetEntity="AppEntityUser", inversedBy="applications")
* @ORMJoinColumn(nullable=false)
*/
private $author;
}
我想要的是一个投票者,如果当前用户已经申请了特定广告(在查看广告时(,则可以拒绝操作 迄今 在选民尝试
类广告应用程序选民扩展选民 {
protected function supports($attribute, $subject)
{
return in_array($attribute, ['APP_APPLY', 'ADVERT_APPLY']) && $subject instanceof Application;
}
/**
* @param string $attribute
* @param mixed $subject
* @param TokenInterface $token
* @return bool
*/
protected function voteOnAttribute($attribute, $subject, TokenInterface $token)
{
$user = $token->getUser();
$apl = $user->getApplications();
// if the user is anonymous, do not grant access
if (!$user instanceof User) {
return false;
}
switch ($attribute) {
case 'APP_APPLY':
return $this->dw($user, $subject);
break;
case 'ADVERT_APPLY':
// logic to determine if the user can VIEW
// return true or false
break;
}
return false;
}
private function applied(?User $user, Advert $app)
{
return $user->getId() === $app->getApplications()->getKeys();
}
private function aa(?User $user, Application $app)
{
return $user->getApplications() === $app->getAdvert();
}
private function dw(?User $user, Application $app)
{
foreach($user->getApplications() as $app) {
if ($app->getAdvert() === $app) {
return false;
}
}
return true;
}
}
我的访问权限弃权,我不知道如何调试选民
提前致谢
假设你正确设置了你的选民(所以使用支持和voteOnAttribute方法(。
然后你只需要改变你的方法:
private function dw(?User $user, Advert $advert)
{
foreach($user->getApplications() as $userApplication) {
if ($userApplication->getAdvert() === $advert) {
return false;
}
}
return true;
}
}
您的方法的问题在于您正在将ArrayCollection与应用程序对象进行比较。