cylt
12-29-2005, 01:55 PM
I'm trying to use a combination of a "Introduction" , "Before advice" and a custom attribute for implementing a custom security check. The "Introduction" extend some classes with a property representative of a user cretentials and the "Before advice" implementation test this property to check the user role and determine if the current user is allow or not for calling a method. The class's methods to be controled are tagged with a custom attribute.
here his the code snippets :
The AcessControled advice Interface
public interface IAccessControled:IAdvice,ITargetAware
{
UserCredential Credential { get;set;}
}
The Advisor
public class AccessControledAdvisor:DefaultIntroductionAdvisor
{
public AccessControledAdvisor():base(new AccessControledMixin())
{
}
}
The Mixin
public class AccessControledMixin:IAccessControled
{
private UserCredential m_Credential;
private IAopProxy m_TargetProxy;
public DML.UserCredential Credential
{
get{return m_Credential;}set{m_Credential = value;}
}
public IAopProxy TargetProxy
{
set { m_TargetProxy=value; }
}
}
The Factory
ProxyFactory factory = new ProxyFactory(_obj);
factory.AddIntroduction(new AccessControledAdvisor());
factory.AddAdvice(new BLLTransactionAdvice());
factory.AddAdvice(new BLLSecurityAdvice());
Object _proxy = factory.GetProxy();
The advice implementation
public void Before(System.Reflection.MethodInfo method, object[] args, object target)
{
object _returnValue;
if(method.IsDefined(typeof(AccessControlAttribute) , true))
{
object[] attrs = method.GetCustomAttributes(typeof(AccessControlAtt ribute), true);
AccessControlAttribute _trsAttr = (AccessControlAttribute)attrs[0];
string login = ((IAccessControled)target).Credential.Login;
if(!Security.SecurityControler.IsInRole(login, (int)_trsAttr.Role))
{
throw new AccessControlException();
}
}
}
the problem is that the target objet i get with the Before method parameter is not the "proxified" instance (it's the original one) and could not be cast in IAccessControled. If i try to cast the objet directly returned by the "factory.GetProxy();" it work fine... this one has been corretly "mixed" by the introduction operation.
How could i get this "mixed" instance in the Before advice implementation scope ?
cylt.
here his the code snippets :
The AcessControled advice Interface
public interface IAccessControled:IAdvice,ITargetAware
{
UserCredential Credential { get;set;}
}
The Advisor
public class AccessControledAdvisor:DefaultIntroductionAdvisor
{
public AccessControledAdvisor():base(new AccessControledMixin())
{
}
}
The Mixin
public class AccessControledMixin:IAccessControled
{
private UserCredential m_Credential;
private IAopProxy m_TargetProxy;
public DML.UserCredential Credential
{
get{return m_Credential;}set{m_Credential = value;}
}
public IAopProxy TargetProxy
{
set { m_TargetProxy=value; }
}
}
The Factory
ProxyFactory factory = new ProxyFactory(_obj);
factory.AddIntroduction(new AccessControledAdvisor());
factory.AddAdvice(new BLLTransactionAdvice());
factory.AddAdvice(new BLLSecurityAdvice());
Object _proxy = factory.GetProxy();
The advice implementation
public void Before(System.Reflection.MethodInfo method, object[] args, object target)
{
object _returnValue;
if(method.IsDefined(typeof(AccessControlAttribute) , true))
{
object[] attrs = method.GetCustomAttributes(typeof(AccessControlAtt ribute), true);
AccessControlAttribute _trsAttr = (AccessControlAttribute)attrs[0];
string login = ((IAccessControled)target).Credential.Login;
if(!Security.SecurityControler.IsInRole(login, (int)_trsAttr.Role))
{
throw new AccessControlException();
}
}
}
the problem is that the target objet i get with the Before method parameter is not the "proxified" instance (it's the original one) and could not be cast in IAccessControled. If i try to cast the objet directly returned by the "factory.GetProxy();" it work fine... this one has been corretly "mixed" by the introduction operation.
How could i get this "mixed" instance in the Before advice implementation scope ?
cylt.