ISocialSecurityTrimmer interface

Defines methods for security trimming lists that contain social data.

Namespace:  Microsoft.Office.Server.SocialData
Assembly:  Microsoft.Office.Server.UserProfiles (in Microsoft.Office.Server.UserProfiles.dll)

Syntax

'Declaration
<SharePointPermissionAttribute(SecurityAction.LinkDemand, ObjectModel := True)> _
<SharePointPermissionAttribute(SecurityAction.InheritanceDemand, ObjectModel := True)> _
Public Interface ISocialSecurityTrimmer
'Usage
Dim instance As ISocialSecurityTrimmer
[SharePointPermissionAttribute(SecurityAction.LinkDemand, ObjectModel = true)]
[SharePointPermissionAttribute(SecurityAction.InheritanceDemand, ObjectModel = true)]
public interface ISocialSecurityTrimmer

Remarks

The following code sample uses this interface to create a custom SearchSocialSecurityTrimmer.

using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Text.RegularExpressions;
using Microsoft.Office.Server.SocialData;

namespace CustomSecurityTrimmer
{
    public sealed class CustomSocialSecurityTrimmer : SearchSocialSecurityTrimmer
    {
        private const string C_KEY_EXCLUDEPATHS = "ExcludePaths";
        private const string C_KEY_INCLUDEPATHS = "IncludePaths";

        private static object syncRoot = new Object();
        private static volatile List<Regex> m_excludePaths;
        private static volatile List<Regex> m_includePaths;

        #region ISocialSecurityTrimmer interface
        public override void Initialize(NameValueCollection CustomProperty)
        {
            lock (syncRoot)
            {
                if (m_includePaths != null)
                    return;

                m_includePaths = new List<Regex>();
                m_excludePaths = new List<Regex>();

                // Cache parsed Url Regex objects's list onto static area to improve performance.
                // If you change the properties, you need to restart IIS to clear cache.
                string strIncludePaths = CustomProperty[C_KEY_INCLUDEPATHS];
                string strExcludePaths = CustomProperty[C_KEY_EXCLUDEPATHS];

                if (!String.IsNullOrEmpty(strIncludePaths))
                {
                    string[] strArray = strIncludePaths.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
                    foreach (string strRegex in strArray)
                    {
                        m_includePaths.Add(new Regex(strRegex, RegexOptions.Singleline | RegexOptions.IgnoreCase));
                    }
                }

               if (!String.IsNullOrEmpty(strExcludePaths))
                {
                    string[] strArray = strExcludePaths.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
                    foreach (string strRegex in strArray)
                    {
                        m_excludePaths.Add(new Regex(strRegex, RegexOptions.Singleline | RegexOptions.IgnoreCase));
                    }
                }
            }
        }

        public override List<Uri> Trim(List<Uri> uris)
        {
            List<Uri> resultUris = new List<Uri>();
            List<Uri> toBeTrimmedUris = new List<Uri>();

            foreach (Uri checkUri in uris)
            {
                if (IsUrlMatched(checkUri, m_includePaths))
                {
                    toBeTrimmedUris.Add(checkUri);
                }
                else if (IsUrlMatched(checkUri, m_excludePaths))
                {
                    resultUris.Add(checkUri);
                }
                else
                {
                    toBeTrimmedUris.Add(checkUri);
                }
            }

            // Execute Security Trimming.
            resultUris.AddRange(base.Trim(toBeTrimmedUris));

            return resultUris;
        }
        #endregion

        private static bool IsUrlMatched(Uri uri, List<Regex> strPatterns)
        {
            bool isMatch = false;

            foreach (Regex strPattern in strPatterns)
            {
                if (strPattern.IsMatch(uri.AbsoluteUri))
                {
                    isMatch = true;
                    break;
                }
            }
            return isMatch;
        }
    }
}

See also

Reference

ISocialSecurityTrimmer members

Microsoft.Office.Server.SocialData namespace