public AuthorizationInterceptor_Tests()
        {
            //SUT: AuthorizationInterceptor and AuthorizeAttributeHelper
            LocalIocManager.IocContainer.Register(
                Component.For <IFeatureChecker>().Instance(Substitute.For <IFeatureChecker>())
                );
            LocalIocManager.Register <AuthorizationInterceptor>(DependencyLifeStyle.Transient);
            LocalIocManager.Register <IAuthorizationHelper, AuthorizationHelper>(DependencyLifeStyle.Transient);
            LocalIocManager.IocContainer.Register(
                Component.For <MyTestClassToBeAuthorized_Sync>().Interceptors <AuthorizationInterceptor>().LifestyleTransient(),
                Component.For <MyTestClassToBeAuthorized_Async>().Interceptors <AuthorizationInterceptor>().LifestyleTransient()
                );

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

            session.TenantId.Returns(1);
            session.UserId.Returns(1);
            LocalIocManager.IocContainer.Register(Component.For <IAbpSession>().UsingFactoryMethod(() => 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
            LocalIocManager.IocContainer.Register(Component.For <IPermissionChecker>().UsingFactoryMethod(() => permissionChecker));

            _syncObj  = LocalIocManager.Resolve <MyTestClassToBeAuthorized_Sync>();
            _asyncObj = LocalIocManager.Resolve <MyTestClassToBeAuthorized_Async>();
        }
        public AuthorizationInterceptor_Tests()
        {
            //SUT: AuthorizationInterceptor and AuthorizeAttributeHelper
            LocalIocManager.IocContainer.Register(
                Component.For<IFeatureChecker>().Instance(Substitute.For<IFeatureChecker>())
                );

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

            //Mock session
            var session = Substitute.For<IAbpSession>();
            session.TenantId.Returns(1);
            session.UserId.Returns(1);
            LocalIocManager.IocContainer.Register(Component.For<IAbpSession>().UsingFactoryMethod(() => 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
            LocalIocManager.IocContainer.Register(Component.For<IPermissionChecker>().UsingFactoryMethod(() => permissionChecker));

            _syncObj = LocalIocManager.Resolve<MyTestClassToBeAuthorized_Sync>();
            _asyncObj = LocalIocManager.Resolve<MyTestClassToBeAuthorized_Async>();
        }
示例#3
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>();
        }
示例#4
0
        public AuthorizationInterceptor_Tests()
        {
            //SUT: AuthorizationInterceptor and AuthorizeAttributeHelper
            LocalIocManager.Register <AuthorizationInterceptor <int, long> >(DependencyLifeStyle.Transient);
            LocalIocManager.Register <IAuthorizeAttributeHelper <int, long>, AuthorizeAttributeHelper <int, long> >(DependencyLifeStyle.Transient);


            LocalIocManager.IocContainer.Register(
                Component.For(typeof(IAuthorizeAttributeHelper <,>)).ImplementedBy(typeof(AuthorizeAttributeHelper <,>)),
                Component.For <MyTestClassToBeAuthorized_Sync>().Interceptors <AuthorizationInterceptor <int, long> >().LifestyleTransient(),
                Component.For <MyTestClassToBeAuthorized_Async>().Interceptors <AuthorizationInterceptor <int, long> >().LifestyleTransient()
                );

            var handlers = LocalIocManager.IocContainer.Kernel.GetAssignableHandlers(typeof(object));
            var obj      = handlers.FirstOrDefault(
                x => x.ComponentModel.Implementation.IsGenericType && x.ComponentModel.Implementation.GetGenericTypeDefinition() == typeof(AuthorizationInterceptor <,>));

            var i     = obj.ComponentModel.Implementation;
            var type1 = i.GetGenericArguments()[0];
            var type2 = i.GetGenericArguments()[1];

            dynamic helper = LocalIocManager.ResolveAsDisposable(typeof(IAuthorizeAttributeHelper <,>).MakeGenericType(type1, type2));
            dynamic obj2   = helper.Object;

            var ss = Substitute.For <AbpAuthorizeAttribute>();


            //obj2.Authorize(ss);


            //helper.Object.
            //Mock session
            var session = Substitute.For <IAbpSession <int, long> >();

            session.TenantId.Returns(1);
            session.UserId.Returns(1);
            LocalIocManager.IocContainer.Register(Component.For <IAbpSession <int, long> >().UsingFactoryMethod(() => session));

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

            permissionChecker.IsGrantedAsync("Permission1").Returns(Task.FromResult(true));
            permissionChecker.IsGrantedAsync("Permission2").Returns(Task.FromResult(true));
            permissionChecker.IsGrantedAsync("Permission3").Returns(Task.FromResult(false)); //Permission3 is not granted
            LocalIocManager.IocContainer.Register(Component.For <IPermissionChecker <int, long> >().UsingFactoryMethod(() => permissionChecker));

            _syncObj  = LocalIocManager.Resolve <MyTestClassToBeAuthorized_Sync>();
            _asyncObj = LocalIocManager.Resolve <MyTestClassToBeAuthorized_Async>();
        }
        public AuthorizationInterceptor_Tests()
        {
            //SUT: AuthorizationInterceptor and AuthorizeAttributeHelper
            LocalIocManager.Register<AuthorizationInterceptor<int,long>>(DependencyLifeStyle.Transient);
            LocalIocManager.Register<IAuthorizeAttributeHelper<int, long>, AuthorizeAttributeHelper<int, long>>(DependencyLifeStyle.Transient);


            LocalIocManager.IocContainer.Register(
                Component.For(typeof (IAuthorizeAttributeHelper<,>)).ImplementedBy(typeof (AuthorizeAttributeHelper<,>)),
                Component.For<MyTestClassToBeAuthorized_Sync>().Interceptors<AuthorizationInterceptor<int, long>>().LifestyleTransient(),
                Component.For<MyTestClassToBeAuthorized_Async>().Interceptors<AuthorizationInterceptor<int, long>>().LifestyleTransient()
                );

            var handlers = LocalIocManager.IocContainer.Kernel.GetAssignableHandlers(typeof(object));
            var obj = handlers.FirstOrDefault(
                x => x.ComponentModel.Implementation.IsGenericType && x.ComponentModel.Implementation.GetGenericTypeDefinition() == typeof (AuthorizationInterceptor<,>));

            var i = obj.ComponentModel.Implementation;
            var type1 = i.GetGenericArguments()[0];
            var type2 = i.GetGenericArguments()[1];

            dynamic helper = LocalIocManager.ResolveAsDisposable(typeof(IAuthorizeAttributeHelper<,>).MakeGenericType(type1, type2));
            dynamic obj2 = helper.Object;

            var ss = Substitute.For<AbpAuthorizeAttribute>();
            

            //obj2.Authorize(ss);
            

            //helper.Object.
            //Mock session
            var session = Substitute.For<IAbpSession<int, long>>();
            session.TenantId.Returns(1);
            session.UserId.Returns(1);
            LocalIocManager.IocContainer.Register(Component.For<IAbpSession<int, long>>().UsingFactoryMethod(() => session));

            //Mock permission checker
            var permissionChecker = Substitute.For<IPermissionChecker<int, long>>();
            permissionChecker.IsGrantedAsync("Permission1").Returns(Task.FromResult(true));
            permissionChecker.IsGrantedAsync("Permission2").Returns(Task.FromResult(true));
            permissionChecker.IsGrantedAsync("Permission3").Returns(Task.FromResult(false)); //Permission3 is not granted
            LocalIocManager.IocContainer.Register(Component.For<IPermissionChecker<int, long>>().UsingFactoryMethod(() => permissionChecker));

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