6
6
using rubberduckvba . Server . Model . Entity ;
7
7
using rubberduckvba . Server . Services ;
8
8
using rubberduckvba . Server . Services . rubberduckdb ;
9
+ using System . Security . Principal ;
9
10
10
11
namespace rubberduckvba . Server . Api . Features ;
11
12
@@ -21,17 +22,19 @@ public class FeaturesController : RubberduckApiController
21
22
{
22
23
private readonly CacheService cache ;
23
24
private readonly IRubberduckDbService db ;
25
+ private readonly IAuditService admin ;
24
26
private readonly FeatureServices features ;
25
27
private readonly IRepository < TagAssetEntity > assetsRepository ;
26
28
private readonly IRepository < TagEntity > tagsRepository ;
27
29
private readonly IMarkdownFormattingService markdownService ;
28
30
29
- public FeaturesController ( CacheService cache , IRubberduckDbService db , FeatureServices features , IMarkdownFormattingService markdownService ,
31
+ public FeaturesController ( CacheService cache , IRubberduckDbService db , IAuditService admin , FeatureServices features , IMarkdownFormattingService markdownService ,
30
32
IRepository < TagAssetEntity > assetsRepository , IRepository < TagEntity > tagsRepository , ILogger < FeaturesController > logger )
31
33
: base ( logger )
32
34
{
33
35
this . cache = cache ;
34
36
this . db = db ;
37
+ this . admin = admin ;
35
38
this . features = features ;
36
39
this . assetsRepository = assetsRepository ;
37
40
this . tagsRepository = tagsRepository ;
@@ -175,11 +178,15 @@ public async Task<ActionResult<FeatureEditViewModel>> Create([FromBody] FeatureE
175
178
}
176
179
177
180
var feature = model . ToFeature ( ) ;
178
-
179
- var result = await db . SaveFeature ( feature ) ;
180
- var features = await GetFeatureOptions ( model . RepositoryId ) ;
181
-
182
- return Ok ( new FeatureEditViewModel ( result , features , RepositoryOptions ) ) ;
181
+ if ( User . Identity is IIdentity identity )
182
+ {
183
+ await admin . CreateFeature ( feature , identity ) ;
184
+ return Ok ( feature ) ;
185
+ }
186
+ else
187
+ {
188
+ return Unauthorized ( "User identity is not available." ) ;
189
+ }
183
190
}
184
191
185
192
[ HttpPost ( "features/update" ) ]
@@ -198,11 +205,15 @@ public async Task<ActionResult<FeatureEditViewModel>> Update([FromBody] FeatureE
198
205
}
199
206
200
207
var feature = model . ToFeature ( ) ;
201
-
202
- var result = await db . SaveFeature ( feature ) ;
203
- var features = await GetFeatureOptions ( model . RepositoryId ) ;
204
-
205
- return new FeatureEditViewModel ( result , features , RepositoryOptions ) ;
208
+ if ( User . Identity is IIdentity identity )
209
+ {
210
+ await admin . UpdateFeature ( feature , identity ) ;
211
+ return Ok ( feature ) ;
212
+ }
213
+ else
214
+ {
215
+ return Unauthorized ( "User identity is not available." ) ;
216
+ }
206
217
}
207
218
208
219
[ HttpPost ( "features/delete" ) ]
@@ -213,13 +224,20 @@ public async Task Delete([FromBody] IFeature model)
213
224
{
214
225
throw new ArgumentException ( "Model is invalid for this endpoint." ) ;
215
226
}
216
- var existingId = await db . GetFeatureId ( RepositoryId . Rubberduck , model . Name ) ;
217
- if ( existingId is null )
227
+ var existing = await db . ResolveFeature ( RepositoryId . Rubberduck , model . Name ) ;
228
+ if ( existing is null )
218
229
{
219
230
throw new ArgumentException ( "Model is invalid for this endpoint." ) ;
220
231
}
221
232
222
- await db . DeleteFeature ( existingId . Value ) ;
233
+ if ( User . Identity is IIdentity identity )
234
+ {
235
+ await admin . DeleteFeature ( existing , identity ) ;
236
+ }
237
+ else
238
+ {
239
+ throw new UnauthorizedAccessException ( "User identity is not available." ) ;
240
+ }
223
241
}
224
242
225
243
[ HttpPost ( "markdown/format" ) ]
0 commit comments