protected void Guardar_Click(object sender, EventArgs e) { entidadRFV.Enabled = true; Page.Validate(); User user = Session["UserObj"] == null ? new User() : (User)Session["UserObj"]; if (user == null) { user = new User(); } //entidadRFV.Enabled = true; //Page.Validate(); if(Page.IsValid) { emailHF.Value = CorreoElectronico.Text; //string allTextBoxValues = ""}; //foreach (Control c in Page.Controls) //{ // foreach (Control childc in c.Controls) // { // if (childc is TextBox) // { // allTextBoxValues += ((TextBox)childc).Text + ","}; // } // } //} //if (allTextBoxValues != "") //{ // // Label1.Text = allTextBoxValues}; //} int[] err = new int[12]; //string Contrasena = Page.Request.Form["Contrasena"]; //string ConfirmarContrasena = Page.Request.Form["ConfirmarContrasena"]; //if (NombreUsuario.Text.Equals("")) { err[1] = 1; err[0] = 1; } //if (Inicial.Text.Equals("")) { err[2] = 1; err[0] = 1; } //if (ApellidoPaterno.Text.Equals("")) { err[3] = 1; err[0] = 1; } //if (ApellidoMaterno.Text.Equals("")) { err[4] = 1; err[0] = 1; } //if (CorreoElectronico.Text.Equals("")) { err[5] = 1; err[0] = 1; } //if (ConfirmarCorreoElectronico.Text.Equals("")) { err[6] = 1; err[0] = 1; } //if (Contrasena.Equals("")) { err[7] = 1; err[0] = 1; } //if (ConfirmarContrasena.Equals("")) { err[8] = 1; err[0] = 1; } //if (PreguntaSeguridad.SelectedIndex == 0) { err[9] = 1; err[0] = 1; } //if (RespuestaSeguridad.Text.Equals("")) { err[10] = 1; err[0] = 1; } //if (Telefono.Text.Equals("")) { err[11] = 1; err[0] = 1; } err[0] = 0; ////////////////////////////// EAF /////////////////////////////////// if (err[0] == 0) { string cs = ConfigurationManager.ConnectionStrings["EXC"].ConnectionString; using (SqlConnection conn = new SqlConnection(cs)) { using (SqlCommand command = new SqlCommand()) { command.Connection = conn; command.CommandType = CommandType.Text; var role = roles.Where(x => x.Nombre == roleDDL.SelectedValue).First(); var inspectores = new LinkedList<Inspector>(); if (string.IsNullOrWhiteSpace(Request.QueryString["Email"])) { #region Create //command.CommandText = @"DELETE FROM Usuarios WHERE CorreoElectronico = @CorreoElectronico;"; command.CommandText += @"INSERT INTO Usuarios VALUES (@NombreUsuario, @Inicial, @ApellidoPaterno, @ApellidoMaterno, @CorreoElectronico, @ConfirmarCorreoElectronico, @Contrasena, @ConfirmarContrasena, @PreguntaSeguridad, @RespuestaSeguridad, @Telefono, @UsuarioInterno, @EmpresaID, @Rol);"; if (user.Rol.ID == (int)Role.TipoRoles.Admin) { if (role.ID == (int)Role.TipoRoles.Inspector || role.ID == (int)Role.TipoRoles.SuperInspector) { inspectores = new SolicitudesInicialesServicio().GetInspectores(); int ultimaPlaca = Convert.ToInt32(inspectores.ElementAt(inspectores.Count - 1).NumeroPlacaID); command.CommandText += @"INSERT INTO Inspectores VALUES (@PlacaInspector, @NombreCompleto, @CorreoElectronico);"; command.Parameters.AddWithValue("@PlacaInspector", (ultimaPlaca + 1).ToString("D3")); command.Parameters.AddWithValue("@NombreCompleto", NombreUsuario.Text + " " + ApellidoPaterno.Text + " " + ApellidoMaterno.Text); command.Parameters.AddWithValue("@UsuarioInterno", true); } else if (role.ID == (int)Role.TipoRoles.Coordinador) { command.CommandText += @"INSERT INTO Coordinadores VALUES (@NombreCompleto);"; command.Parameters.AddWithValue("@NombreCompleto", NombreUsuario.Text + " " + ApellidoPaterno.Text + " " + ApellidoMaterno.Text); command.Parameters.AddWithValue("@UsuarioInterno", true); } else if (role.ID == (int)Role.TipoRoles.Admin || role.ID == (int)Role.TipoRoles.SuperInspector || role.ID == (int)Role.TipoRoles.Director) { command.Parameters.AddWithValue("@UsuarioInterno", true); } else { command.Parameters.AddWithValue("@UsuarioInterno", false); } //command.Parameters.AddWithValue("@Rol", role.ID); } else { //command.Parameters.AddWithValue("@Rol", ((int)Role.TipoRoles.Excavador - 1)); command.Parameters.AddWithValue("@UsuarioInterno", false); } #endregion } else { #region Edit command.CommandText += @"UPDATE Usuarios SET NombreUsuario = @NombreUsuario, Inicial = @Inicial, ApellidoPaterno = @ApellidoPaterno, ApellidoMaterno = @ApellidoMaterno, CorreoElectronico = @CorreoElectronico, ConfirmarCorreoElectronico = @ConfirmarCorreoElectronico, Contrasena = @Contrasena, ConfirmarContrasena = @ConfirmarContrasena, PreguntaSeguridad = @PreguntaSeguridad, RespuestaSeguridad = @RespuestaSeguridad, Telefono = @Telefono, UsuarioInterno = @UsuarioInterno, EmpresaID = @EmpresaID, Rol = @Rol WHERE CorreoElectronico = @OldEmail;"; command.Parameters.AddWithValue("@OldEmail", (string)Session["UserEmail"]); if (role.ID == (int)Role.TipoRoles.Inspector || role.ID == (int)Role.TipoRoles.SuperInspector) { inspectores = new SolicitudesInicialesServicio().GetInspectores(); string placaInspector = inspectores.Where(x => x.CorreoElectronico.Equals((string)Session["UserEmail"])).First().NumeroPlacaID; command.CommandText += @"UPDATE Inspectores SET NombreInspector = @NombreCompleto , CorreoElectronico = @CorreoElectronico WHERE PlacaInspector = @PlacaInspector;"; command.Parameters.AddWithValue("@PlacaInspector", placaInspector); command.Parameters.AddWithValue("@NombreCompleto", NombreUsuario.Text + " " + ApellidoPaterno.Text + " " + ApellidoMaterno.Text); command.Parameters.AddWithValue("@UsuarioInterno", true); } else if (role.ID == (int)Role.TipoRoles.Coordinador) { command.CommandText += @"UPDATE Coordinadores SET NombreCoordinador = @NombreCompleto WHERE NombreCoordinador = @NombreCoordinador;"; command.Parameters.AddWithValue("@NombreCoordinador", (string)Session["Username"]); command.Parameters.AddWithValue("@NombreCompleto", NombreUsuario.Text + " " + ApellidoPaterno.Text + " " + ApellidoMaterno.Text); command.Parameters.AddWithValue("@UsuarioInterno", true); } else if (role.ID == (int)Role.TipoRoles.Admin || role.ID == (int)Role.TipoRoles.SuperInspector || role.ID == (int)Role.TipoRoles.Director) { command.Parameters.AddWithValue("@UsuarioInterno", true); } else { command.Parameters.AddWithValue("@UsuarioInterno", false); } #endregion } command.Parameters.AddWithValue("@Rol", role.ID); command.Parameters.AddWithValue("@NombreUsuario", NombreUsuario.Text.ToUpper()); command.Parameters.AddWithValue("@Inicial", Inicial.Text.ToUpper()); command.Parameters.AddWithValue("@ApellidoPaterno", ApellidoPaterno.Text.ToUpper()); command.Parameters.AddWithValue("@ApellidoMaterno", ApellidoMaterno.Text.ToUpper()); ; command.Parameters.AddWithValue("@CorreoElectronico", CorreoElectronico.Text.ToLower()); command.Parameters.AddWithValue("@ConfirmarCorreoElectronico", ConfirmarCorreoElectronico.Text.ToLower()); if (string.IsNullOrWhiteSpace(ConfirmarContrasena.Value)) { string oldEmail = (string)Session["UserEmail"]; if(oldEmail.Equals(user.Email)) { command.Parameters.AddWithValue("@Contrasena", user.Password ); command.Parameters.AddWithValue("@ConfirmarContrasena", user.Password); } else { User selectedUser = new SolicitudesInicialesServicio().GetUser(Request.QueryString["Email"].Replace("%40", "@")); command.Parameters.AddWithValue("@Contrasena", selectedUser.Password); command.Parameters.AddWithValue("@ConfirmarContrasena", selectedUser.Password); } } else { command.Parameters.AddWithValue("@Contrasena", Contrasena.Value); command.Parameters.AddWithValue("@ConfirmarContrasena", Contrasena.Value); } //command.Parameters.AddWithValue("@Contrasena", string.IsNullOrWhiteSpace(ConfirmarContrasena.Value) // ? user.Password : Contrasena.Value ); //command.Parameters.AddWithValue("@ConfirmarContrasena", string.IsNullOrWhiteSpace(ConfirmarContrasena.Value) // ? user.Password : Contrasena.Value); command.Parameters.AddWithValue("@PreguntaSeguridad", PreguntaSeguridad.SelectedValue.ToString()); command.Parameters.AddWithValue("@RespuestaSeguridad", RespuestaSeguridad.Text.ToUpper()); command.Parameters.AddWithValue("@Telefono", Telefono.Text); //command.Parameters.AddWithValue("@UsuarioInterno", false); command.Parameters.AddWithValue("@EmpresaID", ddlEmpresas.SelectedValue); try { conn.Open(); int recordsAffected = command.ExecuteNonQuery(); if (string.IsNullOrWhiteSpace(Request.QueryString["Email"])) { #region Create user document directories Directory.CreateDirectory(Server.MapPath(string.Format("{0}/{1}", new DocumentoRequerido().DocumentosInicialesSavePath, CorreoElectronico.Text))); if (role.ID == (int)Role.TipoRoles.Inspector || role.ID == (int)Role.TipoRoles.SuperInspector) { Directory.CreateDirectory(Server.MapPath(string.Format("{0}/{1}", new DocumentoRequerido().DocumentosInspeccionesSavePath, CorreoElectronico.Text))); } Directory.CreateDirectory(Server.MapPath(string.Format("{0}/{1}", new DocumentoRequerido().DocumentosPrimerasSavePath, CorreoElectronico.Text))); Directory.CreateDirectory(Server.MapPath(string.Format("{0}/{1}", new DocumentoRequerido().DocumentosSegundasSavePath, CorreoElectronico.Text))); #endregion Response.Redirect("~/Account/Ingresar.aspx"); //Cancelar_Click(sender, e); } else { Entidad.User updatedUser = new SolicitudesInicialesServicio().GetUser(CorreoElectronico.Text); #region Update current user variable if (updatedUser.Email.Equals(user.Email)) { foreach (var r in roles) { if (r.ID == (user.Rol.Tipo)) { user.Rol = r; Session["UserObj"] = user; break; } } } #endregion Response.Redirect("~/Default.aspx"); } //command.ExecuteNonQuery(); //if (recordsAffected == 1) //{ //} //else //{ // throw new Exception(); //} } catch (Exception ex) { ScriptManager.RegisterStartupScript(this, this.GetType(), "Pop", "sqlerr()", true); } finally { conn.Close(); } } } } } }
protected void Guardar_Click(object sender, EventArgs e) { #region Evita issue donde deja crear user sin entidad entidadRFV.Enabled = true; Page.Validate(); #endregion User user = Session["UserObj"] == null ? new User() : (User)Session["UserObj"]; if (user == null) { user = new User(); } Page.Validate(); if(Page.IsValid) { emailHF.Value = CorreoElectronico.Text; int[] err = new int[12]; err[0] = 0; ////////////////////////////// EAF /////////////////////////////////// if (err[0] == 0) { string cs = ConfigurationManager.ConnectionStrings["EXC"].ConnectionString; using (SqlConnection conn = new SqlConnection(cs)) { using (SqlCommand command = new SqlCommand()) { command.Connection = conn; command.CommandType = CommandType.Text; var role = roles.Where(x => x.Nombre == roleDDL.SelectedValue).First(); var inspectores = new LinkedList<Inspector>(); if (string.IsNullOrWhiteSpace(Request.QueryString["Email"])) { #region Create command.CommandText += @"INSERT INTO Usuarios VALUES (@NombreUsuario, @Inicial, @ApellidoPaterno, @ApellidoMaterno, @CorreoElectronico, @ConfirmarCorreoElectronico, @Contrasena, @ConfirmarContrasena, @PreguntaSeguridad, @RespuestaSeguridad, @Telefono, @UsuarioInterno, @EmpresaID, @Rol, @Activo);"; if (user.Rol.ID == (int)Role.TipoRoles.Admin) { if (role.ID == (int)Role.TipoRoles.Inspector || role.ID == (int)Role.TipoRoles.SuperInspector) { inspectores = new SolicitudesInicialesServicio().GetInspectores(); int ultimaPlaca = Convert.ToInt32(inspectores.ElementAt(inspectores.Count - 1).NumeroPlacaID); command.CommandText += @"INSERT INTO Inspectores VALUES (@PlacaInspector, @NombreCompleto, @CorreoElectronico);"; command.Parameters.AddWithValue("@PlacaInspector", (ultimaPlaca + 1).ToString("D3")); command.Parameters.AddWithValue("@NombreCompleto", NombreUsuario.Text + " " + ApellidoPaterno.Text + " " + ApellidoMaterno.Text); command.Parameters.AddWithValue("@UsuarioInterno", true); } else if (role.ID == (int)Role.TipoRoles.Coordinador) { command.CommandText += @"INSERT INTO Coordinadores VALUES (@NombreCompleto);"; command.Parameters.AddWithValue("@NombreCompleto", NombreUsuario.Text + " " + ApellidoPaterno.Text + " " + ApellidoMaterno.Text); command.Parameters.AddWithValue("@UsuarioInterno", true); } else if (role.ID == (int)Role.TipoRoles.Admin || role.ID == (int)Role.TipoRoles.SuperInspector || role.ID == (int)Role.TipoRoles.Director) { command.Parameters.AddWithValue("@UsuarioInterno", true); } else { command.Parameters.AddWithValue("@UsuarioInterno", false); } } else { command.Parameters.AddWithValue("@UsuarioInterno", false); } #endregion } else { #region Edit command.CommandText += @"UPDATE Usuarios SET NombreUsuario = @NombreUsuario, Inicial = @Inicial, ApellidoPaterno = @ApellidoPaterno, ApellidoMaterno = @ApellidoMaterno, CorreoElectronico = @CorreoElectronico, ConfirmarCorreoElectronico = @ConfirmarCorreoElectronico, Contrasena = @Contrasena, ConfirmarContrasena = @ConfirmarContrasena, PreguntaSeguridad = @PreguntaSeguridad, RespuestaSeguridad = @RespuestaSeguridad, Telefono = @Telefono, UsuarioInterno = @UsuarioInterno, EmpresaID = @EmpresaID, Rol = @Rol, Activo = @Activo WHERE CorreoElectronico = @OldEmail;"; command.Parameters.AddWithValue("@OldEmail", (string)ViewState["UserEmail"]); if (role.ID == (int)Role.TipoRoles.Inspector || role.ID == (int)Role.TipoRoles.SuperInspector) { inspectores = new SolicitudesInicialesServicio().GetInspectores(); string placaInspector = inspectores.Where(x => x.CorreoElectronico.Equals((string)ViewState["UserEmail"])).First().NumeroPlacaID; command.CommandText += @"UPDATE Inspectores SET NombreInspector = @NombreCompleto , CorreoElectronico = @CorreoElectronico WHERE PlacaInspector = @PlacaInspector;"; command.Parameters.AddWithValue("@PlacaInspector", placaInspector); command.Parameters.AddWithValue("@NombreCompleto", NombreUsuario.Text + " " + ApellidoPaterno.Text + " " + ApellidoMaterno.Text); command.Parameters.AddWithValue("@UsuarioInterno", true); } else if (role.ID == (int)Role.TipoRoles.Coordinador) { command.CommandText += @"UPDATE Coordinadores SET NombreCoordinador = @NombreCompleto WHERE NombreCoordinador = @NombreCoordinador;"; command.Parameters.AddWithValue("@NombreCoordinador", (string)Session["Username"]); command.Parameters.AddWithValue("@NombreCompleto", NombreUsuario.Text + " " + ApellidoPaterno.Text + " " + ApellidoMaterno.Text); command.Parameters.AddWithValue("@UsuarioInterno", true); } else if (role.ID == (int)Role.TipoRoles.Admin || role.ID == (int)Role.TipoRoles.SuperInspector || role.ID == (int)Role.TipoRoles.Director) { command.Parameters.AddWithValue("@UsuarioInterno", true); } else { command.Parameters.AddWithValue("@UsuarioInterno", false); } #endregion } command.Parameters.AddWithValue("@Rol", role.ID); command.Parameters.AddWithValue("@Activo", true); command.Parameters.AddWithValue("@NombreUsuario", NombreUsuario.Text.ToUpper()); command.Parameters.AddWithValue("@Inicial", Inicial.Text.ToUpper()); command.Parameters.AddWithValue("@ApellidoPaterno", ApellidoPaterno.Text.ToUpper()); command.Parameters.AddWithValue("@ApellidoMaterno", ApellidoMaterno.Text.ToUpper()); ; command.Parameters.AddWithValue("@CorreoElectronico", CorreoElectronico.Text.ToLower()); command.Parameters.AddWithValue("@ConfirmarCorreoElectronico", ConfirmarCorreoElectronico.Text.ToLower()); //Si el pass es empty y el logged user esta updating su profile then use pass from logged user if (string.IsNullOrWhiteSpace(ConfirmarContrasena.Value)) { string oldEmail = (string)ViewState["UserEmail"]; if(oldEmail.Equals(user.Email)) { command.Parameters.AddWithValue("@Contrasena", user.Password ); command.Parameters.AddWithValue("@ConfirmarContrasena", user.Password); } //No uses pass del logged user sino, Get pass del user que se esta updating else { User selectedUser = new SolicitudesInicialesServicio().GetUser(Request.QueryString["Email"].Replace("%40", "@")); command.Parameters.AddWithValue("@Contrasena", selectedUser.Password); command.Parameters.AddWithValue("@ConfirmarContrasena", selectedUser.Password); } } else { command.Parameters.AddWithValue("@Contrasena", Contrasena.Value); command.Parameters.AddWithValue("@ConfirmarContrasena", Contrasena.Value); } command.Parameters.AddWithValue("@PreguntaSeguridad", PreguntaSeguridad.SelectedValue.ToString()); command.Parameters.AddWithValue("@RespuestaSeguridad", RespuestaSeguridad.Text.ToUpper()); command.Parameters.AddWithValue("@Telefono", Telefono.Text); command.Parameters.AddWithValue("@EmpresaID", ddlEmpresas.SelectedValue); try { conn.Open(); int recordsAffected = command.ExecuteNonQuery(); if (string.IsNullOrWhiteSpace(Request.QueryString["Email"])) { CreateUserDir(role, CorreoElectronico.Text); Response.Redirect("~/Account/Ingresar.aspx"); } else { string oldEmail = (string)ViewState["UserEmail"]; #region Update user dir if (!oldEmail.Equals(CorreoElectronico.Text)) { Directory.Move(Server.MapPath(string.Format("{0}/{1}", new DocumentoRequerido().DocumentosInicialesSavePath, oldEmail)), Server.MapPath(string.Format("{0}/{1}", new DocumentoRequerido().DocumentosInicialesSavePath, CorreoElectronico.Text))); Directory.Move(Server.MapPath(string.Format("{0}/{1}", new DocumentoRequerido().DocumentosPrimerasSavePath, oldEmail)), Server.MapPath(string.Format("{0}/{1}", new DocumentoRequerido().DocumentosPrimerasSavePath, CorreoElectronico.Text))); Directory.Move(Server.MapPath(string.Format("{0}/{1}", new DocumentoRequerido().DocumentosSegundasSavePath, oldEmail)), Server.MapPath(string.Format("{0}/{1}", new DocumentoRequerido().DocumentosSegundasSavePath, CorreoElectronico.Text))); if (role.ID == (int)Role.TipoRoles.Inspector || role.ID == (int)Role.TipoRoles.SuperInspector) { Directory.Move(Server.MapPath(string.Format("{0}/{1}", new DocumentoRequerido().DocumentosInspeccionesSavePath, oldEmail)), Server.MapPath(string.Format("{0}/{1}", new DocumentoRequerido().DocumentosInspeccionesSavePath, CorreoElectronico.Text))); } } #endregion #region Update current user variable if (oldEmail.Equals(user.Email)) { foreach (var r in roles) { if (r.ID == (user.Rol.Tipo)) { user.Rol = r; user.Email = CorreoElectronico.Text; user.EmpresaID = ddlEmpresas.SelectedValue; Session["UserObj"] = user; break; } } } #endregion Response.Redirect("~/Default.aspx"); } } catch (Exception) { ScriptManager.RegisterStartupScript(this, this.GetType(), "Pop", "sqlerr()", true); } finally { conn.Close(); } } } } } }