public static bool TryCreate(IOwinContext context, out PublicationVisibility visibility)
        {
            string subscription = context.Request.Query["subscription"];

            string organization = context.Request.Query["organization"];

            if (subscription == null && organization == null)
            {
                visibility = new PublicationVisibility { Visibility = VisibilityScope.Public };
                return true;
            }
            if (subscription != null && organization != null)
            {
                visibility = null;
                return false;
            }
            if (subscription != null)
            {
                visibility = new PublicationVisibility { Visibility = VisibilityScope.Subscription, Subscription = subscription };
                return true;
            }
            if (organization != null)
            {
                visibility = new PublicationVisibility { Visibility = VisibilityScope.Organization, Organization = organization };
                return true;
            }

            visibility = null;
            return false;
        }
        public static async Task<PublicationDetails> CreatePublicationDetails(IRegistrationOwnership registrationOwnership, PublicationVisibility publicationVisibility)
        {
            string userId = registrationOwnership.GetUserId();
            string userName = await registrationOwnership.GetPublisherName();
            string tenantId = registrationOwnership.GetTenantId();

            //TODO: requires Graph access
            string tenantName = string.Empty;
            //string tenantName = await _registrationOwnership.GetTenantName();

            //var client = await ServiceHelpers.GetActiveDirectoryClient();

            PublicationDetails publicationDetails = new PublicationDetails
            {
                Published = DateTime.UtcNow,
                Owner = OwnershipOwner.Create(ClaimsPrincipal.Current),
                TenantId = tenantId,
                TenantName = tenantName,
                Visibility = publicationVisibility
            };

            return publicationDetails;
        }
        public static bool TryCreate(IOwinContext context, out PublicationVisibility visibility)
        {
            string subscription = context.Request.Query["subscription"];

            string organization = context.Request.Query["organization"];

            if (subscription == null && organization == null)
            {
                visibility = new PublicationVisibility {
                    Visibility = VisibilityScope.Public
                };
                return(true);
            }
            if (subscription != null && organization != null)
            {
                visibility = null;
                return(false);
            }
            if (subscription != null)
            {
                visibility = new PublicationVisibility {
                    Visibility = VisibilityScope.Subscription, Subscription = subscription
                };
                return(true);
            }
            if (organization != null)
            {
                visibility = new PublicationVisibility {
                    Visibility = VisibilityScope.Organization, Organization = organization
                };
                return(true);
            }

            visibility = null;
            return(false);
        }
        public static async Task <PublicationDetails> CreatePublicationDetails(IRegistrationOwnership registrationOwnership, PublicationVisibility publicationVisibility)
        {
            string userId   = registrationOwnership.GetUserId();
            string userName = await registrationOwnership.GetPublisherName();

            string tenantId = registrationOwnership.GetTenantId();

            //TODO: requires Graph access
            string tenantName = string.Empty;
            //string tenantName = await _registrationOwnership.GetTenantName();

            //var client = await ServiceHelpers.GetActiveDirectoryClient();

            PublicationDetails publicationDetails = new PublicationDetails
            {
                Published  = DateTime.UtcNow,
                Owner      = OwnershipOwner.Create(ClaimsPrincipal.Current),
                TenantId   = tenantId,
                TenantName = tenantName,
                Visibility = publicationVisibility
            };

            return(publicationDetails);
        }
        public async Task Upload(IOwinContext context)
        {
            Trace.TraceInformation("PublishImpl.Upload");

            if (!_registrationOwnership.IsAuthenticated)
            {
                await ServiceHelpers.WriteErrorResponse(context, "user does not have access to the service", HttpStatusCode.Forbidden);
                return;
            }

            if (!await _registrationOwnership.HasTenantEnabled())
            {
                await ServiceHelpers.WriteErrorResponse(context, "package publication has not been enabled in this tenant", HttpStatusCode.Forbidden);
                return;
            }

            PublicationVisibility publicationVisibility;
            if (!PublicationVisibility.TryCreate(context, out publicationVisibility))
            {
                await ServiceHelpers.WriteErrorResponse(context, "specify either organization OR subscription NOT BOTH", HttpStatusCode.BadRequest);
                return;
            }

            //  no-commit mode - used for just running the validation

            bool isCommit = GetIsCommit(context);

            Stream packageStream = context.Request.Body;
            
            //  validation

            ValidationResult validationResult = await Validate(packageStream);

            if (validationResult.HasErrors)
            {
                await ServiceHelpers.WriteErrorResponse(context, validationResult.Errors, HttpStatusCode.BadRequest);
                return;
            }
            
            //  registration authorization

            IList<string> authorizationErrors = await OwnershipHelpers.CheckRegistrationAuthorization(_registrationOwnership, validationResult.PackageIdentity);

            if (authorizationErrors.Count > 0)
            {
                await ServiceHelpers.WriteErrorResponse(context, authorizationErrors, HttpStatusCode.Forbidden);
                return;
            }

            //  listed

            bool isListed = true;
            string unlist = context.Request.Query["unlist"];
            if (unlist != null)
            {
                isListed = !unlist.Equals(Boolean.TrueString, StringComparison.InvariantCultureIgnoreCase);
            }

            Trace.TraceInformation("UPLOAD Processing package {0}/{1}/{2} isListed: {3} isCommit: {4}", validationResult.PackageIdentity.Namespace, validationResult.PackageIdentity.Id, validationResult.PackageIdentity.Version, isListed, isCommit);

            //  process the package

            IDictionary<string, JObject> metadata = new Dictionary<string, JObject>();

            //  (1) save all the artifacts

            if (isCommit)
            {
                await Artifacts.Save(metadata, packageStream, Configuration.StoragePrimary, Configuration.StorageContainerArtifacts);

                Trace.TraceInformation("Save");
            }

            InferArtifactTypes(metadata);

            //  (2) promote the relevant peices of metadata so they later can appear on the catalog page 

            await ExtractMetadata(metadata, packageStream);

            Trace.TraceInformation("ExtractMetadata");

            //  (3) gather all the publication details

            PublicationDetails publicationDetails = await OwnershipHelpers.CreatePublicationDetails(_registrationOwnership, publicationVisibility);

            Trace.TraceInformation("CreatePublicationDetails");

            //  (4) add the new item to the catalog

            Uri catalogAddress = null;

            if (isCommit)
            {
                catalogAddress = await AddToCatalog(metadata["nuspec"], GetItemType(), publicationDetails, isListed);

                Trace.TraceInformation("AddToCatalog");
            }

            //  (5) update the registration ownership record

            if (isCommit)
            {
                await UpdateRegistrationOwnership(validationResult.PackageIdentity);

                Trace.TraceInformation("UpdateRegistrationOwnership");
            }

            //  (6) create response

            if (isCommit)
            {
                JToken response = new JObject
                { 
                    { "download", metadata["nuspec"]["packageContent"] },
                    { "catalog", catalogAddress.ToString() }
                };

                await ServiceHelpers.WriteResponse(context, response, HttpStatusCode.Created);
            }
            else
            {
                context.Response.StatusCode = (int)HttpStatusCode.OK;
            }
        }
        public async Task Edit(IOwinContext context)
        {
            Trace.TraceInformation("PublishImpl.Edit");

            if (!_registrationOwnership.IsAuthenticated)
            {
                await ServiceHelpers.WriteErrorResponse(context, "user does not have access to the service", HttpStatusCode.Forbidden);
                return;
            }

            if (!await _registrationOwnership.HasTenantEnabled())
            {
                await ServiceHelpers.WriteErrorResponse(context, "package publication has not been enabled in this tenant", HttpStatusCode.Forbidden);
                return;
            }

            PublicationVisibility publicationVisibility;
            if (!PublicationVisibility.TryCreate(context, out publicationVisibility))
            {
                await ServiceHelpers.WriteErrorResponse(context, "specify either organization OR subscription NOT BOTH", HttpStatusCode.BadRequest);
                return;
            }

            Stream metadataStream = context.Request.Body;

            //  validation

            EditValidationResult validationResult = await ValidateEdit(metadataStream);

            if (validationResult.HasErrors)
            {
                await ServiceHelpers.WriteErrorResponse(context, validationResult.Errors, HttpStatusCode.BadRequest);
                return;
            }

            //  registration authorization

            IList<string> authorizationErrors = await OwnershipHelpers.CheckRegistrationAuthorizationForEdit(_registrationOwnership, validationResult.PackageIdentity);

            if (authorizationErrors.Count > 0)
            {
                await ServiceHelpers.WriteErrorResponse(context, authorizationErrors, HttpStatusCode.Forbidden);
                return;
            }

            Trace.TraceInformation("EDIT Processing package {0}/{1}/{2} listed: {3}", validationResult.PackageIdentity.Namespace, validationResult.PackageIdentity.Id, validationResult.PackageIdentity.Version, validationResult.Listed);

            //  process the edit

            IDictionary<string, JObject> metadata = new Dictionary<string, JObject>();

            //  (1) generate any new or replacement artifacts based on the current catalogEntry and the editMetadata

            IDictionary<string, PackageArtifact> artifacts = await GenerateNewArtifactsFromEdit(metadata, validationResult.CatalogEntry, validationResult.EditMetadata, Configuration.StoragePrimary);

            Trace.TraceInformation("GenerateNewArtifactsFromEdit");
            
            //  (2) save the new package

            await Artifacts.Save(metadata, artifacts, Configuration.StoragePrimary, Configuration.StorageContainerArtifacts);

            InferArtifactTypes(metadata);

            Trace.TraceInformation("Save");

            //  (3) promote the relevant peices of metadata so they later can appear on the catalog page 

            await GenerateNuspec(metadata);

            Trace.TraceInformation("GenerateNuspec");

            //  (4) gather all the publication details

            PublicationDetails publicationDetails = await OwnershipHelpers.CreatePublicationDetails(_registrationOwnership, publicationVisibility);

            Trace.TraceInformation("CreatePublicationDetails");

            //  (5) add the new item to the catalog

            Uri catalogAddress = await AddToCatalog(metadata["nuspec"], GetItemType(), publicationDetails, validationResult.Listed);

            Trace.TraceInformation("AddToCatalog");

            //  (6) update the registration ownership record

            await UpdateRegistrationOwnership(validationResult.PackageIdentity);

            Trace.TraceInformation("UpdateRegistrationOwnership");

            //  (7) create response

            JToken response = new JObject
            { 
                { "download", metadata["nuspec"]["packageContent"] },
                { "catalog", catalogAddress.ToString() }
            };

            await ServiceHelpers.WriteResponse(context, response, HttpStatusCode.OK);
        }
