public async Task CheckValidityAsync(CallContext callContext) { if (QueryValidationHelper.IsReservedGuid(UserId)) { throw new InvalidOperationException("Invalid user id"); } if (QueryValidationHelper.IsReservedGuid(ImageId)) { throw new InvalidOperationException("Invalid image id"); } var imageDataBeforeUpdate = await callContext.DbContext.Images .AsNoTracking() .Where(img => img.Id == ImageId) .Select(img => new { nameBeforeUpdate = img.Name, sourceBeforeUpdate = img.Source, descriptionBeforeUpdate = img.Description }) .SingleAsync(); if (imageDataBeforeUpdate.nameBeforeUpdate == Name && imageDataBeforeUpdate.sourceBeforeUpdate == Source && imageDataBeforeUpdate.descriptionBeforeUpdate == Description) { throw new RequestInputException(callContext.Localized.Get("CanNotUpdateMetadataBecauseSameAsOriginal")); } if (imageDataBeforeUpdate.nameBeforeUpdate != Name) { await QueryValidationHelper.CheckCanCreateImageWithNameAsync(Name, callContext.DbContext, callContext.Localized); } QueryValidationHelper.CheckCanCreateImageWithSource(Source, callContext.Localized); QueryValidationHelper.CheckCanCreateImageWithDescription(Description, callContext.Localized); QueryValidationHelper.CheckCanCreateImageWithVersionDescription(VersionDescription, callContext.Localized); }
} //null means that we return only cards which have no tag (we exclude all tags) public async Task CheckValidityAsync(CallContext callContext) { if (QueryValidationHelper.IsReservedGuid(UserId)) { throw new RequestInputException("Invalid user id"); } if (ExcludedDeck != Guid.Empty) { var deck = callContext.DbContext.Decks.Include(d => d.Owner).SingleOrDefault(d => d.Id == ExcludedDeck); if (deck == null) { throw new RequestInputException($"Invalid deck id '{ExcludedDeck}'"); } if (deck.Owner.Id != UserId) { throw new RequestInputException($"Deck not allowed for user '{UserId}': '{ExcludedDeck}'"); } } if (ExcludedTags != null) { foreach (var excludedTag in ExcludedTags) { if (!await callContext.DbContext.Tags.AnyAsync(t => t.Id == excludedTag)) { throw new RequestInputException($"Invalid excluded tag id '{excludedTag}'"); } } if (ExcludedTags.GroupBy(guid => guid).Where(guid => guid.Count() > 1).Any()) { throw new RequestInputException("Excluded tag list contains duplicate"); } } foreach (var requiredTag in RequiredTags) { if (!callContext.DbContext.Tags.Any(t => t.Id == requiredTag)) { throw new RequestInputException($"Invalid required tag id '{requiredTag}'"); } } if (RequiredTags.GroupBy(guid => guid).Where(guid => guid.Count() > 1).Any()) { throw new RequestInputException("Required tag list contains duplicate"); } int userSubscriptionsCount = callContext.DbContext.SearchSubscriptions.Count(s => s.UserId == UserId); if (userSubscriptionsCount >= MaxSubscriptionCount) { throw new RequestInputException($"User already has {userSubscriptionsCount} subscriptions, can not have more than {MaxSubscriptionCount}"); } }
public async Task CheckValidityAsync(CallContext callContext) { if (QueryValidationHelper.IsReservedGuid(UserId)) { throw new InvalidOperationException("Invalid user ID"); } await QueryValidationHelper.CheckUserIsOwnerOfDeckAsync(callContext.DbContext, UserId, DeckId); CardVisibilityHelper.CheckUserIsAllowedToViewCards(callContext.DbContext, UserId, CardIds); }
public async Task CheckValidityAsync(CallContext callContext) { if (QueryValidationHelper.IsReservedGuid(UserId)) { throw new RequestInputException("Invalid user id"); } if (CardIds.Any(cardId => QueryValidationHelper.IsReservedGuid(cardId))) { throw new RequestInputException($"Invalid card id"); } await Task.CompletedTask; }
public async Task CheckValidityAsync(CallContext callContext) { QueryValidationHelper.CheckNotReservedGuid(UserId); if (CardIds.Any(cardId => QueryValidationHelper.IsReservedGuid(cardId))) { throw new RequestInputException($"Invalid card id"); } foreach (var cardId in CardIds) { CardVisibilityHelper.CheckUserIsAllowedToViewCards(callContext.DbContext, UserId, cardId); } await Task.CompletedTask; }
public async Task CheckValidityAsync(CallContext callContext) { if (QueryValidationHelper.IsReservedGuid(UserId)) { throw new InvalidOperationException("Invalid user ID"); } var user = await callContext.DbContext.Users.SingleAsync(u => u.Id == UserId); var cardVersion = await callContext.DbContext.CardPreviousVersions.Include(v => v.UsersWithView).SingleAsync(v => v.Id == VersionId); if (!CardVisibilityHelper.CardIsVisibleToUser(UserId, cardVersion.UsersWithView.Select(uwv => uwv.AllowedUserId))) { throw new InvalidOperationException("Original not visible to user"); } }
public async Task CheckValidityAsync(CallContext callContext) { await QueryValidationHelper.CheckUserExistsAsync(callContext.DbContext, UserId); if (CardIds.Any(cardId => QueryValidationHelper.IsReservedGuid(cardId))) { throw new RequestInputException($"Invalid card id"); } CardVisibilityHelper.CheckUserIsAllowedToViewCards(callContext.DbContext, UserId, CardIds.ToArray()); foreach (var cardId in CardIds) { await CheckUsersWithCardInADeckAsync(cardId, callContext.DbContext, callContext.Localized); await CheckCardVersionsCreatorsAsync(cardId, callContext.DbContext, callContext.Localized); } }
public async Task CheckValidityAsync(CallContext callContext) { if (!CardIds.Any()) { throw new RequestInputException("No card to add label to"); } if (QueryValidationHelper.IsReservedGuid(VersionCreator.Id)) { throw new RequestInputException("Invalid user id"); } if (QueryValidationHelper.IsReservedGuid(TagId)) { throw new RequestInputException("Reserved tag id"); } foreach (var cardId in CardIds) { CardVisibilityHelper.CheckUserIsAllowedToViewCards(callContext.DbContext, VersionCreator.Id, cardId); } if (!await callContext.DbContext.Tags.Where(tag => tag.Id == TagId).AnyAsync()) { throw new RequestInputException("Invalid tag id"); } }
public async Task CheckValidityAsync(CallContext callContext) { if (QueryValidationHelper.IsReservedGuid(CurrentUserId)) { throw new RequestInputException($"Invalid user id '{CurrentUserId}'"); } if (QueryValidationHelper.IsReservedGuid(DeckId)) { throw new RequestInputException($"Invalid deck id '{DeckId}'"); } if (ExcludedCardIds.Any(cardId => QueryValidationHelper.IsReservedGuid(cardId))) { throw new RequestInputException($"Invalid card id"); } if (ExcludedTagIds.Any(cardId => QueryValidationHelper.IsReservedGuid(cardId))) { throw new RequestInputException($"Invalid tag id"); } if (CardsToDownload < 1 || CardsToDownload > 100) { throw new RequestInputException($"Invalid CardsToDownload: {CardsToDownload}"); } await QueryValidationHelper.CheckUserIsOwnerOfDeckAsync(callContext.DbContext, CurrentUserId, DeckId); }
public static void Run(ICardInput input, IStringLocalizer localizer) { if (QueryValidationHelper.IsReservedGuid(input.VersionCreator.Id)) { throw new RequestInputException(localizer["InvalidOwner"].Value); } if (input.FrontSide != input.FrontSide.Trim()) { throw new InvalidOperationException("Invalid front side: not trimmed"); } if (input.FrontSide.Length < minFrontSideLength || input.FrontSide.Length > maxFrontSideLength) { throw new RequestInputException(localizer["InvalidFrontSideLength"] + $" {input.FrontSide.Length}" + localizer["MustBeBetween"] + $" {minFrontSideLength} " + localizer["And"] + $" {maxFrontSideLength}"); } if (input.FrontSideImageList.Count() > maxImageCountPerSide) { throw new RequestInputException(localizer["InvalidFrontSideImageCount"] + $" {input.FrontSideImageList.Count()}" + localizer["MustBeNotBeAvove"] + $" {maxImageCountPerSide}"); } if (input.BackSide != input.BackSide.Trim()) { throw new InvalidOperationException("Invalid back side: not trimmed"); } if ((input.BackSide.Length < minBackSideLength || input.BackSide.Length > maxBackSideLength) && !(input.BackSide.Length == 0 && input.BackSideImageList.Any())) { throw new RequestInputException(localizer["InvalidBackSideLength"] + $" {input.BackSide.Length}" + localizer["MustBeBetween"] + $" {minBackSideLength} " + localizer["And"] + $" {maxBackSideLength}"); } if (input.BackSideImageList.Count() > maxImageCountPerSide) { throw new RequestInputException(localizer["InvalidBackSideImageCount"] + $" {input.BackSideImageList.Count()}" + localizer["MustBeNotBeAvove"] + $" {maxImageCountPerSide}"); } if (input.AdditionalInfo != input.AdditionalInfo.Trim()) { throw new InvalidOperationException("Invalid additional info: not trimmed"); } if (input.AdditionalInfo.Length < minAdditionalInfoLength || input.AdditionalInfo.Length > maxAdditionalInfoLength) { throw new RequestInputException(localizer["InvalidAdditionalInfoLength"] + $" {input.AdditionalInfo.Length}" + localizer["MustBeBetween"] + $" {minAdditionalInfoLength} " + localizer["And"] + $" {maxAdditionalInfoLength}"); } if (input.AdditionalInfoImageList.Count() > maxImageCountPerSide) { throw new RequestInputException(localizer["InvalidAdditionalInfoImageCount"] + $" {input.AdditionalInfoImageList.Count()}" + localizer["MustBeNotBeAvove"] + $" {maxImageCountPerSide}"); } if (input.VersionDescription != input.VersionDescription.Trim()) { throw new InvalidOperationException("Invalid VersionDescription: not trimmed"); } if (input.VersionDescription.Length < minVersionDescriptionLength || input.VersionDescription.Length > maxVersionDescriptionLength) { throw new RequestInputException(localizer["InvalidVersionDescriptionLength"] + $" {input.VersionDescription.Length}" + localizer["MustBeBetween"] + $" {minVersionDescriptionLength} " + localizer["And"] + $" {maxVersionDescriptionLength}"); } var unionedImageLists = input.FrontSideImageList.Concat(input.BackSideImageList).Concat(input.AdditionalInfoImageList); if (unionedImageLists.GroupBy(guid => guid).Where(guid => guid.Count() > 1).Any()) { throw new RequestInputException(localizer["ImageDuplicated"].Value); } if (QueryValidationHelper.IsReservedGuid(input.LanguageId)) { throw new RequestInputException(localizer["InvalidInputLanguage"].Value); } if (input.Tags.Where(tag => QueryValidationHelper.IsReservedGuid(tag)).Any()) { throw new RequestInputException(localizer["InvalidTag"].Value); } if (input.UsersWithVisibility.Where(userWithVisibility => QueryValidationHelper.IsReservedGuid(userWithVisibility)).Any()) { throw new RequestInputException(localizer["InvalidUserWithVisibility"].Value); } if (input.UsersWithVisibility.Any() && !input.UsersWithVisibility.Any(userWithVisibility => userWithVisibility == input.VersionCreator.Id)) { //To be reviewed when we support card versions: I suspect we want visibility for all past owners throw new RequestInputException(localizer["OwnerMustHaveVisibility"].Value); } }