Is it possible to use a custom User Provider to sign a comment when using FOSCommentBundle?

271 Views Asked by At

I am using a custom UserProvider for the authentication in my Symfony2.1 application. I would like to use FOSCommentBundle to implement comments. But when it comes to sign the comment by the comment's author, I am stuck.

Basically, I have two databases. One from which I can retrieve users' credentials (username, salt, password,...) but which I can't make any modification, the other one I can use to store the users information (like her/his comment(s)) in an User entity.

When I am mapping the Comment entity with this User entity, there is a problem since FOSCommentBundle retrieves the entity which implements the UserInterface (in my security bundle) and not this User entity.

Basically, is there a way to tell FOSCommentBundle to retrieve another User entity than the one used for Authentication?

Thanks

1

There are 1 best solutions below

0
On

Did you try FOSUserBundle integration with FOSCommentsBundle?

You need to implement SignedCommentInterface like this.

<?php
// src/MyProject/MyBundle/Entity/Comment.php

namespace MyProject\MyBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use FOS\CommentBundle\Entity\Comment as BaseComment;
use FOS\CommentBundle\Model\SignedCommentInterface;
use Symfony\Component\Security\Core\User\UserInterface;

/**
 * @ORM\Entity
 */
class Comment extends BaseComment implements SignedCommentInterface
{
    // .. fields

    /**
     * Author of the comment
     *
     * @ORM\ManyToOne(targetEntity="MyProject\MyBundle\Entity\User")
     * @var User
     */
    protected $author;

    public function setAuthor(UserInterface $author)
    {
        $this->author = $author;
    }

    public function getAuthor()
    {
        return $this->author;
    }

    public function getAuthorName()
    {
        if (null === $this->getAuthor()) {
            return 'Anonymous';
        }

        return $this->getAuthor()->getUsername();
    }
}