Пример #7
0
        public async Task Delete(IOwinContext context)
        {
            Trace.TraceInformation("DeleteImpl.Upload");

            if (!_registrationOwnership.IsAuthenticated)
            {
                await ServiceHelpers.WriteErrorResponse(context, "user does not have access to the service", HttpStatusCode.Forbidden);

                return;
            }

            if (!await _registrationOwnership.HasTenantEnabled())
            {
                await ServiceHelpers.WriteErrorResponse(context, "package publication has not been enabled in this tenant", HttpStatusCode.Forbidden);

                return;
            }

            PublicationVisibility publicationVisibility;

            if (!PublicationVisibility.TryCreate(context, out publicationVisibility))
            {
                await ServiceHelpers.WriteErrorResponse(context, "specify either organization OR subscription NOT BOTH", HttpStatusCode.BadRequest);

                return;
            }

            Stream packageStream = context.Request.Body;

            //  validation

            ValidationResult validationResult = await Validate(packageStream);

            if (validationResult.HasErrors)
            {
                await ServiceHelpers.WriteErrorResponse(context, validationResult.Errors, HttpStatusCode.BadRequest);

                return;
            }

            //  registration authorization

            IList <string> authorizationErrors = await OwnershipHelpers.CheckRegistrationAuthorizationForEdit(_registrationOwnership, validationResult.PackageIdentity);

            if (authorizationErrors.Count > 0)
            {
                await ServiceHelpers.WriteErrorResponse(context, authorizationErrors, HttpStatusCode.Forbidden);

                return;
            }

            Trace.TraceInformation("DELETE Processing package {0}/{1}/{2}", validationResult.PackageIdentity.Namespace, validationResult.PackageIdentity.Id, validationResult.PackageIdentity.Version);

            //  process delete

            //  (1) gather all the publication details

            PublicationDetails publicationDetails = await OwnershipHelpers.CreatePublicationDetails(_registrationOwnership, publicationVisibility);

            Trace.TraceInformation("CreatePublicationDetails");

            //  (2) add the new item to the catalog

            Uri catalogAddress = await AddToCatalog(validationResult.PackageIdentity, publicationDetails);

            Trace.TraceInformation("AddToCatalog");

            //  (3) update the registration ownership record

            await UpdateRegistrationOwnership(validationResult.PackageIdentity);

            Trace.TraceInformation("UpdateRegistrationOwnership");

            //  (4) create response

            JToken response = new JObject
            {
                { "catalog", catalogAddress.ToString() }
            };

            await ServiceHelpers.WriteResponse(context, response, HttpStatusCode.OK);
        }