Determining whether property pattern applies to user

The following code sample checks whether a property pattern is effective for a user and outputs activity scope items that include or exclude it.

PowerShell
[Reflection.Assembly]::LoadWithPartialName("Softerra.Adaxes.Adsi")

$patternName = "My Pattern"
$userDN = "CN=John Smith,CN=Users,DC=domain,DC=com"

# Connect to the Adaxes service
$ns = New-Object "Softerra.Adaxes.Adsi.AdmNamespace"
$service = $ns.GetServiceDirectly("localhost")

# Build ADS path to the property pattern
$propertyPatternsPath = $service.Backend.GetConfigurationContainerPath(
    "PropertyPatterns")
$propertyPatternsPathObj = New-Object "Softerra.Adaxes.Adsi.AdsPath" `
    $propertyPatternsPath
$propertyPatternPath = $propertyPatternsPathObj.CreateChildPath(
    "CN=$patternName")

# Bind to the property pattern
$pattern = $service.OpenObject($propertyPatternPath, $null, $null, 0)

# Bind to the user
$user = $service.OpenObject("Adaxes://$userDN", $null, $null, 0)

# Iterate through activity scope items
$itemsIncludingUser = @()
$itemsExcludingUser = @()
foreach ($item in $pattern.ActivityScopeItems)
{
    if (-not($item.IsEffectiveForEx($user, "adm-PropertyPattern")))
    {
        continue # the item neither includes nor excludes the user
    }
    
    # Build the activity scope item description
    if ($item.Type -eq "ADM_SCOPEBASEOBJECTTYPE_ALL_DIRECTORY")
    {
        $itemDescription = "All Objects"
    }
    else
    {
        $itemDescription = $item.BaseObject.AdsPath
    }
    
    if ($item.Exclude)
    {
        $itemsExcludingUser += $itemDescription
    }
    else
    {
        $itemsIncludingUser += $itemDescription
    }
}

if ($itemsExcludingUser.Length -gt 0)
{
    Write-Host "The property pattern is NOT effective for the user."
    Write-Host "Activity scope item(s) that exclude the user from the scope:"
    foreach ($itemDescription in $itemsExcludingUser)
    {
        Write-Host "`t$itemDescription" 
    }
}
elseif ($itemsIncludingUser.Length -gt 0)
{
    Write-Host "The property pattern IS effective for the user."
    Write-Host "Activity scope item(s) that include the user to the scope:"
    foreach ($itemDescription in $itemsIncludingUser)
    {
        Write-Host "`t$itemDescription"
    }
}
else
{
    Write-Host "The property pattern is NOT effective for the user."
}
C#
using System;
using System.Collections.Generic;
using Softerra.Adaxes.Adsi;
using Softerra.Adaxes.Interop.Adsi;
using Softerra.Adaxes.Interop.Adsi.PersistentObjects;
using Softerra.Adaxes.Interop.Adsi.PropertyPatterns;

class Program
{
    static void Main(string[] args)
    {
        const string patternName = "My Pattern";
        const string userPath = "Adaxes://CN=John Smith,CN=Users,DC=domain,DC=com";

        // Connect to the Adaxes service
        AdmNamespace ns = new AdmNamespace();
        IAdmService2 service = (IAdmService2)ns.GetServiceDirectly("localhost");

        // Build ADS path to the property pattern
        string propertyPatternsPath = service.Backend.GetConfigurationContainerPath(
            "PropertyPatterns");
        AdsPath propertyPatternsPathObj = new AdsPath(propertyPatternsPath);
        AdsPath propertyPatternPath = propertyPatternsPathObj.CreateChildPath(
            "CN=" + patternName);

        // Bind to the property pattern
        IAdmPropertyPattern pattern = (IAdmPropertyPattern)service.OpenObject(
            propertyPatternPath.ToString(), null, null, 0);

        // Bind to the user
        IAdmTop user = (IAdmTop)service.OpenObject(userPath, null, null, 0);

        // Iterate through activity scope items
        List<string> itemsIncludingUser = new List<string>();
        List<string> itemsExcludingUser = new List<string>();
        foreach (IAdmActivityScopeItem2 item in pattern.ActivityScopeItems)
        {
            if (!(item.IsEffectiveForEx(user, "adm-PropertyPattern")))
            {
                continue; // the item neither includes nor excludes the user
            }

            // Build the activity scope item description
            string itemDescription = string.Empty;
            if (item.Type == ADM_SCOPEBASEOBJECTTYPE_ENUM.ADM_SCOPEBASEOBJECTTYPE_ALL_DIRECTORY)
            {
                itemDescription = "All Objects";
            }
            else
            {
                itemDescription = item.BaseObject.ADsPath;
            }

            if (item.Exclude)
            {
                itemsExcludingUser.Add(itemDescription);
            }
            else
            {
                itemsIncludingUser.Add(itemDescription);
            }
        }

        if (itemsExcludingUser.Count > 0)
        {
            Console.WriteLine("The property pattern is NOT effective for the user.");
            Console.WriteLine("Activity scope item(s) that exclude the user from the scope:");
            foreach (string itemDescription in itemsExcludingUser)
            {
                Console.WriteLine("\t" + itemDescription);
            }
        }
        else if (itemsIncludingUser.Count > 0)
        {
            Console.WriteLine("The property pattern IS effective for the user.");
            Console.WriteLine("Activity scope item(s) that include the user to the scope:");
            foreach (string itemDescription in itemsIncludingUser)
            {
                Console.WriteLine("\t" + itemDescription);
            }
        }
        else
        {
            Console.WriteLine("The property pattern is NOT effective for the user.");
        }
    }
}

See also