Script Repository


Check whether email and username are unique

January 11, 2017
1432

The script checks whether the email address and username specified for a new user are unique. If either the username or the email address are not unique, new user creation will be cancelled. To use the script with Adaxes, create a Business Rule triggered before creating a user. For details, see Validate/Modify User Input Using a Script.

Edit Remove
PowerShell
# Build search filter
$filter = "(&(sAMAccountType=805306368)(|(sAMAccountName=%username%)"
if (-not([System.String]::IsNullOrEmpty("%mail%")))
{
    $filter += "(mail=%mail%)"
}
$filter += "))"

# Search for users with the username or email address specified
$searcher = $Context.BindToObject("Adaxes://rootDSE")
$searcher.SearchFilter = $filter
$searcher.SearchScope = "ADS_SCOPE_SUBTREE"
$searcher.PageSize = 500
$searcher.ReferralChasing = "ADS_CHASE_REFERRALS_NEVER"
$searcher.VirtualRoot = $True
$searcher.SizeLimit = 1

try
{
    $searchResultIterator = $searcher.ExecuteSearch()
    $searchResults = $searchResultIterator.FetchAll()
    
    if ($searchResults.Length -ne 0)
    {
        $Context.Cancel("A user with the same username or email address already exists")
        return
    }
}
finally
{
    # Release resources
    $searchResultIterator.Dispose()
}

Comments ( 0 )
No results found.
Leave a comment