Validator class questions

Posts   
 
    
erichar11
User
Posts: 268
Joined: 08-Dec-2003
# Posted on: 19-Jan-2005 09:34:05   

Haven't used validators before but wanted to know if they can help with a potential problem.

I have a multitenent application (single database, multiple companies). Each table has associated with it a companyId and each user, upon accessing the application, gets their companyId set in a session variable. Now I need to make sure that for all entities and/or managers that data returned is for that specific companyId only (Don't want someone to get access to someone elses data rage . I could require that each method require a companyId (i.e. ModuleManager.GetAllModules(int companyId) but I was wondering if the validators could be used here instead since each entity contains a companyId. Any thoughts.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 19-Jan-2005 10:53:58   

Validator classes work on fields only, they check field values. IEntityValidator implementations work on entire entities but from memory to database (will be the other way around also, not yet).

From db to memory, you can limit the data read by filters. So what you want is that with every fetch, the companyID filter is added as well. You can accomplish this for example by deriving a class from DataAccessAdapter and always make sure that predicate is added to the filter.

Frans Bouma | Lead developer LLBLGen Pro
erichar11
User
Posts: 268
Joined: 08-Dec-2003
# Posted on: 19-Jan-2005 11:39:45   

Frans, what you suggest makes sense. Is there any docs on how to derive a DataAccessAdapter and add you own functionality? I see this for entities but not for the dataaccess adapter. I could be missing it.

Fabrice
User
Posts: 180
Joined: 25-May-2004
# Posted on: 19-Jan-2005 12:09:16   

inherit the dataAccess adapter and override these methods : - FetchEntity (2 with different parameters) - FetchEntityCollection (3 with diff param) - FetchEntityUsingUniqueConstraint

In each override add a filter on the given IRelationPredicateBucket parameter (or IPredicateExpression for FetchEntityUsingUniqueConstraint)

erichar11
User
Posts: 268
Joined: 08-Dec-2003
# Posted on: 19-Jan-2005 19:15:13   

First attempt at overriding the dataaccessadapter method for FetchEntity. I create a relationpredicatebucket but I can't figure out how to apply that to the the entityToFetch.

public override bool FetchEntity(SD.LLBLGen.Pro.ORMSupportClasses.IEntity2 entityToFetch)
{
     IRelationPredicateBucket bucket = new RelationPredicateBucket();
     bucket.PredicateExpression.Add(PredicateFactory.CompareValue(entityToFetch.Field["CompanyId"],ComparisonOperator.Equal, 0));
            
     return base.FetchEntity (entityToFetch);
        }

I also get an error "can't convert from IEntityField2 to PageTypeFieldIndex. Somehow I have got to convert the entityToFetch.Field["CompanyId"] to an enum I think.

Fabrice
User
Posts: 180
Joined: 25-May-2004
# Posted on: 19-Jan-2005 22:26:55   

The prediate was in my mind for FetchEntityCollection or FetchEntityWithUniqueConstraint

For the fetch of a single entity maybe you can fetch the entity (IEntity2 entity = base.FetchEntity(...)) and then check if the entity you get can be used by current user (entity.Fields["CompanyId"] == ... ?). If he can't, you have several options : - return null - return an empty object (but so you've to use Reflection to find and create an object on the same type that the object requested) - throw exception

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 20-Jan-2005 10:05:12   

erichar11 wrote:

First attempt at overriding the dataaccessadapter method for FetchEntity. I create a relationpredicatebucket but I can't figure out how to apply that to the the entityToFetch.

public override bool FetchEntity(SD.LLBLGen.Pro.ORMSupportClasses.IEntity2 entityToFetch)
{
     IRelationPredicateBucket bucket = new RelationPredicateBucket();
     bucket.PredicateExpression.Add(PredicateFactory.CompareValue(entityToFetch.Field["CompanyId"],ComparisonOperator.Equal, 0));
            
     return base.FetchEntity (entityToFetch);
        }

I also get an error "can't convert from IEntityField2 to PageTypeFieldIndex". Somehow I have got to convert the entityToFetch.Field["CompanyId"] to an enum I think.

If you're not in the posession of the enum, don't use the enum but use a FieldCompareValuePredicate instance: bucket.PredicateExpression.Add(new FieldCompareValuePredicate(entityToFetch.Fields["CompanyId"], null, ComparisonOperator.Equal, 0));

Frans Bouma | Lead developer LLBLGen Pro