-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
195b54b
commit de5a011
Showing
22 changed files
with
864 additions
and
1 deletion.
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
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" /> | ||
</ItemGroup> | ||
|
||
</Project> |
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.