Oct 2, 2017

How to Create and Use the Authorization Objects in ABAP

Published by rson.

Authorization Objects are used to manipulate the current user’s privileges for specific data selection and activities from within a program.

We could always create our own authorization objects and implement it in our own abap programs. As an example, we will create our own authorization field similar to TCD used in S_TCODE Authorization object

Steps to create authorization field
1. Go to transaction code SU20
2. Click the create new button on the application toolbar.
3. Enter “ZTCODE” in the Field Name and “TCODE” in the Data Element, then hit Enter.
4. Click the save button on the system toolbar.

Steps to create authorization class
1. Go to transaction code SU21
2. Click on the Create button’s drop down icon and select “Object Class”.
3. Enter “ZTRN” on the Object Class field.
4. Give it a description and save it.

Steps to create authorization object
1. Again in SU21, in the list of authorization class(folder icon), click the one that we’ve created(ZTRN).
2. Click on the Create buttodrop down, this time selecting “Authorization Object”.
3. Enter “Z_TCODE” on the Object field and give it a description.
4. On the authorization fields section, enter ACTVT and ZTCODE. ACTVT is used to set and limit the activity of the user, while the ZTCODE is the authorization field that we’ve created earlier which is responsible for holding a list of tcodes.
5. On the Further Authorization Object Settings, click on “Permitted activities” button. Here we will select the specific activities that we want to be available for our authorization object.
6. As an example, we will select 01(Create), 02(Change), and 03(Display).
7. Save and Exit.

Now we’re done creating our own authorization object, let us now use and assign it to a user.

Steps to create a role
1. Go to transaction code PFCG.
2. Enter “ZAUTHTEST” on Role field and click the “Single Role” button.
3. Now give it a description, click the save button and click the Authorization tab.
4. Click the “Change Authorization Data” button inside the authorization tab.
5. Then click the “Manually” button on the application toolbar and type in the name of the authorization object that we’ve created earlier(”Z_TCODE”) and press enter.
6. Expand all the nodes, double click on the input field of the Activity and select activity 01 and 02.
7. Enter the tcode of our own abap program in ZTCODE field, in our example I used “ZCOMM” .
8. And also don’t forget to add the S_TCODE authorization object and enter ZCOMM on it’s field.
9. Now Click on the Generate button in the application toolbar and press enter on the pop-up screen.
10. press the back button and assign a specific user on the user tab and click User Comparison button.
11. Now create another role by repeating steps 1 to 9 but this time select activity 03 on step 6.
12. Then assign this 2nd role to another user.

Now let’s implement this authorization in our ABAP program. Let say we have a dialog program(ZCOMM) wherein we have a button on the screen that when clicked, the user will go to the Create/Edit screen(1000) if he’s authorized. On the other hand, he will go to display only screen(2000) if he’s not authorized. To do that, simply add the code below on your program.

Code:
  AUTHORITY-CHECK OBJECT 'Z_TCODE' "authorization object that we’ve created
      ID ‘ACTVT’ FIELD '01'            "Activity = 01, authorized to create
      ID ‘ZTCODE’ FIELD 'ZCOMM'. "tcodes that we wants to check for authorization
  IF sy-subrc EQ 0.
      CALL SCREEN 1000. "The user is authorized to create
  ELSE.
      CALL SCREEN 2000. "User is not authorized to create (Display only)
  ENDIF.

Sep 19, 2016

Colors in ALV Grid Using Function Module 'REUSE_ALV_GRID_DISPLAY'


From sapdev.co.uk


Source code:

*&-------------------------------------------------------------*
*& Report  YALVCOL_GRID
*&
*&-------------------------------------------------------------*
*& Author: SAP Development
*& Demonstrates simple ALV GRID report along with setting colour of
*& individual cells/fields
*&-------------------------------------------------------------*

REPORT  yalvcol_grid                .

TABLES:     ekko.

TYPE-POOLS: slis.                                 "ALV Declarations
*Data Declaration
*----------------
TYPES: BEGIN OF t_ekko,
         ebeln     
TYPE ekpo-ebeln,
         ebelp     
TYPE ekpo-ebelp,
         statu     
TYPE ekpo-statu,
         aedat     
TYPE ekpo-aedat,
         matnr     
TYPE ekpo-matnr,
         menge     
TYPE ekpo-menge,
         meins     
TYPE ekpo-meins,
         netpr     
TYPE ekpo-netpr,
         peinh     
TYPE ekpo-peinh,
         cellcolor 
TYPE lvc_t_scol,

       
END OF t_ekko.

DATA: it_ekko TYPE STANDARD TABLE OF t_ekko INITIAL SIZE 0,
      wa_ekko 
TYPE t_ekko.

*ALV data declarations
DATA: fieldcatalog  TYPE slis_t_fieldcat_alv WITH HEADER LINE,
      gd_tab_group  
TYPE slis_t_sp_group_alv,
      gd_layout     
TYPE slis_layout_alv,
      gd_repid      
LIKE sy-repid,
      gt_events     
TYPE slis_t_event,
      gd_prntparams 
TYPE slis_print_alv.


