Skip to content

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.

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

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

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

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

# Bind to the Property Pattern
$pattern = $admService.OpenObject($propertyPatternPath, $NULL, $NULL, 0)

# Bind to the user
$user = $admService.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."
}
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 adsNS = new AdmNamespace();
        IAdmService2 admService = (IAdmService2)adsNS.GetServiceDirectly("localhost");

        // Build ADS path to the Property Pattern
        String propertyPatternsPath = admService.Backend.GetConfigurationContainerPath(
            "PropertyPatterns");
        AdsPath propertyPatternsPathObj = new AdsPath(propertyPatternsPath);
        AdsPath propertyPatternPath = propertyPatternsPathObj.CreateChildPath(
            "CN=" + patternName);

        // Bind to the Property Pattern
        IAdmPropertyPattern pattern = (IAdmPropertyPattern)admService.OpenObject(
            propertyPatternPath.ToString(), null, null, 0);

        // Bind to the user
        IAdmTop user = (IAdmTop)admService.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