Subversion Repositories Integrator Subversion

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1 espaco 1
package br.com.ec.controller.seguranca;
2
 
3
import java.io.IOException;
4
import java.io.Serializable;
5
 
6
import javax.faces.context.FacesContext;
7
import javax.inject.Inject;
8
import javax.inject.Named;
9
 
10
import org.springframework.context.annotation.Scope;
11
 
12
import br.com.ec.domain.model.Loja;
13
import br.com.ec.domain.model.Parametro;
14
import br.com.ec.domain.model.Usuario;
15
import br.com.ec.domain.model.UsuarioLoja;
16
import br.com.ec.domain.service.perfil.PerfilService;
17
import br.com.ec.domain.service.seguranca.ContextoSeguranca;
18
import br.com.ec.domain.service.usuario.UsuarioService;
19
import br.com.ec.domain.shared.ConstantesSEC;
20
import br.edu.cesmac.core.util.VerificadorUtil;
21
import br.edu.cesmac.web.exception.VerificadorLancamentoException;
22
import br.edu.cesmac.web.exception.VerificadorLancamentoException.CommandBean;
23
import br.edu.cesmac.web.message.LancadorMensagem;
24
 
25
@Named
26
@Scope("view")
27
public class SegurancaBean implements Serializable{
28
 
29
        private static final long serialVersionUID = 1L;
30
 
31
        private ContextoSeguranca contextoSeguranca;
32
        private Usuario usuario;
33
        private String novaSenha;
34
 
35
        private UsuarioService usuarioService;
36
        private PerfilService perfilService;
37
 
38
        @Inject
39
        public SegurancaBean(ContextoSeguranca contextoSeguranca, UsuarioService usuarioService, PerfilService perfilService) {
40
                this.contextoSeguranca = contextoSeguranca;
41
                this.usuarioService = usuarioService;
42
                this.perfilService = perfilService;
43
        }
44
 
45
        public ContextoSeguranca getContextoSeguranca() {
46
                return contextoSeguranca;
47
        }
48
        public void setContextoSeguranca(ContextoSeguranca contextoSeguranca) {
49
                this.contextoSeguranca = contextoSeguranca;
50
        }
51
 
52
        public Usuario getUsuario() {
53
                if (usuario == null) {
54
                        setUsuario(contextoSeguranca.obterUsuario());
55
                }
56
                return usuario;
57
        }
58
        public void setUsuario(Usuario usuario) {
59
                this.usuario = usuario;
60
        }
61
 
62
        public String getNovaSenha() {
63
                return novaSenha;
64
        }
65
        public void setNovaSenha(String novaSenha) {
66
                this.novaSenha = novaSenha;
67
        }
68
 
69
        /************************************************************************/
70
 
71
        public Boolean temPerfilAdministrador() {
72
                return perfilService.temPerfilAdministrador(getUsuario());
73
        }
74
 
75
        public Boolean temPerfilGerenteAdministrativo() {
76
                return perfilService.temPerfilGerenteAdministrativo(getUsuario());
77
        }
78
 
79
        /************************************************************************/
80
 
81
        public Boolean temPerfilGerenteFinanceiro() {
82
                return perfilService.temPerfilGerenteFinanceiro(getUsuario());
83
        }
84
 
85
        public Boolean temPerfilGerenteEcommerce() {
86
                return perfilService.temPerfilGerenteEcommerce(getUsuario());
87
        }
88
 
89
        public Boolean temPerfilGerenteDeLojistica() {
90
                return perfilService.temPerfilGerenteDeLojistica(getUsuario());
91
        }
92
 
93
        public Boolean temPerfilVendedor() {
94
                return perfilService.temPerfilVendedor(getUsuario());
95
        }
96
 
97
        public Boolean temPerfilGerenteDeCompras() {
98
                return perfilService.temPerfilGerenteDeCompras(getUsuario());
99
        }
100
 
101
        public Boolean temPerfilGerenteVivo() {
102
                return perfilService.temPerfilGerenteVivo(getUsuario());
103
        }
104
 
105
        public Boolean temAcessoLoja(Long sequencialLoja) {
106
                return temPerfilGerenteAdministrativo() || verificarAcessoLoja(getUsuario(), sequencialLoja);
107
        }
108
 
109
        public Boolean temAcessoLojaVivo() {
110
                return temPerfilGerenteAdministrativo() || verificarAcessoLoja(getUsuario(), ConstantesSEC.SEQUENCIAL_LOJA_VIVO_4);
111
        }
112
 
113
        private Boolean verificarAcessoLoja(Usuario usuario, Long sequencialLoja) {
114
                for (UsuarioLoja usuarioLoja : usuario.getLojas()) {
115
                        if (usuarioLoja.getLoja().getSequencial().equals(sequencialLoja)) {
116
                                return true;
117
                        }
118
                }
119
                return false;
120
        }
121
 
122
        public void restaurarTelaPDV(Loja loja) throws IOException {
123
            FacesContext.getCurrentInstance().getExternalContext().redirect(FacesContext.getCurrentInstance().getExternalContext().getRequestContextPath() + "/sistema/pdv.xhtml?sequencialLoja=" + loja.getSequencial());
124
        }
125
 
126
        public void restaurarTelaPDVVivo(Loja loja) throws IOException {
127
            FacesContext.getCurrentInstance().getExternalContext().redirect(FacesContext.getCurrentInstance().getExternalContext().getRequestContextPath() + "/sistema/pdv_vivo.xhtml?sequencialLoja=" + loja.getSequencial());
128
        }
129
 
130
        public void restaurarTelaPDVFarma(Loja loja) throws IOException {
131
            FacesContext.getCurrentInstance().getExternalContext().redirect(FacesContext.getCurrentInstance().getExternalContext().getRequestContextPath() + "/sistema/pdv.xhtml?sequencialLoja=" + loja.getSequencial());
132
        }
133
 
134
        public void invalidarSessao() {
135
                FacesContext.getCurrentInstance().getExternalContext().invalidateSession();
136
        }
137
 
138
        public void alterarSenhaUsuario() {
139
                new VerificadorLancamentoException().tratarIhRelancarExcecaoSemLimparEntidade(new CommandBean() {
140
                        public void execute() {
141
                                getUsuario().setSenha(getNovaSenha());
142
                                usuarioService.alterarSenhaUsuario(getUsuario());
143
                                setNovaSenha("");
144
                                LancadorMensagem.lancarSucesso("SENHA ALTERADA COM SUCESSO");
145
                        }
146
                });
147
        }
148
 
149
        public Integer getTempoEstoqueMinimoParaComprasEmDias() {
150
                Parametro parametro = new Parametro();
151
                parametro.setCodigo(ConstantesSEC.Parametro.CODIGO_PARAMETRO_TEMPO_ESTOQUE_MINIMO_PARA_COMPRAS);
152
                parametro = getUsuario().getParametros().get(getUsuario().getParametros().indexOf(parametro));
153
                return VerificadorUtil.naoEstaNulo(parametro)? new Integer(parametro.getValor()) : 15;
154
        }
155
 
156
        public Integer getTempoEstoqueParaReporEmDias() {
157
                Parametro parametro = new Parametro();
158
                parametro.setCodigo(ConstantesSEC.Parametro.CODIGO_PARAMETRO_TEMPO_ESTOQUE_REPOSICAO_PARA_COMPRAS);
159
                parametro = getUsuario().getParametros().get(getUsuario().getParametros().indexOf(parametro));
160
                return VerificadorUtil.naoEstaNulo(parametro)? new Integer(parametro.getValor()) : 60;
161
        }
162
 
163
}