Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2850 from NuGet/issue-2849
Title of newly uploaded package should not match existing package registration id
- Loading branch information
Showing
12 changed files
with
339 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
src/NuGetGallery/Services/PackageIdAndTitleValidationService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
namespace NuGetGallery | ||
{ | ||
public interface IPackageNamingConflictValidator | ||
{ | ||
bool TitleConflictsWithExistingRegistrationId(string registrationId, string packageTitle); | ||
bool IdConflictsWithExistingPackageTitle(string registrationId); | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
src/NuGetGallery/Services/PackageNamingConflictValidator.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using System.Linq; | ||
|
||
namespace NuGetGallery | ||
{ | ||
public class PackageNamingConflictValidator | ||
: IPackageNamingConflictValidator | ||
{ | ||
private readonly IEntityRepository<PackageRegistration> _packageRegistrationRepository; | ||
private readonly IEntityRepository<Package> _packageRepository; | ||
|
||
public PackageNamingConflictValidator( | ||
IEntityRepository<PackageRegistration> packageRegistrationRepository, | ||
IEntityRepository<Package> packageRepository) | ||
{ | ||
_packageRegistrationRepository = packageRegistrationRepository; | ||
_packageRepository = packageRepository; | ||
} | ||
|
||
public bool TitleConflictsWithExistingRegistrationId(string registrationId, string packageTitle) | ||
{ | ||
if (string.IsNullOrEmpty(registrationId)) | ||
{ | ||
throw new ArgumentNullException("registrationId"); | ||
} | ||
|
||
if (!string.IsNullOrEmpty(packageTitle)) | ||
{ | ||
var cleanedTitle = packageTitle.Trim().ToLowerInvariant(); | ||
if (!string.IsNullOrEmpty(cleanedTitle)) | ||
{ | ||
var packageRegistration = _packageRegistrationRepository.GetAll() | ||
.SingleOrDefault(pr => pr.Id.ToLower() == cleanedTitle); | ||
|
||
if (packageRegistration != null | ||
&& !String.Equals(packageRegistration.Id, registrationId, StringComparison.OrdinalIgnoreCase)) | ||
{ | ||
return true; | ||
} | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
public bool IdConflictsWithExistingPackageTitle(string registrationId) | ||
{ | ||
if (string.IsNullOrEmpty(registrationId)) | ||
{ | ||
throw new ArgumentNullException("registrationId"); | ||
} | ||
|
||
registrationId = registrationId.ToLowerInvariant(); | ||
|
||
return _packageRepository.GetAll() | ||
.Any(p => p.Title.ToLower() == registrationId); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.