Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

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)

[SharePointPermissionAttribute(SecurityAction.LinkDemand, ObjectModel = true)]
[SharePointPermissionAttribute(SecurityAction.InheritanceDemand, ObjectModel = true)]
public interface ISocialSecurityTrimmer

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;
        }
    }
}


Show:
© 2015 Microsoft