I'm facing a problem betweek "claim passage", "token" and Search. Looking online this is a well known bug that everybody solves creating a custom security trimmer...
My solution to the problem works like this:
- Search With Elevated Privileges
- Get the token for the user I want to act like
- Try to access the Url returned from search
- If ok => Return it
- If not ok => not return it
This works correctly but.... Is there any way MORE efficent? If I access to the Item with Elevated Privileges and Check security for the user I pass it is that more efficent?
The way I'm using is like that:
using (SPSite site = new SPSite(ItemURL, token))
{
using (SPWeb web = site.OpenWeb())
{
bool security = SPSecurity.CatchAccessDeniedException;
SPSecurity.CatchAccessDeniedException = false;
try
{
Match m = Regex.Match(ItemURL, "ID=[0-9]+".ToLower()); //GET ID
int listItemID;
if (!int.TryParse(Regex.Match(m.Value, "[0-9]+".ToLower()).Value, out listItemID))
{
throw new Exception("Cannot access to the content: " + ItemURL + " ID: " + m.Value);
}
SPList list = web.Lists.TryGetList("Contenuti");
if (list != null)
{
SPListItem item = null;
try
{
item = list.Items.GetItemById(listItemID);
//OK CODE
}
catch (Exception exx)
{
//FAIL CODE
}
The other way I see is something like this:
SPSecurity.RunWithElevatedPrivileges(delegate(){
using (SPSite site = new SPSite(ItemURL))
{
using (SPWeb web = site.OpenWeb())
{
try
{
Match m = Regex.Match(ItemURL, "ID=[0-9]+".ToLower()); //Prende l'ID
int listItemID;
if (!int.TryParse(Regex.Match(m.Value, "[0-9]+".ToLower()).Value, out listItemID))
{
throw new Exception("Cannot access: " + ItemURL + " ID: " + m.Value);
}
SPList list = web.Lists.TryGetList("Contenuti");
if (list != null)
{
SPListItem item = null;
item = list.Items.GetItemById(listItemID);
var permission = item.GetUserEffectivePermissions("Username");
if (permission >= SPBasePermissions.Open)
//OK CODE
else
//FAIL CODE
I think first way is more efficent for the access, but has worst performance caused by exception...
Any suggestion / alternative'?
Thank you!