 
/*------------------------------------------------------------------------
   File        : adresse_dict
   Purpose     : 
   Syntax      : 
   Description : 
   Author(s)   : walter.riechsteiner
   Created     : Thu Jun 26 13:56:20 CEST 2025
   Notes       : 
 ----------------------------------------------------------------------*/
 
BLOCK-LEVEL ON ERROR UNDO, THROW.

USING OpenEdge.Core.String FROM PROPATH.
USING OpenEdge.Net.HTTP.IHttpResponse FROM PROPATH.
USING OpenEdge.Net.HTTP.StatusCodeEnum FROM PROPATH.
USING OpenEdge.Web.IWebRequest FROM PROPATH.
USING OpenEdge.Web.WebHandler FROM PROPATH.
USING OpenEdge.Web.WebResponseWriter FROM PROPATH.
USING Progress.Json.ObjectModel.JsonObject FROM PROPATH.
USING Progress.Json.ObjectModel.JsonArray FROM PROPATH.
USING Progress.Json.ObjectModel.ObjectModelParser FROM PROPATH.
USING src.ch.adprime.gemis.WebHandlerUtilities FROM PROPATH.
USING utilities.utilitiesHandler FROM PROPATH.
USING OpenEdge.Net.HTTP.HttpHeader FROM PROPATH.

CLASS auth.loginImpl INHERITS WebHandler: 

    DEFINE VARIABLE outilitiesHandler AS utilitiesHandler NO-UNDO.

    DEFINE TEMP-TABLE tcompanies
        SERIALIZE-NAME 'companies'
        FIELD company      AS CHARACTER SERIALIZE-NAME 'company'
        FIELD company_Name AS CHARACTER SERIALIZE-NAME 'company_name'
        FIELD lselected    AS LOGICAL   SERIALIZE-NAME 'selected'
        .
        
    /*------------------------------------------------------------------------------
     Purpose:
     Notes:
    ------------------------------------------------------------------------------*/

    METHOD OVERRIDE PROTECTED INTEGER HandleGet (INPUT poRequest AS IWebRequest ):
        DEFINE VARIABLE oResponse       AS IHttpResponse     NO-UNDO.         
        DEFINE VARIABLE oWriter         AS WebResponseWriter NO-UNDO.
        DEFINE VARIABLE oBody           AS String            NO-UNDO.
        DEFINE VARIABLE oJsonResponse   AS JsonObject        NO-UNDO.
        DEFINE VARIABLE oJsoncompanies  AS JsonObject        NO-UNDO.
        DEFINE VARIABLE oJsonArray      AS JsonArray         NO-UNDO.
        DEFINE VARIABLE cDefautlCharSet AS CHARACTER         NO-UNDO INIT 'UTF-8'.
        DEFINE VARIABLE oParser         AS ObjectModelParser NO-UNDO.           
        DEFINE VARIABLE cCorralationID  AS CHARACTER         NO-UNDO.
        DEFINE VARIABLE cContent-Type   AS CHARACTER         NO-UNDO.
        DEFINE VARIABLE htcompanies     AS HANDLE            NO-UNDO.
        DEFINE VARIABLE lcJsonInhalt    AS LONGCHAR          NO-UNDO.
        DEFINE VARIABLE lOk             AS LOGICAL           NO-UNDO.
        DEFINE VARIABLE ii              AS INTEGER           NO-UNDO.
        
        DEFINE VARIABLE cuser_name      AS CHARACTER         NO-UNDO.
        
        cuser_name = poRequest:URI:GetQueryValue("user_name").
MESSAGE '1'.
        ii = 0.
        EMPTY TEMP-TABLE tcompanies.
        FOR EACH companies NO-LOCK
            WHERE companies.active = TRUE:
            
            ii = ii + 1.        
            CREATE tcompanies.
            ASSIGN 
                tcompanies.company      = companies.company
                tcompanies.company_Name = companies.company_name
                tcompanies.lselected    = (IF ii = 1 THEN TRUE ELSE FALSE).
        END.
        
        outilitiesHandler = NEW utilitiesHandler().
