ChatController.php 2.18 KB
Newer Older
Mat's avatar
Mat committed
1 2 3 4
<?php

namespace SocketBundle\Controller;

Mat's avatar
Mat committed
5 6
use SocketBundle\Entity\User;
use SocketBundle\Form\UserType;
Mat's avatar
Mat committed
7
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
Mat's avatar
Mat committed
8
use Symfony\Component\HttpFoundation\Request;
Mat's avatar
Mat committed
9
use Symfony\Component\Routing\Annotation\Route;
10
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
Mat's avatar
Mat committed
11
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
Mat's avatar
Mat committed
12 13 14

class ChatController extends Controller
{
Mat's avatar
Mat committed
15
 
16 17
         //  on peut mettre ça aussi dans les annotations
         //  * @Security("has_role('ROLE_ADMIN')")
Mat's avatar
Mat committed
18

Mat's avatar
Mat committed
19
    /**
20
     * @Route(name="chat_message", path="/socket/chat")
Mat's avatar
Mat committed
21 22 23
     */
    public function messageAction()
    {
Mat's avatar
Mat committed
24 25
        
        return $this->render('@Socket/Chat/message.html.twig');
Mat's avatar
Mat committed
26
    }
Mat's avatar
Mat committed
27
    
28
    
Mat's avatar
Mat committed
29 30 31 32 33 34 35 36 37
    /**
     * @Route(name="login", path="/login")
     * @param AuthenticationUtils $utils
     * @return \Symfony\Component\HttpFoundation\Response
     */
    public function loginAction(AuthenticationUtils $utils)
    {
        $username = $utils->getLastUsername();
        $error = $utils->getLastAuthenticationError();
Mat's avatar
Mat committed
38
        
Mat's avatar
Mat committed
39 40 41 42 43 44
        return $this->render('@Socket/Login/login.html.twig', array(
           'username' => $username,
           'error' => $error
        ));
    }
    
Mat's avatar
Mat committed
45 46 47 48
    /**
     * @Route(name="register", path="/register")
     * @return \Symfony\Component\HttpFoundation\Response
     */
49
    public function registerAction(Request $request, UserPasswordEncoderInterface $encoder)
Mat's avatar
Mat committed
50 51 52 53 54 55 56 57 58 59
    {
        $user = new User();
        
        $form = $this->createForm(UserType::class, $user);
        $form->handleRequest($request);
        
        if ($form->isSubmitted() && $form->isValid())
        {
            $user->setRole(["ROLE_USER"]);   // on affecte un rôle
            
60 61 62
            $pwd = $encoder->encodePassword($user, $user->getPassword());
            $user->setPassword($pwd);
            
Mat's avatar
Mat committed
63 64 65 66 67 68 69 70 71 72 73 74 75
            $em = $this->getDoctrine()->getManager();
            $em->persist($user);
            $em->flush();
            
            return $this->redirectToRoute('login');
        }
        
        return $this->render('@Socket/Login/register.html.twig', array(
            'form' => $form->createView()
        ));
    }
    
    
Mat's avatar
Mat committed
76
}