I have created a simple application with symfony2. the user can login with his/her username and password. I have created a simple view for login screen and then i check the user credintials in my controller. I havent used the symfony2 security. here is my controller code :-
namespace College\UserBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use College\UserBundle\Entity\User;
use College\UserBundle\Entity\Usertype;
use College\UserBundle\Form\LoginForm;
use College\UserBundle\Form\RegisterForm;
class UserController extends Controller
{
public function indexAction()
{
$entity = new User();
$form = $this->createForm(new LoginForm(), $entity);
$request = $this->getRequest();
if ($request->getMethod() == 'POST') {
$form->bindRequest($request);
if ($form->isValid()) {
$em = $this->getDoctrine()
->getEntityManager();
$em->persist($entity);
$userrepository = $em->getRepository('CollegeUserBundle:User');
$auth = $userrepository->findOneBy(array('login' => $entity->getLogin(), 'password' => $entity->getPassword()));
if($auth)
{
$session = $this->getRequest()->getSession();
$session->set('user', $auth);
$this->get('session')->setFlash('notice', 'You Have Successfully Logged In!');
return $this->redirect($this->generateUrl('CollegeUserBundle_home'));
}
else
return $this->render('CollegeUserBundle:User:loginpage.html.twig',array(
'form' => $form->createView(), 'error' => 'Please Correct Your Login Details And Enter the Correct login and Password', ));
}
}
return $this->render('CollegeUserBundle:User:loginpage.html.twig',array(
'form' => $form->createView()
));
}
public function loginAction()
{
$session = $this->get('session')->get('user');
return $this->render('CollegeUserBundle:User:home.html.twig', array(
'info' => $session,));
}
public function logoutAction()
{
$this->get('request')->getSession()->invalidate();
return $this->redirect($this->generateUrl('CollegeUserBundle_index'));
}
Routing.yml
CollegeUserBundle_index:
pattern: /
defaults: { _controller: CollegeUserBundle:User:index }
requirements:
_method: GET|POST
CollegeUserBundle_home:
pattern: /home
defaults: { _controller: CollegeUserBundle:User:login }
requirements:
_method: GET|POST
CollegeUserBundle_logout:
pattern: /logout
defaults: { _controller: CollegeUserBundle:User:logout }
requirements:
_method: GET
Entity (User.php)
This file contains all user details including Username, Created Date, login, password and usertype.
Now I want to do everything Authentication and autherization with symfony security. I read the tutorial but couldn't understand everything. like how can i authenticate with my same controller, how it takes ADMIN_ROLE, USER_ROLE, Do I need to create a table for these ROLES. I have a lot of Confusions related to this topic. I found the tutorial great till now, but here i am Lost and need someone who can help me with it.