Rhino.Security and IEntityInformationExtractor

I recently downloaded Rhino.Security, and I'm trying to implement permissions for an object. Since I like Ninject (v2), I would like to put together a simple example to get you started. In my NinjectModule, I linked the repository and 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 application and everything is working fine. I can run this test without problems:

 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 determine the permission for the account; something like that:

 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 defined my class, as Ayende did in the code example found in the solution:

 public class AccountInfromationExtractor : IEntityInformationExtractor<Model.Account> { ... } 

and I tried to associate (in my ninject module) the IEntityInformationExtractor interface with the class

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

but when I run my application, I get the link " Object not installed in the instance of the object ." in the safety class. The service locator cannot resolve the instance, and it generates and excludes here:

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

Is there anyone who tried to use Ninject with Rhino.Security and could help me?

thanks

+2
source share
1 answer

In the end, I was not able to get it to work with Ninject, so I switched to StructureMap.
Rhino.Security works with MS ServiceLocator to resolve IEntityInformationExtractor. I found a ServiceLocator adapter for StructureMap

So, in my project I referred to the following assemblies:

Microsoft.Practices.ServiceLocation
Structuremap
StructureMapAdapter

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

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 can help someone.

+1
source

Source: https://habr.com/ru/post/1337393/


All Articles