private async Task RemovePackageOwnerImplAsync(PackageRegistration packageRegistration, User ownerToBeRemoved)
        {
            // Remove the user from owners list of package registration
            await _packageService.RemovePackageOwnerAsync(packageRegistration, ownerToBeRemoved, commitChanges : false);

            // Remove this package registration from the namespaces owned by this user that are owned by no other package owners
            foreach (var reservedNamespace in packageRegistration.ReservedNamespaces.ToArray())
            {
                if (!packageRegistration.Owners
                    .Any(o => ActionsRequiringPermissions.AddPackageToReservedNamespace
                         .CheckPermissionsOnBehalfOfAnyAccount(o, reservedNamespace) == PermissionsCheckResult.Allowed))
                {
                    _reservedNamespaceService.RemovePackageRegistrationFromNamespace(reservedNamespace, packageRegistration);
                }
            }

            // Remove the IsVerified flag from package registration if all the matching namespaces are owned by this user alone (no other package owner owns a matching namespace for this PR)
            if (packageRegistration.IsVerified && !packageRegistration.ReservedNamespaces.Any())
            {
                await _packageService.UpdatePackageVerifiedStatusAsync(new List <PackageRegistration> {
                    packageRegistration
                }, isVerified : false, commitChanges : false);
            }

            await _entitiesContext.SaveChangesAsync();
        }
示例#2
0
        private async Task RemovePackageOwnerImplAsync(PackageRegistration packageRegistration, User requestingOwner, User ownerToBeRemoved)
        {
            // 1. Remove this package registration from the namespaces owned by this user if he is the only package owner in the set of matching namespaces
            // 2. Remove the IsVerified flag from package registration if all the matching namespaces are owned by this user alone (no other package owner owns a matching namespace for this PR)
            var allMatchingNamespacesForRegistration = packageRegistration.ReservedNamespaces;

            if (allMatchingNamespacesForRegistration.Any())
            {
                var allPackageOwners = packageRegistration.Owners;
                var matchingNamespacesOwnedByUser = allMatchingNamespacesForRegistration
                                                    .Where(rn => rn.Owners.Any(o => o == ownerToBeRemoved));
                var namespacesToModify = matchingNamespacesOwnedByUser
                                         .Where(rn => rn.Owners.Intersect(allPackageOwners).Count() == 1)
                                         .ToList();

                if (namespacesToModify.Any())
                {
                    // The package will lose its 'IsVerified' flag if the user is the only package owner who owns all the namespaces that match this registration
                    var shouldModifyIsVerified = allMatchingNamespacesForRegistration.Count() == namespacesToModify.Count();
                    if (shouldModifyIsVerified && packageRegistration.IsVerified)
                    {
                        await _packageService.UpdatePackageVerifiedStatusAsync(new List <PackageRegistration> {
                            packageRegistration
                        }, isVerified : false);
                    }

                    namespacesToModify
                    .ForEach(rn => _reservedNamespaceService.RemovePackageRegistrationFromNamespace(rn.Value, packageRegistration));

                    await _entitiesContext.SaveChangesAsync();
                }
            }

            // Remove the user from owners list of package registration
            await _packageService.RemovePackageOwnerAsync(packageRegistration, ownerToBeRemoved);
        }
        public async Task RemovePackageOwnerAsync(PackageRegistration packageRegistration, User requestingOwner, User ownerToBeRemoved)
        {
            if (packageRegistration == null)
            {
                throw new ArgumentNullException(nameof(packageRegistration));
            }

            if (requestingOwner == null)
            {
                throw new ArgumentNullException(nameof(requestingOwner));
            }

            if (ownerToBeRemoved == null)
            {
                throw new ArgumentNullException(nameof(ownerToBeRemoved));
            }

            if (OwnerHasPermissionsToRemove(requestingOwner, ownerToBeRemoved, packageRegistration))
            {
                using (var strategy = new SuspendDbExecutionStrategy())
                    using (var transaction = _entitiesContext.GetDatabase().BeginTransaction())
                    {
                        // 1. Remove this package registration from the namespaces owned by this user if he is the only package owner in the set of matching namespaces
                        // 2. Remove the IsVerified flag from package registration if all the matching namespaces are owned by this user alone (no other package owner owns a matching namespace for this PR)
                        var allMatchingNamespacesForRegistration = packageRegistration.ReservedNamespaces;
                        if (allMatchingNamespacesForRegistration.Any())
                        {
                            var allPackageOwners = packageRegistration.Owners;
                            var matchingNamespacesOwnedByUser = allMatchingNamespacesForRegistration
                                                                .Where(rn => rn.Owners.Any(o => o == ownerToBeRemoved));
                            var namespacesToModify = matchingNamespacesOwnedByUser
                                                     .Where(rn => rn.Owners.Intersect(allPackageOwners).Count() == 1)
                                                     .ToList();

                            if (namespacesToModify.Any())
                            {
                                // The package will lose its 'IsVerified' flag if the user is the only package owner who owns all the namespaces that match this registration
                                var shouldModifyIsVerified = allMatchingNamespacesForRegistration.Count() == namespacesToModify.Count();
                                if (shouldModifyIsVerified && packageRegistration.IsVerified)
                                {
                                    await _packageService.UpdatePackageVerifiedStatusAsync(new List <PackageRegistration> {
                                        packageRegistration
                                    }, isVerified : false);
                                }

                                namespacesToModify
                                .ForEach(rn => _reservedNamespaceService.RemovePackageRegistrationFromNamespace(rn.Value, packageRegistration));

                                await _entitiesContext.SaveChangesAsync();
                            }
                        }

                        // Remove the user from owners list of package registration
                        await _packageService.RemovePackageOwnerAsync(packageRegistration, ownerToBeRemoved);

                        transaction.Commit();
                    }

                await _auditingService.SaveAuditRecordAsync(
                    new PackageRegistrationAuditRecord(packageRegistration, AuditedPackageRegistrationAction.RemoveOwner, ownerToBeRemoved.Username));
            }
            else
            {
                throw new InvalidOperationException(string.Format(Strings.RemoveOwner_NotAllowed, requestingOwner.Username, ownerToBeRemoved.Username));
            }
        }