Rhino.Security和IEntityInformationExtractor

时间:2010-11-23 16:03:06

标签: structuremap rhino ninject-2 rhino-security

我最近下载了Rhino.Security,我正在尝试在实体上实现权限。 因为我喜欢Ninject(v2),所以我想把一个简单的例子放在一起开始。 在我的NinjectModule中,我绑定了存储库和服务:

Bind<ISessionFactory>()
    .ToProvider(new SessionFactoryProvider())
    .InSingletonScope();
Bind<ISession>().ToProvider(new SessionProvider())
    .InSingletonScope();
Bind<IAuthorizationRepository>()
     .To<AuthorizationRepository>()
     .InSingletonScope();
Bind<IPermissionsService>()
    .To<PermissionsService>()
    .InSingletonScope();
Bind<IAuthorizationService>()
    .To<AuthorizationService>()
    .InSingletonScope();
Bind<IPermissionsBuilderService>()
    .To<PermissionsBuilderService>()
    .InSingletonScope();

我在控制台应用程序中测试所有内容,一切正常。 我可以毫无问题地运行这个测试:

public void RunTest()
{
Model.User user1;
Rhino.Security.Model.UsersGroup grp1;

    using (session)
    {
        session.BeginTransaction();

        user1 = new Model.User { Name = "xxx xxx" };
        session.Save(user1);
        session.Flush();

        grp1 = authorizationRepository.CreateUsersGroup("Administrators");
        session.Flush();

        authorizationRepository.AssociateUserWith(user1, grp1);
        session.Flush();

        var OpAccountEdit = authorizationRepository.CreateOperation("/Account/Edit");
        session.Flush();

        permissionsBuilderService
        .Allow(OpAccountEdit)
        .For(grp1)
        .OnEverything()
        .Level(10)
        .Save();

        permissionsBuilderService
        .Deny(OpAccountEdit)
        .For(user1)
        .OnEverything()
        .Level(10)
        .Save();

        Console.WriteLine(authorizationService.IsAllowed(user1, "/Account/Edit"));

        session.Transaction.Rollback();

        Console.ReadLine();
       }
    }

现在,我想为帐户定义权限;像这样的东西:

account1 = new Model.Account() { Name = "My Account", SecurityKey = new Guid(), Id = 1 };
session.Save(account1);
session.Flush();

permissionsBuilderService
   .Allow("/Account/Delete")
   .For(user1)
   .On(account1)
   .Level(20)
   .Save();

我已经定义了我的课程,就像Ayende在解决方案中找到的示例代码中所做的那样:

public class AccountInfromationExtractor : IEntityInformationExtractor<Model.Account>
{
...
}

我尝试将(在我的ninject模块中)接口IEntityInformationExtractor绑定到类

 Bind<IEntityInformationExtractor<Model.Account>>()
   .To<AccountInfromationExtractor>();

但是当我运行我的应用程序时,我得到一个“对象引用未设置为对象的实例”。在安全类中。 服务定位器无法解析实例,它在此处生成和异常:

var extractor = ServiceLocator.Current.GetInstance<IEntityInformationExtractor<TEntity>>();

是否有人试图将Ninject与Rhino.Security一起使用并能够帮助我?

由于

1 个答案:

答案 0 :(得分:1)

最后我无法使其与Ninject一起工作,所以我切换到StructureMap Rhino.Security与MS ServiceLocator一起使用来解析IEntityInformationExtractor。 我找到了StructureMap

的ServiceLocator适配器

所以我在我的项目中引用了以下程序集:

Microsoft.Practices.ServiceLocation
StructureMap
StructureMapAdapter

将我的代码更改为使用StructureMap:

public static class Bootstrapper
    {
        public static void Initialize()
        {
            ObjectFactory.Initialize(cfg =>
            {
                cfg.AddRegistry<StructureMapRegistry>();
            });
            ServiceLocator.SetLocatorProvider(() => new StructureMapServiceLocator(ObjectFactory.Container));
        }
    }

和我的StructureMapRegistry类:

public class StructureMapRegistry : Registry
{
    public StructureMapRegistry()
    {
        string ConnDb = "Data Source=(local); Initial Catalog=RhinoSecurity_Test; Trusted_Connection=true;";

        For<ISessionFactory>()
            .Singleton()
            .TheDefault.Is.ConstructedBy(() => new NHSessionFactory(ConnDb, false).SessionFactory);
        For<ISession>()
            .Singleton()
            .TheDefault.Is.ConstructedBy(x => x.GetInstance<ISessionFactory>().OpenSession());
        For<IAuthorizationRepository>()
             .Use<AuthorizationRepository>();
        For<IPermissionsService>()
            .Use<PermissionsService>();
        For<IAuthorizationService>()
            .Use<AuthorizationService>();
        For<IPermissionsBuilderService>()
            .Use<PermissionsBuilderService>();
        For<IEntityInformationExtractor<Model.Account>>()
            .Use(p =>
                {
                return (new AccountInfromationExtractor(p.GetInstance<ISession>()));
                });
    }
}

我希望这可以帮助别人。