Example #1
0
 //Returns true if there are any assigned profiles that cover the resource and profile sent as paramenters
 private bool IsResourceCoveredByAnotherAssignedProfile(
     List <string> assignedProfiles,
     string resourceItemName,
     string profileContentTypeProfileName)
 {
     return
         (assignedProfiles.Intersect(
              _profileResourceNamesProvider.GetProfileResourceNames()
              .Where(
                  x => x.ResourceName.EqualsIgnoreCase(resourceItemName) &&
                  x.ProfileName.EqualsIgnoreCase(profileContentTypeProfileName))
              .Select(x => x.ProfileName))
          .Any());
 }
        private IEnumerable <OpenApiContent> CreateProfileSection()
        {
            // profiles using tightly coupled extensions
            var resourceFilter = _openApiMetadataResourceFilters
                                 .FirstOrDefault(x => x.Key.Equals(Profiles));

            return(_profileResourceNamesProvider
                   .GetProfileResourceNames()
                   .Select(x => x.ProfileName)
                   .Select(
                       x => new SwaggerProfileContext
            {
                ProfileName = x,
                ProfileResourceModel = _profileResourceModelProvider
                                       .GetProfileResourceModel(x)
            })
                   .Select(
                       x => new SwaggerDocumentContext(_resourceModelProvider.GetResourceModel())
            {
                ProfileContext = x,
                IsIncludedExtension = r => true
            })
                   .Select(
                       c =>
                       new OpenApiContent(
                           OpenApiMetadataSections.Profiles,
                           c.ProfileContext.ProfileName,
                           new Lazy <string>(() => new SwaggerDocumentFactory(c).Create(resourceFilter.Value)),
                           _odsDataBasePath,
                           $"{OpenApiMetadataSections.Profiles}/{c.ProfileContext.ProfileName}")));
        }
            protected override void Arrange()
            {
                _compositesMetadataProvider = Stub <ICompositesMetadataProvider>();

                A.CallTo(() => _compositesMetadataProvider.GetAllCategories())
                .Returns(new List <CompositeCategory>());

                _profileResourceModelProvider = Stub <IProfileResourceModelProvider>();

                _profileResourceNamesProvider = Stub <IProfileResourceNamesProvider>();

                A.CallTo(() => _profileResourceNamesProvider.GetProfileResourceNames())
                .Returns(new List <ProfileAndResourceNames>());

                var _openAPIMetadataRouteInformation = Stub <List <IOpenApiMetadataRouteInformation> >();

                var _openApiContentProviders = Stub <List <IOpenApiContentProvider> >();

                var defaultPageSieLimitProvider = new DefaultPageSizeLimitProvider(GetConfiguration());

                var openApiMetadataDocumentFactory = new OpenApiMetadataDocumentFactory(CreateApiSettings(), defaultPageSieLimitProvider);

                var openApiMetadataRouteInformation = new List <IOpenApiMetadataRouteInformation>();

                var resourceModelProvider = Stub <IResourceModelProvider>();

                var resourceModel = ResourceModelProvider.GetResourceModel();

                A.CallTo(() => resourceModelProvider.GetResourceModel()).Returns(resourceModel);

                _openApiMetadataCacheProvider = new OpenApiMetadataCacheProvider(
                    resourceModelProvider, GetTestRouteInformation(CreateApiSettings()).ToList(), TestOpenApiContentProviders, openApiMetadataDocumentFactory);
            }
Example #4
0
            public void Should_be_a_swagger_document_for_each_distinct_profile_in_the_ProfileResourceNamesProvider()
            {
                var profileNames = _profileResourceNamesProvider.GetProfileResourceNames()
                                   .Select(n => n.ProfileName)
                                   .Distinct();

                Assert.That(
                    _actualMetadata.Select(m => m.Name),
                    Is.EquivalentTo(profileNames));
            }
Example #5
0
        private bool PublishProfiles()
        {
            try
            {
                //get the set of profiles from any Profiles.xml files found in assemblies
                var definedProfiles =
                    _profileResourceNamesProvider.GetProfileResourceNames()
                    .Select(x => x.ProfileName)
                    .Distinct();

                using (var usersContext = _usersContextFactory.CreateContext())
                {
                    //determine which Profiles from the Profiles.xml files do not exist in the admin database
                    var publishedProfiles = usersContext.Profiles
                                            .Select(x => x.ProfileName)
                                            .ToList();

                    var profilesToInsert = definedProfiles
                                           .Except(publishedProfiles)
                                           .ToList();

                    //if there are none to insert return
                    if (!profilesToInsert.Any())
                    {
                        return(true);
                    }

                    //for each profile not in the database, add it
                    foreach (var profileName in profilesToInsert)
                    {
                        usersContext.Profiles.Add(
                            new Profile
                        {
                            ProfileName = profileName
                        });
                    }

                    usersContext.SaveChanges();
                }

                return(true);
            }
            catch (Exception exception)
            {
                //If an exception occurs log it and return false since it is an async call.
                _logger.Error("An error occured when attempting to publish Profiles to the admin database.", exception);
                return(false);
            }
        }
