Merge pull request #58 from oqtane/master

sync
This commit is contained in:
Shaun Walker 2020-10-20 09:05:32 -04:00 committed by GitHub
commit fc7000394f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 94 additions and 48 deletions

View File

@ -1,5 +1,5 @@
@namespace Oqtane.Modules.Controls
@inherits ModuleControlBase
@namespace Oqtane.Modules.Controls
@inherits LocalizableComponent
@inject IUserService UserService
@if (_authorized)
@ -56,6 +56,8 @@
protected override void OnParametersSet()
{
base.OnParametersSet();
_text = Action;
if (!string.IsNullOrEmpty(Text))
{
@ -93,6 +95,7 @@
}
_text = Localize(nameof(Text));
_url = EditUrl(Action, _parameters);
_authorized = IsAuthorized();
}

View File

@ -1,6 +1,5 @@
@namespace Oqtane.Modules.Controls
@inherits ModuleControlBase
@using Microsoft.Extensions.Localization
@namespace Oqtane.Modules.Controls
@inherits LocalizableComponent
@if (!string.IsNullOrEmpty(HelpText))
{
@ -27,11 +26,10 @@ else
[Parameter]
public string HelpText { get; set; } // optional - tooltip for this label
[Parameter]
public string ResourceKey { get; set; }
protected override void OnParametersSet()
{
base.OnParametersSet();
_openLabel = "<label";
if (!string.IsNullOrEmpty(For))
{
@ -45,23 +43,10 @@ else
_openLabel += ">";
if (!string.IsNullOrEmpty(ResourceKey))
if (IsLocalizable)
{
if (ModuleState?.ModuleType != null)
{
var moduleType = Type.GetType(ModuleState.ModuleType);
var localizerTypeName = $"Microsoft.Extensions.Localization.IStringLocalizer`1[[{moduleType.AssemblyQualifiedName}]], Microsoft.Extensions.Localization.Abstractions";
var localizerType = Type.GetType(localizerTypeName);
// HACK: Use ServiceActivator instead of injecting IHttpContextAccessor, because HttpContext throws NRE in WebAssembly runtime
using (var scope = ServiceActivator.GetScope())
{
var localizer = (IStringLocalizer)scope.ServiceProvider.GetService(localizerType);
ChildContent = @<text>@localizer[$"{ResourceKey}.Text"]</text>;
HelpText = localizer[$"{ResourceKey}.{nameof(HelpText)}"];
}
}
ChildContent =@<text>@Localize("Text")</text>;
HelpText = Localize(nameof(HelpText));
}
}
}

View File

@ -0,0 +1,54 @@
using System;
using Microsoft.AspNetCore.Components;
using Microsoft.Extensions.Localization;
using Oqtane.Shared;
namespace Oqtane.Modules.Controls
{
public class LocalizableComponent : ModuleControlBase
{
private IStringLocalizer _localizer;
[Parameter]
public string ResourceKey { get; set; }
protected bool IsLocalizable { get; private set; }
protected string Localize(string name)
{
if (!IsLocalizable)
{
return null;
}
var key = $"{ResourceKey}.{name}";
return _localizer?[key] ?? key;
}
protected override void OnParametersSet()
{
if (!String.IsNullOrEmpty(ResourceKey))
{
if (ModuleState?.ModuleType != null)
{
var moduleType = Type.GetType(ModuleState.ModuleType);
var localizerTypeName = $"Microsoft.Extensions.Localization.IStringLocalizer`1[[{moduleType.AssemblyQualifiedName}]], Microsoft.Extensions.Localization.Abstractions";
var localizerType = Type.GetType(localizerTypeName);
// HACK: Use ServiceActivator instead of injecting IHttpContextAccessor, because HttpContext throws NRE in WebAssembly runtime
using (var scope = ServiceActivator.GetScope())
{
_localizer = (IStringLocalizer)scope.ServiceProvider.GetService(localizerType);
}
}
IsLocalizable = true;
}
else
{
IsLocalizable = false;
}
}
}
}

View File

@ -3,7 +3,6 @@
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<OutputType>Exe</OutputType>
<LangVersion>7.3</LangVersion>
<RazorLangVersion>3.0</RazorLangVersion>
<Configurations>Debug;Release</Configurations>
<Version>1.0.4</Version>

View File

@ -45,7 +45,7 @@ namespace Oqtane
_supportedCultures = localizationManager.GetSupportedCultures();
_runtime = (Configuration.GetSection("Runtime").Value == "WebAssembly") ? Runtime.WebAssembly : Runtime.Server;
//add possibility to switch off swagger on production.
_useSwagger = Configuration.GetSection("UseSwagger").Value != "false";
@ -92,13 +92,13 @@ namespace Oqtane
// register custom authorization policies
services.AddAuthorizationCore(options =>
{
options.AddPolicy("ViewPage", policy => policy.Requirements.Add(new PermissionRequirement(EntityNames.Page, PermissionNames.View)));
options.AddPolicy("EditPage", policy => policy.Requirements.Add(new PermissionRequirement(EntityNames.Page, PermissionNames.Edit)));
options.AddPolicy("ViewModule", policy => policy.Requirements.Add(new PermissionRequirement(EntityNames.Module, PermissionNames.View)));
options.AddPolicy("EditModule", policy => policy.Requirements.Add(new PermissionRequirement(EntityNames.Module, PermissionNames.Edit)));
options.AddPolicy("ViewFolder", policy => policy.Requirements.Add(new PermissionRequirement(EntityNames.Folder, PermissionNames.View)));
options.AddPolicy("EditFolder", policy => policy.Requirements.Add(new PermissionRequirement(EntityNames.Folder, PermissionNames.Edit)));
options.AddPolicy("ListFolder", policy => policy.Requirements.Add(new PermissionRequirement(EntityNames.Folder, PermissionNames.Browse)));
options.AddPolicy(PolicyNames.ViewPage, policy => policy.Requirements.Add(new PermissionRequirement(EntityNames.Page, PermissionNames.View)));
options.AddPolicy(PolicyNames.EditPage, policy => policy.Requirements.Add(new PermissionRequirement(EntityNames.Page, PermissionNames.Edit)));
options.AddPolicy(PolicyNames.ViewModule, policy => policy.Requirements.Add(new PermissionRequirement(EntityNames.Module, PermissionNames.View)));
options.AddPolicy(PolicyNames.EditModule, policy => policy.Requirements.Add(new PermissionRequirement(EntityNames.Module, PermissionNames.Edit)));
options.AddPolicy(PolicyNames.ViewFolder, policy => policy.Requirements.Add(new PermissionRequirement(EntityNames.Folder, PermissionNames.View)));
options.AddPolicy(PolicyNames.EditFolder, policy => policy.Requirements.Add(new PermissionRequirement(EntityNames.Folder, PermissionNames.Edit)));
options.AddPolicy(PolicyNames.ListFolder, policy => policy.Requirements.Add(new PermissionRequirement(EntityNames.Folder, PermissionNames.Browse)));
});
// register scoped core services
@ -133,7 +133,7 @@ namespace Oqtane
services.AddDbContext<MasterDBContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")
.Replace("|DataDirectory|", AppDomain.CurrentDomain.GetData("DataDirectory")?.ToString())
.Replace("|DataDirectory|", AppContext.GetData("DataDirectory")?.ToString())
));
services.AddDbContext<TenantDBContext>(options => { });

