我最近下载了 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 一起使用并且能够帮助我?
谢谢