<commit_before><?php

/*
 * This file is part of the pvrEzComment package.
 *
 * (c) <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace pvr\EzCommentBundle\Controller;

use eZ\Bundle\EzPublishCoreBundle\Controller;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Validator\Constraints\Email;
use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\Validator\Constraints\Collection;

class CommentController extends Controller
{
    /**
     * List comments from a certain contentId
     *
     * @param $contentId id from current content
     * @return Response
     */
    public function getCommentsAction( $contentId, $locationId )
    {
        $response = new Response();
        $response->setMaxAge( $this->container->getParameter( 'pvr_ezcomment.maxage' ) );
        $response->headers->set( 'X-Location-Id', $locationId );

        $pvrEzCommentManager = $this->container->get( 'pvr_ezcomment.manager' );
        $connection = $this->container->get( 'ezpublish.connection' );

        $comments = $pvrEzCommentManager->getComments( $connection, $contentId );

        return $this->render(
            'pvrEzCommentBundle:blog:list_comments.html.twig',
            array(
                'comments'  => $comments,
                'contentId' => $contentId
            ),
            $response
        );
    }

    /**
     * This function get comment form depends of configuration
     *
     * @param $contentId id of content
     * @return \Symfony\Component\HttpFoundation\Response
     */
    public function getFormCommentAction( $contentId )
    {
        $pvrEzCommentManager = $this->container->get( 'pvr_ezcomment.manager' );
        if ( $pvrEzCommentManager->hasAnonymousAccess() )
        {
            $form = $pvrEzCommentManager->createAnonymousForm();
        }
        else
        {
            $form = $pvrEzCommentManager->createUserForm();
        }

        return $this->render(
            'pvrEzCommentBundle:blog:form_comments.html.twig',
            array(
                'form' => $form->createView(),
                'contentId' => $contentId
            )
        );
    }

    /**
     * Add a comment via ajax call
     *
     * @param Request $request
     * @param $contentId id of content to insert comment
     * @return Response
     */
    public function addCommentAction( Request $request, $contentId )
    {
        $pvrEzCommentManager = $this->container->get( 'pvr_ezcomment.manager' );
        if ( $request->isXmlHttpRequest() )
        {
            // Check if user is anonymous or not and generate correct form
            $isAnonymous = false;
            if ( $pvrEzCommentManager->hasAnonymousAccess() )
            {
                $form = $pvrEzCommentManager->createAnonymousForm();
                $isAnonymous = true;
            }
            else
            {
                $form = $pvrEzCommentManager->createUserForm();
            }

            $form->bind( $request );
            if ( $form->isValid() )
            {
                $connection = $this->container->get( 'ezpublish.connection' );
                $localeService = $this->container->get( 'ezpublish.locale.converter' );

                // Save data depending of user (anonymous or ezuser)
                if ( $isAnonymous )
                {
                    $commentId = $pvrEzCommentManager->addAnonymousComment(
                        $connection,
                        $request,
                        $localeService,
                        $form->getData(),
                        $contentId,
                        $this->getRequest()->getSession()->getId()
                    );
                }
                else
                {
                    $currentUser = $this->getRepository()->getCurrentUser();

                    $commentId = $pvrEzCommentManager->addComment(
                        $connection,
                        $request,
                        $currentUser,
                        $localeService,
                        $form->getData(),
                        $contentId,
                        $this->getRequest()->getSession()->getId()
                    );
                }

                // Check if you need to moderate comment or not
                if ( $pvrEzCommentManager->hasModeration() )
                {
                    if (!isset( $currentUser )) $currentUser = null;

                    $pvrEzCommentManager->sendMessage(
                        $form->getData(),
                        $currentUser,
                        $contentId,
                        $this->getRequest()->getSession()->getId(),
                        $commentId
                    );
                    $response = new Response(
                        $this->container->get( 'translator')->trans( 'Your comment should be moderate before publishing' )
                    );
                    return $response;
                }
                else
                {
                    $response = new Response(
                        $this->container->get( 'translator')->trans( 'Your comment has been added correctly' )
                    );
                    return $response;
                }
            }
            else
            {
                $errors = $pvrEzCommentManager->getErrorMessages( $form );

                $response = new Response( json_encode( $errors ), 406 );
                $response->headers->set( 'Content-Type', 'application/json' );
                return $response;
            }
        }
        return new Response(
            $this->container->get( 'translator')->trans( 'Something goes wrong !' ), 400
        );
    }

    /**
     * After receiving email choose if you would like to approve it or not
     *
     * @param $contentId id of content
     * @param $sessionHash hash session do decrypt for transaction
     * @param $action approve|reject value
     * @return Response
     */
    public function commentModerateAction( $contentId, $sessionHash, $action, $commentId )
    {
        $pvrEzCommentManager = $this->container->get( 'pvr_ezcomment.manager' );
        $connection = $this->container->get( 'ezpublish.connection' );

        // Check if comment has waiting status..
        $canUpdate = $pvrEzCommentManager->canUpdate( $contentId, $sessionHash, $connection, $commentId );

        if ( $canUpdate )
        {
            if ( $action == "approve" )
            {
                // Update status
                if ( $pvrEzCommentManager->updateStatus( $connection, $commentId ) )
                {
                    return new Response(
                        $this->container->get( 'translator' )->trans( "Comment publish !" )
                    );
                }
            }
            else
            {
                // Update status
                if ( $pvrEzCommentManager->updateStatus( $connection, $commentId, $pvrEzCommentManager::COMMENT_REJECTED ) )
                {
                    return new Response(
                        $this->container->get( 'translator' )->trans( "Comment rejected !" )
                    );
                }
            }

        }
        return new Response(
            $this->container->get( 'translator' )->trans(
                "An unexpected error has occurred, please contact the webmaster !"
            ), 406 );
    }

}
<commit_msg>Fix: check user before display ezuser form<commit_after><?php

