Skip to content

Commit

Permalink
Continuing work on the behaviors, first working results with the Auth…
Browse files Browse the repository at this point in the history
…enticationProvider
  • Loading branch information
Lakritzator committed Nov 17, 2016
1 parent 889bb09 commit 08f2064
Show file tree
Hide file tree
Showing 29 changed files with 1,552 additions and 111 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@
<HintPath>..\packages\Caliburn.Micro.3.0.1\lib\net45\Caliburn.Micro.Platform.Core.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Dapplo.Addons, Version=0.3.88.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Dapplo.Addons.0.3.88\lib\net45\Dapplo.Addons.dll</HintPath>
<Reference Include="Dapplo.Addons, Version=0.3.89.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Dapplo.Addons.0.3.89\lib\net45\Dapplo.Addons.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Dapplo.Config, Version=0.4.2.0, Culture=neutral, processorArchitecture=MSIL">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,12 @@

using System.ComponentModel.Composition;
using Caliburn.Micro;
using Dapplo.CaliburnMicro.Behaviors.Security;
using Dapplo.CaliburnMicro.Configuration;
using Dapplo.CaliburnMicro.Demo.Addon.Languages;
using Dapplo.CaliburnMicro.Demo.UseCases.Configuration;
using Dapplo.CaliburnMicro.Extensions;
using Dapplo.CaliburnMicro.Security;

#endregion

Expand All @@ -45,6 +47,10 @@ public sealed class AddonSettingsViewModel : ConfigScreen, IPartImportsSatisfied
[Import]
private IEventAggregator EventAggregator { get; set; }


[Import]
private IAuthenticationProvider AuthenticationProvider { get; set; }

public AddonSettingsViewModel()
{
ParentId = nameof(ConfigIds.Addons);
Expand All @@ -59,5 +65,18 @@ public void DoSomething()
{
EventAggregator.PublishOnUIThread("Addon button clicked");
}

public void AddAdmin()
{
var authenticationProvider = AuthenticationProvider as SimpleAuthenticationProvider;
authenticationProvider?.AddPermission("Admin");

}
public void RemoveAdmin()
{
var authenticationProvider = AuthenticationProvider as SimpleAuthenticationProvider;
authenticationProvider?.RemovePermission("Admin");

}
}
}
6 changes: 4 additions & 2 deletions Dapplo.CaliburnMicro.Demo.Addon/Views/AddonSettingsView.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
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"
xmlns:security1="clr-namespace:Dapplo.CaliburnMicro.Behaviors.Security;assembly=Dapplo.CaliburnMicro"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<StackPanel>
<Button x:Name="DoSomething" Content="Do something!" />
<Button x:Name="AdminButton" Content="Only visible when admin rights are available!" security:AuthenticationVisibility.Permission="Admin"/>
<Button x:Name="AddAdmin" Content="Add admin permission" security1:AuthenticationEnabled.Permission="Admin" security1:AuthenticationEnabled.WhenPermissionMissing="true" security1:AuthenticationEnabled.WhenPermission="false"/>
<Button x:Name="RemoveAdmin" Content="Remove admin permission" security1:AuthenticationEnabled.Permission="Admin" />
<Label Content="You have the 'Admin' permission" security1:AuthenticationVisibility.Permission="Admin" />
</StackPanel>
</UserControl>
2 changes: 1 addition & 1 deletion Dapplo.CaliburnMicro.Demo.Addon/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<packages>
<package id="Caliburn.Micro" version="3.0.1" targetFramework="net452" />
<package id="Caliburn.Micro.Core" version="3.0.1" targetFramework="net452" />
<package id="Dapplo.Addons" version="0.3.88" targetFramework="net452" />
<package id="Dapplo.Addons" version="0.3.89" targetFramework="net452" />
<package id="Dapplo.Config" version="0.4.2" targetFramework="net452" />
<package id="Dapplo.InterfaceImpl" version="0.2.5" targetFramework="net452" />
<package id="Dapplo.Log" version="1.0.22" targetFramework="net452" />
Expand Down
4 changes: 2 additions & 2 deletions Dapplo.CaliburnMicro.Demo/Dapplo.CaliburnMicro.Demo.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@
<HintPath>..\packages\Caliburn.Micro.3.0.1\lib\net45\Caliburn.Micro.Platform.Core.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Dapplo.Addons, Version=0.3.88.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Dapplo.Addons.0.3.88\lib\net45\Dapplo.Addons.dll</HintPath>
<Reference Include="Dapplo.Addons, Version=0.3.89.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Dapplo.Addons.0.3.89\lib\net45\Dapplo.Addons.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Dapplo.Config, Version=0.4.2.0, Culture=neutral, processorArchitecture=MSIL">
Expand Down
4 changes: 2 additions & 2 deletions Dapplo.CaliburnMicro.Demo/Services/AuthenticationProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@

