NHIbernate & Rhino-Security does not save unless I call session.Flush()

367 views Asked by At

I am having an issue using nHibernate and Rhino.Security. After struggling for hours to get the right config setup, I finally got the code to run without any errors. However, no entries are being saved to the database unless I call session.Flush().

Looking at various examples on the net; I should not have to call flush.

Here’s my config code:

var cfg = new Configuration()
             .SetProperty(Environment.ConnectionDriver, typeof(SqlClientDriver).AssemblyQualifiedName)
             .SetProperty(Environment.Dialect, typeof(MsSql2008Dialect).AssemblyQualifiedName)
             .SetProperty(Environment.ConnectionString, "………")
             .SetProperty(Environment.ProxyFactoryFactoryClass, typeof(ProxyFactoryFactory).AssemblyQualifiedName)
             .SetProperty(Environment.ReleaseConnections, "on_close")
             .SetProperty(Environment.UseSecondLevelCache, "true")
             .SetProperty(Environment.UseQueryCache, "true")
             .SetProperty(Environment.CacheProvider, typeof(HashtableCacheProvider).AssemblyQualifiedName)
             .AddAssembly("GA.CAP.Website")
        ;

        Security.Configure<AspNetUser>(cfg, SecurityTableStructure.Prefix);

        var factory = cfg.BuildSessionFactory();

        var session = factory.OpenSession();


        var authorizationRepository = new AuthorizationRepository(session);
        IoC.Container.RegisterInstance<IAuthorizationRepository>(authorizationRepository);
        var permissionBuilderService = new PermissionsBuilderService(session, authorizationRepository);
        IoC.Container.RegisterInstance<IPermissionsBuilderService>(permissionBuilderService);
        var permissionService = new PermissionsService(authorizationRepository, session);
        IoC.Container.RegisterInstance<IPermissionsService>(permissionService);
        var authService = new AuthorizationService(permissionService, authorizationRepository);
        IoC.Container.RegisterInstance<IAuthorizationService>(authService);

Test code:

    authorizationRepository.CreateUsersGroup("GAAdmins");
    var group = authorizationRepository.GetUsersGroupByName("GAAdmins");

The GetUsersGroupByName call returns null. If I add a session.Flush call in between the two calls, it works fine and returns the group.

Based on examples in various blogs, such as this one, I should not have to call flush. In addition, the test cases included with the Rhino.Security code do not do any flushing as shown here:

This is straight out of Rhino.Security's test case fixture:

    // on first deploy
    Operation operation = authorizationRepository.CreateOperation("/Account/View");
    // when creating account
    UsersGroup group = authorizationRepository.CreateUsersGroup("Belongs to " + account.Name);

    // setting permission so only associated users can view
    permissionsBuilderService
        .Allow(operation)
        .For(group)
        .On(account)
        .DefaultLevel()
        .Save();

    // when adding user to account
    authorizationRepository.AssociateUserWith(user, group);

    bool allowed = authorizationService.IsAllowed(user, account, "/Account/View");
    Assert.True(allowed);

Is there some setting I am missing somewhere?

Thanks,

Rick

1

There are 1 answers

0
Ayende Rahien On BEST ANSWER

That is expected, RS uses the same session as your code, and calling Flush internally may result in unintended consequences. Commit your transaction or call Flush