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();
I've defined my class, as Ayende did in the sample code found in the solution:
public class AccountInfromationExtractor : IEntityInformationExtractor<Model.Account>
{
...
}
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>>();
Is there anyone who tried to use Ninject with Rhino.Security and would be able to help me?
Thanks