我正在使用foscommentBundle,并试图加载带有学说固定装置的注释。问题是FoScommentBundle被配置为仅允许cole_user在我的项目中创建评论,如下所示:
acl_roles:
comment:
create: ROLE_USER
view: IS_AUTHENTICATED_ANONYMOUSLY
edit: ROLE_ADMIN
delete: ROLE_ADMIN
thread:
create: ROLE_USER
view: IS_AUTHENTICATED_ANONYMOUSLY
edit: ROLE_ADMIN
delete: ROLE_ADMIN
vote:
create: ROLE_USER
view: IS_AUTHENTICATED_ANONYMOUSLY
edit: ROLE_ADMIN
delete: ROLE_ADMIN
但是,在加载固定装置时,不允许将其添加,因为必须将其登录为cole_user。
这是我的评论固定装置:
//First depth comments array
$comments = [];
//Create a data faker
$faker = FakerFactory::create();
//FOS Comment manager
$commentManager = $this->container->get('fos_comment.manager.comment');
//First depth comments creation
for($i=0; $i<$this->nb; $i++){
$comment = $commentManager->createComment($this->getReference('thread '.rand(0, $this->nb-1)));
$comment->setAuthor($this->getReference('user '.rand(0, $this->nb-1)));
$comment->setBody($faker->text(250));
$comment->setState($faker->boolean);
$commentManager->saveComment($comment);
$comments[] = $comment;
}
$manager->flush();
- 是否有与学说有关的角色(诸如cole_fixtures_load之类的东西)?
- 是否可以模拟固定装置中的coole_user?
谢谢您的帮助
您可能应该绕过fos_comment.manager.com,它将验证逻辑适用于"生产"环境。您想要的是持续使用固定装置?与您的实体经理保持传统方式?学说不在乎您的应用程序中的角色,它可以自己写入数据库。如果这里出现问题,那是经理的错。
$manager = $this->container->get('doctrine')->getManager();
for($i=0; $i<$this->nb; $i++){
$comment = $commentManager->createComment($this->getReference('thread '.rand(0, $this->nb-1)));
$comment->setAuthor($this->getReference('user '.rand(0, $this->nb-1)));
$comment->setBody($faker->text(250));
$comment->setState($faker->boolean);
$manager->persist($comment)
$comments[] = $comment;
}
$manager->flush();
如果要继续使用FosComments Manager,则应尝试手动对这样的用户进行身份验证:
$repo = $this->get('doctrine')->getRepository("AppBundle:User");
$user = $repo->findOneBy(['username' => 'a_valid_and_authorized_user']);
$token = new UsernamePasswordToken(
$user,
null,
'main',
$user->getRoles()
);
$this->container->get('security.token_storage')->setToken($token);