RecuperarSenhaBean.java
6.42 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
package br.gov.mc.cadsei.bean;
import java.text.Normalizer;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;
import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;
import br.gov.arquitetura.bean.BaseBean;
import br.gov.mc.arquitetura.util.GeradorSenha;
import br.gov.mc.cadsei.entidade.CadProponente;
import br.gov.mc.cadsei.service.GenericService;
import br.gov.mc.cadsei.service.ProponenteService;
import br.gov.mc.cadsei.util.Constantes;
import br.gov.mc.cadsei.util.CpfCnpjUtil;
import br.gov.mc.cadsei.util.CriptoUtil;
import br.gov.mc.cadsei.util.EnvioMail;
import br.gov.mc.corporativo.entidade.CrpPessoaFisica;
import br.gov.mc.corporativo.entidade.CrpTipoPessoa;
@ManagedBean(name = "recuperarSenhaBean")
@ViewScoped
public class RecuperarSenhaBean extends BaseBean<CadProponente> {
private static final long serialVersionUID = 1L;
@ManagedProperty(value = "#{genericService}")
private transient GenericService genericService;
@ManagedProperty(value = "#{proponenteService}")
private transient ProponenteService proponenteService;
private String cpfProponente;
private String email;
private Date dataNascimento;
private boolean captchaValido;
private String ipUsuario;
public String getCpfProponente() {
return cpfProponente;
}
public void setCpfProponente(String cpfProponente) {
this.cpfProponente = cpfProponente;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Date getDataNascimento() {
return dataNascimento;
}
public void setDataNascimento(Date dataNascimento) {
this.dataNascimento = dataNascimento;
}
public boolean isCaptchaValido() {
return captchaValido;
}
public void setCaptchaValido(boolean captchaValido) {
this.captchaValido = captchaValido;
}
public String getIpUsuario() {
return ipUsuario;
}
public void setIpUsuario(String ipUsuario) {
this.ipUsuario = ipUsuario;
}
public void buscaIp(){
if(this.getIpUsuario() == null || this.getIpUsuario().isEmpty()){
ExternalContext curContext = FacesContext.getCurrentInstance().getExternalContext();
HttpServletRequest request = (HttpServletRequest)curContext.getRequest();
this.setIpUsuario(request.getRemoteAddr());
}
}
/**
* Retira acentos do texto informado
* @param Texto a ser ajustado
* @return Texto ajustado
*/
private String normalize(String input){
return Normalizer.normalize(input, Normalizer.Form.NFD).replaceAll("[^\\p{ASCII}]", "").trim();
}
public String recuperar() throws Exception {
if (validaEnvio()){
CadProponente p = proponenteService.recuperarSenha(this.getEmail(), this.getDataNascimento(), cpfProponente.replace("-", "").replace("/", "").replace(".", ""));
if (p!=null){
try{
//Criando nova senha com criptografia
CadProponente prop = p;
String senhaUsuario = GeradorSenha.gerarSenha();
prop.setSenhaProponente(CriptoUtil.getCriptografia(senhaUsuario));
this.getProponenteService().atualizaSenhaProponente(prop);
prop.setSenha(senhaUsuario);
String descEmail = "sua senha foi recuperada com sucesso!";
EnvioMail.sendEmail(p, descEmail);
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO,"", "As informações foram enviadas para o e-mail cadastrado"));
this.clearForm();
}catch (Exception e) {
e.printStackTrace();
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,"", "Não foi possível gerar nova senha para o usuário."));
}
} else {
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,"", "Usuário não foi cadastrado ou houve uma falha na recuperação da sua senha"));
}
}
return "";
}
private void clearForm(){
this.setCpfProponente(null);
this.setEmail(null);
this.setCaptchaValido(false);
this.setDataNascimento(null);
}
@SuppressWarnings("unchecked")
public String validarCaptcha() {
buscaIp();
if (Constantes.USAR_CAPTCHA) {
if (getSessionMap().get("captchaValido") != null) {
captchaValido = (Boolean) getSessionMap().get("captchaValido");
}
if (captchaValido) {
getSessionMap().put("captchaValido", captchaValido);
return redirect("principal.jsf");
} else {
return redirect("login_externo.jsf");
}
} else {
captchaValido = true;
return "";
}
}
@Override
public CadProponente createModel() {
CadProponente proponente = new CadProponente();
CrpPessoaFisica crpf = new CrpPessoaFisica();
crpf.setTipoPessoa(new CrpTipoPessoa(CrpTipoPessoa.FISICA));
proponente.setPessoaProponente(crpf);
return proponente;
}
@Override
public String getQualifiedName() {
return "Proponente";
}
@Override
public boolean isFeminino() {
return false;
}
@Override
public String getRequiredMessage() {
return super.getRequiredMessage();
}
public boolean validaEnvio() throws Exception {
if(cpfProponente == ""){
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "", "Campo CPF Obrigatório"));
return false;
}
if(this.email == ""){
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "", "Campo E-mail Obrigatório"));
return false;
}
// if(this.dataNascimento == null){
// FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "", "Campo Data Nascimento Obrigatório"));
// return false;
// }
if(!(CpfCnpjUtil.ValidateCPF(cpfProponente))){
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,"", "CPF Inválido"));
return false;
}
return true;
}
public GenericService getGenericService() {
return genericService;
}
public void setGenericService(GenericService genericService) {
this.genericService = genericService;
}
public ProponenteService getProponenteService() {
return proponenteService;
}
public void setProponenteService(ProponenteService proponenteService) {
this.proponenteService = proponenteService;
}
}