MESSAGE '2'.
        //  Ist dem Benutzer ein company zugeteilt ?
        FIND FIRST users NO-LOCK
            WHERE users.user_name = cuser_name NO-ERROR.
        IF AVAILABLE users THEN 
        DO:
            FOR EACH tcompanies:
                tcompanies.lselected = (IF tcompanies.company = users.company THEN TRUE ELSE FALSE).
            END.
        END.  
MESSAGE '3'.
        htcompanies     = TEMP-TABLE tcompanies:DEFAULT-BUFFER-HANDLE.
        cContent-Type   = "application/json".
        cDefautlCharSet = 'utf-8'. 
        oJsonResponse   = NEW JsonObject().
        oParser         = NEW ObjectModelParser().
        
        oJsonResponse:ADD('user_name', cuser_name).

        oJsoncompanies = NEW JsonObject().
        htcompanies:WRITE-JSON('JsonObject', oJsoncompanies).
        lcJsonInhalt   = oJsoncompanies:GetJsonText('companies').
        oJsonArray     = CAST(oParser:Parse(lcJsonInhalt), JsonArray) NO-ERROR.
        oJsonResponse:ADD('companies', oJsonArray) NO-ERROR.

        oJsonResponse:WRITE(lcJsonInhalt, FALSE).
MESSAGE STRING(lcJsonInhalt).
        oBody = NEW STRING(lcJsonInhalt).
        WebHandlerUtilities:createHttpResponse(INPUT INTEGER(StatusCodeEnum:OK), INPUT oBody, INPUT cContent-Type, INPUT cDefautlCharSet, INPUT cCorralationID, OUTPUT oResponse).
           
        oWriter = NEW WebResponseWriter(oResponse).
        oWriter:Open(). 
        oWriter:Close(). 

        RETURN 0.
        
        CATCH e AS Progress.Lang.Error:

        END CATCH.

        FINALLY:

        END FINALLY.

    END METHOD.

    METHOD OVERRIDE PROTECTED INTEGER HandleNotAllowedMethod(INPUT poRequest AS IWebRequest):
        DEFINE VARIABLE result AS INTEGER NO-UNDO.
        MESSAGE "HandleNotAllowedMethod called" VIEW-AS ALERT-BOX.
        RETURN result.
    END METHOD.

    METHOD OVERRIDE PROTECTED INTEGER HandleNotImplemented(INPUT poRequest AS IWebRequest):
        DEFINE VARIABLE result AS INTEGER NO-UNDO.
        MESSAGE "HandleNotImplemented called" VIEW-AS ALERT-BOX.
        RETURN result.
    END METHOD.

    /*------------------------------------------------------------------------------
     Purpose:
     Notes:
    ------------------------------------------------------------------------------*/

    METHOD OVERRIDE PROTECTED INTEGER HandlePost(INPUT poRequest AS IWebRequest):
        
        DEFINE VARIABLE oResponse      AS IHttpResponse                NO-UNDO.
        DEFINE VARIABLE oWriter        AS WebResponseWriter            NO-UNDO.
        DEFINE VARIABLE oParser        AS ObjectModelParser            NO-UNDO.           
        DEFINE VARIABLE oJsonResponse  AS JsonObject                   NO-UNDO.
        DEFINE VARIABLE oJsonSession   AS JsonObject                   NO-UNDO.
        DEFINE VARIABLE oJsonLabels    AS JsonObject                   NO-UNDO.
        DEFINE VARIABLE oMessage       AS JsonObject                   NO-UNDO.
        DEFINE VARIABLE oJsonData      AS JsonObject                   NO-UNDO.

        DEFINE VARIABLE lRetVal        AS LOGICAL                      NO-UNDO.
        DEFINE VARIABLE lcJsonString   AS LONGCHAR                     NO-UNDO.
        DEFINE VARIABLE i1             AS INTEGER                      NO-UNDO.

        DEFINE VARIABLE cPasswort      AS CHARACTER                    NO-UNDO.
        DEFINE VARIABLE cpassword      AS CHARACTER                    NO-UNDO.
        DEFINE VARIABLE ccompany       AS CHARACTER                    NO-UNDO.
        DEFINE VARIABLE cuser_name     AS CHARACTER                    NO-UNDO.
        DEFINE VARIABLE ilanguage_id   AS INTEGER                      NO-UNDO.
        DEFINE VARIABLE lcMessage      AS LONGCHAR                     NO-UNDO.
        DEFINE VARIABLE lcErrorMessage AS LONGCHAR                     NO-UNDO.
        DEFINE VARIABLE lError         AS LOGICAL                      NO-UNDO.
        DEFINE VARIABLE cString        AS CHARACTER                    NO-UNDO.
        DEFINE VARIABLE cMessage       AS CHARACTER                    NO-UNDO.
        DEFINE VARIABLE csession_token AS CHARACTER                    NO-UNDO.
        DEFINE VARIABLE rUsers         AS RECID                        NO-UNDO.

        DEFINE VARIABLE poHeaders      AS HttpHeader                   NO-UNDO EXTENT.
        DEFINE VARIABLE ix             AS INTEGER                      NO-UNDO.
        DEFINE VARIABLE lcRetVal       AS LONGCHAR                     NO-UNDO.
        DEFINE VARIABLE oHeader        AS OpenEdge.Net.HTTP.HttpHeader NO-UNDO.
