gleimar 2014-07-15 23:35:03 +00:00
parent 0f3fb937b1
commit 05ecfe8ad7
9 changed files with 191 additions and 44 deletions

View File

@ -7,6 +7,7 @@ package com.rjconsultores.ventaboletos.dao.hibernate;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.FetchMode;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.MatchMode;
@ -99,8 +100,11 @@ public class PuntoVentaHibernateDAO extends GenericHibernateDAO<PuntoVenta, Inte
public List<PuntoVenta> buscaPuntoVentaParada(Parada parada) {
Criteria c = getSession().createCriteria(getPersistentClass());
c.add(Restrictions.eq("activo", Boolean.TRUE));
c.add(Restrictions.eq("parada", parada));
c.addOrder(Order.asc("nombpuntoventa"));
return c.list();
}

View File

@ -14,6 +14,7 @@ import java.util.ArrayList;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Order;
@ -76,12 +77,10 @@ public class UsuarioHibernateDAO extends GenericHibernateDAO<Usuario, Integer> i
}
public List<Usuario> buscarPelaCveUsuario(String cveUsuario) {
Criteria c = getSession().createCriteria(getPersistentClass());
c.add(Restrictions.eq("activo", Boolean.TRUE));
c.add(Restrictions.eq("claveUsuario", cveUsuario));
return c.list();
Query query = getSession().createQuery("select u from Usuario u where u.activo =1 and u.claveUsuario = :clave order by u.claveUsuario" );
query.setParameter("clave", cveUsuario);
return query.list();
}
@SuppressWarnings("unchecked")

View File

