2

我最近下载了 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>
{
...
}

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

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

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

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

有没有人尝试将 Ninject 与 Rhino.Security 一起使用并且能够帮助我?

谢谢

4

1 回答 1

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>()));
                });
    }
}

我希望这可以帮助某人。

于 2010-11-30T10:16:45.907 回答