• EsbAOP应用--权限管理


        前文介绍了异常关闭器的AOP实现,而权限管理也是一个AOP可以大展拳脚的地方,下面就来看看如何使用EsbAOP实现权限管理。
        首先要解决的问题是如何判断用户是否拥有调用某个操作的权限,这个判断是由各个应用程序自己实现了,为了统一对权限的判断,所以我们指定了接口IPermissionVerifier:

        /// <summary>
        
    /// IPermissionVerifier 用于验证当前用户对目标类的目标方法的调用权限是否足够。
        
    /// 如果需要使用Aop来进行权限管理,则要实现IPermissionVerifier接口。    
        
    /// </summary>
        public interface IPermissionVerifier
        {
            
    bool QualifiedToOperation(object permissionNeeded ,string destClassFullName ,string destMethodName) ;
        }

        其中,permissionNeeded通过应用于方法的特性的参数指定(后面可以看到),destClassFullName表示目标方法所在class的全称,destMethodName是目标方法名。
        当用户的权限不满足条件时,对目标方法的调用将抛出PermissionLimittedException异常,该异常定义如下:

        /// <summary>
        
    /// 当没有权限的用户调用某项操作时,将抛出此异常
        
    /// </summary>
        [Serializable]
        
    public class PermissionLimittedException : Exception
        {
            
    public PermissionLimittedException()
            {
            }

            
    public PermissionLimittedException(string msg):base(msg)
            {            
            }

            
    public PermissionLimittedException(string msg ,Exception innerException) : base(msg ,innerException)
            {            
            }
        }

        注意,自定义异常必须是可序列化的,这样就可以保证异常通过remoting传递。

        接下来,就可以实现权限方面PermissionAspect了。

        /// <summary>
        
    /// PermissionAspect  权限方面,如果当前用户不够权限调用某个方法,则抛出PermissionLimittedException。
        
    /// aspectClassArgument :实现了IPermissionVerifier接口的类型
        
    /// aspectMethodArgument:操作所需的权限,由用户自定义    
        
    /// </summary>
        public class PermissionAspect :IAspect
        {
            
    public PermissionAspect()
            {
            }
            
            
    #region IAspect 成员
            
    public void PreProcess(IMethodCallMessage requestMsg, object aspectClassArgument, object aspectMethodArgument)
            {
                Type verifierType 
    = (Type)aspectClassArgument ;
                Type destType     
    = typeof(IPermissionVerifier) ;
                
    if(! destType.IsAssignableFrom(verifierType))
                {
                    
    throw new Exception("the PermissionVerifierType is invalid !") ;
                }

                IPermissionVerifier pmVerifier 
    = (IPermissionVerifier)Activator.CreateInstance(verifierType) ;
                
    if(pmVerifier == null)
                {
                    
    throw new Exception("the PermissionVerifierType is invalid !") ;
                }
                
                
    object pmNeeded   = aspectMethodArgument ;
                
    string className  = AopHelper.GetFullClassName(requestMsg) ;
                
    string methodName = AopHelper.GetMethodName(requestMsg) ;

                
    bool qualified = pmVerifier.QualifiedToOperation(pmNeeded ,className ,methodName) ;

                
    if(! qualified)
                {
                    
    throw new PermissionLimittedException(string.Format("Current user have no permission to call dest method : {0}.{1}!" ,className ,methodName)) ;
                }
            }

            
    public void PostProcess(IMethodCallMessage requestMsg, ref IMethodReturnMessage respond, object aspectClassArgument, object aspectMethodArgument)
            {
                
    // TODO:  添加 PermissionAspect.EnterpriseServerBase.Aop.ComplexAop.IAspect.PostProcess 实现
            }
        
            
    #endregion
        }    

        关于aspectClassArgument 和aspectMethodArgument的含义注释中已经给出了,如果要知道这两个参数是如何使用的,可以回顾EsbAOP实现中的说明。权限方面在前处理的时候,根据IPermissionVerifier判断用户是否满足权限要求,如果不满足,则引发PermissionLimittedException异常。

        最后给出一个示例:

        public class class1
        {
            [STAThread]
            
    static void Main(string[] args)
            {
                
    try
                {
                    Example exa 
    = new Example() ;        
                    exa.SayHello(
    "sky") ;
                    exa.SayByebye(
    "sky") ;                    
                }
                
    catch(Exception ee)
                {
                    Console.WriteLine(ee.Message) ;                
                }

                Console.Read() ;
            }
        }    

        [Aspect(
    typeof(PermissionAspectWrap))]        
        
    public class Example :ContextBoundObject
        {                
            [AspectSwitcher(
    typeof(PermissionAspectWrap) ,true ,Permission.Common)]       
            
    public void SayHello(string name)
            {
                Console.WriteLine(
    "Hello ," + name) ;            
            }
            
            [AspectSwitcher(
    typeof(PermissionAspectWrap) ,true ,Permission.Super)]     //调用此方法需要Permission.Super权限   
            
    public void SayByebye(string name)
            {
                Console.WriteLine(
    "Byebye ," + name) ;
            }

            
        }

        
    #region PermissionAspectWrap
        
    public class PermissionAspectWrap :IAspectProcessorWrap
        {
            
    #region IAspectProcessorWrap 成员

            
    public Type AspectProcessorType
            {
                
    get
                {                
                    
    return typeof(PermissionAspect);
                }
            }

            
    public object AspectClassArgument
            {
                
    get
                {
                    
    return typeof(PermissionVerifier) ;
                }
            }

            
    public AspectSwitcherState DefaultAspectSwitcherState
            {
                
    get
                {                
                    
    return AspectSwitcherState.On ;
                }
            }
            
    #endregion
        }

        
    #endregion    

        
    #region PermissionVerifier ,Permission ,Logger
        
    public class PermissionVerifier :IPermissionVerifier
        {
            
    private static int curPermission = Permission.Common ;

            
    #region IPermissionVerifier 成员

            
    public bool QualifiedToOperation(object permissionNeeded, string destClassFullName, string destMethodName)
            {
                
    int destPermission = (int)permissionNeeded ;                
                
    if(PermissionVerifier.curPermission > destPermission)
                {
                    
    return true ;
                }

                
    return false;
            }

            
    #endregion
        }

        
    public class Permission
        {
            
    public const int Common = 0 ;
            
    public const int Super  = 1 ;
        }    
        
    #endregion

        由于当前用户级别为Permission.Common,所以在调用SayByebye的时候会抛出异常,而在调用SayHello的时候却可以正常进行。

  • 相关阅读:
    Java基础教程(20)--数字和字符串
    Java基础教程(19)--Object类
    python 选择和循环结构
    购物清单
    第五次安卓作业(计算器和增删改查)
    第四次安卓作业(用户添加)
    第三次安卓作业(用户登录)
    第二次安卓作业(九宫格)
    5.22作业
    5.29作业
  • 原文地址:https://www.cnblogs.com/zhuweisky/p/245778.html
Copyright © 2020-2023  润新知