1
1
using System ;
2
2
using System . Threading ;
3
3
using System . Threading . Tasks ;
4
+ using BaGet . Core . Configuration ;
4
5
using BaGet . Core . Services ;
5
6
using BaGet . Extensions ;
6
7
using Microsoft . AspNetCore . Mvc ;
7
8
using Microsoft . Extensions . Logging ;
9
+ using Microsoft . Extensions . Options ;
8
10
using NuGet . Versioning ;
9
11
10
12
namespace BaGet . Controllers
@@ -15,26 +17,30 @@ public class PackagePublishController : Controller
15
17
private readonly IPackageIndexingService _indexer ;
16
18
private readonly IPackageService _packages ;
17
19
private readonly IPackageDeletionService _deleteService ;
20
+ private readonly IOptionsSnapshot < BaGetOptions > _options ;
18
21
private readonly ILogger < PackagePublishController > _logger ;
19
22
20
23
public PackagePublishController (
21
24
IAuthenticationService authentication ,
22
25
IPackageIndexingService indexer ,
23
26
IPackageService packages ,
24
27
IPackageDeletionService deletionService ,
28
+ IOptionsSnapshot < BaGetOptions > options ,
25
29
ILogger < PackagePublishController > logger )
26
30
{
27
31
_authentication = authentication ?? throw new ArgumentNullException ( nameof ( authentication ) ) ;
28
32
_indexer = indexer ?? throw new ArgumentNullException ( nameof ( indexer ) ) ;
29
33
_packages = packages ?? throw new ArgumentNullException ( nameof ( packages ) ) ;
30
34
_deleteService = deletionService ?? throw new ArgumentNullException ( nameof ( deletionService ) ) ;
35
+ _options = options ?? throw new ArgumentNullException ( nameof ( options ) ) ;
31
36
_logger = logger ?? throw new ArgumentNullException ( nameof ( logger ) ) ;
32
37
}
33
38
34
39
// See: https://docs.microsoft.com/en-us/nuget/api/package-publish-resource#push-a-package
35
40
public async Task Upload ( CancellationToken cancellationToken )
36
41
{
37
- if ( ! await _authentication . AuthenticateAsync ( Request . GetApiKey ( ) ) )
42
+ if ( _options . Value . IsReadOnlyMode ||
43
+ ! await _authentication . AuthenticateAsync ( Request . GetApiKey ( ) ) )
38
44
{
39
45
HttpContext . Response . StatusCode = 401 ;
40
46
return ;
@@ -79,6 +85,11 @@ public async Task Upload(CancellationToken cancellationToken)
79
85
[ HttpDelete ]
80
86
public async Task < IActionResult > Delete ( string id , string version , CancellationToken cancellationToken )
81
87
{
88
+ if ( _options . Value . IsReadOnlyMode )
89
+ {
90
+ return Unauthorized ( ) ;
91
+ }
92
+
82
93
if ( ! NuGetVersion . TryParse ( version , out var nugetVersion ) )
83
94
{
84
95
return NotFound ( ) ;
@@ -102,6 +113,11 @@ public async Task<IActionResult> Delete(string id, string version, CancellationT
102
113
[ HttpPost ]
103
114
public async Task < IActionResult > Relist ( string id , string version )
104
115
{
116
+ if ( _options . Value . IsReadOnlyMode )
117
+ {
118
+ return Unauthorized ( ) ;
119
+ }
120
+
105
121
if ( ! NuGetVersion . TryParse ( version , out var nugetVersion ) )
106
122
{
107
123
return NotFound ( ) ;
0 commit comments