예제 #1
0
 public async Task CheckValidityAsync(CallContext callContext)
 {
     QueryValidationHelper.CheckNotReservedGuid(CurrentUserId);
     QueryValidationHelper.CheckNotReservedGuid(DeckId);
     QueryValidationHelper.CheckNotReservedGuid(CardId);
     await QueryValidationHelper.CheckUserIsOwnerOfDeckAsync(callContext.DbContext, CurrentUserId, DeckId);
 }
예제 #2
0
            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);
            }
예제 #3
0
            public async Task CheckValidityAsync(CallContext context)
            {
                QueryValidationHelper.CheckNotReservedGuid(CurrentUserId);
                QueryValidationHelper.CheckNotReservedGuid(CardId);
                await QueryValidationHelper.CheckCardExistsAsync(context.DbContext, CardId);

                CardVisibilityHelper.CheckUserIsAllowedToViewCards(context.DbContext, CurrentUserId, CardId);
            }
예제 #4
0
 public async Task CheckValidityAsync(CallContext callContext)
 {
     QueryValidationHelper.CheckNotReservedGuid(DeckId);
     if (TargetHeap < CardInDeck.UnknownHeap || TargetHeap > CardInDeck.MaxHeapValue)
     {
         throw new InvalidOperationException($"Invalid target heap {TargetHeap}");
     }
     await QueryValidationHelper.CheckUserIsOwnerOfDeckAsync(callContext.DbContext, UserId, DeckId);
 }
예제 #5
0
            public async Task CheckValidityAsync(CallContext callContext)
            {
                QueryValidationHelper.CheckNotReservedGuid(UserId);
                var user = await callContext.DbContext.Users.SingleAsync(u => u.Id == UserId);

                if (!await callContext.RoleChecker.UserIsAdminAsync(user))
                {
                    throw new InvalidOperationException($"User not admin: {user.UserName}");
                }
            }
예제 #6
0
            }                                               //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}");
                }
            }
예제 #7
0
            public async Task CheckValidityAsync(CallContext callContext)
            {
                await QueryValidationHelper.CheckUserExistsAsync(callContext.DbContext, UserId);

                await QueryValidationHelper.CheckCardExistsAsync(callContext.DbContext, CardId);

                if (Rating < 1 || Rating > 5)
                {
                    throw new RequestInputException($"Invalid rating: {Rating}");
                }
            }
예제 #8
0
            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);
            }
예제 #9
0
 public async Task CheckValidityAsync(CallContext callContext)
 {
     QueryValidationHelper.CheckNotReservedGuid(CurrentUserId);
     QueryValidationHelper.CheckNotReservedGuid(DeckId);
     QueryValidationHelper.CheckContainsNoReservedGuid(ExcludedCardIds);
     QueryValidationHelper.CheckContainsNoReservedGuid(ExcludedTagIds);
     if (CardsToDownload < 1 || CardsToDownload > 100)
     {
         throw new RequestInputException($"Invalid CardsToDownload: {CardsToDownload}");
     }
     await QueryValidationHelper.CheckUserIsOwnerOfDeckAsync(callContext.DbContext, CurrentUserId, DeckId);
 }
예제 #10
0
 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;
 }
예제 #11
0
            public async Task CheckValidityAsync(CallContext callContext)
            {
                await QueryValidationHelper.CheckUserExistsAsync(callContext.DbContext, UserId);

                if (CultureName != CultureName.Trim())
                {
                    throw new InvalidOperationException("Invalid Name: not trimmed");
                }
                if (CultureName.Length < MinNameLength || CultureName.Length > MaxNameLength)
                {
                    throw new InvalidOperationException($"Invalid culture name '{CultureName}'");
                }
            }
예제 #12
0
            public async Task CheckValidityAsync(CallContext callContext)
            {
                await QueryValidationHelper.CheckCanCreateLanguageWithName(Name, callContext.DbContext, callContext.Localized);

                await QueryValidationHelper.CheckUserExistsAsync(callContext.DbContext, UserId);

                var user = await callContext.DbContext.Users.AsNoTracking().SingleAsync(user => user.Id == UserId);

                if (!await callContext.RoleChecker.UserIsAdminAsync(user))
                {
                    throw new InvalidOperationException("User not admin");
                }
            }
예제 #13
0
 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;
 }
예제 #14
0
            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);
                }
            }
예제 #15
0
            public async Task CheckValidityAsync(CallContext callContext)
            {
                QueryValidationHelper.CheckNotReservedGuid(UserId);
                QueryValidationHelper.CheckNotReservedGuid(SubscriptionId);
                var subscription = await callContext.DbContext.SearchSubscriptions.Where(s => s.Id == SubscriptionId).SingleOrDefaultAsync();

                if (subscription == null)
                {
                    throw new RequestInputException("Subscription not found");
                }
                if (subscription.UserId != UserId)
                {
                    throw new RequestInputException("User not owner of subscription");
                }
            }
예제 #16
0
            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");
                }
            }
예제 #17
0
            public async Task CheckValidityAsync(CallContext callContext)
            {
                //We allow viewing the history of a card as soon as the user can access the current version of the card. Of course the differ will refuse to give details to a user not allowed

                QueryValidationHelper.CheckNotReservedGuid(UserId);
                QueryValidationHelper.CheckNotReservedGuid(CardId);

                var user = await callContext.DbContext.Users.SingleAsync(u => u.Id == UserId);

                var card = await callContext.DbContext.Cards.Include(v => v.UsersWithView).SingleAsync(v => v.Id == CardId);

                if (!CardVisibilityHelper.CardIsVisibleToUser(UserId, card.UsersWithView))
                {
                    throw new InvalidOperationException("Current not visible to user");
                }
            }
