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 _elements; private static bool _initialized; static AuthorizationHelper() { _elements = new List(); } #region Mode /// /// Determined the resolution mode for the trigger. /// public static readonly DependencyProperty ModeProperty = DependencyProperty.RegisterAttached("Mode", typeof(BlockingMode?), typeof(AuthorizationHelper), new FrameworkPropertyMetadata(null, Invalidate)); /// /// Sets the Mode attached property. /// /// The element. /// if set to true [value]. public static void SetMode(FrameworkElement element, BlockingMode? value) { element.SetValue(ModeProperty, value); } /// /// Gets the Mode attached property. /// /// The element. /// public static BlockingMode? GetMode(FrameworkElement element) { return (BlockingMode?)element.GetValue(ModeProperty); } #endregion #region Permission /// /// Determined the resolution Permission for the trigger. /// public static readonly DependencyProperty PermissionProperty = DependencyProperty.RegisterAttached("Permission", typeof(Permissions?), typeof(AuthorizationHelper), new FrameworkPropertyMetadata(null, Invalidate)); /// /// Sets the Permission attached property. /// /// The element. /// if set to true [value]. public static void SetPermission(FrameworkElement element, Permissions? value) { element.SetValue(PermissionProperty, value); } /// /// Gets the Permission attached property. /// /// The element. /// 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((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); } } } }