#region Usings

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

#endregion
Expand All @@ -42,7 +42,7 @@ public class AuthenticationProvider : SimpleAuthenticationProvider
{
public AuthenticationProvider()
{
Permissions = new List<string> {"Developer"};
AddPermission("Admin");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@

#region Usings

using System;
using System.ComponentModel.Composition;
using Caliburn.Micro;
using Dapplo.CaliburnMicro.Demo.Languages;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
using System.Windows;
using System.Windows.Media;
using Caliburn.Micro;
using Dapplo.CaliburnMicro.Behaviors;
using Dapplo.CaliburnMicro.Demo.Languages;
using Dapplo.CaliburnMicro.Demo.ViewModels;
using Dapplo.CaliburnMicro.Menu;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@
#region Usings

using System.ComponentModel.Composition;
using System.Security.Permissions;
using Caliburn.Micro;
using Dapplo.CaliburnMicro.Behaviors.Security;
using Dapplo.CaliburnMicro.Demo.Languages;
using Dapplo.CaliburnMicro.Demo.UseCases.Wizard.ViewModels;
using Dapplo.CaliburnMicro.Extensions;
Expand All @@ -41,6 +43,7 @@ namespace Dapplo.CaliburnMicro.Demo.UseCases.ContextMenu
/// This will add an extry for the wizard to the context menu
/// </summary>
[Export("contextmenu", typeof(IMenuItem))]
[UiEnabledPermissions(Permissions = "Admin")]
public sealed class WizardMenuItem : MenuItem
{
[Import]
Expand Down
2 changes: 1 addition & 1 deletion Dapplo.CaliburnMicro.Demo/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<packages>
<package id="Caliburn.Micro" version="3.0.1" targetFramework="net452" />
<package id="Caliburn.Micro.Core" version="3.0.1" targetFramework="net452" />
<package id="Dapplo.Addons" version="0.3.88" targetFramework="net452" />
<package id="Dapplo.Addons" version="0.3.89" targetFramework="net452" />
<package id="Dapplo.Config" version="0.4.2" targetFramework="net452" />
<package id="Dapplo.InterfaceImpl" version="0.2.5" targetFramework="net452" />
<package id="Dapplo.Log" version="1.0.22" targetFramework="net452" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@
<HintPath>..\packages\Caliburn.Micro.3.0.1\lib\net45\Caliburn.Micro.Platform.Core.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Dapplo.Addons, Version=0.3.88.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Dapplo.Addons.0.3.88\lib\net45\Dapplo.Addons.dll</HintPath>
<Reference Include="Dapplo.Addons, Version=0.3.89.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Dapplo.Addons.0.3.89\lib\net45\Dapplo.Addons.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Dapplo.Log, Version=1.0.22.0, Culture=neutral, processorArchitecture=MSIL">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
using System.Windows;
using Caliburn.Micro;
using Dapplo.CaliburnMicro.Behaviors;
using Dapplo.CaliburnMicro.Behaviors.Security;
using Dapplo.CaliburnMicro.Menu;
using Dapplo.CaliburnMicro.Tree;
using Dapplo.Log;
Expand Down Expand Up @@ -125,6 +126,7 @@ protected void ConfigureMenuItems(IEnumerable<IMenuItem> menuItems)

foreach (var contextMenuItem in items.CreateTree())
{
//UiEnabledPermissionsAttribute.ApplyBehaviorWhenAttribute(contextMenuItem);
TrayMenuItems.Add(contextMenuItem);
}
}
Expand Down
2 changes: 1 addition & 1 deletion Dapplo.CaliburnMicro.NotifyIconWpf/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<packages>
<package id="Caliburn.Micro" version="3.0.1" targetFramework="net452" />
<package id="Caliburn.Micro.Core" version="3.0.1" targetFramework="net452" />
<package id="Dapplo.Addons" version="0.3.88" targetFramework="net45" />
<package id="Dapplo.Addons" version="0.3.89" targetFramework="net45" />
<package id="Dapplo.Log" version="1.0.22" targetFramework="net45" />
<package id="Dapplo.Utils" version="1.0.128" targetFramework="net45" />
<package id="Hardcodet.NotifyIcon.Wpf" version="1.0.8" targetFramework="net45" />
Expand Down
26 changes: 13 additions & 13 deletions Dapplo.CaliburnMicro/Behaviors/AttachedBehavior.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,42 +73,42 @@ private static string GetNextPropertyName()
/// <summary>
/// Update (or create) the Behavior for the DependencyObject
/// </summary>
/// <param name="host"></param>
public void Update(DependencyObject host)
/// <param name="host">DependencyObject</param>
/// <param name="propertyChangedEventArgs">DependencyPropertyChangedEventArgs</param>
public void Update(DependencyObject host, DependencyPropertyChangedEventArgs propertyChangedEventArgs)
{
Contract.Requires(host != null);

var behavior = (IBehavior) host.GetValue(_property);

if (behavior == null)
{
TryCreateBehavior(host);
TryCreateBehavior(host, propertyChangedEventArgs);
}
else
{
UpdateBehavior(host, behavior);
UpdateBehavior(host, behavior, propertyChangedEventArgs);
}
}

private void TryCreateBehavior(DependencyObject host)
private void TryCreateBehavior(DependencyObject host, DependencyPropertyChangedEventArgs propertyChangedEventArgs)
{
var behavior = _behaviorFactory(host);

if (behavior.IsApplicable())
if (!behavior.IsApplicable())
{
behavior.Attach();

host.SetValue(_property, behavior);

behavior.Update();
return;
}
behavior.Attach();
host.SetValue(_property, behavior);
behavior.Update(propertyChangedEventArgs);
}

private void UpdateBehavior(DependencyObject host, IBehavior behavior)
private void UpdateBehavior(DependencyObject host, IBehavior behavior, DependencyPropertyChangedEventArgs propertyChangedEventArgs)
{
if (behavior.IsApplicable())
{
behavior.Update();
behavior.Update(propertyChangedEventArgs);
}
else
{
Expand Down
17 changes: 9 additions & 8 deletions Dapplo.CaliburnMicro/Behaviors/Behavior.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,12 @@ public abstract class Behavior<THost> : IBehavior where THost : DependencyObject
/// <summary>
/// Constructor
/// </summary>
/// <param name="host">DependencyObject</param>
protected Behavior(THost host)
/// <param name="uiElement">DependencyObject</param>
protected Behavior(THost uiElement)
{
Contract.Requires(host is THost, "Host is not the expected type");
Contract.Requires(uiElement is THost, "Host is not the expected type");

_hostReference = new WeakReference(host);
_hostReference = new WeakReference(uiElement);
}

private THost GetHost()
Expand Down Expand Up @@ -86,8 +86,9 @@ protected virtual void Detach(THost host)
/// <summary>
/// Let the behavior update it's "stuffT for the specified host
/// </summary>
/// <param name="host">THost which extends DependencyObject</param>
protected abstract void Update(THost host);
/// <param name="uiElement">THost which extends DependencyObject</param>
/// <param name="dependencyPropertyChangedEventArgs">DependencyPropertyChangedEventArgs</param>
protected abstract void Update(THost uiElement, DependencyPropertyChangedEventArgs? dependencyPropertyChangedEventArgs);

/// <summary>
/// Let the behavior update it's "stuffT for the specified host
Expand Down Expand Up @@ -138,13 +139,13 @@ public void Detach()
}

/// <inheritdoc />
public void Update()
public void Update(DependencyPropertyChangedEventArgs? dependencyPropertyChangedEventArgs)
{
var host = GetHost();

if (host != null)
{
Update(host);
Update(host, dependencyPropertyChangedEventArgs);
}
}

Expand Down
10 changes: 5 additions & 5 deletions Dapplo.CaliburnMicro/Behaviors/BooleanVisibility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,9 @@ public static class BooleanVisibility

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

private static void OnVisibilityChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
private static void OnVisibilityChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs)
{
Behavior.Update(d);
Behavior.Update(dependencyObject, dependencyPropertyChangedEventArgs);
}

/// <summary>
Expand Down Expand Up @@ -151,15 +151,15 @@ public static Visibility GetWhenFalse(UIElement uiElement)
/// </summary>
private sealed class BooleanVisibilityBehavior : Behavior<UIElement>
{
internal BooleanVisibilityBehavior(UIElement host) : base(host)
internal BooleanVisibilityBehavior(UIElement uiElement) : base(uiElement)
{
// Does not propagate external changes in visibility to binding source - that will be
// covered in a future post
}

protected override void Update(UIElement host)
protected override void Update(UIElement uiElement, DependencyPropertyChangedEventArgs? dependencyPropertyChangedEventArgs)
{
host.Visibility = GetValue(host) ? GetWhenTrue(host) : GetWhenFalse(host);
uiElement.Visibility = GetValue(uiElement) ? GetWhenTrue(uiElement) : GetWhenFalse(uiElement);
}
}
}
Expand Down
12 changes: 6 additions & 6 deletions Dapplo.CaliburnMicro/Behaviors/EnumVisibility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public static class EnumVisibility
/// <param name="dependencyPropertyChangedEventArgs">DependencyPropertyChangedEventArgs</param>
private static void OnArgumentsChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs)
{
Behavior.Update(dependencyObject);
Behavior.Update(dependencyObject, dependencyPropertyChangedEventArgs);
}

/// <summary>
Expand Down Expand Up @@ -182,17 +182,17 @@ public static void SetWhenNotMatched(UIElement uiElement, Visibility visibility)
/// </summary>
private sealed class EnumVisibilityBehavior : Behavior<UIElement>
{
private readonly EnumCheck _enumCheck = new EnumCheck();
private readonly ValueChecker<object> _enumCheck = new ValueChecker<object>();

internal EnumVisibilityBehavior(UIElement host) : base(host)
internal EnumVisibilityBehavior(UIElement uiElement) : base(uiElement)
{
}

protected override void Update(UIElement host)
protected override void Update(UIElement uiElement, DependencyPropertyChangedEventArgs? dependencyPropertyChangedEventArgs)
{
_enumCheck.Update(GetValue(host), GetTargetValue(host));
_enumCheck.Update(GetValue(uiElement), GetTargetValue(uiElement));

host.Visibility = _enumCheck.IsMatch ? GetWhenMatched(host) : GetWhenNotMatched(host);
uiElement.Visibility = _enumCheck.IsMatch ? GetWhenMatched(uiElement) : GetWhenNotMatched(uiElement);
}
}
}
Expand Down
Loading

0 comments on commit 08f2064

Please sign in to comment.