/*
 * This file is part of the pvrEzComment package.
 *
 * (c) <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace pvr\EzCommentBundle\Controller;

use eZ\Bundle\EzPublishCoreBundle\Controller;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Validator\Constraints\Email;
use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\Validator\Constraints\Collection;

class CommentController extends Controller
{
    /**
     * List comments from a certain contentId
     *
     * @param $contentId id from current content
     * @return Response
     */
    public function getCommentsAction( $contentId, $locationId )
    {
        $response = new Response();
        $response->setMaxAge( $this->container->getParameter( 'pvr_ezcomment.maxage' ) );
        $response->headers->set( 'X-Location-Id', $locationId );

        $pvrEzCommentManager = $this->container->get( 'pvr_ezcomment.manager' );
        $connection = $this->container->get( 'ezpublish.connection' );

        $comments = $pvrEzCommentManager->getComments( $connection, $contentId );

        return $this->render(
            'pvrEzCommentBundle:blog:list_comments.html.twig',
            array(
                'comments'  => $comments,
                'contentId' => $contentId
            ),
            $response
        );
    }

    /**
     * This function get comment form depends of configuration
     *
     * @param $contentId id of content
     * @return \Symfony\Component\HttpFoundation\Response
     */
    public function getFormCommentAction( $contentId )
    {
        $pvrEzCommentManager = $this->container->get( 'pvr_ezcomment.manager' );
        if ( $pvrEzCommentManager->hasAnonymousAccess() )
        {
            $form = $pvrEzCommentManager->createAnonymousForm();
        }
        else
        {
            if ( $this->get('security.context')->getToken()->isAuthenticated() )
            {
                $form = $pvrEzCommentManager->createUserForm();
            }
            else
            {
                $form = null;
            }
        }

        return $this->render(
            'pvrEzCommentBundle:blog:form_comments.html.twig',
            array(
                'form' => $form ? $form->createView() : null,
                'contentId' => $contentId
            )
        );
    }

    /**
     * Add a comment via ajax call
     *
     * @param Request $request
     * @param $contentId id of content to insert comment
     * @return Response
     */
    public function addCommentAction( Request $request, $contentId )
    {
        $pvrEzCommentManager = $this->container->get( 'pvr_ezcomment.manager' );
        if ( $request->isXmlHttpRequest() )
        {
            // Check if user is anonymous or not and generate correct form
            $isAnonymous = false;
            if ( $pvrEzCommentManager->hasAnonymousAccess() )
            {
                $form = $pvrEzCommentManager->createAnonymousForm();
                $isAnonymous = true;
            }
            else
            {
                $form = $pvrEzCommentManager->createUserForm();
            }

            $form->bind( $request );
            if ( $form->isValid() )
            {
                $connection = $this->container->get( 'ezpublish.connection' );
                $localeService = $this->container->get( 'ezpublish.locale.converter' );

                // Save data depending of user (anonymous or ezuser)
                if ( $isAnonymous )
                {
                    $commentId = $pvrEzCommentManager->addAnonymousComment(
                        $connection,
                        $request,
                        $localeService,
                        $form->getData(),
                        $contentId,
                        $this->getRequest()->getSession()->getId()
                    );
                }
                else
                {
                    $currentUser = $this->getRepository()->getCurrentUser();

                    $commentId = $pvrEzCommentManager->addComment(
                        $connection,
                        $request,
                        $currentUser,
                        $localeService,
                        $form->getData(),
                        $contentId,
                        $this->getRequest()->getSession()->getId()
                    );
                }

                // Check if you need to moderate comment or not
                if ( $pvrEzCommentManager->hasModeration() )
                {
                    if (!isset( $currentUser )) $currentUser = null;

                    $pvrEzCommentManager->sendMessage(
                        $form->getData(),
                        $currentUser,
                        $contentId,
                        $this->getRequest()->getSession()->getId(),
                        $commentId
                    );
                    $response = new Response(
                        $this->container->get( 'translator')->trans( 'Your comment should be moderate before publishing' )
                    );
                    return $response;
                }
                else
                {
                    $response = new Response(
                        $this->container->get( 'translator')->trans( 'Your comment has been added correctly' )
                    );
                    return $response;
                }
            }
            else
            {
                $errors = $pvrEzCommentManager->getErrorMessages( $form );

                $response = new Response( json_encode( $errors ), 406 );
                $response->headers->set( 'Content-Type', 'application/json' );
                return $response;
            }
        }
        return new Response(
            $this->container->get( 'translator')->trans( 'Something goes wrong !' ), 400
        );
    }

    /**
     * After receiving email choose if you would like to approve it or not
     *
     * @param $contentId id of content
     * @param $sessionHash hash session do decrypt for transaction
     * @param $action approve|reject value
     * @return Response
     */
    public function commentModerateAction( $contentId, $sessionHash, $action, $commentId )
    {
        $pvrEzCommentManager = $this->container->get( 'pvr_ezcomment.manager' );
        $connection = $this->container->get( 'ezpublish.connection' );

        // Check if comment has waiting status..
        $canUpdate = $pvrEzCommentManager->canUpdate( $contentId, $sessionHash, $connection, $commentId );

        if ( $canUpdate )
        {
            if ( $action == "approve" )
            {
                // Update status
                if ( $pvrEzCommentManager->updateStatus( $connection, $commentId ) )
                {
                    return new Response(
                        $this->container->get( 'translator' )->trans( "Comment publish !" )
                    );
                }
            }
            else
            {
                // Update status
                if ( $pvrEzCommentManager->updateStatus( $connection, $commentId, $pvrEzCommentManager::COMMENT_REJECTED ) )
                {
                    return new Response(
                        $this->container->get( 'translator' )->trans( "Comment rejected !" )
                    );
                }
            }

        }
        return new Response(
            $this->container->get( 'translator' )->trans(
                "An unexpected error has occurred, please contact the webmaster !"
            ), 406 );
    }

}

{'fraction_non_alphanumeric': 0.10840794083300895, 'fraction_numerical': 0.0011677695601401323, 'mean_word_length': 1.4778974441408135, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 172, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1187811', 'n_tokens_mistral': 3948, 'n_tokens_neox': 3723, 'n_words': 827}