Subversion Repositories Integrator Subversion

Rev

Rev 305 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
106 espaco 1
package br.com.ec.controller.seguranca;
2
 
356 espaco 3
import java.io.BufferedInputStream;
106 espaco 4
import java.io.IOException;
356 espaco 5
import java.io.InputStream;
106 espaco 6
import java.io.Serializable;
356 espaco 7
import java.net.InetAddress;
8
import java.net.NetworkInterface;
9
import java.net.SocketException;
10
import java.net.UnknownHostException;
11
import java.text.ParseException;
12
import java.util.Enumeration;
13
import java.util.StringTokenizer;
106 espaco 14
 
15
import javax.faces.context.FacesContext;
16
import javax.inject.Inject;
17
import javax.inject.Named;
356 espaco 18
import javax.servlet.http.HttpServletRequest;
19
import javax.servlet.http.HttpServletResponse;
106 espaco 20
 
21
import org.springframework.context.annotation.Scope;
22
 
195 espaco 23
import br.com.ec.core.util.VerificadorUtil;
106 espaco 24
import br.com.ec.domain.model.Loja;
25
import br.com.ec.domain.model.Parametro;
26
import br.com.ec.domain.model.Usuario;
27
import br.com.ec.domain.model.UsuarioLoja;
28
import br.com.ec.domain.model.tipos.TipoAno;
29
import br.com.ec.domain.model.tipos.TipoMes;
30
import br.com.ec.domain.service.perfil.PerfilService;
31
import br.com.ec.domain.service.seguranca.ContextoSeguranca;
32
import br.com.ec.domain.service.usuario.UsuarioService;
33
import br.com.ec.domain.shared.ConstantesSEC;
195 espaco 34
import br.com.ec.web.exception.VerificadorLancamentoException;
35
import br.com.ec.web.exception.VerificadorLancamentoException.CommandBean;
36
import br.com.ec.web.message.LancadorMensagem;
106 espaco 37
 
