public async Task <IActionResult> Post([FromBody] SayingVM saying) { if (!ModelState.IsValid) { return(BadRequest(ModelState.ToValidationMessages())); } if (saying.SageId == 0) { return(Ok(SaveResult.Fail( new ValidationMessages( new ValidationMessage( name: ValidationHelpers.GetFieldName(saying, x => x.SageId), // eg "saying.sageId" message: "Please select a sage." ) ) ))); } if (saying.Id > 0) { var sayingToUpdate = await _sayingQuery.GetByIdAsync(saying.Id); saying.UpdateSaying(sayingToUpdate); await _sayingCommand.UpdateAsync(sayingToUpdate); return(Ok(SaveResult.Success(saying.Id))); } var sayingToAdd = new Saying(); var sayingId = await _sayingCommand.CreateAsync(sayingToAdd); return(Ok(SaveResult.Success(sayingId))); }
public async Task <SaveResult> CambiarClave(Guid userId, string claveAnterior, string nuevaClave) { try { _userManager = new UserManager <ApplicationUser>(new UserStore <ApplicationUser>(UnitOfWork.Db)); var usuario = GetById(userId); var applicationUser = await _userManager.FindByIdAsync(usuario.ApplicationUserId); IdentityResult result; if (applicationUser.PasswordHash != null) { result = await _userManager.ChangePasswordAsync(usuario.ApplicationUserId, claveAnterior, nuevaClave); } else { result = await _userManager.AddPasswordAsync(usuario.ApplicationUserId, nuevaClave); } return(result.Succeeded ? SaveResult.Success() : SaveResult.Failed(result.Errors)); } catch (Exception ex) { return(ex.SaveResult()); } finally { _userManager.Dispose(); } }
public async Task <IActionResult> Post([FromBody] SageVM sage) { if (!ModelState.IsValid) { return(Ok(SaveResult.Fail(ModelState.ToValidationMessages()))); } var user = await _sageQuery.GetByIdAsync(sage.Id); sage.UpdateUser(user); await _sageCommand.UpdateAsync(user); _logger.LogInformation("Sage " + sage.Name + " [id: " + sage.Id + "] updated by " /* + _userHelper.UserName*/); return(Ok(SaveResult.Success(user.Id))); }
public override async Task <SaveResult> DeleteAsync(Usuario entity) { try { using (var tran = UnitOfWork.StartTransaction()) { await base.DeleteAsync(entity); _userManager = new UserManager <ApplicationUser>(new UserStore <ApplicationUser>(UnitOfWork.Db)); var userAplication = await _userManager.FindByIdAsync(entity.ApplicationUserId); await _userManager.DeleteAsync(userAplication); await UnitOfWork.CommitAsync(); } return(SaveResult.Success(entity)); } catch (Exception ex) { return(ex.SaveResult()); } }
public SaveResult Save(string roleId, IEnumerable <Guid> menuIds) { var roleStore = new RoleStore <ApplicationRole>(Context); var roleManager = new RoleManager <ApplicationRole>(roleStore); try { var menus = Context.Menus.Where(x => menuIds.Any(s => x.Id.Equals(s))).ToList(); var role = roleManager.FindById(roleId); var oldMenus = role.Menus.Where(m => menus.All(m2 => m2.Id != m.Id)).ToList(); var newMenus = menus.Where(m => role.Menus.All(m2 => m2.Id != m.Id)).ToList(); foreach (var newMenu in newMenus) { role.Menus.Add(newMenu); } foreach (var oldMenu in oldMenus) { role.Menus.Remove(oldMenu); } Context.SaveChanges(); return(SaveResult.Success()); } catch (Exception ex) { return(ex.SaveResult()); } finally { roleStore.Dispose(); roleManager.Dispose(); } }
public async Task <SaveResult> SaveAsync(Usuario entity, string roleName) { try { using (var tran = UnitOfWork.StartTransaction()) { try { _userManager = new UserManager <ApplicationUser>(new UserStore <ApplicationUser>(UnitOfWork.Db)); _roleStore = new RoleStore <ApplicationRole>(UnitOfWork.Db); _roleManager = new RoleManager <ApplicationRole>(_roleStore); if (entity.IsNew()) { var userAplication = new ApplicationUser { UserName = entity.Identificacion, Email = entity.Email }; var result = await _userManager.CreateAsync(userAplication, entity.Identificacion); if (!result.Succeeded) { throw new Exception("Error al agregar usuario " + string.Join(",", result.Errors.ToArray())); } entity.ApplicationUserId = userAplication.Id; entity.FechaCreacion = DateTime.Now; entity.Estado = EstadoEnum.Activo; await _userManager.AddToRoleAsync(userAplication.Id, roleName); await CreateAsync(entity); } else { var userAplication = await _userManager.FindByIdAsync(entity.ApplicationUserId); var roles = await _userManager.GetRolesAsync(userAplication.Id); foreach (var role in roles) { if (role != roleName) { await _userManager.RemoveFromRoleAsync(userAplication.Id, role); } } var isInRole = roles.Any(r => r.Trim().ToUpper().Equals(roleName.Trim().ToUpper())); if (!isInRole) { await _userManager.AddToRoleAsync(userAplication.Id, roleName); } userAplication.Email = entity.Email; userAplication.FechaModificacion = DateTime.Now; UnitOfWork.Db.Entry(userAplication).State = EntityState.Modified; await UpdateAsync(entity); } await UnitOfWork.CommitAsync(); return(SaveResult.Success(entity)); } catch (Exception ex) { tran.Rollback(); throw ex; } } } catch (Exception ex) { return(ex.SaveResult()); } finally { _userManager.Dispose(); _roleStore.Dispose(); _roleManager.Dispose(); } }