Esempio n. 1
0
        public async Task DeleteReservedNamespaceAsync(string existingNamespace)
        {
            if (string.IsNullOrWhiteSpace(existingNamespace))
            {
                throw new ArgumentException(ServicesStrings.ReservedNamespace_InvalidNamespace);
            }

            using (var strategy = new SuspendDbExecutionStrategy())
                using (var transaction = EntitiesContext.GetDatabase().BeginTransaction())
                {
                    var namespaceToDelete = FindReservedNamespaceForPrefix(existingNamespace)
                                            ?? throw new InvalidOperationException(string.Format(
                                                                                       CultureInfo.CurrentCulture, ServicesStrings.ReservedNamespace_NamespaceNotFound, existingNamespace));

                    // Delete verified flags on corresponding packages for this prefix if
                    // it is the only prefix matching the package registration.
                    var packageRegistrationsToMarkUnverified = namespaceToDelete
                                                               .PackageRegistrations
                                                               .Where(pr => pr.ReservedNamespaces.Count() == 1)
                                                               .ToList();

                    if (packageRegistrationsToMarkUnverified.Any())
                    {
                        await PackageService.UpdatePackageVerifiedStatusAsync(packageRegistrationsToMarkUnverified, isVerified : false);
                    }

                    ReservedNamespaceRepository.DeleteOnCommit(namespaceToDelete);
                    await ReservedNamespaceRepository.CommitChangesAsync();

                    transaction.Commit();

                    await AuditingService.SaveAuditRecordAsync(
                        new ReservedNamespaceAuditRecord(namespaceToDelete, AuditedReservedNamespaceAction.UnreserveNamespace));
                }
        }
Esempio n. 2
0
 public virtual IReadOnlyCollection <ReservedNamespace> GetReservedNamespacesForId(string id)
 {
     return((from request in ReservedNamespaceRepository.GetAll()
             where (request.IsPrefix && id.StartsWith(request.Value)) ||
             (!request.IsPrefix && id.Equals(request.Value))
             select request).ToList());
 }
 public override IReadOnlyCollection <ReservedNamespace> GetReservedNamespacesForId(string id)
 {
     return((from request in ReservedNamespaceRepository.GetAll()
             where (request.IsPrefix && id.StartsWith(request.Value, StringComparison.OrdinalIgnoreCase)) ||
             (!request.IsPrefix && id.Equals(request.Value, StringComparison.OrdinalIgnoreCase))
             select request).ToList());
 }
Esempio n. 4
0
        public async Task AddReservedNamespaceAsync(ReservedNamespace newNamespace)
        {
            if (newNamespace == null)
            {
                throw new ArgumentNullException(nameof(newNamespace));
            }

            ValidateNamespace(newNamespace.Value);

            var matchingReservedNamespaces = FindAllReservedNamespacesForPrefix(prefix: newNamespace.Value, getExactMatches: !newNamespace.IsPrefix);

            if (matchingReservedNamespaces.Any())
            {
                throw new InvalidOperationException(Strings.ReservedNamespace_NamespaceNotAvailable);
            }

            // Mark the new namespace as shared if it matches any liberal namespace which is a shared
            // namespace. For eg: A.B.* is a shared namespace, when reserving A.B.C.* namespace,
            // make it a shared namespace. This ensures that all namespaces under a shared
            // namespace are also shared to keep the data consistent.
            if (!newNamespace.IsSharedNamespace && ShouldForceSharedNamespace(newNamespace.Value))
            {
                newNamespace.IsSharedNamespace = true;
            }

            ReservedNamespaceRepository.InsertOnCommit(newNamespace);
            await ReservedNamespaceRepository.CommitChangesAsync();
        }
Esempio n. 5
0
 public virtual IReadOnlyCollection <ReservedNamespace> FindReservedNamespacesForPrefixList(IReadOnlyCollection <string> prefixList)
 {
     return((from dbPrefix in ReservedNamespaceRepository.GetAll()
             join queryPrefix in prefixList
             on dbPrefix.Value equals queryPrefix
             select dbPrefix).ToList());
 }
Esempio n. 6
0
        public async Task DeleteOwnerFromReservedNamespaceAsync(string prefix, string username)
        {
            if (string.IsNullOrWhiteSpace(prefix))
            {
                throw new ArgumentException(Strings.ReservedNamespace_InvalidNamespace);
            }

            if (string.IsNullOrWhiteSpace(username))
            {
                throw new ArgumentException(Strings.ReservedNamespace_InvalidUsername);
            }

            using (var strategy = new SuspendDbExecutionStrategy())
                using (var transaction = EntitiesContext.GetDatabase().BeginTransaction())
                {
                    var namespaceToModify = FindReservedNamespaceForPrefix(prefix)
                                            ?? throw new InvalidOperationException(string.Format(
                                                                                       CultureInfo.CurrentCulture, Strings.ReservedNamespace_NamespaceNotFound, prefix));

                    var userToRemove = UserService.FindByUsername(username)
                                       ?? throw new InvalidOperationException(string.Format(
                                                                                  CultureInfo.CurrentCulture, Strings.ReservedNamespace_UserNotFound, username));

                    if (!namespaceToModify.Owners.Contains(userToRemove))
                    {
                        throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, Strings.ReservedNamespace_UserNotAnOwner, username));
                    }

                    var packagesOwnedByUserMatchingPrefix = namespaceToModify
                                                            .PackageRegistrations
                                                            .Where(pr => pr
                                                                   .Owners
                                                                   .Any(pro => pro.Username == userToRemove.Username))
                                                            .ToList();

                    // Remove verified mark for package registrations if the user to be removed is the only prefix owner
                    // for the given package registration.
                    var packageRegistrationsToMarkUnverified = packagesOwnedByUserMatchingPrefix
                                                               .Where(pr => pr.Owners.Intersect(namespaceToModify.Owners).Count() == 1)
                                                               .ToList();

                    if (packageRegistrationsToMarkUnverified.Any())
                    {
                        packageRegistrationsToMarkUnverified
                        .ForEach(pr => namespaceToModify.PackageRegistrations.Remove(pr));

                        await PackageService.UpdatePackageVerifiedStatusAsync(packageRegistrationsToMarkUnverified, isVerified : false);
                    }

                    namespaceToModify.Owners.Remove(userToRemove);
                    await ReservedNamespaceRepository.CommitChangesAsync();

                    transaction.Commit();
                }
        }
        public async Task AddOwnerToReservedNamespaceAsync(string prefix, string username)
        {
            if (string.IsNullOrWhiteSpace(prefix))
            {
                throw new ArgumentException(Strings.ReservedNamespace_InvalidNamespace);
            }

            if (string.IsNullOrWhiteSpace(username))
            {
                throw new ArgumentException(Strings.ReservedNamespace_InvalidUsername);
            }

            using (var strategy = new SuspendDbExecutionStrategy())
                using (var transaction = EntitiesContext.GetDatabase().BeginTransaction())
                {
                    var namespaceToModify = FindReservedNamespaceForPrefix(prefix)
                                            ?? throw new InvalidOperationException(string.Format(
                                                                                       CultureInfo.CurrentCulture, Strings.ReservedNamespace_NamespaceNotFound, prefix));

                    var userToAdd = UserService.FindByUsername(username)
                                    ?? throw new InvalidOperationException(string.Format(
                                                                               CultureInfo.CurrentCulture, Strings.ReservedNamespace_UserNotFound, username));

                    if (namespaceToModify.Owners.Contains(userToAdd))
                    {
                        throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, Strings.ReservedNamespace_UserAlreadyOwner, username));
                    }

                    // Mark all packages owned by this user that start with the given namespace as verified.
                    var allPackageRegistrationsForUser        = PackageService.FindPackageRegistrationsByOwner(userToAdd);
                    var packageRegistrationsMatchingNamespace = allPackageRegistrationsForUser
                                                                .Where(pr => pr.Id.StartsWith(namespaceToModify.Value, StringComparison.OrdinalIgnoreCase))
                                                                .ToList();

                    if (packageRegistrationsMatchingNamespace.Any())
                    {
                        packageRegistrationsMatchingNamespace
                        .ForEach(pr => namespaceToModify.PackageRegistrations.Add(pr));

                        await PackageService.UpdatePackageVerifiedStatusAsync(packageRegistrationsMatchingNamespace.AsReadOnly(), isVerified : true);
                    }

                    namespaceToModify.Owners.Add(userToAdd);
                    await ReservedNamespaceRepository.CommitChangesAsync();

                    transaction.Commit();
                }
        }
        public override IReadOnlyCollection <ReservedNamespace> FindAllReservedNamespacesForPrefix(string prefix, bool getExactMatches)
        {
            Expression <Func <ReservedNamespace, bool> > prefixMatch;

            if (getExactMatches)
            {
                prefixMatch = dbPrefix => dbPrefix.Value.Equals(prefix, StringComparison.OrdinalIgnoreCase);
            }
            else
            {
                prefixMatch = dbPrefix => dbPrefix.Value.StartsWith(prefix, StringComparison.OrdinalIgnoreCase);
            }

            return(ReservedNamespaceRepository.GetAll()
                   .Where(prefixMatch)
                   .ToList());
        }
Esempio n. 9
0
        public async Task AddReservedNamespaceAsync(ReservedNamespace newNamespace)
        {
            if (newNamespace == null)
            {
                throw new ArgumentNullException(nameof(newNamespace));
            }

            ValidateNamespace(newNamespace.Value);

            var matchingReservedNamespaces = FindAllReservedNamespacesForPrefix(prefix: newNamespace.Value, getExactMatches: !newNamespace.IsPrefix);

            if (matchingReservedNamespaces.Any())
            {
                throw new InvalidOperationException(Strings.ReservedNamespace_NamespaceNotAvailable);
            }

            ReservedNamespaceRepository.InsertOnCommit(newNamespace);
            await ReservedNamespaceRepository.CommitChangesAsync();
        }
Esempio n. 10
0
        private async Task <List <PackageRegistration> > DeleteOwnerFromReservedNamespaceImplAsync(string prefix, string username, ReservedNamespace namespaceToModify, bool commitChanges = true)
        {
            var userToRemove = UserService.FindByUsername(username)
                               ?? throw new InvalidOperationException(string.Format(
                                                                          CultureInfo.CurrentCulture, ServicesStrings.ReservedNamespace_UserNotFound, username));

            if (!namespaceToModify.Owners.Contains(userToRemove))
            {
                throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, ServicesStrings.ReservedNamespace_UserNotAnOwner, username));
            }

            var packagesOwnedByUserMatchingPrefix = namespaceToModify
                                                    .PackageRegistrations
                                                    .Where(pr => pr
                                                           .Owners
                                                           .Any(pro => pro.Username == userToRemove.Username))
                                                    .ToList();

            namespaceToModify.Owners.Remove(userToRemove);

            // Remove verified mark for package registrations if the user to be removed is the only prefix owner
            // for the given package registration.
            var packageRegistrationsToMarkUnverified = packagesOwnedByUserMatchingPrefix
                                                       .Where(pr => !pr.Owners.Any(o =>
                                                                                   ActionsRequiringPermissions.AddPackageToReservedNamespace.CheckPermissionsOnBehalfOfAnyAccount(
                                                                                       o, new[] { namespaceToModify }) == PermissionsCheckResult.Allowed))
                                                       .ToList();

            if (packageRegistrationsToMarkUnverified.Any())
            {
                packageRegistrationsToMarkUnverified
                .ForEach(pr => namespaceToModify.PackageRegistrations.Remove(pr));

                await PackageService.UpdatePackageVerifiedStatusAsync(packageRegistrationsToMarkUnverified, isVerified : false, commitChanges : false);
            }

            if (commitChanges)
            {
                await ReservedNamespaceRepository.CommitChangesAsync();
            }

            return(packageRegistrationsToMarkUnverified);
        }
        /// <summary>
        /// This method fetches the reserved namespace matching the prefix and adds the
        /// package registration entry to the reserved namespace, the provided package registration
        /// should be an entry in the database or an entity from memory to be committed.
        /// </summary>
        /// <param name="prefix">The prefix value of the reserved namespace to modify</param>
        /// <param name="packageRegistration">The package registration entity entry to be added.</param>
        /// <param name="commitChanges">Flag to commit the modifications to the database, if set to false
        /// the caller of this method should take care of saving changes for entities context.</param>
        /// <returns>Awaitable task</returns>
        public async Task AddPackageRegistrationToNamespaceAsync(string prefix, PackageRegistration packageRegistration, bool commitChanges = true)
        {
            if (string.IsNullOrWhiteSpace(prefix))
            {
                throw new ArgumentException(Strings.ReservedNamespace_InvalidNamespace);
            }

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

            var namespaceToModify = FindReservedNamespaceForPrefix(prefix)
                                    ?? throw new InvalidOperationException(string.Format(
                                                                               CultureInfo.CurrentCulture, Strings.ReservedNamespace_NamespaceNotFound, prefix));

            namespaceToModify.PackageRegistrations.Add(packageRegistration);

            if (commitChanges)
            {
                await ReservedNamespaceRepository.CommitChangesAsync();
            }
        }
        private async Task <List <PackageRegistration> > DeleteOwnerFromReservedNamespaceImplAsync(string prefix, string username, ReservedNamespace namespaceToModify)
        {
            var userToRemove = UserService.FindByUsername(username)
                               ?? throw new InvalidOperationException(string.Format(
                                                                          CultureInfo.CurrentCulture, Strings.ReservedNamespace_UserNotFound, username));

            if (!namespaceToModify.Owners.Contains(userToRemove))
            {
                throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, Strings.ReservedNamespace_UserNotAnOwner, username));
            }

            var packagesOwnedByUserMatchingPrefix = namespaceToModify
                                                    .PackageRegistrations
                                                    .Where(pr => pr
                                                           .Owners
                                                           .Any(pro => pro.Username == userToRemove.Username))
                                                    .ToList();

            // Remove verified mark for package registrations if the user to be removed is the only prefix owner
            // for the given package registration.
            var packageRegistrationsToMarkUnverified = packagesOwnedByUserMatchingPrefix
                                                       .Where(pr => pr.Owners.Intersect(namespaceToModify.Owners).Count() == 1)
                                                       .ToList();

            if (packageRegistrationsToMarkUnverified.Any())
            {
                packageRegistrationsToMarkUnverified
                .ForEach(pr => namespaceToModify.PackageRegistrations.Remove(pr));

                await PackageService.UpdatePackageVerifiedStatusAsync(packageRegistrationsToMarkUnverified, isVerified : false);
            }

            namespaceToModify.Owners.Remove(userToRemove);
            await ReservedNamespaceRepository.CommitChangesAsync();

            return(packageRegistrationsToMarkUnverified);
        }
 public override IReadOnlyCollection <ReservedNamespace> FindReservedNamespacesForPrefixList(IReadOnlyCollection <string> prefixList)
 {
     return((from dbPrefix in ReservedNamespaceRepository.GetAll()
             where (prefixList.Any(p => p.Equals(dbPrefix.Value, StringComparison.OrdinalIgnoreCase)))
             select dbPrefix).ToList());
 }
