Ejemplo n.º 1
0
        public async Task <TSource> UpdateAsync <TSource>(IResolveFieldContext <object> context, string sourceName,
                                                          Func <ClaimsPrincipal, AssertAction, bool> claimsPrincipalAssertion) where TSource : class
        {
            AssertWithClaimsPrincipal(AssertAction.Update, context, claimsPrincipalAssertion);
            var item = context.GetArgument <TSource>(sourceName);
            var ctx  = context.GetGraphRequestContext();

            await TransformObject(item, ctx, MutationActions.Update);

            var cloned = item.Clone();

            try
            {
                var mutationActionItem = new MutationActionItem <TSource>
                {
                    Action             = MutationActions.Update,
                    RequestContext     = ctx,
                    Item               = item,
                    RepositoryProvider = _graphQlRepositoryProvider
                };

                await _mutationActionsProvider.HandlePreActions(mutationActionItem);

                await _graphQlRepositoryProvider.GetRepository <TSource>().UpdateAsync(item, context.GetGraphRequestContext());

                await _mutationActionsProvider.HandlePostActions(mutationActionItem);
            }
            catch (Exception e)
            {
                _logger.LogError(e, "An error has occured while performing an update operation.");
                throw;
            }
            return(cloned);
        }
Ejemplo n.º 2
0
        private async Task <TSource> InternalDeleteAsync <TSource>(IResolveFieldContext <object> context, TSource item) where TSource : class
        {
            try
            {
                IGraphRequestContext ctx = context.GetGraphRequestContext();

                await TransformObject(item, ctx, MutationActions.Delete);

                var mutationActionItem = new MutationActionItem <TSource>
                {
                    Action             = MutationActions.Delete,
                    RequestContext     = ctx,
                    Item               = item,
                    RepositoryProvider = _graphQlRepositoryProvider
                };

                var cloned = item.Clone();

                await _mutationActionsProvider.HandlePreActions(mutationActionItem);

                await _graphQlRepositoryProvider.GetRepository <TSource>().DeleteAsync(item, ctx);

                await _mutationActionsProvider.HandlePostActions(mutationActionItem);

                return(cloned);
            }
            catch (Exception e)
            {
                _logger.LogError(e, "An error has occured while performing a delete operation.");
                throw;
            }
        }
Ejemplo n.º 3
0
        public async Task <TDeleteOutput> DeleteAllAsync <TSource, TDeleteOutput>(IResolveFieldContext <object> context, string sourceName,
                                                                                  Func <TDeleteOutput> getOutput,
                                                                                  Func <ClaimsPrincipal, AssertAction, bool> claimsPrincipalAssertion) where TSource : class
        {
            AssertWithClaimsPrincipal(AssertAction.DeleteAll, context, claimsPrincipalAssertion);
            try
            {
                var ctx = context.GetGraphRequestContext();

                var mutationActionItem = new MutationActionItem <TSource>
                {
                    Action             = MutationActions.DeleteAll,
                    RequestContext     = ctx,
                    RepositoryProvider = _graphQlRepositoryProvider
                };
                await _mutationActionsProvider.HandlePreActions(mutationActionItem);

                await _graphQlRepositoryProvider.GetRepository <TSource>().DeleteAllAsync <TSource>(ctx);

                await _mutationActionsProvider.HandlePostActions(mutationActionItem);
            }
            catch (Exception e)
            {
                _logger.LogError(e, "An error has occured while performing a delete-all operation.");
                throw;
            }

            return(getOutput());
        }
Ejemplo n.º 4
0
        private async Task <IEnumerable <TSource> > QueryAsync(IResolveFieldContext <object> context)
        {
            var graphRequestContext = context.GetGraphRequestContext();

            if (_claimsPrincipalAssertion != null)
            {
                if (graphRequestContext == null || !_claimsPrincipalAssertion(graphRequestContext.HttpRequest.Security.ClaimsPrincipal))
                {
                    throw new ExecutionError("Query execution has been denied due to insufficient permissions.", new SecurityException("Query execution has been denied due to insufficient permissions."));
                }
            }

            var steps = _queryParameterBuilder.GetQuerySteps(context).ToList();

            if (_cacheInSeconds > 0)
            {
                var key = steps.GetCacheKey <TSource>();
                _logger.LogInformation($"CacheKey: {key}");

                return((await TryGetOrSetIfNotExistAsync(
                            () => _queryExecutor.ExecuteAsync(context.FieldName, steps, graphRequestContext,
                                                              _queryParameterBuilder.ConnectionEdgeDestinationFilters).Result.ToList(), key,
                            new DistributedCacheEntryOptions
                {
                    // ReSharper disable once PossibleInvalidOperationException
                    AbsoluteExpiration = DateTimeOffset.UtcNow.AddSeconds(_cacheInSeconds.Value)
                })).Value);
            }

            return(await _queryExecutor.ExecuteAsync(context.FieldName, steps,
                                                     graphRequestContext, _queryParameterBuilder.ConnectionEdgeDestinationFilters));
        }
Ejemplo n.º 5
0
 private void AssertWithClaimsPrincipal(AssertAction assertAction, IResolveFieldContext <object> context, Func <ClaimsPrincipal, AssertAction, bool> claimsPrincipalAssertion)
 {
     if (claimsPrincipalAssertion != null)
     {
         var graphRequestContext = context.GetGraphRequestContext();
         if (graphRequestContext == null ||
             graphRequestContext.HttpRequest.Security.ClaimsPrincipal == null ||
             !claimsPrincipalAssertion(graphRequestContext.HttpRequest.Security.ClaimsPrincipal, assertAction))
         {
             var message = $"{assertAction} execution has been denied due to insufficient permissions.";
             throw new ExecutionError(message, new SecurityException(message));
         }
     }
 }
