-
Notifications
You must be signed in to change notification settings - Fork 2.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
OrchardCore.Cors #2682
Merged
Merged
OrchardCore.Cors #2682
Changes from 19 commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
3521303
Merge pull request #2 from OrchardCMS/dev
Tsjerno ea3ef90
Merge branch 'dev' of https://github.com/MatthijsKrempel/OrchardCore …
a6f88c1
Initial commit of Cors
fe8a3cb
Used Json.Serialize instead of JsonConvert.SerializeObject
42b35ed
Updated serializer with settings so to escape HTML XSS
9887599
Merged from upstream
4d385b3
Processed code review comments
101e946
Merge remote-tracking branch 'upstream/dev' into dev
bb2b638
Merge remote-tracking branch 'upstream/dev' into feature/cors
2fdfec6
Merge remote-tracking branch 'upstream/dev' into feature/cors
64ead01
Merge branch 'dev' into feature/cors
88463d0
Merge remote-tracking branch 'upstream/dev' into feature/cors
d17beaf
Reworked UI
49396e3
Fixed bug when no policies are supplied
cec215a
Added support for default policy selection
a71d387
Merge branch 'dev' into feature/cors
agriffard 3a52d99
Added T[""] around the buttons
256aa9b
Merge remote-tracking branch 'origin/dev' into feature/cors
dd1096d
Code review comments
ce1dc1e
Code review comments
b835ed5
Added documentation
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
using Microsoft.Extensions.Localization; | ||
using OrchardCore.Navigation; | ||
|
||
namespace OrchardCore.Cors | ||
{ | ||
public class AdminMenu : INavigationProvider | ||
{ | ||
public AdminMenu(IStringLocalizer<AdminMenu> localizer) | ||
{ | ||
T = localizer; | ||
} | ||
|
||
public IStringLocalizer T { get; set; } | ||
|
||
public Task BuildNavigationAsync(string name, NavigationBuilder builder) | ||
{ | ||
if (!String.Equals(name, "admin", StringComparison.OrdinalIgnoreCase)) | ||
{ | ||
return Task.CompletedTask; | ||
} | ||
|
||
builder | ||
.Add(T["Configuration"], configuration => configuration | ||
.Add(T["Settings"], settings => settings | ||
.Add(T["Cors"], "100", entry => entry | ||
.Action("Index", "Admin", new { area = "OrchardCore.Cors" }) | ||
.Permission(Permissions.ManageCorsSettings) | ||
.LocalNav() | ||
)) | ||
); | ||
|
||
return Task.CompletedTask; | ||
} | ||
} | ||
} |
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 @@ | ||
[ | ||
{ | ||
"inputs": [ | ||
"Assets/Admin/cors-admin.js" | ||
], | ||
"output": "wwwroot/Scripts/cors-admin.js" | ||
} | ||
] |
83 changes: 83 additions & 0 deletions
83
src/OrchardCore.Modules/OrchardCore.Cors/Assets/Admin/cors-admin.js
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,83 @@ | ||
var optionsList = Vue.component('options-list', | ||
{ | ||
props: ['options', 'optionType', 'title', 'subTitle'], | ||
template: '#options-list', | ||
data: function () { | ||
return { | ||
'newOption': '' | ||
}; | ||
}, | ||
methods: { | ||
addOption: function (value) { | ||
this.options.push(value); | ||
}, | ||
deleteOption: function (value) { | ||
this.options.splice($.inArray(value[0], this.options), 1); | ||
} | ||
} | ||
}); | ||
|
||
var policyDetails = Vue.component('policy-details', | ||
{ | ||
components: { optionsList : optionsList }, | ||
props: ['policy'], | ||
template: '#policy-details' | ||
}); | ||
|
||
var corsApp = new Vue({ | ||
el: '#corsAdmin', | ||
components: { policyDetails : policyDetails, optionsList : optionsList }, | ||
data: { | ||
selectedPolicy: null, | ||
policies: null, | ||
defaultPolicyName: null | ||
}, | ||
methods: { | ||
newPolicy: function () { | ||
this.selectedPolicy = { | ||
Name: 'New policy', | ||
AllowedOrigins: [], | ||
AllowAnyOrigin: true, | ||
AllowedMethods: [], | ||
AllowAnyMethod: true, | ||
AllowedHeaders: [], | ||
AllowAnyHeader: true, | ||
AllowCredentials: true, | ||
IsDefaultPolicy: false | ||
}; | ||
}, | ||
editPolicy: function (policy) { | ||
this.selectedPolicy = Object.assign({}, policy); | ||
this.selectedPolicy.OriginalName = this.selectedPolicy.Name; | ||
}, | ||
deletePolicy: function (policy, event) { | ||
this.selectedPolicy = null; | ||
var policyToRemove = this.policies.filter(function (item) { return item.Name === policy.Name; }); | ||
if (policyToRemove.length > 0) | ||
this.policies.splice($.inArray(policyToRemove[0], this.policies), 1); | ||
event.stopPropagation(); | ||
this.save(); | ||
}, | ||
updatePolicy: function (policy, event) { | ||
if (policy.IsDefaultPolicy) { | ||
this.policies.forEach(p => p.IsDefaultPolicy = false); | ||
} | ||
if (policy.OriginalName) { | ||
var policyIndex = this.policies.findIndex((oldPolicy) => oldPolicy.Name === policy.OriginalName); | ||
this.policies[policyIndex] = policy; | ||
} | ||
else { | ||
this.policies.push(policy); | ||
} | ||
this.save(); | ||
this.back(); | ||
}, | ||
save: function () { | ||
document.getElementById('CorsSettings').value = JSON.stringify(this.policies); | ||
document.getElementById('corsForm').submit(); | ||
}, | ||
back: function () { | ||
this.selectedPolicy = null; | ||
} | ||
} | ||
}); |
127 changes: 127 additions & 0 deletions
127
src/OrchardCore.Modules/OrchardCore.Cors/Controllers/AdminController.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,127 @@ | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Authorization; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.AspNetCore.Mvc.Localization; | ||
using Microsoft.Extensions.Localization; | ||
using Newtonsoft.Json; | ||
using OrchardCore.Admin; | ||
using OrchardCore.Cors.Services; | ||
using OrchardCore.Cors.Settings; | ||
using OrchardCore.Cors.ViewModels; | ||
using OrchardCore.DisplayManagement.Notify; | ||
|
||
namespace OrchardCore.Cors.Controllers | ||
{ | ||
[Admin] | ||
public class AdminController : Controller | ||
{ | ||
private readonly IAuthorizationService _authorizationService; | ||
private readonly CorsService _corsService; | ||
private readonly INotifier _notifier; | ||
|
||
private readonly IStringLocalizer T; | ||
private readonly IHtmlLocalizer<AdminController> TH; | ||
|
||
public AdminController( | ||
IAuthorizationService authorizationService, | ||
IStringLocalizer<AdminController> stringLocalizer, | ||
IHtmlLocalizer<AdminController> htmlLocalizer, | ||
CorsService corsService, | ||
INotifier notifier | ||
) | ||
{ | ||
TH = htmlLocalizer; | ||
_notifier = notifier; | ||
_corsService = corsService; | ||
T = stringLocalizer; | ||
_authorizationService = authorizationService; | ||
} | ||
|
||
[HttpGet] | ||
public async Task<ActionResult> Index() | ||
{ | ||
if (!await _authorizationService.AuthorizeAsync(User, Permissions.ManageCorsSettings)) | ||
{ | ||
return Unauthorized(); | ||
} | ||
|
||
var settings = await _corsService.GetSettingsAsync(); | ||
|
||
var list = new List<CorsPolicyViewModel>(); | ||
|
||
if (settings?.Policies != null) | ||
{ | ||
foreach (var policySetting in settings.Policies) | ||
{ | ||
var policyViewModel = new CorsPolicyViewModel() | ||
{ | ||
Name = policySetting.Name, | ||
AllowAnyHeader = policySetting.AllowAnyHeader, | ||
AllowedHeaders = policySetting.AllowedHeaders, | ||
AllowAnyMethod = policySetting.AllowAnyMethod, | ||
AllowedMethods = policySetting.AllowedMethods, | ||
AllowAnyOrigin = policySetting.AllowAnyOrigin, | ||
AllowedOrigins = policySetting.AllowedOrigins, | ||
AllowCredentials = policySetting.AllowCredentials, | ||
IsDefaultPolicy = policySetting.IsDefaultPolicy | ||
}; | ||
|
||
list.Add(policyViewModel); | ||
} | ||
} | ||
|
||
var viewModel = new CorsSettingsViewModel | ||
{ | ||
Policies = list.ToArray() | ||
}; | ||
|
||
return View(viewModel); | ||
} | ||
|
||
[HttpPost] | ||
[ActionName(nameof(Index))] | ||
public async Task<ActionResult> IndexPOST() | ||
{ | ||
if (!await _authorizationService.AuthorizeAsync(User, Permissions.ManageCorsSettings)) | ||
{ | ||
return Unauthorized(); | ||
} | ||
|
||
var model = new CorsSettingsViewModel(); | ||
var configJson = Request.Form["CorsSettings"].First(); | ||
model.Policies = JsonConvert.DeserializeObject<CorsPolicyViewModel[]>(configJson); | ||
|
||
var corsPolicies = new List<CorsPolicySetting>(); | ||
|
||
foreach (var settingViewModel in model.Policies) | ||
{ | ||
corsPolicies.Add(new CorsPolicySetting | ||
{ | ||
Name = settingViewModel.Name, | ||
AllowAnyHeader = settingViewModel.AllowAnyHeader, | ||
AllowAnyMethod = settingViewModel.AllowAnyMethod, | ||
AllowAnyOrigin = settingViewModel.AllowAnyOrigin, | ||
AllowCredentials = settingViewModel.AllowCredentials, | ||
AllowedHeaders = settingViewModel.AllowedHeaders, | ||
AllowedMethods = settingViewModel.AllowedMethods, | ||
AllowedOrigins = settingViewModel.AllowedOrigins, | ||
IsDefaultPolicy =settingViewModel.IsDefaultPolicy | ||
|
||
}); | ||
} | ||
|
||
var corsSettings = new CorsSettings() | ||
{ | ||
Policies = corsPolicies | ||
}; | ||
|
||
await _corsService.UpdateSettingsAsync(corsSettings); | ||
|
||
_notifier.Success(TH["Cors settings are updated"]); | ||
|
||
return View(model); | ||
} | ||
} | ||
} |
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 OrchardCore.Modules.Manifest; | ||
|
||
[assembly: Module( | ||
Name = "Cors Configuration", | ||
Author = "The Orchard Team", | ||
Website = "http://orchardproject.net", | ||
Version = "2.0.0", | ||
Description = "Enables configuration of Cors settings", | ||
Category = "Security")] |
20 changes: 20 additions & 0 deletions
20
src/OrchardCore.Modules/OrchardCore.Cors/OrchardCore.Cors.csproj
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,20 @@ | ||
<Project Sdk="Microsoft.NET.Sdk.Razor"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>$(AspNetCoreTargetFramework)</TargetFramework> | ||
<AddRazorSupportForMvc>true</AddRazorSupportForMvc> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<FrameworkReference Include="Microsoft.AspNetCore.App" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\OrchardCore\OrchardCore.Module.Targets\OrchardCore.Module.Targets.csproj" /> | ||
<ProjectReference Include="..\..\OrchardCore\OrchardCore.Navigation.Core\OrchardCore.Navigation.Core.csproj" /> | ||
<ProjectReference Include="..\..\OrchardCore\OrchardCore.Admin.Abstractions\OrchardCore.Admin.Abstractions.csproj" /> | ||
<ProjectReference Include="..\..\OrchardCore\OrchardCore.DisplayManagement\OrchardCore.DisplayManagement.csproj" /> | ||
<ProjectReference Include="..\..\OrchardCore\OrchardCore.Users.Core\OrchardCore.Users.Core.csproj" /> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing reference to |
||
</ItemGroup> | ||
|
||
</Project> | ||
MatthijsKrempel marked this conversation as resolved.
Show resolved
Hide resolved
|
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,32 @@ | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using OrchardCore.Security.Permissions; | ||
|
||
namespace OrchardCore.Cors | ||
{ | ||
public class Permissions : IPermissionProvider | ||
{ | ||
public static readonly Permission ManageCorsSettings = new Permission("ManageCorsSettings", "Managing Cors Settings", isSecurityCritical: true); | ||
|
||
public Task<IEnumerable<Permission>> GetPermissionsAsync() | ||
{ | ||
return Task.FromResult(new[] | ||
{ | ||
ManageCorsSettings | ||
} | ||
.AsEnumerable()); | ||
} | ||
|
||
public IEnumerable<PermissionStereotype> GetDefaultStereotypes() | ||
{ | ||
return new[] { | ||
new PermissionStereotype { | ||
Name = "Administrator", | ||
Permissions = new[] { ManageCorsSettings } | ||
}, | ||
}; | ||
} | ||
|
||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nah no one notice this ;)