-
Notifications
You must be signed in to change notification settings - Fork 65
Expand file tree
/
Copy pathAuthController.php
More file actions
112 lines (83 loc) · 3.2 KB
/
AuthController.php
File metadata and controls
112 lines (83 loc) · 3.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
<?php
namespace CTRV\CommonBundle\Controller;
use CTRV\CommonBundle\Form\RegistrationType;
use Symfony\Component\Security\Core\SecurityContext;
use CTRV\CommonBundle\Form\UserAuthType;
use Symfony\Component\HttpFoundation\Response;
use CTRV\CommonBundle\DependencyInjection\Constants;
use Doctrine\ORM\EntityRepository;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use CTRV\CommonBundle\Entity\User;
use JMS\SecurityExtraBundle\Annotation\Secure;
/**
* User controller.
*
*
*/
class AuthController extends Controller
{
/**
* Affiche le formulaire de connexion
* @Template()
*/
public function loginAction () {
$request = $this->getRequest();
$session = $request->getSession();
// get the login error if there is one
if ($request->attributes->has(SecurityContext::AUTHENTICATION_ERROR)) {
$error = $request->attributes->get(
SecurityContext::AUTHENTICATION_ERROR
);
} else {
$error = $session->get(SecurityContext::AUTHENTICATION_ERROR);
$session->remove(SecurityContext::AUTHENTICATION_ERROR);
}
return array(
// last username entered by the user
'last_username' => $session->get(SecurityContext::LAST_USERNAME),
'error' => $error
);
}
/**
* Affiche la page indiquant que vous n'avez pas les droits d'acces
* @Route("/insufficient_access",name="insufficient_access")
* @Template()
*/
public function insufficientAccessAction () {
$referer = $this->getRequest()->headers->get('referer');
return array("url"=>$referer);
}
/**
* Afficher et traite le formulaire d'inscription d'un utilisateur
* @Route("/register",name="register")
* @Template()
* @Secure(roles="ROLE_ADMIN")
*/
public function registerAction () {
$em = $this->getDoctrine()->getEntityManager();
$form = $this->createForm(new RegistrationType(), new User());
if ($this->getRequest()->getMethod()=="POST") {
$form->bind($this->getRequest());
if ($form->isValid()) {
$user = $form->getData();
$userid = hash('ripemd160',$user->getLogin().$user->getEmail().date('d/m/y-H:i:s'));
$encoder = $this->get('password_service');
$role = $this->getDoctrine()->getEntityManager()->getRepository("CTRVCommonBundle:Role")->findOneBy(array("name"=>Constants::ROLE_USER));
$user->setSalt(uniqid(mt_rand()));
$user->setRole($role);
$user->setUserid($userid);
$user->setPassword($encoder->encodePassword($user->getPassword(), $user->getSalt()));
$user->setIsActive(true);
$user->setIsBlocked(false);
$em->persist($user);
$em->flush();
return $this->redirect($this->generateUrl("home"));
}
}
return array('form' => $form->createView());
}
}