This method works correctly and verifies whether a user has permissions to view or edit the first parameter (value). I think it is not the best implementation, because there are too many conditions and the List
object isn't necessary. How to improve it?
protected override Exception GetValidationException( object value, string name )
{
List<Entity> entities = value is int ? new List<Entity>( 1 ) { EntityCache.Entity.GetEntity( (int) value ) } :
value is Guid ? new List<Entity>( 1 ) { EntityCache.Entity.GetEntityByGuid( (Guid) value ) } :
value is int[] ? (value as int[]).Select( i => EntityCache.Entity.GetEntity( i ) ).ToList() :
value is Entity ? new List<Entity>( 1 ) { value as Entity } :
value is Entity[] ? (value as Entity[]).ToList() :
null;
if( entities == null )
throw new ArgumentException( string.Format( RI.UnknownArgumentType, name, value, value.GetType().Name ) );
bool hasPermission = (from one in entities
select EntityPermissionCache.PermissionChecker( one.Id, one.EntityOrganizationId ) &&
_checkEditInsteadOfView ? EntityTypePermissionCache.PermissionChecker( true, one.TypeId ) : true)
.All( a => a );
return hasPermission ? null : new AccessViolationException( RI.AccessDenied );
}
List<Entity>
and then a second method to validate that list. \$\endgroup\$