Commit 914163589b24c36502004644802dcaa07f384699
1 parent
04a6ed49
Exists in
master
redmine #4583 validacao usuario ldap ao assinar
Showing
3 changed files
with
127 additions
and
12 deletions
Show diff stats
cit-ecm-api/src/main/java/br/com/centralit/api/service/impl/AssinaturaServiceImpl.java
1 | 1 | package br.com.centralit.api.service.impl; |
2 | 2 | |
3 | +import java.util.Hashtable; | |
4 | + | |
5 | +import javax.naming.AuthenticationException; | |
6 | +import javax.naming.Context; | |
7 | +import javax.naming.NamingEnumeration; | |
8 | +import javax.naming.NamingException; | |
9 | +import javax.naming.directory.DirContext; | |
10 | +import javax.naming.directory.InitialDirContext; | |
11 | +import javax.naming.directory.SearchControls; | |
12 | +import javax.naming.directory.SearchResult; | |
13 | + | |
3 | 14 | import org.springframework.beans.factory.annotation.Autowired; |
4 | 15 | import org.springframework.beans.factory.annotation.Value; |
5 | 16 | import org.springframework.security.core.context.SecurityContextHolder; |
... | ... | @@ -24,7 +35,10 @@ import br.com.centralit.framework.service.arquitetura.GenericServiceImpl; |
24 | 35 | import br.com.centralit.framework.util.UtilObjeto; |
25 | 36 | import br.com.centralit.framework.util.UtilString; |
26 | 37 | |
38 | + | |
27 | 39 | /** |
40 | + * <p><b>Title: </b></p> | |
41 | + * <p><b>Description: </b></p> | |
28 | 42 | * <p> |
29 | 43 | * <b>Title: </b> |
30 | 44 | * </p> |
... | ... | @@ -36,41 +50,65 @@ import br.com.centralit.framework.util.UtilString; |
36 | 50 | * @author rogerio.cassimiro |
37 | 51 | * |
38 | 52 | */ |
53 | +@SuppressWarnings({ "rawtypes", "unchecked", "unused" }) | |
39 | 54 | @Service("assinaturaService") |
40 | 55 | public class AssinaturaServiceImpl extends GenericServiceImpl<Assinatura, Long> implements AssinaturaService { |
41 | 56 | |
42 | 57 | @Autowired |
43 | 58 | private AssinaturaDao assinaturaDao; |
59 | + | |
44 | 60 | |
45 | 61 | @Autowired |
46 | 62 | private DocumentoGedService documentoGedService; |
63 | + | |
47 | 64 | |
48 | 65 | @Autowired |
49 | 66 | private UsuarioService usuarioService; |
67 | + | |
50 | 68 | |
51 | 69 | @Autowired |
52 | 70 | private DominioService dominioService; |
71 | + | |
53 | 72 | |
54 | 73 | @Autowired |
55 | 74 | private FuncaoService funcaoService; |
75 | + | |
56 | 76 | |
57 | 77 | @Autowired |
58 | 78 | private PessoaService pessoaService; |
79 | + | |
59 | 80 | |
60 | 81 | @Autowired |
61 | 82 | private HistoricoAlteracaoProcessoService historicoAlteracaoProcessoService; |
83 | + | |
62 | 84 | |
63 | 85 | @Autowired |
64 | 86 | private TarjaAssinaturaService tarjaAssinaturaService; |
87 | + | |
65 | 88 | |
66 | 89 | @Value("${metodo.autenticacao.ldap}") |
67 | 90 | private String ldap; |
91 | + | |
92 | + | |
93 | + @Value("${ldap.host.name}") | |
94 | + public String ldapHostName; | |
95 | + | |
96 | + @Value("${ldap.userDn}") | |
97 | + public String ldapUserDn = "CN=CitSmart,OU=CitSmart,OU=Pro,OU=ContasServicos,OU=CentralIT,DC=cit,DC=local"; | |
98 | + | |
99 | + @Value("${ldap.manager.password}") | |
100 | + public String ldapManagerPassword; | |
101 | + | |
102 | + @Value("${ldap.searchDC}") | |
103 | + public String ldapSearchDC; | |
68 | 104 | |
69 | 105 | @Autowired |
106 | + public AssinaturaServiceImpl( AssinaturaDao assinaturaDao) { | |
70 | 107 | public AssinaturaServiceImpl( AssinaturaDao assinaturaDao ) { |
71 | 108 | |
72 | 109 | this.dao = assinaturaDao; |
73 | 110 | } |
111 | + | |
74 | 112 | |
75 | 113 | /** |
76 | 114 | * Salva assinatura do documento e gera histórico |
... | ... | @@ -82,6 +120,7 @@ public class AssinaturaServiceImpl extends GenericServiceImpl<Assinatura, Long> |
82 | 120 | validarSenha(assinatura.getPassword(), usuario); |
83 | 121 | montarTransients(assinatura, usuario); |
84 | 122 | historicoAlteracaoProcessoService.gerarHistoricoAssinatura(assinatura); |
123 | + tarjaAssinaturaService.gerarAssinaturaInternaUsuario(assinatura,assinatura.getDocumentoGed().getFormaCriacao().getCodigo()); | |
85 | 124 | tarjaAssinaturaService.gerarAssinaturaInternaUsuario(assinatura, assinatura.getDocumentoGed().getFormaCriacao().getCodigo()); |
86 | 125 | alterarEstadoDocumento(assinatura.getDocumentoGed()); |
87 | 126 | return super.save(assinatura); |
... | ... | @@ -94,6 +133,7 @@ public class AssinaturaServiceImpl extends GenericServiceImpl<Assinatura, Long> |
94 | 133 | * @param documentoGed |
95 | 134 | */ |
96 | 135 | private void alterarEstadoDocumento(DocumentoGed documentoGed) { |
136 | + if(documentoGed.getFormaCriacao().getCodigo().equals(1L) && !documentoGed.getEstado().getCodigo().equals(2L)) | |
97 | 137 | |
98 | 138 | if (documentoGed.getFormaCriacao().getCodigo().equals(1L) && !documentoGed.getEstado().getCodigo().equals(2L)) |
99 | 139 | documentoGed.setEstado(dominioService.findByChaveAndCodigo("estadoDocumentoGed", 2L)); |
... | ... | @@ -114,6 +154,7 @@ public class AssinaturaServiceImpl extends GenericServiceImpl<Assinatura, Long> |
114 | 154 | assinatura.setTipoAssinatura(dominioService.findByChaveAndCodigo(Dominio.TIPO_ASSINATURA, Dominio.TIPO_ASSINATURA_INTERNA)); |
115 | 155 | assinatura.setFuncao(funcaoService.getReference(assinatura.getFuncao().getId())); |
116 | 156 | } |
157 | + | |
117 | 158 | |
118 | 159 | /** |
119 | 160 | * Método responsável por validar senha para assinar o documento |
... | ... | @@ -123,21 +164,85 @@ public class AssinaturaServiceImpl extends GenericServiceImpl<Assinatura, Long> |
123 | 164 | * @param usuario |
124 | 165 | */ |
125 | 166 | private void validarSenha(String password, Usuario usuario) { |
167 | + | |
168 | + if(!UtilString.isNullOrEmpty(ldap) && ldap.equals("false")) { | |
126 | 169 | |
127 | - if (!UtilString.isNullOrEmpty(ldap) && ldap.equals("false")) { | |
170 | + if(UtilString.isNullOrEmpty(password)) | |
171 | + if (!UtilString.isNullOrEmpty(ldap) && !Boolean.parseBoolean(ldap)) { | |
128 | 172 | |
129 | 173 | if (UtilString.isNullOrEmpty(password)) |
130 | 174 | throw new BusinessException("ECM.VALIDACAO.SENHA_ASSINATURA", CodigoErro.REGRA_NEGOCIO.getValue(), ""); |
175 | + | |
176 | + if(!UtilObjeto.isReferencia(this.usuarioService.loadUserByUsernamePasswordMobile(usuario.getUsername(), password))) | |
131 | 177 | |
132 | 178 | if (!UtilObjeto.isReferencia(this.usuarioService.loadUserByUsernamePasswordMobile(usuario.getUsername(), password))) |
133 | 179 | throw new BusinessException("ECM.VALIDACAO.SENHA_ASSINATURA_INVALIDA", CodigoErro.REGRA_NEGOCIO.getValue(), ""); |
180 | + | |
134 | 181 | |
135 | - } else { | |
136 | - // TODO LDAP validação | |
182 | + } else if (!UtilString.isNullOrEmpty(ldap) && Boolean.parseBoolean(ldap)) { | |
183 | + | |
184 | + autenticarMaster(password, usuario.getUsername()); | |
137 | 185 | } |
138 | 186 | } |
139 | 187 | |
140 | 188 | /** |
189 | + * Método responsável por autenticar usuário master do LDAP | |
190 | + * | |
191 | + * @author rogerio.cassimiro | |
192 | + * @return {@link Boolean} | |
193 | + */ | |
194 | + public boolean autenticarMaster(String password, String userName) { | |
195 | + | |
196 | + Hashtable authEnv = new Hashtable(11); | |
197 | + authEnv.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); | |
198 | + authEnv.put(Context.PROVIDER_URL, ldapHostName); | |
199 | + authEnv.put(Context.SECURITY_AUTHENTICATION, "simple"); | |
200 | + authEnv.put(Context.SECURITY_PRINCIPAL, ldapUserDn); | |
201 | + authEnv.put(Context.SECURITY_CREDENTIALS, ldapManagerPassword); | |
202 | + | |
203 | + try { | |
204 | + | |
205 | + SearchControls groupsSearchCtls = new SearchControls(); | |
206 | + groupsSearchCtls.setSearchScope(SearchControls.SUBTREE_SCOPE); | |
207 | + DirContext authContext = new InitialDirContext(authEnv); | |
208 | + NamingEnumeration results = authContext.search(ldapSearchDC, "(sAMAccountName=" + userName + ")", groupsSearchCtls); | |
209 | + while (results.hasMore()) { | |
210 | + SearchResult result = (SearchResult) results.next(); | |
211 | + if (autenticarUser(result.getName() + "," + ldapSearchDC, password)) { | |
212 | + return Boolean.TRUE; | |
213 | + } else { | |
214 | + //TODO LDAP validação | |
215 | + throw new BusinessException("ECM.VALIDACAO.SENHA_ASSINATURA_INVALIDA", CodigoErro.REGRA_NEGOCIO.getValue(), ""); | |
216 | + } | |
217 | + } | |
218 | + | |
219 | + } catch (AuthenticationException authEx) { | |
220 | + throw new BusinessException("ECM.VALIDACAO.SENHA_ASSINATURA_INVALIDA", CodigoErro.REGRA_NEGOCIO.getValue(), ""); | |
221 | + } catch (NamingException namEx) { | |
222 | + throw new BusinessException("ECM.VALIDACAO.SENHA_ASSINATURA_INVALIDA", CodigoErro.REGRA_NEGOCIO.getValue(), ""); | |
223 | + } | |
224 | + return Boolean.FALSE; | |
225 | + } | |
226 | + | |
227 | + private boolean autenticarUser(String caminho, String senha) { | |
228 | + | |
229 | + Hashtable authEnv = new Hashtable(11); | |
230 | + authEnv.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); | |
231 | + authEnv.put(Context.PROVIDER_URL, ldapHostName); | |
232 | + authEnv.put(Context.SECURITY_AUTHENTICATION, "simple"); | |
233 | + authEnv.put(Context.SECURITY_PRINCIPAL, caminho); | |
234 | + authEnv.put(Context.SECURITY_CREDENTIALS, senha); | |
235 | + try { | |
236 | + DirContext authContext = new InitialDirContext(authEnv); | |
237 | + return true; | |
238 | + } catch (AuthenticationException authEx) { | |
239 | + } catch (NamingException namEx) { | |
240 | + } | |
241 | + return false; | |
242 | + | |
243 | + } | |
244 | + | |
245 | + /** | |
141 | 246 | * Método responsável por validar se o usuário já teve assinatura anterior para o documento informado |
142 | 247 | * |
143 | 248 | * @author rogerio.cassimiro |
... | ... | @@ -145,6 +250,7 @@ public class AssinaturaServiceImpl extends GenericServiceImpl<Assinatura, Long> |
145 | 250 | */ |
146 | 251 | @Override |
147 | 252 | public Boolean validarAssinaturaPorUsuario(Long idDocumento) { |
253 | + return this.assinaturaDao.validarAssinaturaPorUsuario(idDocumento, ((Usuario) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getId()); | |
148 | 254 | |
149 | 255 | return this.assinaturaDao.validarAssinaturaPorUsuario(idDocumento, ( (Usuario) SecurityContextHolder.getContext().getAuthentication().getPrincipal() ).getId()); |
150 | 256 | } |
... | ... | @@ -154,10 +260,10 @@ public class AssinaturaServiceImpl extends GenericServiceImpl<Assinatura, Long> |
154 | 260 | |
155 | 261 | Assinatura assinatura = (Assinatura) this.assinaturaDao.getReference(id); |
156 | 262 | assinatura.getConteudoTarja().setConteudo(tarjaAssinaturaService.decrypted(assinatura.getConteudoTarja().getConteudoCriptografado())); |
263 | + if(UtilObjeto.isReferencia(assinatura.getDocumentoGed().getConteudoTarja())){ | |
157 | 264 | if (UtilObjeto.isReferencia(assinatura.getDocumentoGed().getConteudoTarja())) { |
158 | 265 | assinatura.getDocumentoGed().getConteudoTarja().setConteudo(tarjaAssinaturaService.decrypted(assinatura.getDocumentoGed().getConteudoTarja().getConteudoCriptografado())); |
159 | 266 | } |
160 | 267 | return assinatura; |
161 | 268 | } |
162 | - | |
163 | -} | |
269 | +} | |
164 | 270 | \ No newline at end of file | ... | ... |
cit-ecm-api/src/main/java/br/com/centralit/api/service/impl/TarjaAssinaturaServiceImpl.java
... | ... | @@ -52,6 +52,7 @@ import com.lowagie.text.DocumentException; |
52 | 52 | * @author ciro.junior (<a href="mailto:ciro.junior@centralit.com.br">ciro.junior@centralit.com.br</a>) |
53 | 53 | * |
54 | 54 | */ |
55 | + | |
55 | 56 | @Service("tarjaAssinaturaService") |
56 | 57 | public class TarjaAssinaturaServiceImpl extends GenericServiceImpl<TarjaAssinatura, Long> implements TarjaAssinaturaService { |
57 | 58 | |
... | ... | @@ -147,7 +148,7 @@ public class TarjaAssinaturaServiceImpl extends GenericServiceImpl<TarjaAssinatu |
147 | 148 | TarjaAssinatura tarjaAssinatura = (TarjaAssinatura) tarjaAssinaturaDao.getReference(assinatura.getConteudoTarja().getIdTarjaAssinatura()); |
148 | 149 | String conteudoAssinatura = getConteudoTarja(assinatura, tarjaAssinatura.getConteudo(), tarjaAssinatura); |
149 | 150 | |
150 | - gerarAssinaturaDocAnexo(assinatura, tarjaAssinatura.getAnexoImagem(), conteudoAssinatura); | |
151 | + gerarAssinaturaDocAnexo(assinatura, tarjaAssinatura.getAnexoImagem(), conteudoAssinatura, formaCriacao); | |
151 | 152 | if (formaCriacao == 1) |
152 | 153 | gerarAssinaturaDocOnline(assinatura, tarjaAssinatura.getAnexoImagem(), conteudoAssinatura); |
153 | 154 | } |
... | ... | @@ -196,12 +197,23 @@ public class TarjaAssinaturaServiceImpl extends GenericServiceImpl<TarjaAssinatu |
196 | 197 | * @throws DocumentException |
197 | 198 | * @throws IOException |
198 | 199 | */ |
199 | - private void gerarAssinaturaDocAnexo(Assinatura assinatura, AnexoImagem anexoImagem, String conteudoAssinatura) { | |
200 | + private void gerarAssinaturaDocAnexo(Assinatura assinatura, AnexoImagem anexoImagem, String conteudoAssinatura, Long formaCriacao) { | |
200 | 201 | |
201 | 202 | assinatura.getConteudoTarja().setConteudoCriptografado(this.encrypted(conteudoAssinatura)); |
202 | 203 | assinatura.getConteudoTarja().setAnexoImagem(anexoImagem); |
204 | + | |
205 | + if (formaCriacao == 2){ | |
206 | + StringBuilder conteudoOriginalDocAnexo = new StringBuilder(); | |
207 | + if (UtilObjeto.isReferencia(assinatura.getDocumentoGed().getConteudoTarja())) { | |
208 | + conteudoOriginalDocAnexo.append(decrypted(assinatura.getDocumentoGed().getConteudoTarja().getConteudoCriptografado())); | |
209 | + } else { | |
210 | + assinatura.getDocumentoGed().setConteudoTarja(new ConteudoTarja()); | |
211 | + } | |
212 | + conteudoOriginalDocAnexo.append(conteudoAssinatura); | |
213 | + assinatura.getDocumentoGed().getConteudoTarja().setConteudoCriptografado(this.encrypted(conteudoOriginalDocAnexo.toString())); | |
214 | + } | |
203 | 215 | } |
204 | - | |
216 | + | |
205 | 217 | /** |
206 | 218 | * Método responsável por gerar assinatura em documento online |
207 | 219 | * | ... | ... |
cit-ecm-web/src/main/webapp/html/assinatura/documentoAssinaturas.html
... | ... | @@ -43,10 +43,7 @@ |
43 | 43 | <td colspan="7"> |
44 | 44 | <iframe id="conteudoPrototipo" srcdoc="{{assinaturaSelecionada.documentoGed.conteudoTarja.conteudo}}" allowfullscreen class="iframe-assinatura" ng-if="visualizarAssinaturaOnLine"></iframe> |
45 | 45 | <div class="row" style="margin-top: 10px; margin-bottom: 10px;" ng-hide="visualizarAssinaturaOnLine"> |
46 | - <div class="col-sm-2" ng-show="assinaturaSelecionada.conteudoTarja.anexoImagem.anexo"> | |
47 | - <img ng-src="data:image/JPEG;base64,{{assinaturaSelecionada.conteudoTarja.anexoImagem.anexo}}" class="img-thumbnail" style="max-width: 80px; height: 80px;"> | |
48 | - </div> | |
49 | - <div class="col-md-10"> | |
46 | + <div class="col-md-12"> | |
50 | 47 | <iframe id="conteudoAssinado" srcdoc="{{assinaturaSelecionada.conteudoTarja.conteudo}}" allowfullscreen class="iframe-assinatura" |
51 | 48 | style="min-height: 80px !important; max-height: 120px !important;"> |
52 | 49 | </iframe> | ... | ... |