Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/symfony/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Symfony投票者:访问被拒绝,用户既不是匿名的,也不记得我_Php_Symfony_Security - Fatal编程技术网

Php Symfony投票者:访问被拒绝,用户既不是匿名的,也不记得我

Php Symfony投票者:访问被拒绝,用户既不是匿名的,也不记得我,php,symfony,security,Php,Symfony,Security,我对Symfony很陌生。我正在尝试在管理区使用投票人 我希望管理员(角色超级管理员)能够删除(删除)一个用户,如果他是超级管理员(角色超级管理员) 我的防火墙似乎工作得很好,因为我可以登录管理区,做我想做的事情,直到我不使用投票人。下面是我当前用户对象的转储: User {#300 ▼ -id: 1 -password: "$2y$13$e3LL2N/pYGrGn.7EFikqSuAMSkLolcnggtf1HsBgNMzdXnal1AIua" -username: "JustMe

我对Symfony很陌生。我正在尝试在管理区使用投票人

我希望管理员(角色超级管理员)能够删除(删除)一个用户,如果他是超级管理员(角色超级管理员)

我的防火墙似乎工作得很好,因为我可以登录管理区,做我想做的事情,直到我不使用投票人。下面是我当前用户对象的转储:

User {#300 ▼
  -id: 1
  -password: "$2y$13$e3LL2N/pYGrGn.7EFikqSuAMSkLolcnggtf1HsBgNMzdXnal1AIua"
  -username: "JustMe"
  -email: "me@me.fr"
  -isActive: true
  -roles: array:1 [▼
    0 => "ROLE_ADMIN"
  ]
}
当我在控制器中使用denyUnlessGranted()时,就会出现以下异常:

DEBUG - Access denied, the user is neither anonymous, nor remember-me.
ERROR - Uncaught PHP Exception Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException: "Access Denied." at /Volumes/Work/MAMP htdocs/a-symfony-re/vendor/symfony/symfony/src/Symfony/Component/Security/Http/Firewall/ExceptionListener.php line 119 
这是我的安全配置:

role_hierarchy:
    ROLE_AUTHOR: ROLE_USER
    ROLE_EDITOR: ROLE_AUTHOR
    ROLE_ADMIN : [ROLE_USER, ROLE_ALLOWED_TO_SWITCH]

firewalls:
    # disables authentication for assets and the profiler, adapt it according to your needs
    dev:
        pattern: ^/(_(profiler|wdt)|css|images|js)/
        security: false

    main:
        anonymous: ~
        pattern: ^/
        provider: app_users_provider
        form_login:
            login_path: jst_login
            check_path: jst_login_check
        logout:
            path: jst_logout
            target: /

access_decision_manager:
    strategy: unanimous
这是我的控制器中的一个基本操作,在我不使用投票者之前工作正常:

public function deleteUserAction(User $user)
{
    $this->denyAccessUnlessGranted('delete', $user);
    $currentUser = $this->getUser();
    $role = $currentUser->getRoles[0];

    return new Response('Delete User AppBundle:AdminController:deleteUser : '.$role);
}
这很简单:

namespace AppBundle\Security;

use AppBundle\Entity\User;
use AppBundle\Entity\Role;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
use Symfony\Component\Security\Core\Authorization\AccessDecisionManagerInterface;

class UserVoter extends Voter
{
    const EDIT = 'edit';
    const DELETE = 'delete';
    const CREATE = 'create';

    private $decisionManager;

    public function __construct(AccessDecisionManagerInterface, $decisionManager)
    {
        $this->decisionManager = $decisionManager;
    }

    public function support($attribute, $subject)
    {
        if (!in_array($attribute, array(selt::DELETE))) {
            return false;
        }
        if (!$subject instanceOf USER) {
            return false;
        }

        return true;
    }

    public function voteOnAttribute($attribute, $subject, TokenInterface $token)
    {
        $currentUser = $token->getUser();
        $user = $subject;

        if (!$currentUser instanceOf User) {
            return false;
        }

        switch ($attribute) {
            case self::DELETE :
                //return $this->canDelete( $token );
                return $this->canDelete($user, $currentUser);
                break;
            default:
                throw new \LogicException('this code shoudn\'t be executed');
        }
    }

    private function canDelete($user, $currentUser)
    {
        //return $this->decisionManager->decide( $token, array( 'ROLE_ADMIN' ) );
        return $currentUser->getRoles()[0] == 'SUPER_ADMIN';
    }
}
如您所见,我已经尝试使用AccessDecisionManagerInterface,但没有结果

等一下。。有什么帮助吗?;-)


坦克的

您的
角色超级管理员在您的角色层次结构中的位置在哪里

在您的
voteOnAttribute
函数中尝试此操作

  case self::DELETE:
            // if the user is an super admin, allow them to create new posts
            if ($this->decisionManager->decide($token,  array('ROLE_SUPER_ADMIN'))) {
                return true;
            }

当您登录到该网站时,您的角色是什么?检查档案器

您的
角色超级管理员
在您的角色层次结构中的何处

