private bool PassExists(string password, int idUsuario) { if (password == "" || password == null) { return(false); } password = Encrypted.Encrypt(password); return(_context.Usuarios.Any(u => u.Clave == password && u.Id == idUsuario)); }
public async Task <IActionResult> OnPost() { if (!ModelState.IsValid) { return(NotFound()); } string Ruta = "Empleados"; Imagen Imagen = new Imagen(); if (Archivo != null) { //directorio de destino var filepath = "wwwroot/" + Ruta + "/"; var filename = Archivo.FileName; //validar antes de subir var isValidName = ValidFileName(filepath, filename); //nombre valido y el archivo no existe if (isValidName && !FileExists(filepath, filename)) { await UploadFile(filepath, filename, Archivo); Imagen.Nombre = filename; Imagen.IdRuta = this._context.Rutas.Where(r => r.Nombre.Equals(Ruta)).FirstOrDefault().Id; } }//envio imagen await this._context.Personas.AddAsync(this.Persona); await this._context.SaveChangesAsync(); this.Usuario.Estado = (sbyte)1; await this._context.Imagenes.AddAsync(Imagen); await this._context.SaveChangesAsync(); this.Usuario.IdImagen = Imagen.Id; this.Usuario.Clave = Encrypted.Encrypt(this.Usuario.Clave); await this._context.Usuarios.AddAsync(this.Usuario); await this._context.SaveChangesAsync(); this.Empleado.IdPersona = this.Persona.Id; this.Empleado.IdUsuario = this.Usuario.Id; await this._context.Empleados.AddAsync(this.Empleado); await this._context.SaveChangesAsync(); return(Page()); }
public async Task <IActionResult> OnPostAsync(Boolean Logout) { var Session = HttpContext.Session; if (Logout == true || Usuario.Correo == null) { Session.Clear(); return(Page()); } if (Usuario.Correo == null || Usuario.Clave == null) { Mensaje = "Complete los campos!"; return(Page()); } else { var user = await _context.Usuarios.AnyAsync(uc => uc.Correo == this.Usuario.Correo); var clave = await _context.Usuarios.AnyAsync(uc => uc.Clave == Encrypted.Encrypt(this.Usuario.Clave)); Usuario u = await _context.Usuarios .Where(us => us.Nombre == Usuario.Correo || us.Correo == Usuario.Correo ) .Include(us => us.IdImagenNavigation) .ThenInclude(Img => Img.IdRutaNavigation) .Include(us => us.Empleado) .ThenInclude(e => e.IdPersonaNavigation) .SingleOrDefaultAsync(); if (!user) { Mensaje = "Su usuario no existe"; return(Page()); } if (!clave) { Mensaje = "Contraseña incorrecta!."; return(Page()); } if (u != null && u.Estado == 0) { Mensaje = "¡Acceso denegado. Lo sentimos!"; return(Page()); } if (u != null && u.Estado == 1 && user && clave) { Persona per = u.Empleado.IdPersonaNavigation; string NombreCompleto = per.NombreCompleto(); string ruta = ""; Imagen Imagen = u.IdImagenNavigation; if (Imagen != null) { ruta = Imagen.IdRutaNavigation.Nombre + "/" + Imagen.Nombre; } Bitacora Bitacora = new Bitacora(); Bitacora.IdUsuario = u.Id; Bitacora.InicioSesion = DateTime.Now; this._context.Bitacoras.Add(Bitacora); this._context.SaveChanges(); Session.SetInt32("IdUsuario", u.Id); Session.SetInt32("IdBitacora", Bitacora.Id); Session.SetInt32("Privilegio", u.Privilegio); Session.SetString("NombreCompleto", NombreCompleto); Session.SetString("Ruta", ruta); } } return(RedirectToPage("/Index")); }
public async Task <JsonResult> OnPostActualizar() { //Verificacion y actualizacion de datos de Imagen Imagen newImagen = null; bool cambios = false; if (this.Archivo != null) { newImagen = new Imagen(); //directorio de destino var filepath = "wwwroot/Empleados/"; var filename = Archivo.FileName; //validar antes de subir var isValidName = ValidFileName(filepath, filename); //nombre valido y el archivo no existe if (isValidName && !FileExists(filepath, filename)) { await UploadFile(filepath, filename, Archivo); newImagen.Nombre = filename; newImagen.IdRuta = 3; } this._context.Imagenes.Add(newImagen); this._context.SaveChanges(); }//envio imagen //Verificacion y actualizacion de datos de Usuario if (newImagen == null) { this._context.Entry(Usuario).Property(u => u.IdImagen).IsModified = false; } else { this.Usuario.IdImagen = newImagen.Id; this._context.Entry(Usuario).Property(u => u.IdImagen).IsModified = true; this.HttpContext.Session.SetString("Ruta", "Empleados/" + newImagen.Nombre); cambios = true; } if (this.Usuario.Nombre == "" || this.Usuario.Nombre == null || _context.Usuarios.Any(u => u.Nombre == this.Usuario.Nombre)) { this._context.Entry(Usuario).Property(u => u.Nombre).IsModified = false; } else { this._context.Entry(Usuario).Property(u => u.Nombre).IsModified = true; cambios = true; } if (_context.Usuarios.Any(u => u.Correo == this.Usuario.Correo)) { this._context.Entry(Usuario).Property(u => u.Correo).IsModified = false; } else { this._context.Entry(Usuario).Property(u => u.Correo).IsModified = true; cambios = true; } if (!PassExists(this.Pass, this.Usuario.Id) || this.NewPass == "" || this.NewPass == null || this.Pass == "" || this.Pass == null) { this._context.Entry(Usuario).Property(u => u.Clave).IsModified = false; } else { this.NewPass = Encrypted.Encrypt(this.NewPass); this.Usuario.Clave = this.NewPass; this._context.Entry(Usuario).Property(u => u.Clave).IsModified = true; cambios = true; } //Verificacion y actualizacion de datos de Empleado this._context.Entry(Empleado).Property(e => e.FechaIngreso).IsModified = false; if (this.Empleado.Dui == "" || this.Empleado.Dui == null || _context.Empleados.Any(u => u.Dui == this.Empleado.Dui)) { this._context.Entry(Empleado).Property(e => e.Dui).IsModified = false; } else { this._context.Entry(Empleado).Property(e => e.Dui).IsModified = true; cambios = true; } if (Empleado.FechaNacimiento == null || Empleado.FechaNacimiento == null || _context.Empleados.Any(e => e.FechaNacimiento == this.Empleado.FechaNacimiento && e.Id == this.Empleado.Id)) { this._context.Entry(Empleado).Property(e => e.FechaNacimiento).IsModified = false; } else { this._context.Entry(Empleado).Property(e => e.FechaNacimiento).IsModified = true; cambios = true; } //Verificacion y actualizacion de datos de Persona if (this.Persona.Telefono == "" || this.Persona.Telefono == null || _context.Personas.Any(p => p.Telefono == this.Persona.Telefono)) { this._context.Entry(Persona).Property(p => p.Telefono).IsModified = false; } else { this._context.Entry(Persona).Property(p => p.Telefono).IsModified = true; cambios = true; } if (Persona.Nombres == "" || Persona.Nombres == null || _context.Personas.Any(p => p.Nombres == this.Persona.Nombres && p.Id == this.Persona.Id)) { this._context.Entry(Persona).Property(p => p.Nombres).IsModified = false; } else { this._context.Entry(Persona).Property(p => p.Nombres).IsModified = true; this.HttpContext.Session.SetString("NombreCompleto", (this.Persona.Nombres + " " + this.Persona.Apellidos)); cambios = true; } if (Persona.Apellidos == "" || Persona.Apellidos == null || _context.Personas.Any(p => p.Apellidos == this.Persona.Apellidos && p.Id == this.Persona.Id)) { this._context.Entry(Persona).Property(p => p.Apellidos).IsModified = false; } else { this._context.Entry(Persona).Property(p => p.Apellidos).IsModified = true; this.HttpContext.Session.SetString("NombreCompleto", (this.Persona.Nombres + " " + this.Persona.Apellidos)); cambios = true; } if (Persona.Genero > 1 || Persona.Genero < 0 || _context.Personas.Any(p => p.Genero == this.Persona.Genero && p.Id == this.Persona.Id)) { this._context.Entry(Persona).Property(p => p.Genero).IsModified = false; } else { this._context.Entry(Persona).Property(p => p.Genero).IsModified = true; cambios = true; } if (_context.Personas.Any(p => p.Direccion == this.Persona.Direccion && p.Id == this.Persona.Id)) { this._context.Entry(Persona).Property(p => p.Direccion).IsModified = false; } else { this._context.Entry(Persona).Property(p => p.Direccion).IsModified = true; cambios = true; } try { await this._context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException e) { Console.WriteLine("Mensaje =" + e.Message); throw; } if (cambios == true) { return(new JsonResult("Cambios")); } else { return(new JsonResult("No Cambios")); } }
public IActionResult OnPost() { int?IdUsuarioTemporal = HttpContext.Session.GetInt32("IdUsuarioTemporal"); if (IdUsuarioTemporal == null) { return(RedirectToPage("Welcome/Login")); } if (Clave == null || ReClave == null) { Mensaje = "Complete los campos"; return(Page());// } else if (Clave != ReClave) { Mensaje = "Las contraseñas no coinciden"; return(Page()); } else { Usuario Usuario = _context.Usuarios .Where(us => us.Id == IdUsuarioTemporal.Value ) .Include(us => us.IdImagenNavigation) .ThenInclude(Img => Img.IdRutaNavigation) .Include(us => us.Empleado) .ThenInclude(e => e.IdPersonaNavigation) .SingleOrDefault(); if (Usuario == null) { Mensaje = "Su usuario no existe"; return(Page()); } if (Usuario != null && Usuario.Estado == 0) { Mensaje = "¡Acceso denegado. Lo sentimos!"; return(Page()); } //proceder a cambiar contraseña Usuario.Clave = Encrypted.Encrypt(this.Clave); _context.Usuarios.Attach(Usuario); _context.Entry(Usuario).Property(r => r.Clave).IsModified = true; _context.SaveChanges(); Persona per = Usuario.Empleado.IdPersonaNavigation; string NombreCompleto = per.NombreCompleto(); string ruta = ""; Imagen Imagen = Usuario.IdImagenNavigation; if (Imagen != null) { ruta = Imagen.IdRutaNavigation.Nombre + "/" + Imagen.Nombre; } HttpContext.Session.Clear(); HttpContext.Session.SetInt32("IdUsuario", Usuario.Id); HttpContext.Session.SetInt32("Privilegio", Usuario.Privilegio); HttpContext.Session.SetString("NombreCompleto", NombreCompleto); HttpContext.Session.SetString("Ruta", ruta); return(RedirectToPage("/Index")); } }