Skip to Content
0

how to get the user ip address when logon thru portal and web dispatcher

Feb 22, 2017 at 04:56 PM

61

avatar image

Our aim is to save in database the user name and the ip address each time a user log to the ECC system.

There is no problem when the connection is directly done into ECC or when it's done using a webgui URL.

Even when the connection is made thru a portal without webdispacther, the following code is working fine.

The problem is when the connection is made thru a portal with a webdispacther, we can't find the user IP address. We only find the Webdispatcher IP address.

The parameter wdisp/add_xforwardedfor_header is set to TRUE on the webdispatcher, but the corresponding field doesn't appear in the answer.


My code is below:

DATA :

lr_user TYPE RANGE OF xubname,
ls_usergen TYPE ztrvta_usergen,

lo_client TYPE REF TO if_http_client,

lv_afficher_popup TYPE abap_bool,

lt_dern_connex TYPE STANDARD TABLE OF ztrvta_usergen,
ls_dern_connex TYPE ztrvta_usergen,
lv_delai TYPE i,
lv_delai_tvarvc TYPE tvarv_val,
lv_time_max TYPE systtimlo.


lv_afficher_popup = abap_true.

SELECT sign opti low high INTO TABLE lr_user
FROM tvarvc
WHERE name EQ 'ZTRACEUSER'
AND type EQ 'S'.


CHECK NOT lr_user IS INITIAL.

IF sy-uname IN lr_user.
MOVE-CORRESPONDING sy TO ls_usergen.

IF sy-cprog EQ 'SAPMSYST'.
" La connexion est faite via client lourd

cl_gui_frontend_services=>get_computer_name( CHANGING computer_name = ls_usergen-pc ).
cl_gui_frontend_services=>get_desktop_directory( CHANGING desktop_directory = ls_usergen-dir ).
ls_usergen-ip = cl_gui_frontend_services=>get_ip_address( ).

ELSE.
" La connexion est faite via le portail

" Récupération IP
" .. Préparation appel URL
PERFORM get_url CHANGING gv_url.
CALL METHOD cl_http_client=>create_by_url
EXPORTING
url = 'http://127.0.0.1'
ssl_id = ' '
IMPORTING
client = lo_client
EXCEPTIONS
OTHERS = 1.

" .. Réalisation appel URL
CALL METHOD lo_client->send
EXCEPTIONS
http_communication_failure = 1
http_invalid_state = 2
http_processing_failed = 3
http_invalid_timeout = 4
OTHERS = 5.

" .. Récupération de la réponse
CALL METHOD lo_client->receive
EXCEPTIONS
http_communication_failure = 1
http_invalid_state = 2
http_processing_failed = 3
OTHERS = 4.

" .. Récupération de l'IP dans la réponse
ls_usergen-ip = lo_client->response->get_header_field( name = '~remote_addr' ).
ls_usergen-ip = lo_client->response->get_header_field( name = '~remote_host' ).
ls_usergen-pc = sy-cprog.

" Récupération de la dernière date et heure de connexion de l'utilisateur
SELECT * FROM ztrvta_usergen
INTO TABLE lt_dern_connex
WHERE datum = ls_usergen-datum
AND uname = ls_usergen-uname.

IF sy-subrc IS INITIAL.

DELETE lt_dern_connex WHERE ip NE ls_usergen-ip
OR pc NE ls_usergen-pc.
SORT lt_dern_connex BY timlo DESCENDING.

READ TABLE lt_dern_connex INTO ls_dern_connex INDEX 1.
IF sy-subrc IS INITIAL.
" Une connexion pour le même id utilisateur et dans la même journée a été trouvée

" Récupération du délai de validité de la pop up précédente
SELECT SINGLE low INTO lv_delai_tvarvc
FROM tvarvc
WHERE name EQ 'ZTRACEUSER_DELAI_VALID_PORTAIL'
AND type EQ 'P'.

IF sy-subrc IS INITIAL.
lv_delai = lv_delai_tvarvc.

ENDIF.

" Comparaison avec le délai de validité de la pop up précédente
lv_time_max = ls_dern_connex-timlo + lv_delai.
IF lv_time_max < ls_dern_connex-timlo
OR sy-uzeit < lv_time_max.
" Si le délai de validité de la popup précédente va jusqu'a la fin de la journée
" ou si le délai de validité de la popup précédente n'est pas dépassé,
" ==> il ne faut pas afficher une nouvelle pop up

lv_afficher_popup = abap_false.

ENDIF.

ENDIF.

ENDIF.

ENDIF.

IF lv_afficher_popup = abap_true.

IF sy-cprog EQ 'SAPMHTTP' OR sy-cprog EQ 'SAPMSYST'.
CALL FUNCTION 'MF01_POPUP_RAISON_CONNEXION'
IMPORTING
ev_raison = ls_usergen-raison
ev_commentaire = ls_usergen-commentaire.
ENDIF.

INSERT ztrvta_usergen FROM ls_usergen.
COMMIT WORK.

ENDIF.

ENDIF.

10 |10000 characters needed characters left characters exceeded
* Please Login or Register to Answer, Follow or Comment.

0 Answers