Re: Implement authorization in win forms
- From: parez <psawant@xxxxxxxxx>
- Date: Fri, 11 Apr 2008 11:46:20 -0700 (PDT)
On Apr 11, 2:14 pm, parez <psaw...@xxxxxxxxx> wrote:
On Apr 11, 1:49 pm, parez <psaw...@xxxxxxxxx> wrote:
On Apr 11, 4:25 am, Marc Gravell <marc.grav...@xxxxxxxxx> wrote:
For info, here is a rough sketch of what the component would look
like... this allows both IDE and programmatic usage; note that for
roles-based security you'd also need to initialize the principal - at
the most primative this can be as simple as:
Thread.CurrentPrincipal = new GenericPrincipal(
new GenericIdentity("Marc"), // name of user
new string[] { "BASIC" } // array of roles that the
user has
);
Obviously if your security model is more complex, you may need to
change things ;-p
[ProvideProperty("Role", typeof(Control))]
[ToolboxItemFilter("System.Windows.Forms")]
[Description("Provides automatic role-checking")]
public class RoleDisabler : Component, IExtenderProvider
{
private Dictionary<Control, string> map
= new Dictionary<Control, string>();
[DefaultValue("")]
public string GetRole(Control control)
{
if (control == null) return "";
string role;
map.TryGetValue(control, out role);
return role ?? "";
}
public void SetRole(Control control, string role)
{
if (control == null) return;
bool add = false, remove = false;
if (string.IsNullOrEmpty(role))
{
remove = map.Remove(control);
}
else
{
add = !map.ContainsKey(control);
map[control] = role;
}
if (!DesignMode)
{
SetEnabled(control);
if (add)
{
control.ParentChanged += control_ParentChanged;
}
else if (remove)
{
control.ParentChanged -= control_ParentChanged;
}
}
}
private void SetEnabled(Control control)
{
if (DesignMode || control == null) return;
string role;
if (map.TryGetValue(control, out role))
{
IPrincipal principal = Thread.CurrentPrincipal;
control.Enabled = principal == null ? false :
principal.IsInRole(role);
}
}
void control_ParentChanged(object sender, EventArgs e)
{
SetEnabled(sender as Control);
}
bool IExtenderProvider.CanExtend(object obj)
{
return obj is Control;
}
}
Thanks..
How will i use the RoleDisabler ? would i drag it from the toolbar
like tooltip?
That was great.. thanks...
I have a question..
When will the SetEnabled function execute?
I think i got it. thanks..once again..you were a life saver..
.
- Follow-Ups:
- Re: Implement authorization in win forms
- From: Marc Gravell
- Re: Implement authorization in win forms
- References:
- Implement authorization in win forms
- From: parez
- Re: Implement authorization in win forms
- From: Marc Gravell
- Re: Implement authorization in win forms
- From: parez
- Re: Implement authorization in win forms
- From: Marc Gravell
- Re: Implement authorization in win forms
- From: parez
- Re: Implement authorization in win forms
- From: Marc Gravell
- Re: Implement authorization in win forms
- From: parez
- Re: Implement authorization in win forms
- From: Marc Gravell
- Re: Implement authorization in win forms
- From: Marc Gravell
- Re: Implement authorization in win forms
- From: parez
- Re: Implement authorization in win forms
- From: parez
- Implement authorization in win forms
- Prev by Date: LINQ to SQL as XML
- Next by Date: Re: ListView MouseUp Event
- Previous by thread: Re: Implement authorization in win forms
- Next by thread: Re: Implement authorization in win forms
- Index(es):
Relevant Pages
|