1: using System;
2: using System.Net;
3: using System.Windows;
4: using System.Windows.Controls;
5: using System.Windows.Documents;
6: using System.Windows.Ink;
7: using System.Windows.Input;
8: using System.Windows.Media;
9: using System.Windows.Media.Animation;
10: using System.Windows.Shapes;
11: using System.Windows.Markup;
12: using System.Security.Principal;
13: using System.Windows.Data;
14: using System.Xaml;
15: using System.ComponentModel;
16: using System.Diagnostics;
17: using System.Security;
18: using System.Reflection;
19: using System.Globalization;
20:
21: namespace SLPG.MarkupExtensions
22: {
23: public class RoleBindingExtension : MarkupExtension
24: {
25: /// <summary>
26: /// Gets or sets the name of the group.
27: /// </summary>
28: /// <value>
29: /// The name of the group.
30: /// </value>
31: public string GroupName { get; set; }
32: /// <summary>
33: /// Gets or sets the name of the feature.
34: /// </summary>
35: /// <value>
36: /// The name of the feature.
37: /// </value>
38: public string FeatureName { get; set; }
39: /// <summary>
40: /// Gets or sets the converter.
41: /// </summary>
42: /// <value>
43: /// The converter.
44: /// </value>
45: public IValueConverter Converter { get; set; }
46: /// <summary>
47: /// Gets or sets the converter parameter.
48: /// </summary>
49: /// <value>
50: /// The converter parameter.
51: /// </value>
52: public object ConverterParameter { get; set; }
53:
54: /// <summary>
55: /// Provides the value.
56: /// </summary>
57: /// <param name="serviceProvider">The service provider.</param>
58: /// <returns></returns>
59: public override object ProvideValue(IServiceProvider serviceProvider)
60: {
61: IProvideValueTarget target = (IProvideValueTarget)serviceProvider.GetService(typeof(IProvideValueTarget));
62:
63: bool isAuthorized = false;
64:
65: if (RoleManager.Current != null)
66: isAuthorized = RoleManager.Current.Validator.Authorize(this.GroupName, this.FeatureName);
67: else
68: isAuthorized = true;
69:
70: if (this.Converter != null)
71: return this.MapToType(isAuthorized, target, this.Converter, this.ConverterParameter);
72:
73: return this.MapToType(isAuthorized, target);
74: }
75:
76: /// <summary>
77: /// Maps to type.
78: /// </summary>
79: /// <param name="isAuthorized">if set to <c>true</c> [is authorized].</param>
80: /// <param name="target">The target.</param>
81: /// <param name="converter">The converter.</param>
82: /// <param name="converterParameter">The converter parameter.</param>
83: /// <returns></returns>
84: private object MapToType(bool isAuthorized, IProvideValueTarget target, IValueConverter converter, object converterParameter)
85: {
86: PropertyInfo info = target.TargetProperty as PropertyInfo;
87:
88: if (info != null)
89: return converter.Convert(isAuthorized, info.PropertyType, converterParameter, CultureInfo.CurrentCulture);
90:
91: return isAuthorized;
92: }
93:
94: /// <summary>
95: /// Maps to type.
96: /// </summary>
97: /// <param name="isAuthorized">if set to <c>true</c> [is authorized].</param>
98: /// <param name="target">The target.</param>
99: /// <returns></returns>
100: private object MapToType(bool isAuthorized, IProvideValueTarget target)
101: {
102: PropertyInfo info = target.TargetProperty as PropertyInfo;
103:
104: if (info != null)
105: {
106: if (info.PropertyType == typeof(Visibility))
107: return isAuthorized ? Visibility.Visible : Visibility.Collapsed;
108: }
109:
110: return isAuthorized;
111: }
112: }
113: }