在您的
voteOnAttribute
函数中尝试此操作

  case self::DELETE:
            // if the user is an super admin, allow them to create new posts
            if ($this->decisionManager->decide($token,  array('ROLE_SUPER_ADMIN'))) {
                return true;
            }

当您登录到该网站时,您的角色是什么?检查探查器

您的安全配置中没有角色
role\u SUPER\u ADMIN。 将其添加到您的角色层次结构中:

role_hierarchy:
    ROLE_AUTHOR: ROLE_USER
    ROLE_EDITOR: ROLE_AUTHOR
    ROLE_ADMIN : [ROLE_USER, ROLE_ALLOWED_TO_SWITCH]
    ROLE_SUPER_ADMIN: ROLE_ADMIN
...
评论后更新

//返回$this->decisionManager->decise($token,array('ROLE\u ADMIN'))的问题是,您的逻辑在哪里定义用户是否可以删除另一个?
decisionManager不知道您是否没有实现接口并使用该实例

对于
return$currentUser->getRoles()[0]=“ROLE\u ADMIN”嗯,可能令牌存储返回的用户有几个角色,而
角色\u ADMIN
不是第一个角色。在这种情况下,请尝试使用:

return in_array("ROLE_SUPER_ADMIN", $tokenInterface->getUser()->getRoles();

您的安全配置中没有角色
role\u SUPER\u ADMIN
。 将其添加到您的角色层次结构中:

role_hierarchy:
    ROLE_AUTHOR: ROLE_USER
    ROLE_EDITOR: ROLE_AUTHOR
    ROLE_ADMIN : [ROLE_USER, ROLE_ALLOWED_TO_SWITCH]
    ROLE_SUPER_ADMIN: ROLE_ADMIN
...
评论后更新

//返回$this->decisionManager->decise($token,array('ROLE\u ADMIN'))的问题是,您的逻辑在哪里定义用户是否可以删除另一个?
decisionManager不知道您是否没有实现接口并使用该实例

对于
return$currentUser->getRoles()[0]=“ROLE\u ADMIN”嗯,可能令牌存储返回的用户有几个角色,而
角色\u ADMIN
不是第一个角色。在这种情况下,请尝试使用:

return in_array("ROLE_SUPER_ADMIN", $tokenInterface->getUser()->getRoles();

此错误也可能是由于symfony 3.0与1.6.0版中的
jms/security extra bundle
不兼容造成的


如果您使用jms安全附加包,请尝试在1.6.1或更高版本中使用它

此错误也可能是由于symfony 3.0与1.6.0版中的
jms/security extra bundle
不兼容造成的


如果您使用jms安全附加包,请尝试在1.6.1或更高版本中使用它

您的
支持功能中有一个输入错误:
selt::DELETE
应该是
self::DELETE
?您说得对!但是仍然有这个例外。您的
支持功能中有一个输入错误:
selt::DELETE
应该是
self::DELETE
?您说得对!但是仍然出现了这个异常。是的,我忘记了角色\u SUPER\u ADMIN ROLE un security.yml,但仍然抛出相同的错误。无论如何,我也尝试过简化投票者试图与角色\管理员匹配的过程,但仍然抛出了异常。我在投票者中尝试了两种方法:return$this->decisionManager->decise($token,array('ROLE_ADMIN'));并返回$currentUser->getRoles()[0]=='ROLE_ADMIN';这两种方法都失败了。好吧,我终于在代码中发现了一些错误。谢谢你的建议。但有一点似乎需要注意:在投票者自定义类中,我们可以使用AccessDecisionManager,因为它通过检查令牌用户角色来保存逻辑。所以$this->decisionManager->decise($token,array(“ROLE_ADMIN”));效果很好。这里是我的输入错误和错误:1:构造函数参数类型后的Coma,2:不支持的方法“support”(“supports”是好的),最重要的是,我没有在app/config/services.yml中配置我的服务。是的,我忘记了角色\u SUPER\u ADMIN ROLE un security.yml,但仍然抛出相同的错误。无论如何,我也尝试过简化投票者试图与角色\管理员匹配的过程,但仍然抛出了异常。我在投票者中尝试了两种方法:return$this->decisionManager->decise($token,array('ROLE_ADMIN'));并返回$currentUser->getRoles()[0]=='ROLE_ADMIN';这两种方法都失败了。好吧,我终于在代码中发现了一些错误。谢谢你的建议。但有一点似乎需要注意:在投票者自定义类中,我们可以使用AccessDecisionManager,因为它通过检查令牌用户角色来保存逻辑。所以$this->decisionManager->decise($token,array(“ROLE_ADMIN”));效果很好。这里是我的输入错误:1:Coma后构造函数参数类型,2:unsupported方法“support”(“supports”是个好方法),最重要的是,我没有在app/config/services.yml中配置我的服务。我省略了这个角色是的,但仍然遇到了这个问题。还试图简单地与角色_AMDIN匹配,但仍然抛出异常。我是在管理员的领导下登录的ROLEI省略了这个角色是的,但仍然有这个问题。还试图简单地与角色_AMDIN匹配,但仍然抛出异常。我以管理员的身份登录