-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'upstream/dev' into main
- Loading branch information
Showing
18 changed files
with
399 additions
and
19 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
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 |
---|---|---|
@@ -1 +1 @@ | ||
3.0.1 | ||
3.1.0 |
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
18 changes: 18 additions & 0 deletions
18
src/Moryx.ClientFramework.Kernel/Extensions/ApplicationRuntimeExtensions.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,18 @@ | ||
using Moryx.Identity; | ||
|
||
namespace Moryx.ClientFramework.Kernel | ||
{ | ||
/// <summary> | ||
/// Extensions for the <see cref="HeartOfLead"/> | ||
/// </summary> | ||
public static class ApplicationRuntimeExtensions | ||
{ | ||
/// <summary> | ||
/// Method to register a custom ClaimsAuthorizationManager | ||
/// </summary> | ||
public static void EnableAuthorization(this IApplicationRuntime hol, IAuthorizationContext authorizationContext) | ||
{ | ||
IdentityConfiguration.CurrentContext = authorizationContext; | ||
} | ||
} | ||
} |
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
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
25 changes: 25 additions & 0 deletions
25
src/Moryx.ClientFramework/Principals/BooleanPermissionExtension.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,25 @@ | ||
// Copyright (c) 2021, Phoenix Contact GmbH & Co. KG | ||
// Licensed under the Apache License, Version 2.0 | ||
|
||
using System.Windows.Markup; | ||
|
||
namespace Moryx.ClientFramework.Principals | ||
{ | ||
/// <summary> | ||
/// Extension to determine the boolean result depends to the permission | ||
/// </summary> | ||
public class BooleanPermissionExtension : PermissionExtensionBase | ||
{ | ||
/// <summary> | ||
/// Flag to inverse the boolean result | ||
/// </summary> | ||
[ConstructorArgument("Inverse")] | ||
public bool Inverse { get; set; } | ||
|
||
/// <inheritdoc /> | ||
protected override object ProvidePermissionBasedValue(bool hasPermission) | ||
{ | ||
return Inverse ? !hasPermission : hasPermission; | ||
} | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
src/Moryx.ClientFramework/Principals/ClaimsPrincipalSync.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,26 @@ | ||
// Copyright (c) 2021, Phoenix Contact GmbH & Co. KG | ||
// Licensed under the Apache License, Version 2.0 | ||
|
||
using System; | ||
|
||
namespace Moryx.ClientFramework.Principals | ||
{ | ||
/// <summary> | ||
/// Helper to inform the UI about an update of the ClaimsPrincipal | ||
/// </summary> | ||
public static class ClaimsPrincipalSync | ||
{ | ||
/// <summary> | ||
/// Event to get informed about an update of the ClaimsPrincipal | ||
/// </summary> | ||
public static event EventHandler PrincipalChanged; | ||
|
||
/// <summary> | ||
/// Method to invoke an event after an update of the ClaimsPrincipal | ||
/// </summary> | ||
public static void OnClaimsPrincipalChanged() | ||
{ | ||
PrincipalChanged?.Invoke(typeof(ClaimsPrincipalSync), EventArgs.Empty); | ||
} | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
src/Moryx.ClientFramework/Principals/GridLengthPermissionExtension.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,19 @@ | ||
// Copyright (c) 2021, Phoenix Contact GmbH & Co. KG | ||
// Licensed under the Apache License, Version 2.0 | ||
|
||
using System.Windows; | ||
|
||
namespace Moryx.ClientFramework.Principals | ||
{ | ||
/// <summary> | ||
/// Extension to determine the length of a grid depends to the permission | ||
/// </summary> | ||
public class GridLengthPermissionExtension : PermissionExtensionBase | ||
{ | ||
/// <inheritdoc /> | ||
protected override object ProvidePermissionBasedValue(bool hasPermission) | ||
{ | ||
return hasPermission ? new GridLength(1, GridUnitType.Star) : new GridLength(0); | ||
} | ||
} | ||
} |
112 changes: 112 additions & 0 deletions
112
src/Moryx.ClientFramework/Principals/PermissionExtensionBase.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,112 @@ | ||
// Copyright (c) 2021, Phoenix Contact GmbH & Co. KG | ||
// Licensed under the Apache License, Version 2.0 | ||
|
||
using System; | ||
using System.Reflection; | ||
using System.Text.RegularExpressions; | ||
using System.Windows; | ||
using System.Windows.Markup; | ||
using System.Xaml; | ||
using Moryx.Identity; | ||
|
||
namespace Moryx.ClientFramework.Principals | ||
{ | ||
/// <summary> | ||
/// Base class for permission based value determination | ||
/// </summary> | ||
public abstract class PermissionExtensionBase : MarkupExtension | ||
{ | ||
#region Fields and Properties | ||
|
||
private object _targetObject; | ||
|
||
private object _targetProperty; | ||
|
||
/// <summary> | ||
/// Resource within the action requires permissions | ||
/// </summary> | ||
public string Resource { get; set; } | ||
|
||
/// <summary> | ||
/// The requested action which will be validated by the current permissions | ||
/// </summary> | ||
[ConstructorArgument("action")] | ||
public string Action { get; set; } | ||
|
||
#endregion | ||
|
||
/// <summary> | ||
/// Constructor to prepare the extension to get information about changed principals | ||
/// </summary> | ||
protected PermissionExtensionBase() | ||
{ | ||
ClaimsPrincipalSync.PrincipalChanged += OnPrincipalChanged; | ||
} | ||
|
||
private void OnPrincipalChanged(object sender, EventArgs args) | ||
{ | ||
if (!(_targetObject is DependencyObject targetObject)) | ||
return; | ||
|
||
// Current determined value to update | ||
var value = ProvidePermissionBasedValue(HasPermission()); | ||
if (_targetProperty is DependencyProperty targetProperty) | ||
{ | ||
// Update directly if can be accessed otherwise invoke the dispatcher | ||
if (targetObject.CheckAccess()) | ||
targetObject.SetValue(targetProperty, value); | ||
else | ||
targetObject.Dispatcher.Invoke(() => targetObject.SetValue(targetProperty, value)); | ||
} | ||
else | ||
{ | ||
var propertyInfo = _targetProperty as PropertyInfo; | ||
propertyInfo?.SetValue(targetObject, value, null); | ||
} | ||
} | ||
|
||
/// <inheritdoc /> | ||
public override object ProvideValue(IServiceProvider serviceProvider) | ||
{ | ||
// If resource was not specified, tried to determine from host control | ||
if (string.IsNullOrEmpty(Resource) && serviceProvider.GetService(typeof(IRootObjectProvider)) is IRootObjectProvider root) | ||
{ | ||
// Try to read from root attached property | ||
var rootElement = root.RootObject as DependencyObject; | ||
var defaultResource = rootElement?.GetValue(PermissionProvider.DefaultResourceProperty); | ||
if (defaultResource != null) | ||
{ | ||
Resource = (string) defaultResource; | ||
} | ||
|
||
if (string.IsNullOrEmpty(Resource)) | ||
{ | ||
var regex = new Regex(@"^\w+\.\w+"); | ||
Resource = regex.Match(root.RootObject?.GetType().Namespace ?? "Moryx").Value; | ||
} | ||
} | ||
|
||
if (serviceProvider.GetService(typeof(IProvideValueTarget)) is IProvideValueTarget target) | ||
{ | ||
_targetObject = target.TargetObject; | ||
_targetProperty = target.TargetProperty; | ||
} | ||
|
||
var hasPermission = HasPermission(); | ||
return ProvidePermissionBasedValue(hasPermission); | ||
} | ||
|
||
private bool HasPermission() | ||
{ | ||
if (IdentityConfiguration.CurrentContext != null) | ||
return IdentityConfiguration.CurrentContext.CheckAccess(Resource, Action); | ||
|
||
return true; | ||
} | ||
|
||
/// <summary> | ||
/// Get the permission based value | ||
/// </summary> | ||
protected abstract object ProvidePermissionBasedValue(bool hasPermission); | ||
} | ||
} |
Oops, something went wrong.