// MESSAGE 'start post'.        

        lRetVal = WebHandlerUtilities:getJsonFromRequest(INPUT poRequest, OUTPUT oJsonData) NO-ERROR.
// MESSAGE 'getJsonFromRequest    = ' lRetVal.

        oJsonData:WRITE(lcJsonString, FALSE).
// MESSAGE 'ganzer Eingangsstring = ' STRING(lcJsonString).

        ccompany   = oJsonData:GetCharacter('company' ).
        cuser_name = oJsonData:GetCharacter('user_name' ).
        cpassword  = oJsonData:GetCharacter('password').
// MESSAGE ccompany '-' cuser_name '-' cpassword.

        lcErrorMessage    = ''.
        lError            = FALSE.
        outilitiesHandler = NEW utilitiesHandler().
        
        DO WHILE TRUE:
            FIND FIRST users NO-LOCK
                WHERE users.user_name = cuser_name
                AND   users.active    = TRUE  NO-ERROR.
            IF NOT AVAILABLE users THEN 
            DO:
                lError = TRUE.
                outilitiesHandler:getErrorMessage (
                    INPUT '1990',
                    INPUT 1     ,           /* Sprcd*/
                    INPUT ''    ,           /* Parameter  */
                    OUTPUT lcMessage
                    ).
                LEAVE.
            END.
        
            ilanguage_id = users.language_id.
            rUsers       = RECID(users).
            
            IF users.password <> cpassword THEN 
            DO:
                lError = TRUE.
                outilitiesHandler:getErrorMessage (
                    INPUT '1992'      ,
                    INPUT ilanguage_id,     /* Sprcd*/
                    INPUT ''          ,     /* Parameter  */
                    OUTPUT lcMessage
                    ).
                LEAVE.
            END.
            
            FIND FIRST userauthorization NO-LOCK 
                WHERE userauthorization.company   = ccompany
                AND   userauthorization.user_name = cuser_name NO-ERROR.
            IF NOT AVAILABLE userauthorization THEN 
            DO:
                lError = TRUE.
                outilitiesHandler:getErrorMessage (
                    INPUT '1991'      ,
                    INPUT ilanguage_id,     /* Sprcd*/
                    INPUT ''          ,     /* Parameter  */
                    OUTPUT lcMessage
                    ).
                LEAVE.
            END.

            REPEAT TRANSACTION ON ERROR UNDO, LEAVE:
                csession_token = users.sha_password.
                
                IF users.sha_password <> '' THEN LEAVE.
                IF users.password      = '' THEN cPasswort = users.user_name.
                ELSE                             cPasswort = users.password. 
                outilitiesHandler:generateHash (
                    INPUT 'SHA-512',
                    INPUT SUBSTITUTE('&1:&2', cPasswort, STRING(TIME,'99999999')),
                    INPUT 'Passw0rd',
                    OUTPUT lError,
                    OUTPUT cMessage,
                    OUTPUT csession_token
                    ).
                FIND users EXCLUSIVE-LOCK WHERE RECID(users) = rUsers.
                ASSIGN
                    users.sha_password = csession_token.
                RELEASE users.
                LEAVE.
            END.
            
            FIND users NO-LOCK WHERE RECID(users) = rUsers.
                        
            oResponse             = NEW OpenEdge.Web.WebResponse().
            oResponse:StatusCode  = INTEGER(StatusCodeEnum:OK).
            oResponse:ContentType = "application/json;charset=utf-8".
            oWriter               = NEW WebResponseWriter(oResponse).

            FIND FIRST users NO-LOCK
                WHERE users.user_name = cuser_name NO-ERROR.

            oJsonSession  = NEW JsonObject().                
            oJsonLabels   = NEW JsonObject().                
            oJsonResponse = NEW JsonObject().
            oJsonResponse:ADD('success'      , TRUE              ).

            oJsonSession:ADD ('session_token', csession_token    ).
            oJsonSession:ADD ('company'      , ccompany          ).
            oJsonSession:ADD ('user_name'    , cuser_name        ).
            oJsonSession:ADD ('display_name' , users.display_name).
            oJsonSession:ADD ('language_id'  , ilanguage_id      ).
            oJsonResponse:ADD('session'      , oJsonSession      ).
            
            FOR EACH labeltext NO-LOCK 
                WHERE labeltexte.company     = ccompany
                AND   labeltexte.table_name  = 'table'
                AND   labeltexte.program     = 'buttons'
                AND   labeltexte.language_id = ilanguage_id:
                oJsonLabels:ADD(CAPS(labeltexte.field_name), labeltexte.sidelabel).
            END.
            FOR EACH labeltext NO-LOCK 
                WHERE labeltexte.company     = ccompany
                AND   labeltexte.table_name  = 'table'
                AND   labeltexte.program     = 'MsgBox'
                AND   labeltexte.language_id = ilanguage_id:
                oJsonLabels:ADD(CAPS(labeltexte.field_name), labeltexte.sidelabel).
            END.
            FOR EACH labeltext NO-LOCK 
                WHERE labeltexte.company     = ccompany
                AND   labeltexte.table_name  = 'table'
                AND   labeltexte.program     = 'Labels'
                AND   labeltexte.language_id = ilanguage_id:
                oJsonLabels:ADD(CAPS(labeltexte.field_name), labeltexte.sidelabel).
            END.
            oJsonResponse:ADD('labels', oJsonLabels).
            
            oJsonResponse:WRITE(lcJsonString, FALSE).
// MESSAGE 'Rueckmeldung' STRING(lcJsonString).

            oWriter:Open().
            oWriter:Write(lcJsonString).
            oWriter:Close().
        
            RETURN 0.

        END.
        
// MESSAGE 'Meldung von getErrorMessage ' STRING(lcMessage).
                        
        oResponse             = NEW OpenEdge.Web.WebResponse().
        oResponse:StatusCode  = INTEGER(StatusCodeEnum:OK).
        oResponse:ContentType = "application/json;charset=utf-8".
        oWriter               = NEW WebResponseWriter(oResponse).
        
        oJsonResponse = NEW JsonObject().
        oJsonResponse:ADD('success', FALSE).
        oJsonResponse:ADD('message', lcMessage) NO-ERROR.
        
        oJsonResponse:WRITE(lcJsonString, FALSE).
// MESSAGE 'Rueckmeldung ' STRING(lcJsonString).

        oWriter:Open().
        oWriter:Write(lcJsonString).
        oWriter:Close().
        
        RETURN 0.

        CATCH e AS Progress.Lang.Error:

        END CATCH.

        FINALLY:
            DELETE OBJECT oParser NO-ERROR.
        END FINALLY.

    END METHOD.

END CLASS.