Example #6
0
 private bool ProfileExists(string profileName)
 {
     return(_profileResourceNamesProvider.GetProfileResourceNames()
            .Any(x => x.ProfileName.EqualsIgnoreCase(profileName)));
 }
Example #7
0
        public IEnumerable <ResourceProfileData> GetResourceProfileData()
        {
            if (ProjectHasProfileDefinition)
            {
                var profileNamesAndResourceModels =
                    _profileResourceNamesProvider
                    .GetProfileResourceNames()
                    .Select(prn => prn.ProfileName)
                    .Distinct()
                    .Select(
                        profileName =>
                        new
                {
                    ProfileName          = profileName,
                    ProfileResourceModel = _profileResourceModelProvider.GetProfileResourceModel(profileName)
                });

                var readablesAndWritables =
                    profileNamesAndResourceModels
                    .Select(
                        x =>
                {
                    // we need to include resources if they are derived from other entities that are writable so we can
                    // satisfy the interface
                    var includeInWritable = new List <Resource>();

                    if (x.ProfileResourceModel.Resources.Any(y => y.Writable == null))
                    {
                        var possibleWritable = x.ProfileResourceModel.Resources.Where(
                            y => y.Writable == null)
                                               .Select(y => y.Readable);

                        var writable = x.ProfileResourceModel.Resources.Where(y => y.Writable != null)
                                       .Select(y => y.Writable)
                                       .ToList();

                        foreach (var resource in possibleWritable)
                        {
                            includeInWritable.AddRange(
                                writable.Where(wr => wr.IsDerivedFrom(resource))
                                .Select(wr => resource));
                        }
                    }

                    // to satisfy future profiles that may have this situation also.
                    var includeInReadable = new List <Resource>();

                    if (x.ProfileResourceModel.Resources.Any(y => y.Readable == null))
                    {
                        var possibleReadable = x.ProfileResourceModel.Resources.Where(
                            y => y.Readable == null)
                                               .Select(y => y.Writable);

                        var readable = x.ProfileResourceModel.Resources.Where(y => y.Readable != null)
                                       .Select(y => y.Readable)
                                       .ToList();

                        foreach (var resource in possibleReadable)
                        {
                            includeInReadable.AddRange(
                                readable.Where(wr => wr.IsDerivedFrom(resource))
                                .Select(wr => resource));
                        }
                    }

                    return(new
                    {
                        ProfileName = x.ProfileName,
                        Readable = x.ProfileResourceModel.Resources
                                   .Where(y => y.Readable != null)
                                   .Select(y => y.Readable)
                                   .Concat(
                            includeInReadable)                 // Added to ensure that the pipeline step, which defines 2 generic parameters for readable and writable, has a type available
                                   .ToList(),
                        Writable = x.ProfileResourceModel.Resources
                                   .Where(y => y.Writable != null)
                                   .Select(y => y.Writable)
                                   .Concat(
                            includeInWritable)                 // Added to ensure that the pipeline step, which defines 2 generic parameters for readable and writable, has a type available
                                   .ToList()
                    });
                });

                var finalReadablesAndWritables =
                    readablesAndWritables.Select(
                        prm =>
                {
                    return(new
                    {
                        ProfileName = prm.ProfileName,
                        Readable = prm.Readable
                                   .SelectMany(
                            pct =>
                            CreateProfileModel(
                                pct,
                                prm.ProfileName,
                                ReadableContext,
                                true))
                                   .ToList(),
                        Writable = prm.Writable
                                   .SelectMany(
                            pct => CreateProfileModel(
                                pct,
                                prm.ProfileName,
                                WritableContext,
                                false))
                                   .ToList()
                    });
                })
                    .SelectMany(
                        y => y.Readable.OrderBy(q => q.ResourceName)
                        .Concat(y.Writable.OrderBy(q => q.ResourceName)))
                    .ToList();

                return(finalReadablesAndWritables);
            }

            // sorting for template comparison.
            return(ResourceModel
                   .GetAllResources()
                   .Select(
                       resource =>
                       new ResourceProfileData
            {
                HasProfile = ProjectHasProfileDefinition,
                SuppliedResource = resource,
                UnfilteredResource = (Resource)resource.FilterContext?.UnfilteredResourceClass ?? resource,
                ContextualRootResource = resource
            })
                   .OrderBy(x => x.ResourceName));
        }