internal static DAL.DTO.Usuario ConvertToDAL(DTO.Usuario item) { DAL.DTO.Usuario user = null; if (item != null) { user = new DAL.DTO.Usuario { Id = item.Id, Nombre = item.Nombre, Identity_Code = item.Identity_Code, Password = item.Password }; } return(user); }
public int Agregar(Usuario usuario) { int id = -1; try { log.Debug("Agregar(). Se va a agregar un nuevo usuario"); UsuarioDAL model = new UsuarioDAL(_connectionString); // Las passwords se almacenan cifradas string strPwdCifrada = CifrarPassword(usuario.Password); DAL.DTO.Usuario usrDal = Converter.ConvertToDAL(usuario); usrDal.Password = strPwdCifrada; bool permitidoCrear = true; if (usuario.Identity_Code.Trim().Length > 0) { int idUserIC = model.ValidarIdentityCode_Libre(usuario.Identity_Code); if (idUserIC < 0 || idUserIC == usuario.Id) { // Permitimos que se mantenga el identityCode o se use porque está libre } else { // Ese identity code está en uso log.Debug("El identityCode que intenta asignar y este ya está en uso por otro usuario (IdentityCode: {0}", usuario.Identity_Code); id = -2; permitidoCrear = false; } } if (permitidoCrear) { id = model.Agregar(usrDal); } } catch (Exception er) { log.Error("Agregar()", er); } return(id); }
public int Modificar(Usuario usuario) { int result = -1; try { if (usuario != null) { log.Debug("Modificar(). Se va a modificar un usuario"); bool permititidoModificar = true; UsuarioDAL model = new UsuarioDAL(_connectionString); // Comprobamos si han cambiado el identityCode por otro que ya existía (y no era nuestro) if (usuario.Identity_Code.Trim().Length > 0) { int idUserIC = model.ValidarIdentityCode_Libre(usuario.Identity_Code); if (idUserIC < 0 || idUserIC == usuario.Id) { // Permitimos que se mantenga el identityCode o se use porque está libre } else { // Ese identity code está en uso log.Debug("Se ha cambiado el identityCode, y este ya está en uso por otro usuario (Id_User: {0}, Usuario: {1}", usuario.Id, usuario.Nombre); result = -2; permititidoModificar = false; } } if (permititidoModificar) { DAL.DTO.Usuario usrDal = Converter.ConvertToDAL(usuario); if (usuario.Password != null) { // Las passwords se almacenan cifradas string strPwdCifrada = CifrarPassword(usuario.Password); usrDal.Password = strPwdCifrada; } bool sw = model.Modificar(usrDal); if (sw) { // Como respuesta devolvemos el identificador del usuario result = usuario.Id; // Si había una caché para el usuario la borramos // Borramos la caché para ese usuario string nombreCache = string.Format("user{0}", usuario.Id); CacheData.Remove(nombreCache); } } } } catch (Exception er) { log.Error("Modificar()", er); } return(result); }