1 min read

NHibernate & Spring - User Permissions Thoughts

I’m toying with an auction site idea, mostly to brush up my .Net 3.5, and I have an interesting problem. If I have objects who’s permissions, and possibly behaviour is dependant on the currently active user, how do I tell them about it?

I could first mark every object with an interface.

public interface ICurrentUserAware {        Person CurrentUser { get; set; }    }

Then inject each one as it was loaded from the database using the Hibernate Template’s interceptor functionality.

namespace MySouk.NHibernateData.SessionManagement{    public class UserAndOrWorldAwareEntityInterceptor : EmptyInterceptor, ICurrentUserAware, IWorldAware {        public Person CurrentUser { get; set; } public World MyWorld { get; set; } public override bool OnLoad(object entity, object id, object[] state, string[] propertyNames, NHibernate.Type.IType[] types)        {            if (entity is ICurrentUserAware)            {                ((ICurrentUserAware)entity).CurrentUser = CurrentUser;            } if (entity is IWorldAware)            {                ((IWorldAware)entity).MyWorld = MyWorld;            } return true;        }    }}

Which has the advantage of me being able to just manipulate the property for testing.

Alternatively, I could ignore the interceptor, then drag the current user out of Spring via the thread. I could just override that method when testing. Probably the best idea is to decide that I only want permission to vary, then I can mark that with attributes and wrap proxies around the domain objects as they come out of the database.