Esempio n. 1
0
        private static bool CallHandleAuthAttribute(this Type t,
                                                    SapphireAuthResource.OperationTypeEnum operationTypeEnum,
                                                    HttpInformation httpInformation, object entityObject, IServiceProvider serviceProvider)
        {
            AuthModelInfo authModelInfo = t.GetAuthModelInfos();

            switch (operationTypeEnum)
            {
            case SapphireAuthResource.OperationTypeEnum.Create:
                return(HandleAuthAttributes(t, authModelInfo.CreateAuthAttributes,
                                            httpInformation, operationTypeEnum, entityObject, serviceProvider));

            case SapphireAuthResource.OperationTypeEnum.Remove:
                return(HandleAuthAttributes(t, authModelInfo.RemoveAuthAttributes,
                                            httpInformation, operationTypeEnum, entityObject, serviceProvider));

            case SapphireAuthResource.OperationTypeEnum.Update:
                return(HandleAuthAttributes(t, authModelInfo.UpdateAuthAttributes,
                                            httpInformation, operationTypeEnum, entityObject, serviceProvider));

            default:
                return(HandleAuthAttributes(t, authModelInfo.QueryAuthAttributes,
                                            httpInformation, operationTypeEnum, entityObject, serviceProvider));
            }
        }
Esempio n. 2
0
        public static ResponseBase GetCollection(SapphireDbContext db, QueryCommand command,
                                                 HttpInformation information, IServiceProvider serviceProvider)
        {
            Type dbContextType = db.GetType();
            KeyValuePair <Type, string> property = dbContextType.GetDbSetType(command.CollectionName);

            if (property.Key != null)
            {
                if (!property.Key.CanQuery(information, serviceProvider))
                {
                    return(command.CreateExceptionResponse <QueryResponse>(
                               "Not allowed to query values from collection"));
                }

                IQueryable <object> collectionValues = db.GetCollectionValues(serviceProvider, information, property, command.Prefilters);

                QueryResponse queryResponse = new QueryResponse()
                {
                    ReferenceId = command.ReferenceId,
                };

                IAfterQueryPrefilter afterQueryPrefilter = command.Prefilters.OfType <IAfterQueryPrefilter>().FirstOrDefault();

                if (afterQueryPrefilter != null)
                {
                    afterQueryPrefilter.Initialize(property.Key);
                    queryResponse.Result = afterQueryPrefilter.Execute(collectionValues);
                }
                else
                {
                    IEnumerable <object> values = collectionValues.AsEnumerable();

                    AuthModelInfo authModelInfo = property.Key.GetAuthModelInfos();

                    if (authModelInfo.QueryEntryAuthAttributes.Any())
                    {
                        values = values.Where(value => property.Key.CanQueryEntry(information, serviceProvider, value));
                    }

                    values = values.Select(v => v.GetAuthenticatedQueryModel(information, serviceProvider));

                    queryResponse.Result = values.ToList();
                }

                return(queryResponse);
            }

            return(command.CreateExceptionResponse <QueryResponse>("No set for collection was found."));
        }
        public void HandleCollections(ConnectionBase connection, string contextName, Type dbContextType,
                                      List <ChangeResponse> changes, IServiceProvider requestServiceProvider)
        {
            IEnumerable <IGrouping <string, CollectionSubscription> > subscriptionGroupings = connection.Subscriptions
                                                                                              .Where(s => s.ContextName == contextName)
                                                                                              .GroupBy(s => s.CollectionName);

            foreach (IGrouping <string, CollectionSubscription> subscriptionGrouping in subscriptionGroupings)
            {
                KeyValuePair <Type, string> property = dbContextType.GetDbSetType(subscriptionGrouping.Key);

                List <ChangeResponse> changesForCollection = changes
                                                             .Where(c => c.CollectionName == subscriptionGrouping.Key)
                                                             .ToList();

                AuthModelInfo modelInfo = property.Key.GetAuthModelInfos();

                IEnumerable <ChangeResponse> authenticatedChanges = changesForCollection;

                if (modelInfo.QueryEntryAuthAttributes.Any())
                {
                    authenticatedChanges = changesForCollection
                                           .Where(change => change.State == ChangeResponse.ChangeState.Deleted ||
                                                  property.Key.CanQueryEntry(connection.Information, requestServiceProvider,
                                                                             change.Value));


                    IEnumerable <ChangeResponse> oldLoadedNotAllowed = changesForCollection
                                                                       .Where(change => change.State == ChangeResponse.ChangeState.Modified &&
                                                                              !property.Key.CanQueryEntry(connection.Information, requestServiceProvider,
                                                                                                          change.Value))
                                                                       .Select(change =>
                    {
                        ChangeResponse newChangeResponse = change.CreateResponse(null, change.Value);
                        newChangeResponse.State          = ChangeResponse.ChangeState.Deleted;
                        return(newChangeResponse);
                    });

                    authenticatedChanges = authenticatedChanges.Concat(oldLoadedNotAllowed);
                }

                List <ChangeResponse> collectionChanges = authenticatedChanges.ToList();

                if (collectionChanges.Any())
                {
                    QueryFunctionAttribute queryFunctionAttribute =
                        property.Key.GetCustomAttribute <QueryFunctionAttribute>(false);
                    if (queryFunctionAttribute != null)
                    {
                        var queryFunctionInfo = property.Key.GetMethod(queryFunctionAttribute.Function,
                                                                       BindingFlags.Default | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static);

                        if (queryFunctionInfo != null)
                        {
                            object[] methodParameters =
                                queryFunctionInfo.CreateParameters(connection.Information, serviceProvider);
                            dynamic queryFunctionExpression =
                                ((dynamic)queryFunctionInfo.Invoke(null, methodParameters)).Compile();

                            collectionChanges = collectionChanges.Where(change => queryFunctionExpression(change.Value))
                                                .ToList();
                        }
                    }
                }

                foreach (CollectionSubscription subscription in subscriptionGrouping)
                {
                    Task.Run(() =>
                    {
                        HandleSubscription(subscription, dbContextType, requestServiceProvider, connection,
                                           property, collectionChanges, changes);
                    });
                }
            }
        }