CSLA.NET 6.0.0
CSLA .NET is a software development framework that helps you build a reusable, maintainable object-oriented business layer for your app.
ReadWriteAuthorization.cs
Go to the documentation of this file.
1//-----------------------------------------------------------------------
2// <copyright file="ReadWriteAuthorization.cs" company="Marimer LLC">
3// Copyright (c) Marimer LLC. All rights reserved.
4// Website: https://cslanet.com
5// </copyright>
6// <summary>Windows Forms extender control that automatically</summary>
7//-----------------------------------------------------------------------
8using System;
9using System.Drawing;
10using System.ComponentModel;
11using System.Collections.Generic;
12using System.Windows.Forms;
13using System.Reflection;
14
15namespace Csla.Windows
16{
23 [DesignerCategory("")]
24 [ProvideProperty("ApplyAuthorization", typeof(Control))]
25 [ToolboxItem(true), ToolboxBitmap(typeof(ReadWriteAuthorization), "Csla.Windows.ReadWriteAuthorization")]
26 public class ReadWriteAuthorization : Component, IExtenderProvider
27 {
28 // this class keeps track of the control status
29 private class ControlStatus
30 {
31 public bool ApplyAuthorization { get; set; }
32 public bool CanRead { get; set; }
33 }
34
35 private readonly Dictionary<Control, ControlStatus> _sources =
36 new Dictionary<Control, ControlStatus>();
37
42 public ReadWriteAuthorization(IContainer container)
43 { container.Add(this); }
44
54 public bool CanExtend(object extendee)
55 {
56 if (IsPropertyImplemented(extendee, "ReadOnly")
57 || IsPropertyImplemented(extendee, "Enabled"))
58 return true;
59 else
60 return false;
61 }
62
68 [Category("Csla")]
69 public bool GetApplyAuthorization(Control source)
70 {
71 ControlStatus result;
72 if (_sources.TryGetValue(source, out result))
73 return result.ApplyAuthorization;
74 else
75 return false;
76 }
77
84 [Category("Csla")]
85 public void SetApplyAuthorization(Control source, bool value)
86 {
87 ControlStatus status;
88 if (_sources.TryGetValue(source, out status))
89 status.ApplyAuthorization = value;
90 else
91 _sources.Add(
92 source,
93 new ControlStatus { ApplyAuthorization = value, CanRead = true });
94 }
95
110 {
111 foreach (var item in _sources)
112 if (item.Value.ApplyAuthorization)
113 ApplyAuthorizationRules(item.Key);
114 }
115
116 private void ApplyAuthorizationRules(Control control)
117 {
118 foreach (Binding binding in control.DataBindings)
119 {
120 // get the BindingSource if appropriate
121 if (binding.DataSource is BindingSource)
122 {
123 BindingSource bs =
124 (BindingSource)binding.DataSource;
125 // get the BusinessObject if appropriate
127 bs.Current as Csla.Security.IAuthorizeReadWrite;
128 if (ds != null)
129 {
130 // get the object property name
131 string propertyName =
132 binding.BindingMemberInfo.BindingField;
133
134 ApplyReadRules(
135 control, binding,
136 ds.CanReadProperty(propertyName));
137 ApplyWriteRules(
138 control, binding,
139 ds.CanWriteProperty(propertyName));
140 }
141 }
142 }
143 }
144
145 private void ApplyReadRules(
146 Control ctl, Binding binding,
147 bool canRead)
148 {
149 var status = GetControlStatus(ctl);
150
151 // enable/disable reading of the value
152 if (canRead)
153 {
154 ctl.Enabled = true;
155 // if !CanRead remove format event and refresh value
156 if (!status.CanRead)
157 {
158 binding.Format -= ReturnEmpty;
159 binding.ReadValue();
160 }
161 }
162 else
163 {
164 ctl.Enabled = false;
165 if (status.CanRead)
166 {
167 binding.Format += ReturnEmpty;
168 }
169
170 // clear the value displayed by the control
171 var propertyInfo = ctl.GetType().GetProperty(binding.PropertyName,
172 BindingFlags.FlattenHierarchy |
173 BindingFlags.Instance |
174 BindingFlags.Public);
175 if (propertyInfo != null)
176 {
177 propertyInfo.SetValue(ctl,
178 GetEmptyValue(
179 Utilities.GetPropertyType(
180 propertyInfo.PropertyType)),
181 new object[] { });
182 }
183 }
184
185 // store new status
186 status.CanRead = canRead;
187 }
188
189 private void ApplyWriteRules(
190 Control ctl, Binding binding,
191 bool canWrite)
192 {
193 if (ctl is Label) return;
194
195 // enable/disable writing of the value
196 PropertyInfo propertyInfo =
197 ctl.GetType().GetProperty("ReadOnly",
198 BindingFlags.FlattenHierarchy |
199 BindingFlags.Instance |
200 BindingFlags.Public);
201 if (propertyInfo != null)
202 {
203 bool couldWrite =
204 (!(bool)propertyInfo.GetValue(
205 ctl, new object[] { }));
206 propertyInfo.SetValue(
207 ctl, !canWrite, new object[] { });
208 if ((!couldWrite) && (canWrite))
209 binding.ReadValue();
210 }
211 else
212 {
213 bool couldWrite = ctl.Enabled;
214 ctl.Enabled = canWrite;
215 if ((!couldWrite) && (canWrite))
216 binding.ReadValue();
217 }
218 }
219
220 private void ReturnEmpty(
221 object sender, ConvertEventArgs e)
222 {
223 e.Value = GetEmptyValue(e.DesiredType);
224 }
225
226 private object GetEmptyValue(Type desiredType)
227 {
228 object result = null;
229 if (desiredType.IsValueType)
230 result = Activator.CreateInstance(desiredType);
231 return result;
232 }
233
234 private static bool IsPropertyImplemented(
235 object obj, string propertyName)
236 {
237 if (obj.GetType().GetProperty(propertyName,
238 BindingFlags.FlattenHierarchy |
239 BindingFlags.Instance |
240 BindingFlags.Public) != null)
241 return true;
242 else
243 return false;
244 }
245
246 private ControlStatus GetControlStatus(Control control)
247 {
248 return _sources[control];
249 }
250 }
251}
Windows Forms extender control that automatically enables and disables detail form controls based on ...
void ResetControlAuthorization()
Causes the ReadWriteAuthorization control to apply authorization rules from the business object to al...
bool CanExtend(object extendee)
Gets a value indicating whether the extender control can extend the specified control.
bool GetApplyAuthorization(Control source)
Gets the custom ApplyAuthorization extender property added to extended controls.
void SetApplyAuthorization(Control source, bool value)
Sets the custom ApplyAuthorization extender property added to extended controls.
ReadWriteAuthorization(IContainer container)
Creates an instance of the object.
Defines the authorization interface through which an object can indicate which properties the current...