@ -4,14 +4,25 @@
*/
package com.rjconsultores.ventaboletos.service;
import com.rjconsultores.ventaboletos.entidad.Perfil;
import java.util.List;
import com.rjconsultores.ventaboletos.entidad.Perfil;
/**
*
* @author rodrigo
*/
public interface PerfilService extends GenericService<Perfil, Integer> {
public interface PerfilService{
public List<Perfil> obtenerTodos();
public Perfil obtenerID(Integer id);
public Perfil suscribir(Perfil entidad);
public Perfil actualizacion(Perfil entidad);
public void borrar(Perfil entidad);
public List<Perfil> buscar(String dscPerfil);
}

View File

@ -7,13 +7,21 @@ package com.rjconsultores.ventaboletos.service;
import java.util.List;
import com.rjconsultores.ventaboletos.entidad.Empleado;
import com.rjconsultores.ventaboletos.entidad.Perfil;
import com.rjconsultores.ventaboletos.entidad.Usuario;
import com.rjconsultores.ventaboletos.exception.BusinessException;
/**
*
* @author gleimar
*/
public interface UsuarioService extends GenericService<Usuario, Integer> {
public interface UsuarioService {
public List<Usuario> obtenerTodos();
public Usuario obtenerID(Integer id);
public void borrar(Usuario entidad);
public List<Usuario> buscarPeloNome(String nome);
@ -22,4 +30,6 @@ public interface UsuarioService extends GenericService<Usuario, Integer> {
public List<Usuario> buscarPorEmpleado(Empleado get);
public List<Usuario> buscarPelaCveUsuario(String claveUsuario);
}
public Usuario suscribirActualizar(Usuario entidad, String senha, Perfil perfil) throws BusinessException;
}

View File

@ -4,14 +4,10 @@
*/
package com.rjconsultores.ventaboletos.service.impl;
import com.rjconsultores.ventaboletos.dao.UsuarioDAO;
import com.rjconsultores.ventaboletos.entidad.Empleado;
import com.rjconsultores.ventaboletos.entidad.Usuario;
import com.rjconsultores.ventaboletos.entidad.UsuarioPerfil;
import com.rjconsultores.ventaboletos.service.UsuarioService;
import com.rjconsultores.ventaboletos.utilerias.UsuarioLogado;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.security.core.userdetails.UserDetails;
@ -20,15 +16,29 @@ import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.rjconsultores.ventaboletos.dao.UsuarioDAO;
import com.rjconsultores.ventaboletos.dao.UsuarioPerfilDAO;
import com.rjconsultores.ventaboletos.entidad.Empleado;
import com.rjconsultores.ventaboletos.entidad.Perfil;
import com.rjconsultores.ventaboletos.entidad.Usuario;
import com.rjconsultores.ventaboletos.entidad.UsuarioPerfil;
import com.rjconsultores.ventaboletos.exception.BusinessException;
import com.rjconsultores.ventaboletos.service.UsuarioService;
import com.rjconsultores.ventaboletos.utilerias.ApplicationProperties;
import com.rjconsultores.ventaboletos.utilerias.UsuarioLogado;
import com.rjconsultores.ventaboletos.utilerias.seguridad.ContrasenaUtileria;
/**
*
* @author Administrador
*/
@Service("usuarioService")
public class UsuarioServiceImpl implements UsuarioService, UserDetailsService {
@Autowired
private UsuarioDAO usuarioDAO;
private UsuarioDAO usuarioDAO;
@Autowired
private UsuarioPerfilDAO usuarioPerfilDAO;
public List<Usuario> buscarPeloNome(String nome) {
return usuarioDAO.buscarPeloNome(nome);
@ -41,25 +51,68 @@ public class UsuarioServiceImpl implements UsuarioService, UserDetailsService {
public Usuario obtenerID(Integer id) {
return usuarioDAO.obtenerID(id);
}
@Transactional(rollbackFor = BusinessException.class)
@Override
public Usuario suscribirActualizar(Usuario entidad,String senha,Perfil perfil) throws BusinessException {
//validación duplicado
List<Usuario> lsUsuario = this.buscarPelaCveUsuario(entidad.getClaveUsuario());
boolean podeSalvar = false;
if (lsUsuario.isEmpty()) {
podeSalvar = true;
} else {
if (entidad.getUsuarioId() != null){
for (Usuario u : lsUsuario) {
if (u.getUsuarioId().equals(entidad.getUsuarioId())) {
podeSalvar = true;
}
}
}
}
@Transactional
public Usuario suscribir(Usuario entidad) {
if (!podeSalvar) {
throw new BusinessException("MSG.Registro.Existe");
}
if (senha != null){
//validación complejidad contrasena
if (ApplicationProperties.getInstance().contrasenaValidaComplejidad()){
ContrasenaUtileria.validarContrasenaCompleja(senha);
}
//encriptación contrasena
entidad.setSenha(ContrasenaUtileria.encriptarContrasena(senha));
}
//Perfil
UsuarioPerfil up = new UsuarioPerfil();
up.setUsuario(entidad);
up.setPerfil(perfil);
up.setActivo(Boolean.TRUE);
up.setFecmodif(Calendar.getInstance().getTime());
up.setUsuariomodifId(UsuarioLogado.getUsuarioLogado().getUsuarioId());
if (entidad.getUsuarioId() != null) {
List<UsuarioPerfil> lsUsuarioPerfil = usuarioPerfilDAO.obtenerPorUsuario(entidad);
for (UsuarioPerfil up2 : lsUsuarioPerfil) {
usuarioPerfilDAO.borrar(up2);
}
}
List<UsuarioPerfil> lsUp = new ArrayList<UsuarioPerfil>();
lsUp.add(up);
entidad.setUsuarioPerfilList(lsUp);
entidad.setUsuariomodifId(UsuarioLogado.getUsuarioLogado().getUsuarioId());
entidad.setFecmodif(Calendar.getInstance().getTime());
entidad.setActivo(Boolean.TRUE);
return usuarioDAO.suscribir(entidad);
if (entidad.getUsuarioId() == null){
return usuarioDAO.suscribir(entidad);
}else{
return usuarioDAO.actualizacion(entidad);
}
}
@Transactional
public Usuario actualizacion(Usuario entidad) {
entidad.setUsuariomodifId(UsuarioLogado.getUsuarioLogado().getUsuarioId());
entidad.setFecmodif(Calendar.getInstance().getTime());
entidad.setActivo(Boolean.TRUE);
return usuarioDAO.actualizacion(entidad);
}
@Transactional
public void borrar(Usuario entidad) {

View File

@ -123,5 +123,9 @@ public class ApplicationProperties {
String property = p.getProperty("diagramaautobus.dospestana", "0");
return property.equals("1");
}
public boolean contrasenaValidaComplejidad() {
String property = p.getProperty("contrasena.validaComplejidad", "0");
return property.equals("1");
}
}

View File

@ -4,7 +4,7 @@
* Copyright 2002 Sun Microsystems, Inc. All rights reserved.
* SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
*/
package com.rjconsultores.ventaboletos.utilerias.spring.security;
package com.rjconsultores.ventaboletos.utilerias.seguridad;
/**
* Static methods for translating Base64 encoded strings to byte arrays and vice-versa.

View File

@ -0,0 +1,75 @@
package com.rjconsultores.ventaboletos.utilerias.seguridad;
import java.io.File;
import java.net.URL;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Locale;
import java.util.regex.Pattern;
import org.zkoss.util.resource.LabelLocator;
import org.zkoss.util.resource.Labels;
import com.rjconsultores.ventaboletos.exception.BusinessException;
public class ContrasenaUtileria {
private static final int CANT_MIN_CARACTER = 8;
private static final int CANT_MIN_NUMERO = 1;
private static final int CANT_MIN_LETRA = 1;
private static final int CANT_ESPECIALES = 2;
private static Pattern patternHayNumero = Pattern.compile(".*[0-9]+.*");
private static Pattern patternHayLetra = Pattern.compile(".*[a-zA-Z]+.*");
private static Pattern patternHayEspeciales = Pattern.compile(".*[^\\w]{2,}+.*");
public static String encriptarContrasena(String contrasena){
MessageDigest md;
try {
md = MessageDigest.getInstance("MD5");
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
}
byte[] pass = md.digest(contrasena.getBytes());
return Base64.byteArrayToBase64(pass);
}
public static void validarContrasenaCompleja(String contrasena) throws BusinessException{
int length = contrasena.length();
if (length< CANT_MIN_CARACTER){
throw new BusinessException("complejidadContrasena.CANT_MIN_CARACTER", new Object[]{CANT_MIN_CARACTER});
}
if (!patternHayLetra.matcher(contrasena).find()){
throw new BusinessException("complejidadContrasena.CANT_MIN_LETRA", new Object[]{CANT_MIN_LETRA});
}
if (!patternHayNumero.matcher(contrasena).find()){
throw new BusinessException("complejidadContrasena.CANT_MIN_NUMERO", new Object[]{CANT_MIN_NUMERO});
}
if (!patternHayEspeciales.matcher(contrasena).find()){
throw new BusinessException("complejidadContrasena.CANT_ESPECIALES", new Object[]{CANT_ESPECIALES});
}
}
public static void main(String args[]){
System.out.println(ContrasenaUtileria.encriptarContrasena("gleimar"));
LabelLocator teste = new LabelLocator() {
@Override
public URL locate(Locale arg0) throws Exception {
return new File("E:/scia_ventaboletos_transpais_senda/sco/AdmVenta/Web/trunk/ventaboletos/web/WEB-INF/i3-label_pt_BR.label").toURI().toURL();
}
};
Labels.register(teste);
try {
ContrasenaUtileria.validarContrasenaCompleja("asdfasdf1@@");
System.out.println("ok");
} catch (BusinessException e) {
System.out.println(e.getLocalizedMessage());
}
}
}

View File

@ -1,25 +1,16 @@
package com.rjconsultores.ventaboletos.utilerias.spring.security;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import org.springframework.dao.DataAccessException;
import org.springframework.security.authentication.encoding.PasswordEncoder;
import com.rjconsultores.ventaboletos.utilerias.seguridad.ContrasenaUtileria;
public class MiPasswordEncoder implements PasswordEncoder {
@Override
public String encodePassword(String pwd, Object salt) throws DataAccessException {
try {
MessageDigest md = MessageDigest.getInstance("MD5");
byte[] pass = md.digest(pwd.getBytes());
return Base64.byteArrayToBase64(pass);
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException("No fue posible firmar el usuario. El algoritmo no fue encontrado", e);
}
return ContrasenaUtileria.encriptarContrasena(pwd);
}