************************************************************************
*Start-of-selection.
START-OF-SELECTION.

  
PERFORM data_retrieval.
  
PERFORM build_fieldcatalog.
  
PERFORM build_layout.
  
PERFORM set_cell_colours.
  
PERFORM display_alv_report.


*&---------------------------------------------------------------------*
*&      Form  BUILD_FIELDCATALOG
*&---------------------------------------------------------------------*
*       Build Fieldcatalog for ALV Report
*----------------------------------------------------------------------*
FORM build_fieldcatalog.

* There are a number of ways to create a fieldcat.
* For the purpose of this example i will build the fieldcatalog manualy
* by populating the internal table fields individually and then
* appending the rows. This method can be the most time consuming but can
* also allow you  more control of the final product.

* Beware though, you need to ensure that all fields required are
* populated. When using some of functionality available via ALV, such as
* total. You may need to provide more information than if you were
* simply displaying the result
*               I.e. Field type may be required in-order for
*                    the 'TOTAL' function to work.

  fieldcatalog
-fieldname   = 'EBELN'.
  fieldcatalog
-seltext_m   = 'Purchase Order'.
  fieldcatalog
-col_pos     = 0.
  fieldcatalog
-outputlen   = 10.
  fieldcatalog
-emphasize   = 'X'.
  fieldcatalog
-key         = 'X'.
*  fieldcatalog-do_sum      = 'X'.
*  fieldcatalog-no_zero     = 'X'.
  
APPEND fieldcatalog TO fieldcatalog.
  
CLEAR  fieldcatalog.

  fieldcatalog
-fieldname   = 'EBELP'.
  fieldcatalog
-seltext_m   = 'PO Item'.
  fieldcatalog
-col_pos     = 1.
  
APPEND fieldcatalog TO fieldcatalog.
  
CLEAR  fieldcatalog.

  fieldcatalog
-fieldname   = 'STATU'.
  fieldcatalog
-seltext_m   = 'Status'.
  fieldcatalog
-col_pos     = 2.
  
APPEND fieldcatalog TO fieldcatalog.
  
CLEAR  fieldcatalog.

  fieldcatalog
-fieldname   = 'AEDAT'.
  fieldcatalog
-seltext_m   = 'Item change date'.
  fieldcatalog
-col_pos     = 3.
  
APPEND fieldcatalog TO fieldcatalog.
  
CLEAR  fieldcatalog.

  fieldcatalog
-fieldname   = 'MATNR'.
  fieldcatalog
-seltext_m   = 'Material Number'.
  fieldcatalog
-col_pos     = 4.
  
APPEND fieldcatalog TO fieldcatalog.
  
CLEAR  fieldcatalog.

  fieldcatalog
-fieldname   = 'MENGE'.
  fieldcatalog
-seltext_m   = 'PO quantity'.
  fieldcatalog
-col_pos     = 5.
  
APPEND fieldcatalog TO fieldcatalog.
  
CLEAR  fieldcatalog.

  fieldcatalog
-fieldname   = 'MEINS'.
  fieldcatalog
-seltext_m   = 'Order Unit'.
  fieldcatalog
-col_pos     = 6.
  
APPEND fieldcatalog TO fieldcatalog.
  
CLEAR  fieldcatalog.

  fieldcatalog
-fieldname   = 'NETPR'.
  fieldcatalog
-seltext_m   = 'Net Price'.
  fieldcatalog
-col_pos     = 7.
  fieldcatalog
-outputlen   = 15.
  fieldcatalog
-do_sum      = 'X'.
  fieldcatalog
-datatype     = 'CURR'.
  
APPEND fieldcatalog TO fieldcatalog.
  
CLEAR  fieldcatalog.

  fieldcatalog
-fieldname   = 'PEINH'.
  fieldcatalog
-seltext_m   = 'Price Unit'.
  fieldcatalog
-col_pos     = 8.
  
APPEND fieldcatalog TO fieldcatalog.
  
CLEAR  fieldcatalog.
ENDFORM.                    " BUILD_FIELDCATALOG


*&---------------------------------------------------------------------*
*&      Form  BUILD_LAYOUT
*&---------------------------------------------------------------------*
*       Build layout for ALV grid report
*----------------------------------------------------------------------*
FORM build_layout.
  gd_layout
-no_input          = 'X'.
  gd_layout
-colwidth_optimize = 'X'.
  gd_layout
-totals_text       = 'Totals'(201).
  gd_layout
-coltab_fieldname = 'CELLCOLOR'.  "CTAB_FNAME
ENDFORM.                    " BUILD_LAYOUT


*&---------------------------------------------------------------------*
*&      Form  DISPLAY_ALV_REPORT
*&---------------------------------------------------------------------*
*       Display report using ALV grid
*----------------------------------------------------------------------*
FORM display_alv_report.
  gd_repid 
= sy-repid.
  
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
    
EXPORTING
      i_callback_program     
= gd_repid
      i_callback_top_of_page 
= 'TOP-OF-PAGE'  "see FORM
*     i_callback_user_command = 'USER_COMMAND'
*     i_grid_title           = outtext
      is_layout              
= gd_layout
      it_fieldcat            