Esempio n. 14
0
 public IReadOnlyCollection <ReservedNamespace> GetReservedNamespacesForId(string id)
 {
     return((from request in ReservedNamespaceRepository.GetAll()
             where id.StartsWith(request.Value)
             select request).ToList());
 }
 public override ReservedNamespace FindReservedNamespaceForPrefix(string prefix)
 {
     return((from request in ReservedNamespaceRepository.GetAll()
             where request.Value.Equals(prefix, StringComparison.OrdinalIgnoreCase)
             select request).FirstOrDefault());
 }
Esempio n. 16
0
        public async Task AddOwnerToReservedNamespaceAsync(string prefix, string username)
        {
            if (string.IsNullOrWhiteSpace(prefix))
            {
                throw new ArgumentException(ServicesStrings.ReservedNamespace_InvalidNamespace);
            }

            if (string.IsNullOrWhiteSpace(username))
            {
                throw new ArgumentException(ServicesStrings.ReservedNamespace_InvalidUsername);
            }

            using (var strategy = new SuspendDbExecutionStrategy())
                using (var transaction = EntitiesContext.GetDatabase().BeginTransaction())
                {
                    var namespaceToModify = FindReservedNamespaceForPrefix(prefix)
                                            ?? throw new InvalidOperationException(string.Format(
                                                                                       CultureInfo.CurrentCulture, ServicesStrings.ReservedNamespace_NamespaceNotFound, prefix));

                    var userToAdd = UserService.FindByUsername(username)
                                    ?? throw new InvalidOperationException(string.Format(
                                                                               CultureInfo.CurrentCulture, ServicesStrings.ReservedNamespace_UserNotFound, username));

                    if (namespaceToModify.Owners.Contains(userToAdd))
                    {
                        throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, ServicesStrings.ReservedNamespace_UserAlreadyOwner, username));
                    }

                    Expression <Func <PackageRegistration, bool> > predicate;
                    if (namespaceToModify.IsPrefix)
                    {
                        predicate = registration => registration.Id.StartsWith(namespaceToModify.Value);
                    }
                    else
                    {
                        predicate = registration => registration.Id.Equals(namespaceToModify.Value);
                    }

                    // Mark all packages owned by this user that start with the given namespace as verified.
                    var allPackageRegistrationsForUser = PackageService.FindPackageRegistrationsByOwner(userToAdd);

                    // We need 'AsQueryable' here because FindPackageRegistrationsByOwner returns an IEnumerable
                    // and to evaluate the predicate server side, the casting is essential.
                    var packageRegistrationsMatchingNamespace = allPackageRegistrationsForUser
                                                                .AsQueryable()
                                                                .Where(predicate)
                                                                .ToList();

                    if (packageRegistrationsMatchingNamespace.Any())
                    {
                        packageRegistrationsMatchingNamespace
                        .ForEach(pr => namespaceToModify.PackageRegistrations.Add(pr));

                        await PackageService.UpdatePackageVerifiedStatusAsync(packageRegistrationsMatchingNamespace.AsReadOnly(), isVerified : true);
                    }

                    namespaceToModify.Owners.Add(userToAdd);
                    await ReservedNamespaceRepository.CommitChangesAsync();

                    transaction.Commit();

                    await AuditingService.SaveAuditRecordAsync(
                        new ReservedNamespaceAuditRecord(namespaceToModify, AuditedReservedNamespaceAction.AddOwner, username, packageRegistrationsMatchingNamespace));
                }
        }
Esempio n. 17
0
 public virtual ReservedNamespace FindReservedNamespaceForPrefix(string prefix)
 {
     return((from request in ReservedNamespaceRepository.GetAll()
             where request.Value.Equals(prefix)
             select request).FirstOrDefault());
 }