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)); } }
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(); } }
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); }
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 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)); } }