Example #1
0
        public BanicoMutation(
            IConfiguration configuration,
            ISectionRepository sectionRepository,
            ISectionItemRepository sectionItemRepository,
            IContentItemRepository contentItemRepository,
            IConfigRepository configRepository,
            IAccessService accessService
            )
        {
            _configuration         = configuration;
            _contentItemRepository = contentItemRepository;
            _configRepository      = configRepository;
            _accessService         = accessService;

            Name = "Mutation";

            Field <SectionType>(
                "addOrUpdateSection",
                arguments: new QueryArguments(
                    // <SectionInputType>
                    new QueryArgument <NonNullGraphType <SectionInputType> > {
                Name = "section"
            }
                    ),
                resolve: context =>
            {
                var section = context.GetArgument <Section>("section");
                this.StampItem(section).Wait();
                var isSectionAdmin = _accessService.Allowed("admin/sections", "", false).Result;
                this.WriteDebugMessage("BanicoMutation: isSectionAdmin " + isSectionAdmin.ToString());
                return(sectionRepository.AddOrUpdate(section, isSectionAdmin));
            });

            Field <SectionItemType>(
                "addOrUpdateSectionItem",
                arguments: new QueryArguments(
                    // <SectionInputType>
                    new QueryArgument <NonNullGraphType <SectionItemInputType> > {
                Name = "sectionItem"
            }
                    ),
                resolve: context =>
            {
                var sectionItem = context.GetArgument <SectionItem>("sectionItem");
                this.StampItem(sectionItem).Wait();
                var isSectionItemAdmin = _accessService.Allowed("admin/sectionItems", "", false).Result;
                return(sectionItemRepository.AddOrUpdate(sectionItem, isSectionItemAdmin));
            });

            Field <ContentItemType>(
                "addOrUpdateContentItem",
                arguments: new QueryArguments(
                    new QueryArgument <NonNullGraphType <ContentItemInputType> > {
                Name = "contentItem"
            }
                    ),
                resolve: context =>
            {
                var contentItem = context.GetArgument <ContentItem>("contentItem");
                if (_accessService.IsEnabled(contentItem.Module).Result)
                {
                    if (_accessService.Allowed(contentItem).Result)
                    {
                        if (!this.checkTenant(contentItem))
                        {
                            throw new UnauthorizedAccessException("Update of content item is not allowed.");
                        }

                        this.StampItem(contentItem).Wait();
                        var userId  = _accessService.GetUserId();
                        var isAdmin = _accessService.IsAdminOrSuperAdmin();
                        return(contentItemRepository.AddOrUpdate(contentItem, userId, isAdmin));
                    }
                    else
                    {
                        var userId = _accessService.GetUserId();
                        throw new UnauthorizedAccessException("Add or update of content module / type " + contentItem.Module + " / " +
                                                              contentItem.Type + " is not allowed for user " + userId + ".");
                    }
                }
                else
                {
                    throw new UnauthorizedAccessException("Content module " + contentItem.Module + " is not enabled.");
                }
            });

            Field <ContentItemType>(
                "deleteContentItem",
                arguments: new QueryArguments(
                    new QueryArgument <NonNullGraphType <StringGraphType> > {
                Name = "id"
            }
                    ),
                resolve: context =>
            {
                var id = context.GetArgument <String>("id");
                if (!this.checkTenant(id))
                {
                    throw new UnauthorizedAccessException("Deletion of content item is not allowed.");
                }
                var userId  = _accessService.GetUserId();
                var isAdmin = _accessService.IsAdminOrSuperAdmin();
                return(contentItemRepository.Delete(id, userId, isAdmin));
            });

            Field <ConfigType>(
                "addOrUpdateConfig",
                arguments: new QueryArguments(
                    new QueryArgument <NonNullGraphType <ConfigInputType> > {
                Name = "config"
            }
                    ),
                resolve: context =>
            {
                var config = context.GetArgument <Config>("config");
                this.StampItem(config).Wait();
                var isSuperAdmin = _accessService.IsSuperAdmin();
                return(configRepository.AddOrUpdate(config, isSuperAdmin));
            });
        }
Example #2
0
        public GraphQLIssueMutation(
            ISectionRepository sectionRepository,
            ISectionItemRepository sectionItemRepository,
            IContentItemRepository contentItemRepository,
            IConfigRepository configRepository,
            IAccessService accessService
            )
        {
            _configRepository = configRepository;
            _accessService    = accessService;

            Field <SectionType>(
                "addOrUpdateSection",
                arguments: new QueryArguments(
                    // <SectionInputType>
                    new QueryArgument <NonNullGraphType <SectionInputType> > {
                Name = "section"
            }
                    ),
                resolve: context =>
            {
                var section = context.GetArgument <Section>("section");
                this.StampItem(section);
                return(sectionRepository.AddOrUpdate(section));
            });

            Field <SectionItemType>(
                "addOrUpdateSectionItem",
                arguments: new QueryArguments(
                    // <SectionInputType>
                    new QueryArgument <NonNullGraphType <SectionItemInputType> > {
                Name = "sectionItem"
            }
                    ),
                resolve: context =>
            {
                var sectionItem = context.GetArgument <SectionItem>("sectionItem");
                this.StampItem(sectionItem);
                return(sectionItemRepository.AddOrUpdate(sectionItem));
            });

            Field <ContentItemType>(
                "addOrUpdateContentItem",
                arguments: new QueryArguments(
                    new QueryArgument <NonNullGraphType <ContentItemInputType> > {
                Name = "contentItem"
            },
                    new QueryArgument <StringGraphType> {
                Name = "sectionItems"
            }
                    ),
                resolve: context =>
            {
                var contentItem  = context.GetArgument <ContentItem>("contentItem");
                var sectionItems = context.GetArgument <String>("sectionItems");
                if (_accessService.Allowed(contentItem).Result)
                {
                    this.StampItem(contentItem);
                    var userId  = _accessService.GetCurrentUserId();
                    var isAdmin = _accessService.IsAdmin();
                    return(contentItemRepository.AddOrUpdate(contentItem, sectionItems, userId, isAdmin));
                }
                else
                {
                    return(new ContentItem());
                }
            });

            Field <ConfigType>(
                "addOrUpdateConfig",
                arguments: new QueryArguments(
                    new QueryArgument <NonNullGraphType <ConfigInputType> > {
                Name = "config"
            }
                    ),
                resolve: context =>
            {
                var config = context.GetArgument <Config>("config");
                this.StampItem(config);
                return(configRepository.AddOrUpdate(config));
            });
        }