I have a Sitecore 8.1 instance using Solr 4.10. I have a custom field in my Solr index called read_roles. It is a list of Sitecore roles that can read a given item. Here is an example of what that looks like in the index when viewed through the Solr UI.
"read_roles_sm": [
"extranet\\DistribA",
"extranet\\Anonymous Role",
"extranet\\Approved Role" ]
My first question is this: Are these values really stored with two slashes like that? Or is a slash a special character or something and the Solr UI is adding that in only when it is being displayed on the web page?
I have code that adds a security filter to my query that looks like this:
public static IQueryable<T> ApplySecurityFilter<T>(this IQueryable<T> query) where T : SearchResultItem {
var userRoles = Sitecore.Context.User.Roles.Select(r => r.Name);
var readPredicate = PredicateBuilder.False<T>();
readPredicate = userRoles.Aggregate(readPredicate, (current, role) => current.Or(i => i["read_roles"].Equals(role)));
var denyPredicate = PredicateBuilder.True<T>();
denyPredicate = userRoles.Aggregate(denyPredicate, (current, role) => current.And(i => !i["denied_roles"].Equals(role)));
if (readPredicate.Body.NodeType != System.Linq.Expressions.ExpressionType.Constant) {
query = query.Filter(readPredicate);
}
if (readPredicate.Body.NodeType != System.Linq.Expressions.ExpressionType.Constant) {
query = query.Filter(denyPredicate);
}
return query;
}
The idea here is give me all results where at least one of the user's roles is in the read_roles field and none of the user's roles are in the denied_roles field.
I seem to be running in to all kinds of problems with slashes and spaces and I don't really understand what is going on here and I don't know how to troubleshoot it. I believe this code works fine as long as the user does not belong to any roles with a space in them.
Here is an example. If I am not logged in to the site (anonymous user which belongs to the extranet\Anonymous Role) and I try a search it works properly. I get the results that I would expect. And I see this in the Sitecore Search log file as part of the query that was sent to Solr:
&fq=(((-denied_roles_sm:("extranet\\Anonymous Role") AND read_roles_sm:("extranet\\Anonymous Role"))
However if I log in as a user that is part of the extranet\DistribA role I do not get the results I would expect and I see this in the Sitecore Search log file:
&fq=(((-denied_roles_sm:(extranet\DistribA) AND read_roles_sm:(extranet\DistribA))
What I have noticed is that if the role has a space in it then it seems to put quotes around it and also adds a second slash. But if the role name does not have a space in it then it doesn't do that. I feel like I am not understanding something fundamental about how Solr handles things like spaces and slashes in queries. Any help is greatly appreciated.