Hello,
To implement what you want, first of all, you need to somehow specify that a new user is a contractor. For this purpose you can, for example, add a boolean (True/False) attribute to the form for creating new users. It will serve as a flag to distinguish new contractors from other users. Setting the attribute to True will mean that a new contractor is created. This can be one of Adaxes custom attributes, for example, CustomAttributeBoolean1. Such attributes are not stored in AD, but can be used the same as any other attributes of AD objects.
Also, you'll need to create a Business Rule triggered before creating a new user that will generate and assign the next username in the sequence.
Finally, to give your users an idea about what the custom attribute is used for, you can change the name under which it appears in Adaxes.
To implement such a solution:
i. Add custom attribute to the form for creating users
For information on how to add an attribute to the form for creating users, see the following tutorial (starting from step 6): http://www.adaxes.com/tutorials_WebInte ... tomization.
ii. Create Business Rule to assign username
For information on how to create such a Business Rule, see the following tutorial: http://www.adaxes.com/tutorials_Simplif ... Script.htm. Use it as a guide.
-
On step 5 of the tutorial, add the following script:
$usernameFomrat = "A{0:000000}" # TODO: modify me
$initialNumber = 1 # TODO: modify me
$maxNumber = 900000 # TODO: modify me
$settingsPath = $Context.GetWellKnownContainerPath("ConfigurationSetSettings")
$settings = $Context.BindToObject($settingsPath)
# Get the next contractor number from global configuration
try
{
$number = [int]($settings.Get("adm-CustomAttributeInt1"))
$number++
}
catch
{
# If no number is set in the global configuration, use the initial number
$number = $initialNumber
}
$uniqueUsername = [System.String]::Format($usernameFomrat, $number)
do
{
if ($number -gt [int]$maxNumber)
{
$Context.Cancel("Cannot generate a username for the contractor because the maximum allowed contractor number has been reached. Please contact your system administrator.")
return
}
# Check whether the username is unique
$searcher = $Context.BindToObject("Adaxes://rootDse")
$searcher.PageSize = 500
$searcher.SearchScope = "ADS_SCOPE_SUBTREE"
$searcher.SearchFilter = "(&(sAMAccountType=805306368)(sAMAccountName=$uniqueUsername))"
$searcher.ReferralChasing = "ADS_CHASE_REFERRALS_NEVER"
$searcher.VirtualRoot = $True
try
{
$searchResult = $searcher.ExecuteSearch()
$result = $searchResult.FetchAll()
# If the username is not unique, find a unique one
if ($result.Count -ne 0)
{
$number++
$uniqueUsername = [System.String]::Format($usernameFomrat, $number)
}
}
finally
{
$searchResult.Dispose()
}
}
while ($result.Count -ne 0)
# Save the new number in the gloabal settings
$settings.Put("adm-CustomAttributeInt1", $number)
$settings.SetInfo()
# Update the User Logon Name
$Context.SetModifiedPropertyValue("sAMAccountName", "$uniqueUsername") # User Logon Name (Pre-Windows 2000)
$upnSuffix = $Context.GetObjectDomain("%distinguishedName%")
$userLogonName = $uniqueUsername + "@" + $upnSuffix
$Context.SetModifiedPropertyValue("userPrincipalName", $userLogonName) # User Logon Name
$Context.LogMessage("Contractor username: $userLogonName", "Information")
-
In the script, modify the following to meet your requirements:
- $usernameFomrat - specifies the contractor username format. Currently, it is set to the format you described, however, if you need to change it in the future, see the following MSDN article for instructions: https://msdn.microsoft.com/en-us/librar ... 10%29.aspx.
- $initialNumber - specifies the initial number that will be used on the first run.
- $maxNumber - specifies the maximum number; numbers higher than this won't be used.
-
Also, you'll need to run the Business Rule only when the custom attribute is set to True. For this purpose, you need to add a condition. You will find information on how to do this on step 6 of the tutorial. You'll need to specify the following condition: If CustomAttributeBoolean1 equals True.
where CustomAttributeBoolean1 is the name of the custom attribute you want to use.
iii. Change display name for the property
For information on how to change the name under which the property appears in Adaxes, see the following help article: http://www.adaxes.com/help/?HowDoI.Mana ... Names.html.