1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using Tango.BL.Enumerations;
using Tango.Core.DI;
using Tango.FSE.Common.Authentication;
namespace Tango.FSE.Common.Authorization
{
public static class AuthorizationHelper
{
private static IAuthenticationProvider _authenticationProvider;
private static List<FrameworkElement> _elements;
private static bool _initialized;
static AuthorizationHelper()
{
_elements = new List<FrameworkElement>();
}
#region Mode
/// <summary>
/// Determined the resolution mode for the trigger.
/// </summary>
public static readonly DependencyProperty ModeProperty =
DependencyProperty.RegisterAttached("Mode",
typeof(BlockingMode?), typeof(AuthorizationHelper),
new FrameworkPropertyMetadata(null, Invalidate));
/// <summary>
/// Sets the Mode attached property.
/// </summary>
/// <param name="element">The element.</param>
/// <param name="value">if set to <c>true</c> [value].</param>
public static void SetMode(FrameworkElement element, BlockingMode? value)
{
element.SetValue(ModeProperty, value);
}
/// <summary>
/// Gets the Mode attached property.
/// </summary>
/// <param name="element">The element.</param>
/// <returns></returns>
public static BlockingMode? GetMode(FrameworkElement element)
{
return (BlockingMode?)element.GetValue(ModeProperty);
}
#endregion
#region Permission
/// <summary>
/// Determined the resolution Permission for the trigger.
/// </summary>
public static readonly DependencyProperty PermissionProperty =
DependencyProperty.RegisterAttached("Permission",
typeof(Permissions?), typeof(AuthorizationHelper),
new FrameworkPropertyMetadata(null, Invalidate));
/// <summary>
/// Sets the Permission attached property.
/// </summary>
/// <param name="element">The element.</param>
/// <param name="value">if set to <c>true</c> [value].</param>
public static void SetPermission(FrameworkElement element, Permissions? value)
{
element.SetValue(PermissionProperty, value);
}
/// <summary>
/// Gets the Permission attached property.
/// </summary>
/// <param name="element">The element.</param>
/// <returns></returns>
public static Permissions? GetPermission(FrameworkElement element)
{
return (Permissions?)element.GetValue(PermissionProperty);
}
#endregion
private static void Invalidate(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
FrameworkElement element = d as FrameworkElement;
if (element != null)
{
if (!_initialized)
{
_initialized = true;
TangoIOC.Default.GetInstanceWhenAvailable<IAuthenticationProvider>((x) =>
{
_authenticationProvider = x;
_authenticationProvider.CurrentUserChanged += _authenticationProvider_CurrentUserChanged;
});
}
Invalidate(element);
}
}
private static void Invalidate(FrameworkElement element)
{
if (!_elements.Contains(element))
{
_elements.Add(element);
element.Unloaded += Element_Unloaded;
}
if (_authenticationProvider == null) return;
var currentUser = _authenticationProvider.CurrentUser;
var permission = GetPermission(element);
var mode = GetMode(element);
if (currentUser != null && permission != null && mode != null)
{
bool hasPermission = currentUser.HasPermission(permission.Value);
if (!hasPermission)
{
if (mode == BlockingMode.Collapsed)
{
element.Visibility = Visibility.Collapsed;
}
else if (mode == BlockingMode.Hidden)
{
element.Visibility = Visibility.Hidden;
}
else if (mode == BlockingMode.Disabled)
{
element.IsEnabled = false;
}
}
else
{
if (mode == BlockingMode.Collapsed || mode == BlockingMode.Hidden)
{
element.Visibility = Visibility.Visible;
}
else if (mode == BlockingMode.Disabled)
{
element.IsEnabled = true;
}
}
}
}
private static void Element_Unloaded(object sender, RoutedEventArgs e)
{
FrameworkElement element = sender as FrameworkElement;
if (element != null)
{
element.Unloaded -= Element_Unloaded;
}
}
private static void _authenticationProvider_CurrentUserChanged(object sender, Tango.BL.Entities.User e)
{
foreach (var element in _elements.ToList())
{
Invalidate(element);
}
}
}
}
|