Skip to content

Commit

Permalink
First example with the AuthenticationVisibility behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
Lakritzator committed Nov 17, 2016
1 parent f309212 commit 889bb09
Show file tree
Hide file tree
Showing 7 changed files with 262 additions and 12 deletions.
6 changes: 5 additions & 1 deletion Dapplo.CaliburnMicro.Demo.Addon/Views/AddonSettingsView.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:security="clr-namespace:Dapplo.CaliburnMicro.Security;assembly=Dapplo.CaliburnMicro"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Button x:Name="DoSomething" Content="Do something!" />
<StackPanel>
<Button x:Name="DoSomething" Content="Do something!" />
<Button x:Name="AdminButton" Content="Only visible when admin rights are available!" security:AuthenticationVisibility.Permission="Admin"/>
</StackPanel>
</UserControl>
1 change: 1 addition & 0 deletions Dapplo.CaliburnMicro.Demo/Dapplo.CaliburnMicro.Demo.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@
<Compile Include="Languages\ICredentialsTranslations.cs" />
<Compile Include="Models\IDemoConfiguration.cs" />
<Compile Include="Models\IWizardModel.cs" />
<Compile Include="Services\AuthenticationProvider.cs" />
<Compile Include="Services\ConfigureDefaults.cs" />
<Compile Include="Startup.cs" />
<Compile Include="UseCases\Configuration\ConfigIds.cs" />
Expand Down
48 changes: 48 additions & 0 deletions Dapplo.CaliburnMicro.Demo/Services/AuthenticationProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#region Dapplo 2016 - GNU Lesser General Public License

// Dapplo - building blocks for .NET applications
// Copyright (C) 2016 Dapplo
//
// For more information see: http://dapplo.net/
// Dapplo repositories are hosted on GitHub: https://github.com/dapplo
//
// This file is part of Dapplo.CaliburnMicro
//
// Dapplo.CaliburnMicro is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Dapplo.CaliburnMicro is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have a copy of the GNU Lesser General Public License
// along with Dapplo.CaliburnMicro. If not, see <http://www.gnu.org/licenses/lgpl.txt>.

#endregion

#region Usings

using System.Collections.Generic;
using System.ComponentModel.Composition;
using Dapplo.CaliburnMicro.Security;

#endregion

namespace Dapplo.CaliburnMicro.Demo.Services
{
/// <summary>
/// This exports a IAuthenticationProvider, which is used to show or hide elements in the UI depending on the available
/// rights
/// </summary>
[Export(typeof(IAuthenticationProvider))]
public class AuthenticationProvider : SimpleAuthenticationProvider
{
public AuthenticationProvider()
{
Permissions = new List<string> {"Developer"};
}
}
}
3 changes: 2 additions & 1 deletion Dapplo.CaliburnMicro/Dapplo.CaliburnMicro.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,8 @@
<Compile Include="Extensions\PropertyChangeExtensions.cs" />
<Compile Include="Extensions\TaskExtensions.cs" />
<Compile Include="Menu\MenuItemStyles.cs" />
<Compile Include="Security\AuthenticationProvider.cs" />
<Compile Include="Security\AuthenticationVisibility.cs" />
<Compile Include="Security\SimpleAuthenticationProvider.cs" />
<Compile Include="Security\IAuthenticationProvider.cs" />
<Compile Include="Translations\IConfigTranslations.cs" />
<Compile Include="Extensions\HaveDisplayNameExtensions.cs" />
Expand Down
177 changes: 177 additions & 0 deletions Dapplo.CaliburnMicro/Security/AuthenticationVisibility.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
#region Dapplo 2016 - GNU Lesser General Public License

// Dapplo - building blocks for .NET applications
// Copyright (C) 2016 Dapplo
//
// For more information see: http://dapplo.net/
// Dapplo repositories are hosted on GitHub: https://github.com/dapplo
//
// This file is part of Dapplo.CaliburnMicro
//
// Dapplo.CaliburnMicro is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Dapplo.CaliburnMicro is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have a copy of the GNU Lesser General Public License
// along with Dapplo.CaliburnMicro. If not, see <http://www.gnu.org/licenses/lgpl.txt>.

#endregion

#region Usings

using System.Windows;
using Dapplo.CaliburnMicro.Behaviors;

#endregion

