Function NDW_Verify_User_Events(CtrlEntId, Event, @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 Infineon.

    Name        :   NDW_Verify_User_Events

    Description :   This function acts as a commuter module for all events related to this window.

    Notes       :   Commuter Modules are automatically called from the Promoted_Events function which is called by the
                    application-specific promoted event handler. This makes it possible to add QuickEvents that need to
                    execute Basic+ logic without having use the Form Designer to make the association, although this is
                    limited to the events which are currently promoted.

                    If the form needs to call the commuter module directly then the QuickEvent parameters should be
                    formatted like this:

                        '@SELF','@EVENT',['@PARAM1','@PARAMx']

    Parameters  :
        CtrlEntId   [in] -- The fully qualified name of the control calling the promoted event
        Event       [in] -- The event being executed. See the Notes section regarding "PRE" events
        Param1-15   [in] -- Additional event parameter holders
        EventFlow  [out] -- Set to 1 or 0 so the calling event knows whether or not to chain forward. See comments in
                            EVENT_SETUP insert
        Response   [out] -- Verification succeeds:
                                Response<1> EQ True$(1)                           
                                Response<2> EQ Username verified (table - LSL_USERS, column - Username)
                            Verification fails:
                                Response<1> EQ False$(0)
                                Response<2> EQ NULL ('')                  

    History     :   (Date, Initials, Notes)
        04/30/18    djs     Created initial commuter module.
        06/14/18    djs     Added the ability to pass in more than one user group to check against. Groups should be
                            delimited by @VM (see usage below).
                            
    Usage       :   Response    = Dialog_Box('NDW_VERIFY_USER', @WINDOW, ['Username' : @FM : 'Group1' : @VM : 'Group2'])
                                            

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

#pragma precomp SRP_PreCompiler
#Window NDW_VERIFY_USER

$insert APP_INSERTS
$insert EVENT_SETUP
$insert MSG_EQUATES
$insert LSL_USERS_EQUATES

Equ USERNAME$   To  1
Equ GROUP$      To  2
Equ PASSWORD$   To  3
Equ CONTEXT$	To	4
 
Declare subroutine  SRP_EditTable_Manager, SRP_Show_Window, PlaceDialog
Declare function    SRP_EditTable_Manager, Database_Services, Security_Services, MemberOf

SubclassInfo    = Form_Services('FindSubclassControl')
Subclass        = SubclassInfo<1>

// Update the arguments so that the OpenInsight OLE event will treate the ActiveX event as a native event handler.
If Event EQ 'OLE' then
    Transfer Event to OIEvent
    Transfer Param1 to Event
    Transfer Param2 to Param1
    Transfer Param3 to Param2
    Transfer Param4 to Param3
    Transfer Param5 to Param4
    Transfer Param6 to Param5
    Transfer Param7 to Param6
    Transfer Param8 to Param7
end

GoToEvent Event for CtrlEntID

Return EventFlow else EVENT_CONTINUE$


////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Events
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

Event WINDOW.CREATE(CreateParam)
    
    GoSub Setup_OLE_Controls
    Set_Property(@Window : '.PUB_CLOSE', 'NEXT', @Window : '.EDL_USERNAME')
        
    If CreateParam<USERNAME$> NE '' then
        Username = CreateParam<USERNAME$>
    end else
        Username = @USER4
    end
    
    If CreateParam<GROUP$> NE '' then
        Groups    = CreateParam<GROUP$>       
    end else
        Groups    = ''
    end
    Set_Property(@Window, '@GROUPS', Groups)
    
	If CreateParam<CONTEXT$> NE '' then
		Set_Property(@Window : '.STA_CONTEXT', 'TEXT', CreateParam<CONTEXT$>)
	end else
		ContextHeight = Get_Property(@WINDOW : '.STA_CONTEXT', 'SIZE')<4>
		FormSize = Get_Property(@Window, 'SIZE')
		Set_Property(@Window : '.STA_CONTEXT', 'VISIBLE', 0)
		Set_Property(@Window, 'SIZE', FormSize<1>:@FM:FormSize<2>:@FM:FormSize<3>:@FM:(FormSize<4>-ContextHeight):@FM:FormSize<5>)
	end
    
    User            = Database_Services('ReadDataRow', 'LSL_USERS', Username)
    Password        = User<LSL_USERS_PASSWORD$>
    UserGroups      = User<LSL_USERS_GROUPS$>
    Credentials     = ''
    Credentials<USERNAME$>  = Username
    Credentials<PASSWORD$>  = Password
    Credentials<GROUP$>     = UserGroups
    Set_Property(@Window, '@CREDENTIALS', Credentials)
    Set_Property(@window : '.EDL_USERNAME', 'TEXT', Username)
    Set_Property(@Window : '.EDL_PASSWORD', 'FOCUS', True$)
    FormSize = ''
    PlaceDialog( -2, -2 )

end event


Event WINDOW.CLOSE(CancelFlag)
    
    Result  = False$
    End_Dialog(@Window, Result)
    
end event


Event EDL_USERNAME.GOTFOCUS(PrevFocusID)

	If PrevFocusID NE CtrlEntId then 
		Set_Property(CtrlEntId, 'SELECTION', 1 : @FM : 999)
	end
	
end event


Event EDL_USERNAME.LOSTFOCUS(Flag, FocusID)
    
    Username    = Get_Property(@Window:'.EDL_USERNAME','TEXT')
    If Username [1,2] EQ '1H' then
    	Username = Username[3, 999]
    	Set_Property(CtrlEntId, 'TEXT', Username)
        Set_Property(@Window:'.EDL_PASSWORD', 'FOCUS', True$)
    end
        
end event


Event EDL_USERNAME.CHAR(VirtCode, ScanCode, CtrlKey, ShiftKey, AltKey)

    Form_Services('CloseControlMessage', CtrlEntId)
    Username    = Get_Property(CtrlEntId, 'TEXT')
    Password    = Get_Property(@Window : '.EDL_PASSWORD', 'TEXT')
    If (Username NE '') AND (Password NE '') then
        Set_Property(@Window : '.PUB_OK', 'ENABLED', True$)
    end else
        Set_Property(@Window : '.PUB_OK', 'ENABLED', False$)
    end

end event


Event EDL_PASSWORD.GOTFOCUS(PrevFocusID)

	If PrevFocusID NE CtrlEntId then 
		Set_Property(CtrlEntId, 'SELECTION', 1 : @FM : 999)
	end
	
end event


Event EDL_PASSWORD.LOSTFOCUS(Flag, FocusID)
    
    Password    = Get_Property(@Window:'.EDL_PASSWORD','TEXT')
     If Password [1,3] EQ 'PWD' then
     	Password = Password[4, 999]
     	Set_Property(CtrlEntId, 'TEXT', Password)
        Send_Event(@Window : '.PUB_OK', 'CLICK')
     end
    
end event


Event EDL_PASSWORD.CHAR(VirtCode, ScanCode, CtrlKey, ShiftKey, AltKey)

    PopupVis = Get_Property(@Window:'.OLE_POPUP', "OLE.Visible")
    If PopupVis then Form_Services('CloseControlMessage', CtrlEntId)
    Username = Get_Property(@Window : '.EDL_USERNAME', 'TEXT')
    Password = Get_Property(CtrlEntId, 'TEXT')
    If (Username NE '') AND (Password NE '') then
        Set_Property(@Window : '.PUB_OK', 'ENABLED', True$)
    end else
        Set_Property(@Window : '.PUB_OK', 'ENABLED', False$)
    end

end event


Event PUB_OK.CLICK()
    
    Set_Property(@Window : '.EDL_USERNAME', 'BACKCOLOR', COLOR_BTNFACE$)
    Set_Property(@Window : '.EDL_PASSWORD', 'BACKCOLOR', COLOR_BTNFACE$)
    Set_Property(@Window : '.EDL_USERNAME', 'BACKCOLOR', COLOR_WINDOW$)
    Set_Property(@Window : '.EDL_PASSWORD', 'BACKCOLOR', COLOR_WINDOW$)

    Username    = Get_Property(@Window:'.EDL_USERNAME','TEXT')
    Password    = Get_Property(@Window:'.EDL_PASSWORD','TEXT')
    Credentials = Get_Property(@Window, '@CREDENTIALS')
    // A different user is signing. (eg. LEAD signature override)
    If Username NE Credentials<1> Then
        NewUser           = Database_Services('ReadDataRow', 'LSL_USERS', Username)
        Credentials<USERNAME$>    = Username
        Credentials<PASSWORD$>    = NewUser<LSL_USERS_PASSWORD$>
        Credentials<GROUP$>       = NewUser<LSL_USERS_GROUPS$>
    end

    Member = False$
    Group  = ''
    Groups = Get_Property(@Window, '@GROUPS')
    If Groups NE '' then
        For each Group in Groups using @VM
            Member = MemberOf(Credentials<USERNAME$>, Group)
        Until Member EQ True$
        Next Group
    end else
        Member = True$
    end

    Begin Case
        
        Case (Password EQ Credentials<PASSWORD$>) AND (Member EQ True$)    
            Result = True$ : @FM : Credentials<USERNAME$>
            End_Dialog(@WINDOW, Result)
            
        Case (Password EQ Credentials<PASSWORD$>) AND (Member EQ False$)          
            Set_Property(@Window : '.EDL_USERNAME', 'FOCUS', True$)
            Set_Property(@Window : '.EDL_USERNAME', 'SELECTION', 1 : @FM : 999)
            NumGroups = DCount(Groups, @VM)
            If NumGroups GT 1 then
                Swap @VM with ' or ' in Groups
                Message = 'User is not a member of ':@TM:'the ' : Groups : ' groups.'
            end else
                Message = 'User is not a member of ':@TM:'the ' : Group : ' group.'
            end
            Form_Services('DisplayControlMessage', Message, 'Incorrect Group', @Window : '.EDL_USERNAME', 'VALIDATION', 'RGB(229,20,0)')
            
        Case Password NE Credentials<PASSWORD$>
            Set_Property(@Window : '.EDL_PASSWORD', 'FOCUS', True$)
            Set_Property(@Window : '.EDL_PASSWORD', 'SELECTION', 1 : @FM : 999)
            Message = 'Unable to validate username. Please re-enter.'
            Form_Services('DisplayControlMessage', Message, 'Incorrect Password', @Window : '.EDL_PASSWORD', 'VALIDATION', 'RGB(229,20,0)')
            
    End Case
    
end event


Event PUB_CANCEL.CLICK()

    Result  = False$
    End_Dialog(@Window, Result)

end event


////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Internal GoSubs
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

Setup_OLE_Controls:

    Qualify     = ''
    Qualify<1>  = 1
    Qualify<4>  = 0

return

