Compile function Active_Directory_Services(@Service, @Params)
/***********************************************************************************************************************

    This program is proprietary and is not to be used by or disclosed to others, nor is it to be copied without written
    permission from SRP Computer Solutions, Inc.

    Name        :   Active_Directory_Services

    Description :   Handler program for all module related services.

    Notes       :   The generic parameters should contain all the necessary information to process the services. Often
                    this will be information like the data Record and Key ID.

    Parameters  :
        Service         [in] -- Name of the service being requested
        Param1-10   [in/out] -- Additional request parameter holders
        Response       [out] -- Response to be sent back to the Controller (MCP) or requesting procedure

    History     :   (Date, Initials, Notes)
        02/17/23    djs     Original programmer.

***********************************************************************************************************************/

#pragma precomp SRP_PreCompiler

$Insert SERVICE_SETUP
$Insert APP_INSERTS
$Insert REVDOTNETEQUATES

Declare subroutine Set_Property.Net

GoToService

Return Response or ""

//-----------------------------------------------------------------------------
// SERVICES
//-----------------------------------------------------------------------------


Service AuthenticateUser(Username, Password, Domain)
    
    Authenticated      = False$
    DotNetHandle       = StartDotNet("","4.0")
    DotNetDir          = CheckDotNet('4.0'):'\'
    AccountMgmtDllPath = DotNetDir:'System.DirectoryServices.AccountManagement.dll'
    Set_Property.Net(DotNetHandle, "AssemblyName", AccountMgmtDllPath)
    If Not(Get_Status(errCode)) then
        Params             = 'Domain':@FM:Domain
        ParamTypes         = 'System.DirectoryServices.AccountManagement.ContextType':@FM:'System.String'
        objPC              = Create_Class.Net(DotNetHandle, "System.DirectoryServices.AccountManagement.PrincipalContext", 0, Params, ParamTypes)
        If Not(Get_Status(errCode)) then
            Params        = Username:@FM:Password
            ParamTypes    = 'System.String':@FM:'System.String'
            Authenticated = Send_Message.Net(objPC, 'ValidateCredentials', Params, ParamTypes, 0)
            Swap 'True'  with True$ in Authenticated
            Swap 'False' with False$ in Authenticated
            Free_Class.Net(objPC)
        end
    end
    Response = Authenticated
    
end service


Service GetADGroups(Username, Domain)

    ADGroups           = ''
    DotNetHandle       = StartDotNet("","4.0")
    DotNetDir          = CheckDotNet('4.0'):'\'
    AccountMgmtDllPath = DotNetDir:'System.DirectoryServices.AccountManagement.dll'
    Set_Property.Net(DotNetHandle, "AssemblyName", AccountMgmtDllPath)
    If Not(Get_Status(errCode)) then
        Params             = 'Domain':@FM:'Infineon'
        ParamTypes         = 'System.DirectoryServices.AccountManagement.ContextType':@FM:'System.String'
        objPC              = Create_Class.Net(DotNetHandle, "System.DirectoryServices.AccountManagement.PrincipalContext", 0, Params, ParamTypes)
        If Not(Get_Status(errCode)) then
            objUserPrincipal = Create_Class.Net(DotNetHandle, "System.DirectoryServices.AccountManagement.UserPrincipal", 0, objPC, 'RevDotNet')
            If Not(Get_Status(errCode)) then
                Set_Property.Net(objUserPrincipal, 'Name', Username)
                objPrinSearcher = Create_Class.Net(DotNetHandle, "System.DirectoryServices.AccountManagement.PrincipalSearcher", 0, objUserPrincipal, 'RevDotNet')
                If Not(Get_Status(errCode)) then
                    objPrin = Send_Message.Net(objPrinSearcher, 'FindOne', '', '', 1)
                    If Not(Get_Status(errCode)) then
                        Name                = Get_Property.Net(objPrin, 'Name', 0)
                        objPrinSearchResult = Send_Message.Net(objPrin, 'GetGroups', '', '', 1)
                        If Not(Get_Status(errCode)) then
                            objEnum = Send_Message.Net(objPrinSearchResult, 'GetEnumerator', '', '', 1)
                            If Not(Get_Status(errCode)) then
                                Loop
                                    Done = Send_Message.Net(objEnum, 'MoveNext', '', '', 0)
                                Until Done EQ 'False'
                                    If Not(Get_Status(errCode)) then
                                        objCurrPrin = Get_Property.Net(objEnum, 'Current', 1)
                                        If Not(Get_Status(errCode)) then
                                            CurrPrinName = Get_Property.Net(objCurrPrin, 'Name', 0)
                                            If CurrPrinName NE 'Domain Users' then ADGroups<-1> = CurrPrinName
                                            Free_Class.Net(objCurrPrin)
                                        end
                                    end
                                Repeat
                                Free_Class.Net(objEnum)
                            end
                            Free_Class.Net(objPrinSearchResult)
                        end
                        Free_Class.Net(objPrin)
                    end
                    Free_class.Net(objPrinSearcher)
                end
                Free_Class.Net(objUserPrincipal)
            end
            Free_Class.Net(objPC)
        end
    end
    Response = ADGroups
    
End Service


Service GetComputerDomain()
	
    Domain       = ''
    DotNetHandle = StartDotNet("","4.0")
    DotNetDir    = CheckDotNet('4.0'):'\'
    DirDllPath   = DotNetDir:'System.DirectoryServices.dll'
    Set_Property.Net(DotNetHandle, "AssemblyName", DirDllPath)
    If Not(Get_Status(errCode)) then
    	objDomain =  Create_Class.Net(DotNetHandle, "System.DirectoryServices.ActiveDirectory.Domain", 0, '', '')
    	If Not(Get_status(errCode)) then
			ObjCompDomain = Send_Message.Net(objDomain, 'GetComputerDomain', '', '', True$)
			If Not(Get_Status(errCode)) then
				Domain = Get_Property.Net(objCompDomAin, 'Name', False$)
				Free_class.Net(objCompDomain)
			end
    		Free_Class.Net(objDomain)
    	end
    end
    Response = Domain

end service


