Exemplo n.º 1
0
        public AuthorizationInterceptor_Tests()
        {
            //SUT: AuthorizationInterceptor and AuthorizeAttributeHelper
            LocalIocManager.IocContainer.Register(
                Component.For <IFeatureChecker>().Instance(Substitute.For <IFeatureChecker>())
                );

            LocalIocManager.Register <IAuthorizationConfiguration, AuthorizationConfiguration>();
            LocalIocManager.Register <IMultiTenancyConfig, MultiTenancyConfig>();

            //AuthorizationInterceptor AbpAsyncDeterminationInterceptor<AuthorizationInterceptor> 必须都注册
            //AuthorizationInterceptor不注册时,提示AbpAsyncDeterminationInterceptor<AuthorizationInterceptor>找不到依赖
            //AbpAsyncDeterminationInterceptor<AuthorizationInterceptor>不注册时,容器报错提示未注入
            LocalIocManager.Register <AuthorizationInterceptor>(DependencyLifeStyle.Transient);
            //这个操作就相当于AbpKernelModule中的RegisterInterceptors方法
            LocalIocManager.Register <AbpAsyncDeterminationInterceptor <AuthorizationInterceptor> >(DependencyLifeStyle.Transient);

            LocalIocManager.Register <IAuthorizationHelper, AuthorizationHelper>(DependencyLifeStyle.Transient);
            LocalIocManager.IocContainer.Register(
                //这里的操作有两个功能
                //这里同时对该实例( Component.For<MyTestClassToBeAuthorized_Sync>()返回的)进行两个操作
                Component.For <MyTestClassToBeAuthorized_Sync>()                               //1:将几个类添加到容器中。
                .Interceptors <AbpAsyncDeterminationInterceptor <AuthorizationInterceptor> >() //2:给几个类的实例添加拦截器。因为拦截器也是在IRegistration实例上操作的
                .LifestyleTransient(),                                                         //将拦截器设置为瞬时的:官方建议无特殊原因应始终使用瞬时的,避免比使用拦截器的对象生命周期更长
                Component.For <MyTestClassToBeAuthorized_Async>().Interceptors <AbpAsyncDeterminationInterceptor <AuthorizationInterceptor> >().LifestyleTransient(),
                Component.For <MyTestClassToBeAllowProtected_Async>().Interceptors <AbpAsyncDeterminationInterceptor <AuthorizationInterceptor> >().LifestyleTransient(),
                Component.For <MyTestClassToBeAllowProtected_Sync>().Interceptors <AbpAsyncDeterminationInterceptor <AuthorizationInterceptor> >().LifestyleTransient()
                );

            //Mock session
            var session = Substitute.For <IAbpSession>();

            session.TenantId.Returns(1);
            session.UserId.Returns(1);
            LocalIocManager.IocContainer.Register(Component.For <IAbpSession>().Instance(session));

            //Mock permission checker
            var permissionChecker = Substitute.For <IPermissionChecker>();

            permissionChecker.IsGrantedAsync("Permission1").Returns(true);
            permissionChecker.IsGrantedAsync("Permission2").Returns(true);
            permissionChecker.IsGrantedAsync("Permission3").Returns(false); //Permission3 is not granted

            permissionChecker.IsGranted("Permission1").Returns(true);
            permissionChecker.IsGranted("Permission2").Returns(true);
            permissionChecker.IsGranted("Permission3").Returns(false); //Permission3 is not granted
            //替换默认的权限校验类
            LocalIocManager.IocContainer.Register(Component.For <IPermissionChecker>().Instance(permissionChecker));

            _syncObj  = LocalIocManager.Resolve <MyTestClassToBeAuthorized_Sync>();
            _asyncObj = LocalIocManager.Resolve <MyTestClassToBeAuthorized_Async>();

            _syncObjForProtectedMethod  = LocalIocManager.Resolve <MyTestClassToBeAllowProtected_Sync>();
            _asyncObjForProtectedMethod = LocalIocManager.Resolve <MyTestClassToBeAllowProtected_Async>();
        }
        public AuthorizationInterceptor_Tests()
        {
            //SUT: AuthorizationInterceptor and AuthorizeAttributeHelper
            LocalIocManager.IocContainer.Register(
                Component.For <IFeatureChecker>().Instance(Substitute.For <IFeatureChecker>())
                );

            LocalIocManager.Register <IAuthorizationConfiguration, AuthorizationConfiguration>();
            LocalIocManager.Register <IMultiTenancyConfig, MultiTenancyConfig>();
            LocalIocManager.Register <AuthorizationInterceptor>(DependencyLifeStyle.Transient);
            LocalIocManager.Register <AbpAsyncDeterminationInterceptor <AuthorizationInterceptor> >(DependencyLifeStyle.Transient);
            LocalIocManager.Register <IAuthorizationHelper, AuthorizationHelper>(DependencyLifeStyle.Transient);
            LocalIocManager.IocContainer.Register(
                Component.For <MyTestClassToBeAuthorized_Sync>().Interceptors <AbpAsyncDeterminationInterceptor <AuthorizationInterceptor> >().LifestyleTransient(),
                Component.For <MyTestClassToBeAuthorized_Async>().Interceptors <AbpAsyncDeterminationInterceptor <AuthorizationInterceptor> >().LifestyleTransient(),
                Component.For <MyTestClassToBeAllowProtected_Async>().Interceptors <AbpAsyncDeterminationInterceptor <AuthorizationInterceptor> >().LifestyleTransient(),
                Component.For <MyTestClassToBeAllowProtected_Sync>().Interceptors <AbpAsyncDeterminationInterceptor <AuthorizationInterceptor> >().LifestyleTransient()
                );

            //Mock session
            var session = Substitute.For <IAbpSession>();

            session.TenantId.Returns(1);
            session.UserId.Returns(1);
            LocalIocManager.IocContainer.Register(Component.For <IAbpSession>().Instance(session));

            //Mock permission checker
            var permissionChecker = Substitute.For <IPermissionChecker>();

            permissionChecker.IsGrantedAsync("Permission1").Returns(true);
            permissionChecker.IsGrantedAsync("Permission2").Returns(true);
            permissionChecker.IsGrantedAsync("Permission3").Returns(false); //Permission3 is not granted

            permissionChecker.IsGranted("Permission1").Returns(true);
            permissionChecker.IsGranted("Permission2").Returns(true);
            permissionChecker.IsGranted("Permission3").Returns(false); //Permission3 is not granted

            LocalIocManager.IocContainer.Register(Component.For <IPermissionChecker>().Instance(permissionChecker));

            _syncObj  = LocalIocManager.Resolve <MyTestClassToBeAuthorized_Sync>();
            _asyncObj = LocalIocManager.Resolve <MyTestClassToBeAuthorized_Async>();

            _syncObjForProtectedMethod  = LocalIocManager.Resolve <MyTestClassToBeAllowProtected_Sync>();
            _asyncObjForProtectedMethod = LocalIocManager.Resolve <MyTestClassToBeAllowProtected_Async>();
        }