-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c68946b
commit e012bf2
Showing
11 changed files
with
235 additions
and
80 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
34 changes: 34 additions & 0 deletions
34
backend/src/Designer/Middleware/UserRequestSynchronization/EditingContextResolver.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,34 @@ | ||
using Altinn.Studio.Designer.Helpers; | ||
using Altinn.Studio.Designer.Models; | ||
using Microsoft.AspNetCore.Http; | ||
|
||
namespace Altinn.Studio.Designer.Middleware.UserRequestSynchronization; | ||
|
||
public class EditingContextResolver : IEditingContextResolver | ||
{ | ||
public bool TryResolveContext(HttpContext httpContext, out AltinnRepoEditingContext context) | ||
{ | ||
context = null; | ||
var routeValues = httpContext.Request.RouteValues; | ||
|
||
string org = routeValues.TryGetValue("org", out var orgValue) ? orgValue?.ToString() : null; | ||
string app = null; | ||
|
||
if (routeValues.TryGetValue("app", out object appValue) || routeValues.TryGetValue("repo", out appValue) || | ||
routeValues.TryGetValue("repository", out appValue)) | ||
{ | ||
app = appValue?.ToString(); | ||
} | ||
|
||
string developer = AuthenticationHelper.GetDeveloperUserName(httpContext); | ||
|
||
|
||
if (string.IsNullOrEmpty(org) || string.IsNullOrEmpty(app) || string.IsNullOrEmpty(developer)) | ||
{ | ||
return false; | ||
} | ||
|
||
context = AltinnRepoEditingContext.FromOrgRepoDeveloper(org, app, developer); | ||
return true; | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
backend/src/Designer/Middleware/UserRequestSynchronization/IEditingContextResolver.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,9 @@ | ||
using Altinn.Studio.Designer.Models; | ||
using Microsoft.AspNetCore.Http; | ||
|
||
namespace Altinn.Studio.Designer.Middleware.UserRequestSynchronization; | ||
|
||
public interface IEditingContextResolver | ||
{ | ||
bool TryResolveContext(HttpContext httpContext, out AltinnRepoEditingContext context); | ||
} |
8 changes: 8 additions & 0 deletions
8
backend/src/Designer/Middleware/UserRequestSynchronization/IRequestSyncEvaluator.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,8 @@ | ||
using Microsoft.AspNetCore.Http; | ||
|
||
namespace Altinn.Studio.Designer.Middleware.UserRequestSynchronization; | ||
|
||
public interface IRequestSyncEvaluator | ||
{ | ||
bool EvaluateSyncRequest(HttpContext httpContext); | ||
} |
9 changes: 9 additions & 0 deletions
9
backend/src/Designer/Middleware/UserRequestSynchronization/IRequestSyncResolver.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,9 @@ | ||
using Altinn.Studio.Designer.Models; | ||
using Microsoft.AspNetCore.Http; | ||
|
||
namespace Altinn.Studio.Designer.Middleware.UserRequestSynchronization; | ||
|
||
public interface IRequestSyncResolver | ||
{ | ||
bool TryResolveSyncRequest(HttpContext httpContext, out AltinnRepoEditingContext editingContext); | ||
} |
43 changes: 43 additions & 0 deletions
43
...stSynchronization/RequestSyncEvaluatorImplementations/RepositoryEndpointsSyncEvaluator.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,43 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using Altinn.Studio.Designer.Controllers; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.AspNetCore.Mvc.Controllers; | ||
|
||
namespace Altinn.Studio.Designer.Middleware.UserRequestSynchronization.RequestSyncEvaluatorImplementations; | ||
|
||
public class RepositoryEndpointsSyncEvaluator : IRequestSyncEvaluator | ||
{ | ||
private readonly string _controllerName = nameof(RepositoryController).Replace("Controller", string.Empty); | ||
private readonly List<string> _actionNamesWhiteList = | ||
[ | ||
nameof(RepositoryController.RepoStatus), | ||
nameof(RepositoryController.RepoDiff), | ||
nameof(RepositoryController.Pull), | ||
nameof(RepositoryController.ResetLocalRepository), | ||
nameof(RepositoryController.CommitAndPushRepo), | ||
nameof(RepositoryController.Commit), | ||
nameof(RepositoryController.Push) | ||
]; | ||
|
||
public bool EvaluateSyncRequest(HttpContext httpContext) | ||
{ | ||
var endpoint = httpContext.GetEndpoint(); | ||
|
||
var controllerActionDescriptor = endpoint?.Metadata.GetMetadata<ControllerActionDescriptor>(); | ||
|
||
if (controllerActionDescriptor == null) | ||
{ | ||
return false; | ||
} | ||
|
||
string controllerName = controllerActionDescriptor.ControllerName; | ||
string actionName = controllerActionDescriptor.ActionName; | ||
if (controllerName.Equals(_controllerName, StringComparison.OrdinalIgnoreCase) && _actionNamesWhiteList.Contains(actionName)) | ||
{ | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
backend/src/Designer/Middleware/UserRequestSynchronization/RequestSyncExtensions.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,16 @@ | ||
using Altinn.Studio.Designer.Configuration.Extensions; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace Altinn.Studio.Designer.Middleware.UserRequestSynchronization; | ||
|
||
public static class RequestSyncExtensions | ||
{ | ||
public static IServiceCollection RegisterSynchronizationServices(this IServiceCollection services) | ||
{ | ||
services.AddTransient<IRequestSyncResolver, RequestSyncResolver>(); | ||
services.AddTransient<IEditingContextResolver, EditingContextResolver>(); | ||
services.RegisterTransientServicesByBaseType<IRequestSyncEvaluator>(); | ||
return services; | ||
} | ||
|
||
} |
28 changes: 28 additions & 0 deletions
28
backend/src/Designer/Middleware/UserRequestSynchronization/RequestSyncResolver.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,28 @@ | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using Altinn.Studio.Designer.Models; | ||
using Microsoft.AspNetCore.Http; | ||
|
||
namespace Altinn.Studio.Designer.Middleware.UserRequestSynchronization; | ||
|
||
public class RequestSyncResolver : IRequestSyncResolver | ||
{ | ||
IEnumerable<IRequestSyncEvaluator> _requestSyncEvaluators; | ||
IEditingContextResolver _editingContextResolver; | ||
|
||
public RequestSyncResolver(IEnumerable<IRequestSyncEvaluator> requestSyncEvaluators, IEditingContextResolver editingContextResolver) | ||
{ | ||
_requestSyncEvaluators = requestSyncEvaluators; | ||
_editingContextResolver = editingContextResolver; | ||
} | ||
|
||
public bool TryResolveSyncRequest(HttpContext httpContext, out AltinnRepoEditingContext editingContext) | ||
{ | ||
if (!_editingContextResolver.TryResolveContext(httpContext, out editingContext)) | ||
{ | ||
return false; | ||
} | ||
|
||
return _requestSyncEvaluators.Any(e => e.EvaluateSyncRequest(httpContext)); | ||
} | ||
} |
Oops, something went wrong.