This post is on the older side and its content may be out of date.
Be sure to visit our blogs homepage for our latest news, updates and information.
public
class
SearchResultsByPermissions : SearchResults
{
}
public
class
SearchResultsByPermissions : SearchResults
{
protected
override
void
InitializeControls(GenericContainer container)
{
Label resultsStats =
this
.ResultsStats;
//this is the original localized stats message. It shows all the results
var resultsStatsMessage = resultsStats.Text;
base
.InitializeControls(container);
if
(
string
.IsNullOrEmpty(
this
.Query))
{
this
.ResultsStats.Text =
string
.Empty;
return
;
}
int
numberOfAllResults = 0;
SearchResults.ISearcher searcher =
this
.GetSearcher();
//these are all the results (not filtered by permissions)
var allResults = searcher.Search(
this
.Query,
this
.IndexCatalogue, 0, 0,
out
numberOfAllResults);
if
(allResults ==
null
)
{
return
;
}
//here we will store only the results we have permissions to see
List<IDocument> securedResultSet =
new
List<IDocument>();
foreach
(var document
in
allResults)
{
var type = document.GetValue(
"ContentType"
);
var ID =
new
Guid(document.GetValue(
"OriginalItemId"
));
if
(TypeResolutionService.ResolveType(type) ==
typeof
(PageNode))
{
var manager = PageManager.GetManager();
//suppress the security checks so the code can be executed even if
//the current user doesn't have enough permissions
manager.Provider.SuppressSecurityChecks =
true
;
var page = manager.GetPageNode(ID);
if
(page !=
null
)
{
ISecuredObject securedObject = (ISecuredObject)page;
if
(SecurityExtensions.IsSecurityActionTypeGranted(securedObject, SecurityActionTypes.View))
{
securedResultSet.Add(document);
}
}
manager.Provider.SuppressSecurityChecks =
false
;
}
}
var numberOfSecuredSearchResults = securedResultSet.Count;
char
[] chrArray =
new
char
[] {
'\"'
};
string
str =
this
.Query.Trim(chrArray);
resultsStats.Text =
string
.Format(resultsStatsMessage, numberOfSecuredSearchResults, HttpUtility.HtmlEncode(str));
this
.ConfigurePager(numberOfSecuredSearchResults);
this
.ResultsList.DataSource =
null
;
int
itemsToSkip =
this
.GetItemsToSkip();
int
itemsToTake =
this
.GetItemsToTake();
ResultsList.DataSource = securedResultSet.Skip(itemsToSkip).Take(itemsToTake);
}
private
int
GetItemsToSkip()
{
if
(
this
.AllowPaging)
{
int
pageNumber =
this
.GetPageNumber(
this
.GetUrlEvaluationMode(),
this
.PageKey, 0,
"PageNumber"
);
if
(pageNumber > 0)
{
return
(pageNumber - 1) *
this
.ItemsPerPage;
}
}
return
0;
}
private
int
GetItemsToTake()
{
if
(!
this
.AllowPaging)
{
return
0;
}
return
this
.ItemsPerPage;
}
}
Subscribe to get all the news, info and tutorials you need to build better business apps and sites