View File

@ -10,7 +10,7 @@ using [Owner].[Module].Repository;
namespace [Owner].[Module].Controllers
{
[Route("{alias}/api/[controller]")]
[Route(ControllerRoutes.Default)]
public class [Module]Controller : Controller
{
private readonly I[Module]Repository _[Module]Repository;
@ -30,7 +30,7 @@ namespace [Owner].[Module].Controllers
// GET: api/<controller>?moduleid=x
[HttpGet]
[Authorize(Policy = "ViewModule")]
[Authorize(Policy = PolicyNames.ViewModule)]
public IEnumerable<Models.[Module]> Get(string moduleid)
{
return _[Module]Repository.Get[Module]s(int.Parse(moduleid));
@ -38,7 +38,7 @@ namespace [Owner].[Module].Controllers
// GET api/<controller>/5
[HttpGet("{id}")]
[Authorize(Policy = "ViewModule")]
[Authorize(Policy = PolicyNames.ViewModule)]
public Models.[Module] Get(int id)
{
Models.[Module] [Module] = _[Module]Repository.Get[Module](id);
@ -51,7 +51,7 @@ namespace [Owner].[Module].Controllers
// POST api/<controller>
[HttpPost]
[Authorize(Policy = "EditModule")]
[Authorize(Policy = PolicyNames.EditModule)]
public Models.[Module] Post([FromBody] Models.[Module] [Module])
{
if (ModelState.IsValid && [Module].ModuleId == _entityId)
@ -64,7 +64,7 @@ namespace [Owner].[Module].Controllers
// PUT api/<controller>/5
[HttpPut("{id}")]
[Authorize(Policy = "EditModule")]
[Authorize(Policy = PolicyNames.EditModule)]
public Models.[Module] Put(int id, [FromBody] Models.[Module] [Module])
{
if (ModelState.IsValid && [Module].ModuleId == _entityId)
@ -77,7 +77,7 @@ namespace [Owner].[Module].Controllers
// DELETE api/<controller>/5
[HttpDelete("{id}")]
[Authorize(Policy = "EditModule")]
[Authorize(Policy = PolicyNames.EditModule)]
public void Delete(int id)
{
Models.[Module] [Module] = _[Module]Repository.Get[Module](id);

View File

@ -20,7 +20,7 @@ GO
/*
Create foreign key relationships
*/
ALTER TABLE [dbo].[[Owner][Module]] WITH CHECK ADD CONSTRAINT [FK_[Owner][Module]_Module] FOREIGN KEY([ModuleId])
ALTER TABLE [dbo].[[Owner][Module]] WITH CHECK ADD CONSTRAINT [FK_[Owner][Module]_Module] FOREIGN KEY([ModuleId])
REFERENCES [dbo].Module ([ModuleId])
ON DELETE CASCADE
GO

View File

@ -2,7 +2,6 @@
<PropertyGroup>
<TargetFramework>netstandard2.1</TargetFramework>
<LangVersion>7.3</LangVersion>
<Version>1.0.0</Version>
<Product>[Owner].[Module]</Product>
<Authors>[Owner]</Authors>

View File

@ -2,7 +2,6 @@
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<LangVersion>7.3</LangVersion>
<Configurations>Debug;Release</Configurations>
<Version>1.0.4</Version>
<Product>Oqtane</Product>

View File

@ -1,10 +1,19 @@
using System;
using System;
using System.Collections.Generic;
using System.Text;
namespace Oqtane.Shared {
public class PolicyNames {
namespace Oqtane.Shared
{
public class PolicyNames
{
public const string ViewPage = "ViewPage";
public const string EditPage = "EditPage";
public const string ViewModule = "ViewModule";
public const string EditModule = "EditModule";
public const string ViewFolder = "ViewFolder";
public const string EditFolder = "EditFolder";
public const string ListFolder = "ListFolder";
}
}

View File

@ -2,7 +2,6 @@
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<LangVersion>7.3</LangVersion>
<Configurations>Debug;Release</Configurations>
<Version>1.0.4</Version>
<Product>Oqtane</Product>

View File

@ -2,7 +2,6 @@
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<LangVersion>7.3</LangVersion>
<OutputType>Exe</OutputType>
<Version>1.0.4</Version>
<Product>Oqtane</Product>