Ejemplo n.º 6
0
        public async Task <TSource> AddAsync <TSource>(IResolveFieldContext <object> context, string sourceName,
                                                       Func <ClaimsPrincipal, AssertAction, bool> claimsPrincipalAssertion) where TSource : class
        {
            AssertWithClaimsPrincipal(AssertAction.Create, context, claimsPrincipalAssertion);

            var item = context.GetArgument <TSource>(sourceName);

            var ctx = context.GetGraphRequestContext();

            await TransformObject(item, ctx, MutationActions.Create);

            var cloned = item.Clone();

            try
            {
                var edges = _connectionEdgeResolver.HandleConnectionEdges(item, async(model) =>
                {
                    var mutationActionItem = new MutationActionItem <TSource>
                    {
                        Action             = MutationActions.Create,
                        RequestContext     = ctx,
                        ObjectItem         = model,
                        RepositoryProvider = _graphQlRepositoryProvider
                    };

                    await _mutationActionsProvider.HandlePreActions(mutationActionItem);

                    await _graphQlRepositoryProvider.GetRepository(model.GetType())
                    .AddAsync(model.GetType(), model, ctx);

                    await _mutationActionsProvider.HandlePostActions(mutationActionItem);
                });

                if (edges.Count > 0)
                {
                    await _graphQlRepositoryProvider.GetRepository(typeof(ConnectionEdge)).BatchAddAsync(edges, ctx);
                }
            }
            catch (Exception e)
            {
                _logger.LogError(e, "An error has occured while performing an add operation.");
                throw;
            }
            return(cloned);
        }
Ejemplo n.º 7
0
        private async Task <List <TSource> > InternalBatchAsync <TSource>(
            IResolveFieldContext <object> context, string sourceName,
            AssertAction assertAction,
            Func <ClaimsPrincipal, AssertAction, bool> claimsPrincipalAssertion) where TSource : class
        {
            AssertWithClaimsPrincipal(assertAction, context, claimsPrincipalAssertion);

            var items = context.GetArgument <IEnumerable <TSource> >(sourceName).ToList();
            var ctx   = context.GetGraphRequestContext();

            if (assertAction == AssertAction.BatchCreate)
            {
                await TransformObjects(items.Select(x => (object)x).ToList(), ctx, MutationActions.BatchCreate);
            }

            if (assertAction == AssertAction.BatchCreateOrUpdate)
            {
                await TransformObjects(items.Select(x => (object)x).ToList(), ctx, MutationActions.BatchCreateOrUpdate);
            }

            var cloned = items.Clone();

            var batchItems = new Dictionary <Type, BatchModelList>();

            try
            {
                var edges = _connectionEdgeResolver.HandleConnectionEdges(items, (model) =>
                {
                    var key = model.GetType();
                    BatchModelList itemsList;

                    if (batchItems.ContainsKey(key))
                    {
                        itemsList = batchItems[key];
                    }
                    else
                    {
                        itemsList       = new BatchModelList(key);
                        batchItems[key] = itemsList;
                    }

                    itemsList.Add(model);
                });

                switch (assertAction)
                {
                case AssertAction.BatchCreate:
                    if (edges.Count > 0)
                    {
                        await _graphQlRepositoryProvider.GetRepository(typeof(ConnectionEdge)).BatchAddAsync(edges, ctx);
                    }

                    foreach (var batchItem in batchItems)
                    {
                        var mutationActionItem = new MutationActionItem <TSource>
                        {
                            Action             = MutationActions.BatchCreate,
                            RequestContext     = ctx,
                            ObjectItems        = batchItem.Value.Items,
                            RepositoryProvider = _graphQlRepositoryProvider
                        };

                        await _mutationActionsProvider.HandlePreActions(mutationActionItem);

                        await _graphQlRepositoryProvider.GetRepository(batchItem.Key).BatchAddAsync(batchItem.Key, batchItem.Value.Items, ctx);

                        await _mutationActionsProvider.HandlePostActions(mutationActionItem);
                    }

                    break;

                case AssertAction.BatchCreateOrUpdate:
                    if (edges.Count > 0)
                    {
                        await _graphQlRepositoryProvider.GetRepository(typeof(ConnectionEdge)).BatchAddOrUpdateAsync(edges, ctx);
                    }

                    foreach (var batchItem in batchItems)
                    {
                        var mutationActionItem = new MutationActionItem <TSource>
                        {
                            Action             = MutationActions.BatchCreateOrUpdate,
                            RequestContext     = ctx,
                            ObjectItems        = batchItem.Value.Items,
                            RepositoryProvider = _graphQlRepositoryProvider
                        };
                        await _mutationActionsProvider.HandlePreActions(mutationActionItem);

                        await _graphQlRepositoryProvider.GetRepository(batchItem.Key).BatchAddOrUpdateAsync(batchItem.Key, batchItem.Value.Items, ctx);

                        await _mutationActionsProvider.HandlePostActions(mutationActionItem);
                    }
                    break;

                default:
                    throw new InvalidOperationException("This internal method is only for batch operations.");
                }

                return(cloned);
            }
            catch (Exception e)
            {
                _logger.LogError(e, "An error has occured while performing a batch add operation.");
                throw;
            }
        }