Completed
Push — master ( 4bace5...82c570 )
by Reginaldo
27:50
created

UsuarioController::autentica_email()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 6

Duplication

Lines 11
Ratio 100 %
Metric Value
dl 11
loc 11
rs 9.4286
cc 1
eloc 6
nc 1
nop 2
1
<?php
2
3
class UsuarioController extends AppController{
4
	public function beforeFilter(){
5
		return true;
6
   	}
7
8
	//faz o login no sistema, com a função autentica_email
9
	public function login(){
10
		$this->layout = 'ajax';//chama o layout para executar uma função ajax
11
12
		$login_email = $this->request->data['email'];//recebe o post email
13
		$login_senha = $this->request->data['senha'];//recebe o post senha
14
		echo json_encode(true);
15
	}
16
17
	public function processar_login() {
18
		//destroe alguma session criada anteriomente
19
		$this->Session->Destroy();
20
21
		$dados = $this->request->data('dados');
22
		$this->loadModel('Usuario');
23
		$resposta = $this->Usuario->find('all',
24
			array('conditions' => 
25
				array('Usuario.email' => $dados['email'], 
26
					  'Usuario.senha' => sha1($dados['senha'])
27
				)
28
			)
29
		);
30
		
31
		if (count($resposta) < 1) {
32
			$this->Session->setFlash('Ocorreu um erro ao logar na sua conta, verifique seus dados!');
33
            return $this->redirect('/home/login');
34
		}
35
36
		//faz o foreach com o array de dados do usuario
37
		foreach($resposta as $valor) {
38
			//escreve a sessao do usuario
39
			$this->Session->write('Usuario.id',   $valor['Usuario']['id']);
40
			$this->Session->write('Usuario.nome', $valor['Usuario']['nome']);//nome do usuario
41
			$this->Session->write('Usuario.email',$valor['Usuario']['email']);//email do usuario
42
			$this->Session->write('Usuario.senha',$valor['Usuario']['senha']);//senha do usuario criptografada
43
			$this->Session->write('Usuario.erp',  $valor['Usuario']['erp']);//situacao ativa(1) ou nao(0) no erp
44
			$this->Session->write('Usuario.ead',  $valor['Usuario']['ead']);//situacao ativa(1) ou nao(0) no ead
45
			$this->Session->write('Usuario.site', $valor['Usuario']['site']);//situacao ativa(1) ou nao(0) no site
46
		}
47
48
		$this->Session->setFlash('Bem vindo, '.$this->Session->read('Usuario.nome').'!');
49
        return $this->redirect('/dashboard/home');
50
	}
51
52
	public function processar_logout() {
53
		$this->Session->Destroy();
54
		return $this->redirect('/home/login');
55
	}
56
57
	public function logout(){
58
		$this->Session->Destroy();
59
60
		echo '<script>location.href="/winners/framework/"</script>';
61
	}
62
63
	//autentica email verifica se o email e senha existem para efetuar o login, ou outra acao.
64 View Code Duplication
	public function autentica_email($email,$senha){
65
		$this->loadModel('Usuario');
66
		$resposta = $this->Usuario->find('count', 
67
								array('conditions' => array('AND' => array('Usuario.email' => $email, 'Usuario.senha' => sha1($senha))
68
										)
69
									)
70
								);
71
		$this->set('resposta', $resposta);
72
73
		return $resposta;
74
	}			
75
76
	//se o email estiver livre retorna false, senão retorna true
77
	public function verificar_email($email){
78
		$this->layout = 'ajax';
79
		
80
		if(empty($email)){
81
			$email = $this->request->data['email'];
82
		}
83
84
		$this->loadModel('Usuario');
85
		$resposta = $this->Usuario->find('count',
86
											array('conditions' => array('Usuario.email' => $email))
87
										);
88
		$this->set('resposta', $resposta);
89
90
		if($resposta >= 1){
91
			return true;
92
		}else{
93
			return false;
94
		}
95
	}
96
97 View Code Duplication
	public function recuperar_dados($email,$senha){
98
		$this->loadModel('Usuario');
99
		$resposta = $this->Usuario->find('all', 
100
								array('conditions' => array('AND' => array('Usuario.email' => $email, 'Usuario.senha' => sha1($senha))
101
										)
102
									)
103
								);
104
105
		$this->set('resposta', $resposta);
106
107
		return $resposta;
108
	}
109
110
	public function novo_usuario() {
111
		$dados = $this->request->data('dados');
112
		$dados['senha'] = sha1($dados['senha']);
113
114
		if ($this->verificar_email($dados['email']) !== false) {
115
			$this->Session->setFlash('Email já cadastrado no sistema!');
116
			$this->redirect('/');
117
		}
118
119
		if ($this->Usuario->save($dados)) {
120
			$this->relacionar_modulos_teste($this->Usuario->id);
121
122
			$this->notificar_cadastro($dados['nome'], $dados['email']);
123
			$this->processar_login();
124
		}
125
126
		$this->Session->setFlash('Ocorreu um erro, tente novamente!');
127
		$this->redirect('/');
128
	}
129
130
	public function enviar_email_sucesso($email, $nome) {	
0 ignored issues
show
Unused Code introduced by
The parameter $email is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $nome is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
131
		$name = $dados['name'];
0 ignored issues
show
Bug introduced by
The variable $dados does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
132
		$email_address = $dados['email'];
133
	
134
		// Create the email and send the message
135
		$to = '[email protected]'; // Add your email address inbetween the '' replacing [email protected] - This is where the form will send a message to.
136
		$email_subject = "Contato Winners Desenvolvimento";
137
		$email_body = "Muito Obrigado por nos contactar";
138
		$headers = "From: [email protected]\n"; // This is the email address the generated message will be from. We recommend using something like [email protected].
139
		$headers .= "Reply-To: $email_address";	
140
		mail($to, $email_subject, $email_body, $headers);
141
	}
142
143
	public function notificar_cadastro($nome, $email) {
144
		$headers = "From: [email protected]\n"; // This is the email address the generated message will be from. We recommend using something like [email protected].
145
		$headers .= "Reply-To: $email_address";	
0 ignored issues
show
Bug introduced by
The variable $email_address does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
146
		mail('[email protected], [email protected], [email protected], [email protected]', 'Notificação de cadastro', 'O usuario ' . $nome . ' email ' . $email . ' ', $headers);
147
	}
148
149
	public function relacionar_modulos_teste($id) {
150
		$this->loadModel('ModuloRelacionaUsuario');
151
		
152
		$modulos = array(
153
			0 => array(
154
				'id_usuario' => $id,
155
				'id_modulo' => 1,
156
				'ativo' => 1
157
			),
158
			2 => array(
159
				'id_usuario' => $id,
160
				'id_modulo' => 2,
161
				'ativo' => 1
162
			),
163
			3 => array(
164
				'id_usuario' => $id,
165
				'id_modulo' => 3,
166
				'ativo' => 1
167
			),
168
			4 => array(
169
				'id_usuario' => $id,
170
				'id_modulo' => 4,
171
				'ativo' => 1
172
			)		
173
		);
174
175
		$this->ModuloRelacionaUsuario->saveAll($modulos);
176
177
		return true;
178
	}
179
180
	public function meus_dados() {
181
		$this->verificar_acesso();
182
		$this->layout = 'wadmin';
183
    	$this->set('modulos', $this->modulos);
184
	}
185
186
}