DefaultController.php
5.57 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<?php
namespace AppBundle\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use AppBundle\Entity\Usuarios;
class DefaultController extends Controller
{
/**
* @Route("/", name="homepage")
*/
public function indexAction(Request $request)
{
$form = $this->createForm('AppBundle\Form\SolicitarType');
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
//1. obtener el rol-institucion-persona
$rol = $this->getDoctrine()->getRepository(
'AppBundle:RolInstitucion')->findOneByIdRol(
$this->getDoctrine()->getRepository(
'AppBundle:Rol')->findOneByIdPersona(
$this->getDoctrine()->getRepository('AppBundle:Persona')
->findOneByCedulaPasaporte($form->get('cedula')->getData())->getId()));
//si no existe el rol del docente, enviar correo al encargado de la región para verificar.
if (!$rol) {
throw $this->createNotFoundException(
'No product found for id '. $form->get('cedula')->getData()
);
}
//si el docente existe, crea el nombre de usuario.
$usuario = mb_strtolower($rol->getIdRol()->getIdPersona()->getPrimerNombre()[0] .$rol->getIdRol()->getIdPersona()->getPrimerApellido());
//busca en la base de datos para ver si ese nombre de usuario ya existe
$credenciales = $this->getDoctrine()->getRepository('AppBundle:Usuarios')->findOneByUsername($usuario);
if(!$credenciales){ //si no existe, procede a crear usuario y contraseña.
$login = new Usuarios();
$login->setUsername($usuario);
$login->setPlainPassword($form->get('cedula')->getData());
$password = $this->get('security.password_encoder')
->encodePassword($login, $login->getPlainPassword()); //encripta la contraseña
$login->setPassword($password);
$login->setIdRolInstitucion($rol);
$permiso = $this->getDoctrine()->getRepository('AppBundle:Role')->findOneById(1);
$login->addRol($permiso); //le añade la permisología básica de docente
$em = $this->getDoctrine()->getManager();
$em->persist($login);
$em->flush(); //guarda en la base de datos
$message = \Swift_Message::newInstance()
->setSubject('Bienvenido al sistema CEA@UBV')
->setFrom('wilmer.ramones@gmail.com')
->setTo($form->get('correo')->getData())
->setBody(
$this->renderView(
'correos/solicitud_adscripcion.html.twig',
array(
'nombres' => $form->get('nombres')->getData(),
'apellidos' => $form->get('apellidos')->getData(),
'usuario' => $login->getUsername(),
'contra' => $login->getPlainPassword(),
)
),
'text/html'
)
/*
* If you also want to include a plaintext version of the message
->addPart(
$this->renderView(
'Emails/registration.txt.twig',
array('name' => $name)
),
'text/plain'
)
*/
;
$this->get('mailer')->send($message);
}else{
throw $this->createNotFoundException(
'Ya tiene usuario y contraseña '. $form->get('cedula')->getData()
);
}
throw $this->createNotFoundException(
'Docente Encontrado '. $form->get('cedula')->getData()
);
$message = \Swift_Message::newInstance()
->setSubject('Hello Email')
->setFrom('send@example.com')
->setTo('wilmer.ramones@gmail.com')
->setBody(
$this->renderView(
'correos/solicitud_adscripcion.html.twig',
array(
'nombres' => $form->get('nombres')->getData(),
'apellidos' => $form->get('apellidos')->getData(),
)
),
'text/html'
)
/*
* If you also want to include a plaintext version of the message
->addPart(
$this->renderView(
'Emails/registration.txt.twig',
array('name' => $name)
),
'text/plain'
)
*/
;
//$this->get('mailer')->send($message);
//$request->getSession()->getFlashBag()->add('success', 'Your email has been sent! Thanks!');
}
// replace this example code with whatever you need
return $this->render('default/index.html.twig', array(
'base_dir' => realpath($this->container->getParameter('kernel.root_dir').'/..'),
'form' => $form->createView(),
));
}
}