AOP应用之权限管理

本文介绍在系统实现中AOP常用于并发、权限管理等方面,重点讲解如何使用EnterpriseServerBase类库中的AOP框架实现权限管理,包括需实现的接口、异常定义、实现权限AOP的类,还说明了使用步骤并给出具体例子,后续将介绍AOP日志记录实现。

在系统实现中AOP常用于并发、权限管理、事务处理、日志记录、错误处理方面。本文介绍如何使用EnterpriseServerBase类库中的AOP框架来实现权限管理。

一个用户是否有权限调用某个操作(方法),是由我们的应用决定了,不同的应用有不同的实现,但是,如果需要使用EnterpriseServerBase类库中的AOP的权限管理,则应用必须实现下面的接口IPermissionVerifier:

/**/ ///<summary>
///IPermissionVerifier用于验证当前用户对目标类的目标方法的调用权限是否足够。
///如果需要使用Aop来进行权限管理,则只要实现IPermissionVerifier接口。
///朱伟2005.08.228
///</summary>

public interface IPermissionVerifier
{
boolQualifiedToOperation(objectpermissionNeeded,stringdestClassFullName,stringdestMethodName);
}

当用户调用没有资格的操作时,将抛出PermissionLimittedException异常,其定义如下:

/**/ ///<summary>
///当没有权限的用户调用某项操作时,将抛出此异常
///</summary>

[Serializable]
public class PermissionLimittedException:Exception
{
publicPermissionLimittedException()
{
}


publicPermissionLimittedException(stringmsg):base(msg)
{
}


publicPermissionLimittedException(stringmsg,ExceptioninnerException):base(msg,innerException)
{
}

}

接下来看真正实现权限AOP的PermissionProxy类:

/**/ ///<summary>
///PermissionProxy权限代理,如果当前用户不够权限调用某个方法,则抛出异常。
///朱伟2005.08.22
///</summary>

public class PermissionProxy:AopProxyBase
{
publicPermissionProxy(MarshalByRefObjectobj,Typetype,objectaopArg):base(obj,type,aopArg)
{}

publicoverridevoidPreProcess(IMethodCallMessagerequestMsg)
{
TypeverifierType
=(Type)this.AopClassArgument;
TypedestType
=typeof(IPermissionVerifier);
if(!destType.IsAssignableFrom(verifierType))
{
thrownewException("thePermissionVerifierTypeisinvalid!");
}


IPermissionVerifierpmVerifier
=(IPermissionVerifier)Activator.CreateInstance(verifierType);
if(pmVerifier==null)
{
thrownewException("thePermissionVerifierTypeisinvalid!");
}


objectpmNeeded=this.GetAopMethodArgument((IMethodCallMessage)requestMsg);
stringclassName=AopHelper.GetFullClassName(requestMsg);
stringmethodName=AopHelper.GetMethodName(requestMsg);

boolqualified=pmVerifier.QualifiedToOperation(pmNeeded,className,methodName);

if(!qualified)
{
thrownewPermissionLimittedException(string.Format("Currentuserhavenopermissiontocalldestmethod:{0}.{1}!",className,methodName));
}


}


publicoverridevoidPostProcess(IMethodCallMessagerequestMsg,IMethodReturnMessageRespond)
{

}

}

可以看出PermissionProxy是在预处理中进行权限鉴定的。关于其基类AopProxyBase的更多信息,可以参见前面的文章C# AOP微型框架实现(一)C# AOP微型框架实现(二) 。(注意,现在的AOP框架版本与参文中的介绍稍有出入!)

另外,为了契合EnterpriseServerBase类库中的AOP框架,需要下面的工厂:

/**/ ///<summary>
///PermissionProxyFactory权限代理工厂。
///朱伟2005.08.228
///</summary>

public class PermissionProxyFactory:IAopProxyFactory
{
IAopProxyFactory成员#regionIAopProxyFactory成员

publicAopProxyBaseCreateAopProxyInstance(MarshalByRefObjectobj,Typetype,objectaopArg)
{
returnnewPermissionProxy(obj,type,aopArg);
}


#endregion

}

那么如何使用权限AOP了?

(1)根据目标系统对权限的管理需求实现IPermissionVerifier接口。
(2)在需要进行权限管理的类上加上特性[AopProxyAttribute(typeof(PermissionProxyFactory) ,typeof(PermissionVerifier))],
其中PermissionVerifier是IPermissionVerifier实现。
(3)在需要进行权限管理的方法上加上特性[MethodAopSwitcherAttribute(true ,pmNeeded)],其中pmNeeded为调用此方法所需的资格,
其类型由目标系统决定。

最后给出一个具体的例子说明:

[AopProxyAttribute( typeof (PermissionProxyFactory),Class1.verifier)]
public class Example:ContextBoundObject
{
[MethodAopSwitcherAttribute(
true,Permission.Super)]
publicvoidSayHello(stringname)
{
Console.WriteLine(
"Hello,"+name);
}


[MethodAopSwitcherAttribute(
true,Permission.Common)]
publicvoidSayByebye(stringname)
{
Console.WriteLine(
"Byebye,"+name);
}

}




public class PermissionVerifier:IPermissionVerifier
{
IPermissionVerifier成员#regionIPermissionVerifier成员

publicboolQualifiedToOperation(objectpermissionNeeded,stringdestClassFullName,stringdestMethodName)
{
Permissionpm
=(Permission)permissionNeeded;

if(pm==Permission.Common)
{
returntrue;
}


returnfalse;
}


#endregion


}


public enum Permission
{
Common,Super
}


[STAThread]
static void Main( string []args)
{
try
{
Exampleexa
=newExample();
exa.SayByebye(
"sky");
exa.SayHello(
"sky");
}

catch(Exceptionee)
{
Console.WriteLine(ee.Message);
Console.Read();
}

}

如果需要例子的完整源码,可以email向我索取!也欢迎和我讨论关于AOP应用的更多技术。后面将会给出使用AOP进行日志记录的技术实现。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值