38
@Named
39
@Scope("view")
40
public class SegurancaBean implements Serializable{
41
 
42
        private static final long serialVersionUID = 1L;
43
 
44
        private ContextoSeguranca contextoSeguranca;
45
        private Usuario usuario;
46
        private String novaSenha;
47
 
48
        private UsuarioService usuarioService;
49
        private PerfilService perfilService;
50
 
51
        @Inject
52
        public SegurancaBean(ContextoSeguranca contextoSeguranca, UsuarioService usuarioService, PerfilService perfilService) {
53
                this.contextoSeguranca = contextoSeguranca;
54
                this.usuarioService = usuarioService;
55
                this.perfilService = perfilService;
56
        }
57
 
58
        public ContextoSeguranca getContextoSeguranca() {
59
                return contextoSeguranca;
60
        }
61
        public void setContextoSeguranca(ContextoSeguranca contextoSeguranca) {
62
                this.contextoSeguranca = contextoSeguranca;
63
        }
64
 
65
        public Usuario getUsuario() {
66
                if (usuario == null) {
67
                        setUsuario(contextoSeguranca.obterUsuario());
68
                }
69
                return usuario;
70
        }
71
        public void setUsuario(Usuario usuario) {
72
                this.usuario = usuario;
73
        }
74
 
75
        public String getNovaSenha() {
76
                return novaSenha;
77
        }
78
        public void setNovaSenha(String novaSenha) {
79
                this.novaSenha = novaSenha;
80
        }
81
 
82
        /************************************************************************/
83
 
84
        public Boolean temPerfilAdministrador() {
85
                return perfilService.temPerfilAdministrador(getUsuario());
86
        }
87
 
88
        public Boolean temPerfilGerenteAdministrativo() {
89
                return perfilService.temPerfilGerenteAdministrativo(getUsuario());
90
        }
91
 
92
        /************************************************************************/
93
 
305 espaco 94
        public Boolean temPerfilFinanceiro() {
95
                return perfilService.temPerfilFinanceiro(getUsuario());
106 espaco 96
        }
97
 
305 espaco 98
        public Boolean temPerfilComercial() {
99
                return perfilService.temPerfilComercial(getUsuario());
106 espaco 100
        }
101
 
305 espaco 102
        public Boolean temPerfilLojistica() {
103
                return perfilService.temPerfilLojistica(getUsuario());
106 espaco 104
        }
105
 
106
        public Boolean temPerfilVendedor() {
107
                return perfilService.temPerfilVendedor(getUsuario());
108
        }
109
 
305 espaco 110
        public Boolean temPerfilCompras() {
111
                return perfilService.temPerfilCompras(getUsuario());
106 espaco 112
        }
113
 
114
        public Boolean temPerfilGerenteVivo() {
115
                return perfilService.temPerfilGerenteVivo(getUsuario());
116
        }
117
 
118
        public Boolean temPerfilTecnico() {
119
                return perfilService.temPerfilTecnico(getUsuario());
120
        }
121
 
122
        public Boolean temPerfilRecursosHumanos() {
123
                return perfilService.temPerfilRecursosHumanos(getUsuario());
124
        }
125
 
305 espaco 126
        public Boolean temPerfilOperacoes() {
127
                return perfilService.temPerfilOperacoes(getUsuario());
106 espaco 128
        }
129
 
305 espaco 130
        public Boolean temPerfilProducao() {
131
                return perfilService.temPerfilProducao(getUsuario());
132
        }
133
 
106 espaco 134
        public Boolean temPerfilSupervisor() {
135
                return perfilService.temPerfilSupervisor(getUsuario());
136
        }
137
 
138
        public Boolean temPerfilTreinamento() {
139
                return perfilService.temPerfilTreinamento(getUsuario());
140
        }
141
 
142
        public Boolean temPerfilLoja() {
143
                return perfilService.temPerfilLoja(getUsuario());
144
        }
145
 
146
        public Boolean temAcessoLoja(Long sequencialLoja) {
147
                return temPerfilGerenteAdministrativo() || verificarAcessoLoja(getUsuario(), sequencialLoja);
148
        }
149
 
150
        public Boolean temAcessoLojaVivo() {
151
                return temPerfilGerenteAdministrativo() || verificarAcessoLoja(getUsuario(), ConstantesSEC.SEQUENCIAL_LOJA_VIVO_4);
152
        }
153
 
154
        public Boolean temAcessoLojaCasaDasCapas() {
305 espaco 155
                return temPerfilCompras() || temPerfilComercial() || (verificarAcessoLoja(getUsuario(), ConstantesSEC.SEQUENCIAL_CASA_DAS_CAPAS_17) && temPerfilVendedor());
106 espaco 156
        }
157
 
158
        public Boolean naoTemAcessoLojaCasaDasCapas() {
305 espaco 159
                return temPerfilCompras() || temPerfilComercial() || !(verificarAcessoLoja(getUsuario(), ConstantesSEC.SEQUENCIAL_CASA_DAS_CAPAS_17) && temPerfilVendedor());
106 espaco 160
        }
161
 
162
        private Boolean verificarAcessoLoja(Usuario usuario, Long sequencialLoja) {
163
                for (UsuarioLoja usuarioLoja : usuario.getLojas()) {
164
                        if (usuarioLoja.getLoja().getSequencial().equals(sequencialLoja)) {
165
                                return true;
166
                        }
167
                }
168
                return false;
169
        }
170
 
171
        public void restaurarTelaPDV(Loja loja) throws IOException {
172
            FacesContext.getCurrentInstance().getExternalContext().redirect(FacesContext.getCurrentInstance().getExternalContext().getRequestContextPath() + "/sistema/pdv.xhtml?sequencialLoja=" + loja.getSequencial());
173
        }
174
 
175
        public void restaurarTelaPDVVivo(Loja loja) throws IOException {
176
            FacesContext.getCurrentInstance().getExternalContext().redirect(FacesContext.getCurrentInstance().getExternalContext().getRequestContextPath() + "/sistema/pdv_vivo.xhtml?sequencialLoja=" + loja.getSequencial());
177
        }
178
 
179
        public void restaurarTelaPDVFarma(Loja loja) throws IOException {
180
            FacesContext.getCurrentInstance().getExternalContext().redirect(FacesContext.getCurrentInstance().getExternalContext().getRequestContextPath() + "/sistema/pdv.xhtml?sequencialLoja=" + loja.getSequencial());
181
        }
182
 
183
        public void invalidarSessao() {
184
                FacesContext.getCurrentInstance().getExternalContext().invalidateSession();
185
        }
186
 
187
        public void alterarSenhaUsuario() {
188
                new VerificadorLancamentoException().tratarIhRelancarExcecaoSemLimparEntidade(new CommandBean() {
189
                        public void execute() {
190
                                getUsuario().setSenha(getNovaSenha());
191
                                usuarioService.alterarSenhaUsuario(getUsuario());
192
                                setNovaSenha("");
193
                                LancadorMensagem.lancarSucesso("SENHA ALTERADA COM SUCESSO");
194
                        }
195
                });
196
        }
197
 
198
        public Integer getTempoEstoqueMinimoParaComprasEmDias() {
199
                Parametro parametro = new Parametro();
200
                parametro.setCodigo(ConstantesSEC.Parametro.CODIGO_PARAMETRO_TEMPO_ESTOQUE_MINIMO_PARA_COMPRAS);
201
                parametro = getUsuario().getParametros().get(getUsuario().getParametros().indexOf(parametro));
202
                return VerificadorUtil.naoEstaNulo(parametro)? new Integer(parametro.getValor()) : 15;
203
        }
204
 
205
        public Integer getTempoEstoqueParaReporEmDias() {
206
                Parametro parametro = new Parametro();
207
                parametro.setCodigo(ConstantesSEC.Parametro.CODIGO_PARAMETRO_TEMPO_ESTOQUE_REPOSICAO_PARA_COMPRAS);
208
                parametro = getUsuario().getParametros().get(getUsuario().getParametros().indexOf(parametro));
209
                return VerificadorUtil.naoEstaNulo(parametro)? new Integer(parametro.getValor()) : 60;
210
        }
211
 
212
        public TipoAno[] getTiposAno() {
213
                return TipoAno.values();
214
        }
215
 
216
        public TipoMes[] getTiposMes() {
217
                return TipoMes.values();
218
        }
219
 
152 espaco 220
        public String textoSobreCorMargem() {
221
                return ConstantesSEC.Textos.TEXTO_COR_MARGEM;
222
        }
223
 
356 espaco 224
        /*
225
 
226
        public FacesContext getContext() {
227
                return FacesContext.getCurrentInstance();
228
        }
229
 
230
        private boolean verificarContexto() {
231
                FacesContext context = getContext();
232
                return VerificadorUtil.naoEstaNulo(context) && VerificadorUtil.naoEstaNulo(context.getExternalContext());
233
        }
234
 
235
        public HttpServletRequest getRequest() {
236
                return verificarContexto() ? (HttpServletRequest) getContext().getExternalContext().getRequest() : null;
237
        }
238
 
239
        public HttpServletResponse getResponse() {
240
                return verificarContexto() ? (HttpServletResponse) getContext().getExternalContext().getResponse() : null;
241
        }
242
 
243
        public String nomeMaquina() {
244
                String nomeMaquina = "";
245
                try {
246
                        nomeMaquina = InetAddress.getLocalHost().getHostName();
247
                        nomeMaquina = getRequest().getLocalName();
248
                } catch (UnknownHostException e) {
249
                        e.printStackTrace();
250
                }
251
                return nomeMaquina;
252
        }
253
 
254
        public String ipMaquina() {
255
                String ipMaquina = "";
256
                try {
257
                        ipMaquina = InetAddress.getLocalHost().getHostAddress();
258
                        ipMaquina = getRequest().getLocalAddr();
259
                } catch (UnknownHostException e) {
260
                        e.printStackTrace();
261
                }
262
                return ipMaquina;
263
        }
264
 
265
        public String ipMaquina2() {
266
                String ipMaquina = "";
267
                try {
268
                        ipMaquina = InetAddress.getLocalHost().getHostAddress();
269
//                      ipMaquina = getRequest().getRemoteAddr();
270
                        ipMaquina = getRequest().getHeader("X-FORWARDED-FOR");
271
                    if (ipMaquina == null) {
272
                        return getRequest().getRemoteAddr();
273
                    } else {
274
                        // As of https://en.wikipedia.org/wiki/X-Forwarded-For
275
                        // The general format of the field is: X-Forwarded-For: client, prox y1, proxy2 ...
276
                        // we only want the client
277
                        return new StringTokenizer(ipMaquina, ",").nextToken().trim();
278
                    }
279
                } catch (UnknownHostException e) {
280
                        e.printStackTrace();
281
                }
282
                return ipMaquina;
283
        }
284
 
285
        public String ipMaquina3() {
286
                String ipMaquina = "";
287
                try {
288
                        ipMaquina = InetAddress.getLocalHost().getHostAddress();
289
//                      ipMaquina = getRequest().getRemoteHost();
290
 
291
            Enumeration<NetworkInterface> networks = NetworkInterface.getNetworkInterfaces();
292
            StringBuilder macs = new StringBuilder("");
293
            while (networks.hasMoreElements()) {
294
                NetworkInterface network = networks.nextElement();
295
                byte[] mac = network.getHardwareAddress();
296
                if (mac != null) {
297
//                    System.out.print("Current MAC address : ");
298
                    StringBuilder sb = new StringBuilder();
299
                    for (int i = 0; i < mac.length; i++) {
300
                        sb.append(String.format(
301
                            "%02X%s", mac[i],
302
                            (i < mac.length - 1) ? "-" : ""));
303
                    }
304
                    // MAC Address
305
//                    System.out.println(sb.toString());
306
                    macs.append(sb.toString());
307
                    macs.append("\n");
308
                }
309
            }
310
            ipMaquina = macs.toString();
311
                } catch (UnknownHostException e) {
312
                        e.printStackTrace();
313
                } catch (SocketException e) {
314
                        // TODO Auto-generated catch block
315
                        e.printStackTrace();
316
                }
317
                return ipMaquina;
318
        }
319
 
320
        public String ipMaquina4() {
321
                StringBuilder ipMaquina = new StringBuilder("");
322
                try {
323
//                      ipMaquina = InetAddress.getLocalHost().getHostAddress();
324
                        ipMaquina.append("Informacoes");
325
                        ipMaquina.append("Sistema operacional: " + System.getProperty("os.name"));
326
                        ipMaquina.append("IP/Localhost: " + InetAddress.getLocalHost().getHostAddress());
327
                        ipMaquina.append("Nome da maquina: " + InetAddress.getLocalHost().getHostName());
328
                        ipMaquina.append("Nome completo da maquina: " + InetAddress.getLocalHost().getCanonicalHostName());
329
                        ipMaquina.append("Nome da maquina2: " + InetAddress.getLoopbackAddress().getHostName());
330
                        ipMaquina.append("Nome completo da maquina2: " + InetAddress.getLoopbackAddress().getCanonicalHostName());
331
                        ipMaquina.append("MAC Address: " + getMacAddress());
332
                } catch (UnknownHostException e) {
333
                        e.printStackTrace();
334
                } catch (IOException e) {
335
                        // TODO Auto-generated catch block
336
                        e.printStackTrace();
337
                }
338
                return ipMaquina.toString();
339
        }
340
 
341
        private final static String windowsParseMacAddress(String ipConfigResponse) throws ParseException {
342
            String localHost = null;
343
            try {
344
                localHost = InetAddress.getLocalHost().getHostAddress();
345
            } catch(java.net.UnknownHostException ex) {
346
                ex.printStackTrace();
347
                throw new ParseException(ex.getMessage(), 0);
348
            }
349
 
350
            StringTokenizer tokenizer = new StringTokenizer(ipConfigResponse, "\n");
351
            String lastMacAddress = null;
352
            StringBuilder infor = new StringBuilder("");
353
 
354
            while(tokenizer.hasMoreTokens()) {
355
                String line = tokenizer.nextToken().trim();
356
                infor.append(line);
357
                infor.append("\n");
358
 
359
                //IP
360
                if(line.endsWith(localHost) && lastMacAddress != null) {
361
                    return lastMacAddress;
362
                }
363
 
364
                //MAC address
365
                int macAddressPosition = line.indexOf(":");
366
                if(macAddressPosition <= 0) continue;
367
 
368
                String macAddressCandidate = line.substring(macAddressPosition + 1).trim();
369
                if(windowsIsMacAddress(macAddressCandidate)) {
370
                    lastMacAddress = macAddressCandidate;
371
                    continue;
372
                }
373
            }
374
 
375
            return infor.toString();
376
//          ParseException ex = new ParseException("Nao foi possível ler o MAC address de [" + ipConfigResponse + "]", 0);
377
//          ex.printStackTrace();
378
//          throw ex;
379
        }
380
 
381
        private final static boolean windowsIsMacAddress(String macAddressCandidate) {
382
            if(macAddressCandidate.length() != 17) return false;
383
 
384
            return true;
385
        }
386
 
387
        private final static String windowsRunIpConfigCommand() throws IOException {
388
            Process p = Runtime.getRuntime().exec("ipconfig /all");
389
            InputStream stdoutStream = new BufferedInputStream(p.getInputStream());
390
 
391
            StringBuffer buffer= new StringBuffer();
392
            for (;;) {
393
                int c = stdoutStream.read();
394
                if (c == -1) break;
395
                buffer.append((char)c);
396
            }
397
            String outputText = buffer.toString();
398
 
399
            stdoutStream.close();
400
 
401
            return outputText;
402
        }
403
 
404
 
405
        private final static String getMacAddress() throws IOException {
406
            try {
407
            return windowsParseMacAddress(windowsRunIpConfigCommand());
408
            } catch(ParseException ex) {
409
                ex.printStackTrace();
410
                throw new IOException(ex.getMessage());
411
            }
412
        }
413
 
414
        public final static void main(String[] args) {
415
                try {
416
                        System.out.println("Informacoes");
417
                        System.out.println("Sistema operacional: " + System.getProperty("os.name"));
418
                        System.out.println("IP/Localhost: " + InetAddress.getLocalHost().getHostAddress());
419
                        System.out.println("Nome da maquina: " + InetAddress.getLocalHost().getHostName());
420
                        System.out.println("Nome completo da maquina: " + InetAddress.getLocalHost().getCanonicalHostName());
421
                        System.out.println("Nome da maquina2: " + InetAddress.getLoopbackAddress().getHostName());
422
                        System.out.println("Nome completo da maquina2: " + InetAddress.getLoopbackAddress().getCanonicalHostName());
423
                        System.out.println("MAC Address: " + getMacAddress());
424
 
425
 
426
            // create an Enumeration of type
427
            // NetworkInterface and store the values
428
            // returned by
429
            // NetworkInterface.getNetworkInterfaces()
430
            // method
431
            Enumeration<NetworkInterface> networks = NetworkInterface.getNetworkInterfaces();
432
 
433
            // for every network in the networks Enumeration
434
            while (networks.hasMoreElements()) {
435
                NetworkInterface network
436
                    = networks.nextElement();
437
 
438
                // call getHardwareAddress() method on each
439
                // network and store the returned value in a
440
                // byte array
441
                byte[] mac = network.getHardwareAddress();
442
 
443
                if (mac != null) {
444
                    System.out.print(
445
                        "Current MAC address : ");
446
 
447
                    // convert the obtained byte array into
448
                    // a printable String
449
                    StringBuilder sb = new StringBuilder();
450
                    for (int i = 0; i < mac.length; i++) {
451
                        sb.append(String.format(
452
                            "%02X%s", mac[i],
453
                            (i < mac.length - 1) ? "-"
454
                                                 : ""));
455
                    }
456
 
457
                    // print the final String containing the
458
                    // MAC Address
459
                    System.out.println(sb.toString());
460
                }
461
            }
462
 
463
                } catch(Throwable t) {
464
                        t.printStackTrace();
465
                }
466
        }
467
 
468
        */
469
 
106 espaco 470
}