예제 #18
0
            public async Task CheckValidityAsync(CallContext callContext)
            {
                //ImageMetadataInputValidator.Run(this, localizer);
                if (!supportedContentTypes.Contains(ContentType))
                {
                    throw new InvalidOperationException(callContext.Localized.Get("InvalidImageContentType") + $" '{ContentType}'");
                }
                if (Blob.Length < minBlobLength || Blob.Length > maxBlobLength)
                {
                    throw new RequestInputException(callContext.Localized.Get("InvalidBlobLength") + $" {Blob.Length}" + callContext.Localized.Get("MustBeBetween") + $" {minBlobLength} " + callContext.Localized.Get("And") + $" {maxBlobLength}");
                }

                await QueryValidationHelper.CheckCanCreateImageWithNameAsync(Name, callContext.DbContext, callContext.Localized);

                QueryValidationHelper.CheckCanCreateImageWithDescription(Description, callContext.Localized);
                QueryValidationHelper.CheckCanCreateImageWithSource(Source, callContext.Localized);
            }
예제 #19
0
            public async Task CheckValidityAsync(CallContext callContext)
            {
                if (LoggedUserId != UserToDeleteId)
                {
                    await QueryValidationHelper.CheckUserExistsAndIsAdminAsync(callContext.DbContext, LoggedUserId, callContext.RoleChecker);
                }

                await QueryValidationHelper.CheckUserExistsAsync(callContext.DbContext, UserToDeleteId);

                var userToDelete = await callContext.DbContext.Users.AsNoTracking().SingleAsync(user => user.Id == UserToDeleteId);

                if (await callContext.RoleChecker.UserIsAdminAsync(userToDelete))
                {
                    //Additional security: forbid deleting an admin account
                    throw new InvalidOperationException("User to delete is admin");
                }
            }
예제 #20
0
            public async Task CheckValidityAsync(CallContext callContext)
            {
                QueryValidationHelper.CheckNotReservedGuid(UserId);
                if (PageSize < 1 || PageSize > MaxPageSize)
                {
                    throw new InvalidOperationException($"Invalid page size: {PageSize}");
                }
                if (PageNo < 1)
                {
                    throw new InvalidOperationException($"Invalid page index: {PageNo}");
                }
                var user = await callContext.DbContext.Users.SingleAsync(u => u.Id == UserId);

                if (!await callContext.RoleChecker.UserIsAdminAsync(user))
                {
                    throw new InvalidOperationException($"User not admin: {user.UserName}");
                }
            }
예제 #21
0
 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");
     }
 }
예제 #22
0
            public async Task CheckValidityAsync(CallContext callContext)
            {
                QueryValidationHelper.CheckNotReservedGuid(UserId);
                QueryValidationHelper.CheckNotReservedGuid(SubscriptionId);
                if (Name.Length < MinNameLength)
                {
                    throw new RequestInputException($"Name '{Name}' is too short, must be between {MinNameLength} and {MaxNameLength} chars long, is {Name.Length}");
                }
                if (Name.Length > MaxNameLength)
                {
                    throw new RequestInputException($"Name '{Name}' is too long, must be between {MinNameLength} and {MaxNameLength} chars long, is {Name.Length}");
                }
                var subscription = await callContext.DbContext.SearchSubscriptions.Where(s => s.Id == SubscriptionId).SingleOrDefaultAsync();

                if (subscription == null)
                {
                    throw new RequestInputException("Subscription not found");
                }
                if (subscription.UserId != UserId)
                {
                    throw new RequestInputException("User not owner of subscription");
                }
            }
예제 #23
0
 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);
 }
예제 #24
0
 public async Task CheckValidityAsync(CallContext callContext)
 {
     await QueryValidationHelper.CheckUserIsOwnerOfDeckAsync(callContext.DbContext, UserId, DeckId);
 }
예제 #25
0
        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);
            }
        }
예제 #26
0
 public async Task CheckValidityAsync(CallContext callContext)
 {
     await QueryValidationHelper.CheckUserExistsAsync(callContext.DbContext, UserId);
 }
예제 #27
0
            public async Task CheckValidityAsync(MemCheckDbContext dbContext)
            {
                await QueryValidationHelper.CheckUserExistsAsync(dbContext, UserId);

                await QueryValidationHelper.CheckUserIsOwnerOfDeckAsync(dbContext, UserId, DeckId);
            }
예제 #28
0
 public async Task CheckValidityAsync(CallContext callContext)
 {
     await QueryValidationHelper.CheckCanCreateDeckAsync(UserId, Name, HeapingAlgorithmId, callContext.DbContext, callContext.Localized);
 }
예제 #29
0
 public async Task CheckValidityAsync(CallContext callContext)
 {
     QueryValidationHelper.CheckNotReservedGuid(TagId);
     await Task.CompletedTask;
 }
예제 #30
0
            public async Task CheckValidityAsync(CallContext callContext)
            {
                await QueryValidationHelper.CheckUserExistsAsync(callContext.DbContext, UserId);

                await QueryValidationHelper.CheckCanCreateTag(Name, Description, null, callContext.DbContext, callContext.Localized);
            }