我最近下载了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.保安能帮我吗?
谢谢