= fieldcatalog[]
*     it_special_groups      = gd_tabgroup
*     it_events              = gt_events
*     is_print               = gd_prntparams
      i_save                 
= 'X'
*     is_variant             = z_template
    
TABLES
      t_outtab               
= it_ekko
    
EXCEPTIONS
      program_error          
= 1
      
OTHERS                 = 2.
  
IF sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*         WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
  
ENDIF.
ENDFORM.                    " DISPLAY_ALV_REPORT


*&---------------------------------------------------------------------*
*&      Form  DATA_RETRIEVAL
*&---------------------------------------------------------------------*
*       Retrieve data form EKPO table and populate itab it_ekko
*----------------------------------------------------------------------*
FORM data_retrieval.
  
SELECT ebeln ebelp statu aedat matnr menge meins netpr peinh
   
UP TO 10 ROWS
    
FROM ekpo
    
INTO CORRESPONDING FIELDS OF TABLE it_ekko.
ENDFORM.                    " DATA_RETRIEVAL


*-------------------------------------------------------------------*
* Form  TOP-OF-PAGE                                                 *
*-------------------------------------------------------------------*
* ALV Report Header                                                 *
*-------------------------------------------------------------------*
FORM top-of-page.
*ALV Header declarations
  
DATA: t_header      TYPE slis_t_listheader,
        wa_header     
TYPE slis_listheader,
        t_line        
LIKE wa_header-info,
        ld_lines      
TYPE i,
        ld_linesc
(10) TYPE c.

* Title
  wa_header
-typ  = 'H'.
  wa_header
-info = 'EKKO Table Report'.
  
APPEND wa_header TO t_header.
  
CLEAR wa_header.

* Date
  wa_header
-typ  = 'S'.
  wa_header
-key = 'Date: '.
  
CONCATENATE  sy-datum+6(2) '.'
               sy
-datum+4(2) '.'
               sy
-datum(4) INTO wa_header-info.   "todays date
  
APPEND wa_header TO t_header.
  
CLEAR: wa_header.

* Total No. of Records Selected
  
DESCRIBE TABLE it_ekko LINES ld_lines.
  ld_linesc 
= ld_lines.
  
CONCATENATE 'Total No. of Records Selected: ' ld_linesc
                    
INTO t_line SEPARATED BY space.
  wa_header
-typ  = 'A'.
  wa_header
-info = t_line.
  
APPEND wa_header TO t_header.
  
CLEAR: wa_header, t_line.

  
CALL FUNCTION 'REUSE_ALV_COMMENTARY_WRITE'
    
EXPORTING
      it_list_commentary 
= t_header.
*            i_logo             = 'Z_LOGO'.
ENDFORM.                    "top-of-page


*&---------------------------------------------------------------------*
*&      Form  SET_CELL_COLOURS
*&---------------------------------------------------------------------*
*       Set colour of individual ALV cell, field
*----------------------------------------------------------------------*
FORM set_cell_colours .
  
DATA: wa_cellcolor TYPE lvc_s_scol.
  
DATA: ld_index TYPE sy-tabix.

  
LOOP AT it_ekko INTO wa_ekko.
    ld_index 
= sy-tabix.

*   Set colour of EBELN field to various colors based on sy-tabix value
    wa_cellcolor
-fname = 'EBELN'.
    wa_cellcolor
-color-col = sy-tabix.  "color code 1-7, if outside rage defaults to 7
    wa_cellcolor
-color-int = '1'.  "1 = Intensified on, 0 = Intensified off
    wa_cellcolor
-color-inv = '0'.  "1 = text colour, 0 = background colour
    
APPEND wa_cellcolor TO wa_ekko-cellcolor.
    
MODIFY it_ekko FROM wa_ekko INDEX ld_index TRANSPORTING cellcolor.

*   Set colour of NETPR field to color 4 if gt 0
    
IF wa_ekko-netpr GT 0.
      wa_cellcolor
-fname = 'NETPR'.
      wa_cellcolor
-color-col = 4.  "color code 1-7, if outside rage defaults to 7
      wa_cellcolor
-color-int = '0'.  "1 = Intensified on, 0 = Intensified off
      wa_cellcolor
-color-inv = '0'.  "1 = text colour, 0 = background colour
      
APPEND wa_cellcolor TO wa_ekko-cellcolor.
      
MODIFY it_ekko FROM wa_ekko INDEX ld_index TRANSPORTING cellcolor.
    
ENDIF.

*   Set colour of AEDAT field text to red(6)
    wa_cellcolor
-fname = 'AEDAT'.
    wa_cellcolor
-color-col = 6.  "color code 1-7, if outside rage defaults to 7
    wa_cellcolor
-color-int = '0'.  "1 = Intensified on, 0 = Intensified off
    wa_cellcolor
-color-inv = '1'.  "1 = text colour, 0 = background colour
    
APPEND wa_cellcolor TO wa_ekko-cellcolor.
    
MODIFY it_ekko FROM wa_ekko INDEX ld_index TRANSPORTING cellcolor.
  
ENDLOOP.

ENDFORM.                    " SET_CELL_COLOURS