namespace Dapplo.CaliburnMicro.Security
{
/// <summary>
/// Change the visibility of a UiElement depending on rights
/// </summary>
public static class AuthenticationVisibility
{
/// <summary>
/// The permission to check against
/// </summary>
public static readonly DependencyProperty PermissionProperty = DependencyProperty.RegisterAttached(
"Permission",
typeof(string),
typeof(AuthenticationVisibility),
new PropertyMetadata(OnArgumentsChanged));

/// <summary>
/// Visibility to use when the current user doesn't have the permission
/// Default is Visibility.Collapsed
/// </summary>
public static readonly DependencyProperty WhenPermissionMissingProperty = DependencyProperty.RegisterAttached(
"WhenPermissionMissing",
typeof(Visibility),
typeof(AuthenticationVisibility),
new PropertyMetadata(Visibility.Collapsed, OnArgumentsChanged));

/// <summary>
/// Visibility to use when the permission is available
/// Default is Visibility.Visible
/// </summary>
public static readonly DependencyProperty WhenPermissionProperty = DependencyProperty.RegisterAttached(
"WhenPermission",
typeof(Visibility),
typeof(AuthenticationVisibility),
new PropertyMetadata(Visibility.Visible, OnArgumentsChanged));

private static readonly AttachedBehavior Behavior = AttachedBehavior.Register(host => new AuthenticationVisibilityBehavior((UIElement) host));

/// <summary>
/// This handles the fact that a dependency property has changed on a DependencyObject.
/// It will also register the behavior when it hasn't been yet
/// </summary>
/// <param name="dependencyObject">DependencyObject</param>
/// <param name="dependencyPropertyChangedEventArgs">dependencyPropertyChangedEventArgs</param>
private static void OnArgumentsChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs)
{
Behavior.Update(dependencyObject);
}

/// <summary>
/// Returns the permission from the UIElement of the DependencyProperty specified in PermissionProperty.
/// </summary>
/// <param name="uiElement">UIElement</param>
/// <returns>string which represents the permission of the in PermissionProperty specified DependencyProperty</returns>
public static string GetPermission(UIElement uiElement)
{
return (string) uiElement.GetValue(PermissionProperty);
}

/// <summary>
/// Sets the permission of the UIElement to the DependencyProperty specified in PermissionProperty.
/// </summary>
/// <param name="uiElement">UIElement</param>
/// <param name="value">Permission to assign to the in PermissionProperty specified DependencyProperty</param>
public static void SetPermission(UIElement uiElement, string value)
{
uiElement.SetValue(PermissionProperty, value);
}

/// <summary>
/// Returns the visibility which is used when the user doesn't have the specified permission
/// </summary>
/// <param name="uiElement">UIElement</param>
/// <returns>Visibility</returns>
public static Visibility GetWhenPermissionMissing(UIElement uiElement)
{
return (Visibility) uiElement.GetValue(WhenPermissionMissingProperty);
}

/// <summary>
/// Sets the visibility which is used when the user doesn't have the specified permission
/// </summary>
/// <param name="uiElement">UIElement</param>
/// <param name="visibility">Visibility to use when matched</param>
public static void SetWhenPermissionMissing(UIElement uiElement, Visibility visibility)
{
uiElement.SetValue(WhenPermissionMissingProperty, visibility);
}

/// <summary>
/// Returns the visibility which is used when the user has the specified permission
/// </summary>
/// <param name="uiElement">UIElement</param>
/// <returns>Visibility</returns>
public static Visibility GetWhenPermission(UIElement uiElement)
{
return (Visibility) uiElement.GetValue(WhenPermissionProperty);
}

/// <summary>
/// Sets the visibility which is used when the user has the specified permission
/// </summary>
/// <param name="uiElement">UIElement</param>
/// <param name="visibility">Visibility to use when matched</param>
public static void SetWhenPermission(UIElement uiElement, Visibility visibility)
{
uiElement.SetValue(WhenPermissionProperty, visibility);
}

/// <summary>
/// Implementation of the AuthenticationVisibilityBehavior
/// </summary>
private sealed class AuthenticationVisibilityBehavior : Behavior<UIElement>
{
internal AuthenticationVisibilityBehavior(UIElement host) : base(host)
{
}

/// <summary>
/// check if the supplied permission is available
/// </summary>
/// <param name="permission"></param>
/// <returns>true if the permission is available</returns>
private bool HasPermission(string permission)
{
// Null permission is aways there
if (permission == null)
{
return true;
}
var authenticationProvider = Dapplication.Current.Bootstrapper.GetExport<IAuthenticationProvider>();
return authenticationProvider?.Value.HasPermission(permission) ?? false;
}

protected override void Update(UIElement host)
{
var permission = GetPermission(host);

host.Visibility = HasPermission(permission)
? GetWhenPermission(host)
: GetWhenPermissionMissing(host);
}
}
}
}
35 changes: 27 additions & 8 deletions Dapplo.CaliburnMicro/Security/IAuthenticationProvider.cs
Original file line number Diff line number Diff line change
@@ -1,21 +1,40 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
#region Dapplo 2016 - GNU Lesser General Public License

// Dapplo - building blocks for .NET applications
// Copyright (C) 2016 Dapplo
//
// For more information see: http://dapplo.net/
// Dapplo repositories are hosted on GitHub: https://github.com/dapplo
//
// This file is part of Dapplo.CaliburnMicro
//
// Dapplo.CaliburnMicro is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Dapplo.CaliburnMicro is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have a copy of the GNU Lesser General Public License
// along with Dapplo.CaliburnMicro. If not, see <http://www.gnu.org/licenses/lgpl.txt>.

#endregion

namespace Dapplo.CaliburnMicro.Security
{
/// <summary>
/// Interface which all authentication providers must implement
/// Interface which all authentication providers must implement
/// </summary>
public interface IAuthenticationProvider
{
/// <summary>
/// Returns if the current user has a certain permission
/// Returns if the current user has a certain permission
/// </summary>
/// <param name="permission">string with the name of the permission</param>
/// <returns>true if the current user has the specified permission</returns>
bool HasPermission(string permission);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@
namespace Dapplo.CaliburnMicro.Security
{
/// <summary>
/// A simple implementation of the IAuthenticationProvider
/// A simple implementation of the IAuthenticationProvider, can be used as a base for your own implementation
/// </summary>
public class SimpleAuthenticationProvider : IAuthenticationProvider
{
/// <summary>
/// List of available permissions
/// </summary>
public IList<string> Permissions { get; set; } = new List<string>();
protected IList<string> Permissions { get; set; }

/// <inheritdoc />
public bool HasPermission(string permission)
Expand Down

0 comments on commit 889bb09

Please sign in to comment.