UsuarioController.php
7.17 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
<?php
namespace Cacic\CommonBundle\Controller;
use Doctrine\Common\Util\Debug;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Cacic\CommonBundle\Entity\Usuario;
use Cacic\CommonBundle\Entity\GrupoUsuario;
use Cacic\CommonBundle\Form\Type\UsuarioType;
class UsuarioController extends Controller
{
/**
*
* Listagem dos usuários
* @param $page
*/
public function indexAction()
{
$arrUsuarios = $this->getDoctrine()->getRepository( 'CacicCommonBundle:Usuario' )->listar();
return $this->render( 'CacicCommonBundle:Usuario:index.html.twig', array( 'usuarios' => $arrUsuarios ) );
}
/**
*
* Página de alteraçõo da PRÓPRIA senha
*/
public function trocarpropriasenhaAction( Request $request )
{
if ( ! $request->isXmlHttpRequest() ) // Verifica se se trata de uma requisição AJAX
throw $this->createNotFoundException( 'Página não encontrada' );
$usuario = $this->getUser(); // Recupera o usuário logado
$hash = $this->container->getParameter('cacic_senha_algorithm'); // Algoritmo de encripitação da senha
if ( $usuario->getTeSenha() != hash( $hash, $request->get('senha_atual') ) )
{ // Verifica se a senha atual informada confere
$response = new Response( json_encode( array('status' => 'Senha atual não confere' ) ), 401 );
$response->headers->set('Content-Type', 'application/json');
return $response;
}
# Configura a SENHA do usuário já aplicando o Algoritmo definido (ver services.yml)
$usuario->setTeSenha( hash( $hash, $request->get('senha_nova') ) );
$em = $this->getDoctrine()->getManager();
$em->persist( $usuario );
$em->flush();
$response = new Response( json_encode( array('status' => 'ok') ) );
$response->headers->set('Content-Type', 'application/json');
return $response;
}
/**
*
* Página de alteraçõo de senha
*/
public function trocarsenhaAction( Request $request )
{
if ( ! $request->isXmlHttpRequest() ) // Verifica se se trata de uma requisição AJAX
throw $this->createNotFoundException( 'Página não encontrada' );
$usuario = $this->getDoctrine()->getRepository('CacicCommonBundle:Usuario')->find( $request->get('id') );
if ( ! $usuario )
throw $this->createNotFoundException( 'Usuário não encontrado' );
# Configura a SENHA do usuário já aplicando o Algoritmo definido (ver services.yml)
$usuario->setTeSenha( hash( $this->container->getParameter('cacic_senha_algorithm'), $request->get('senha') ) );
$em = $this->getDoctrine()->getManager();
$em->persist( $usuario );
$em->flush();
$response = new Response( json_encode( array('status' => 'ok') ) );
$response->headers->set('Content-Type', 'application/json');
return $response;
}
/**
* Página de Cadastrar novo usuário.
*
*/
public function cadastrarAction( Request $request)
{
$usuario = new Usuario();
$form = $this->createForm( new UsuarioType(), $usuario );
if ( $request->isMethod('POST') )
{
$form->bind( $request );
# Gera uma senha aleatória para o novo Usuário
$form->getData()->_gerarSenhaAleatoria( $this->container->getParameter('cacic_senha_algorithm') );
if ( $form->isValid() )
{
$this->getDoctrine()->getManager()->persist( $usuario );
$this->getDoctrine()->getManager()->flush(); //Persiste os dados do Usuário
$this->get('session')->getFlashBag()->add('success', 'Dados salvos com sucesso!');
return $this->redirect( $this->generateUrl( 'cacic_usuario_index') );
}
}
return $this->render(
'CacicCommonBundle:Usuario:cadastrar.html.twig',
array(
'form' => $form->createView(),
'grupoDesc' => $this->getDoctrine()->getRepository( 'CacicCommonBundle:GrupoUsuario' )->listar()
)
);
}
/**
* Página de editar dados do Usuário
* @param int $idusuario
*/
public function editarAction( $idUsuario, Request $request )
{
$usuario = $this->getDoctrine()->getRepository('CacicCommonBundle:Usuario')->find( $idUsuario );
if ( ! $usuario )
throw $this->createNotFoundException( 'Usuário não encontrado' );
$form = $this->createForm( new UsuarioType(), $usuario );
// Check if user has permission to generate API
$securityContext = $this->container->get('security.context');
if ($securityContext->isGranted('ROLE_ADMIN')) {
$apiKey = $usuario->getApiKey();
if (empty($apiKey)) {
$apiKey = md5(uniqid(""));
}
$form->add( 'api_key', 'text',
array(
'label' => 'Chave de API',
'required' => false,
'data' => $apiKey
)
);
}
if ( $request->isMethod('POST') )
{
$form->bind( $request );
if ( $form->isValid() )
{
$this->getDoctrine()->getManager()->persist( $usuario );
$this->getDoctrine()->getManager()->flush();// Efetua a edição do Usuário
$this->get('session')->getFlashBag()->add('success', 'Dados salvos com sucesso!');
return $this->redirect($this->generateUrl('cacic_usuario_editar', array( 'idUsuario'=>$usuario->getIdUsuario() ) ) );
}
}
return $this->render(
'CacicCommonBundle:Usuario:cadastrar.html.twig',
array(
'form' => $form->createView(),
'grupoDesc' => $this->getDoctrine()->getRepository( 'CacicCommonBundle:GrupoUsuario' )->listar()
)
);
}
/**
* Página de recuperação de senha
*/
public function recuperarsenhaAction()
{
}
/**
*
* [AJAX] Exclusão de Usuarios já cadastrado
* @param Request $request
*/
public function excluirAction( Request $request )
{
if ( ! $request->isXmlHttpRequest() ) // Verifica se se trata de uma requisição AJAX
throw $this->createNotFoundException( 'Página não encontrada' );
$usuario = $this->getDoctrine()->getRepository('CacicCommonBundle:Usuario')->find( $request->get('id') );
$nivelUser = $this->getDoctrine()->getRepository('CacicCommonBundle:Usuario')->nivel($usuario);
$csNivel = $this->getDoctrine()->getRepository('CacicCommonBundle:Usuario')->csNivelAdm();
if ( ! $usuario )
throw $this->createNotFoundException( 'Usuário não encontrado' );
if($csNivel[0]["cont"] == 1 && $nivelUser[0]["teGrupoUsuarios"] == "Administração"){
$this->get('session')->getFlashBag()->add('error', 'Exclusão não permitida, deve haver ao menos um usuário Administrador');
}else
{
$em = $this->getDoctrine()->getManager();
$em->remove( $usuario );
$em->flush();
$response = new Response( json_encode( array('status' => 'ok') ) );
$response->headers->set('Content-Type', 'application/json');
return $response;
}
}
/**
* [TELA] de exibição dos dados do USUÁRIO logado
*/
public function meusdadosAction()
{
return $this->render(
'CacicCommonBundle:Usuario:meusdados.html.twig',
array(
'usuario' => $this->getUser()
)
);
}
}