Follow the steps explained in this blog to set up a new custom check. We will use these steps to set up an extra SCI class to check if the AUTHORITY-CHECK statement is added in ABAP code or not.
New SCI check coding
Step 1: create the ZCL_CI_SCAN_AUTH class
In SE24 copy class CL_CI_TEST_FREE_SEARCH to ZCL_CI_SCAN_AUTH. In the attributes of the copied class set C_MY_NAME as variable to ‘ZCL_CI_SCAN_AUTH’. Also set the error code MCODE_0001 to ‘Z001’.
Step 2: redo the CONSTRUCTOR
Goto the constructor of the new class and overwrite the existing code with this code snippit:
super->constructor( ). description = 'Search authority check statement'(001). "required category = 'ZCL_OWN_CHECKS'. "required version = '000'. "required has_attributes = c_false. "optional attributes_ok = c_false. "optional DEFINE fill_message. CLEAR smsg. smsg-test = c_my_name. smsg-code = &1. "message code smsg-kind = &2. "message priority smsg-text = &3. "message text smsg-pcom = &4. "pseudocomment INSERT smsg INTO TABLE scimessages. END-OF-DEFINITION. fill_message 'Z001' 'E' 'Search authority check statement'(001) ' '.
Don’t forget to double click on the 001 to generate the text message.
Step 3: adapt the RUN code
Now the check itself has to be built in the RUN method:
DATA: l_include TYPE sobj_name, l_row TYPE token_row, l_column TYPE token_col, l_tokennr LIKE statement_wa-from, l_code TYPE sci_errc, l_search_string LIKE LINE OF search_strings VALUE 'AUTHORITY-CHECK', l_position TYPE i, l_found TYPE c VALUE ' '. * IF search_strings IS INITIAL. * RETURN. * ENDIF. IF ref_scan IS INITIAL. CHECK get( ) = 'X'. ENDIF. CHECK ref_scan->subrc = 0. *-- loop at all tokens LOOP AT ref_scan->statements INTO statement_wa. CHECK statement_wa-from <= statement_wa-to. l_position = sy-tabix. IF statement_wa-type = 'S' OR statement_wa-type = 'P'. CHECK comment_mode = 'X'. ENDIF. LOOP AT ref_scan->tokens INTO token_wa FROM statement_wa-from TO statement_wa-to. l_tokennr = sy-tabix. IF token_wa-type = 'S'. CHECK literal_mode = 'X'. ENDIF. *-- does ABAP-string contain search-string ? IF token_wa-str CP l_search_string. UNPACK sy-tabix TO l_code(4). l_include = get_include( ). l_row = get_line_abs( l_tokennr ). l_column = get_column_abs( l_tokennr ). l_found = 'X'. EXIT. ENDIF. "l_strpos > l_pos ENDLOOP. ENDLOOP. IF l_found NE 'X'. inform( p_sub_obj_type = c_type_include p_sub_obj_name = l_include p_position = l_position p_line = l_row p_column = l_column p_kind = 'E' p_test = c_my_name p_code = 'Z001' p_suppress = '"#EC CI_NOAUTH ' p_param_1 = token_wa-str ). ENDIF.
Basically the code looks for the statement ‘AUTHORITHY-CHECK’. If it found nothing happens. If it is found, it will generate a message.
Step 4: generating the message
In the method GET_MESSAGE_TEXT overwrite the code with this new code:
data: L_CODE type SCI_ERRC. if P_TEST <> MYNAME or p_code = c_code_not_remote_enabled. SUPER->GET_MESSAGE_TEXT( exporting P_TEST = P_TEST P_CODE = P_CODE importing P_TEXT = P_TEXT ). return. endif. L_CODE = P_CODE. shift L_CODE left deleting leading SPACE. P_TEXT = 'No authorithy-check statement found'(101). replace first occurrence of '&N' in P_TEXT with L_CODE.
SCI settings
Use steps from blog xxx to add the new check to the SCI variant ZTEST.
Test program
We have written a simple test program without AUTHORITHY-CHECK.
When running the SCI with our test variant, this is the result: