问题描述
我最近下载了Rhino.Security,并且正在尝试对实体实施权限.由于我喜欢Ninject(v2),因此我想举一个简单的例子作为开始.在我的NinjectModule中,我绑定了存储库和服务:
I've recently downloaded Rhino.Security and I am trying to implement permissions on a entity.Since I like Ninject (v2) I would like to put together a simple example to start with.In my NinjectModule I've bound the repository and the services:
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();
我正在测试控制台应用程序中的所有内容,并且一切正常.我可以毫无问题地运行此测试:
I am testing everything in a console app and everything works fine.I can run this test with no problem:
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();
}
}
现在,我想为一个帐户定义一个权限;像这样的东西:
Now, I would like to define a permission for an account; something like this:
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在解决方案中的示例代码中所做的那样:
I've defined my class, as Ayende did in the sample code found in the solution:
public class AccountInfromationExtractor : IEntityInformationExtractor<Model.Account>
{
...
}
并且我尝试将接口IEntityInformationExtractor绑定(在我的ninject模块中)
and I've tried to bind (in my ninject module) the interface IEntityInformationExtractor to the class
Bind<IEntityInformationExtractor<Model.Account>>()
.To<AccountInfromationExtractor>();
但是当我运行我的应用程序时,我得到一个"对象引用未设置为对象的实例".在安全性类中.服务定位器无法解析该实例,并且它会在此处生成异常:
but when I run my app I get an "Object reference not set to an instance of an object." in the Security class.The service locator cannot resolve the instance and it generates and exception right here:
var extractor = ServiceLocator.Current.GetInstance<IEntityInformationExtractor<TEntity>>();
有人在Rhino.Security上尝试使用Ninject并能够为我提供帮助吗?
Is there anyone who tried to use Ninject with Rhino.Security and would be able to help me?
谢谢
推荐答案
最后,我无法使其与Ninject一起使用,所以我切换到了StructureMap.
Rhino.Security与MS ServiceLocator一起解析IEntityInformationExtractor.我已经找到了 StructureMap
At the end I couldn't manage to make it work with Ninject so I switched to StructureMap.
Rhino.Security works with MS ServiceLocator to resolve IEntityInformationExtractor.I've found a ServiceLocator adapter for StructureMap
所以我在项目中引用了以下程序集:
So I've referenced in my project the following assemblies:
Microsoft.Practices.ServiceLocation
StructureMap
StructureMapAdapter
Microsoft.Practices.ServiceLocation
StructureMap
StructureMapAdapter
将我的代码更改为使用StructureMap:
changed my code to use StructureMap:
public static class Bootstrapper
{
public static void Initialize()
{
ObjectFactory.Initialize(cfg =>
{
cfg.AddRegistry<StructureMapRegistry>();
});
ServiceLocator.SetLocatorProvider(() => new StructureMapServiceLocator(ObjectFactory.Container));
}
}
和我的StructureMapRegistry类:
and my StructureMapRegistry class:
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>()));
});
}
}
我希望这可以对某人有所帮助.
I hope this might help someone.
这篇关于Rhino.Security和IEntityInformationExtractor的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!