仅对特定用户显示一个选项屏幕块。

huangapple go评论66阅读模式
英文:

Show a block of selection screen only to certain users

问题

我试图给选择屏幕的一个块提供访问权限,只有少数人可以看到。对于其他SAP用户,它应该是不可见的,但我犯了一些错误。

我要隐藏的块的声明:

SELECTION-SCREEN BEGIN OF BLOCK b3 WITH FRAME TITLE TEXT-004.
  SELECTION-SCREEN SKIP 2.
  SELECTION-SCREEN PUSHBUTTON 3(45) TEXT-ba5 USER-COMMAND x.
  SELECTION-SCREEN SKIP 2.
  SELECTION-SCREEN PUSHBUTTON 3(45) TEXT-bb3 USER-COMMAND y.
  SELECTION-SCREEN SKIP 2.
SELECTION-SCREEN END OF BLOCK b3.

应该完成任务的代码片段:

DATA: lt_authorized_users TYPE STANDARD TABLE OF sy-uname,
      ls_user             TYPE sy-uname.

 lt_authorized_users = VALUE #( ( 'USER 1' )
                                ( 'USER 2' ) ).

AT SELECTION-SCREEN OUTPUT.
  LOOP AT SCREEN.
    IF screen-group1 = 'BLK'.
      IF sy-uname = ls_user.
        screen-active = 1.
      ELSE.
        screen-active = 0.
      ENDIF.
      MODIFY SCREEN.
    ENDIF.
  ENDLOOP.

请注意,上述代码片段中,我们根据用户的名称(sy-uname)来决定是否激活选择屏幕块。

英文:

I was trying to give an access to one block of selection screen that should only a few people see. For the rest of SAP users, it should be invisible but I've made some mistakes.

Declaration of the block that I want to hide:

SELECTION-SCREEN BEGIN OF BLOCK b3  WITH FRAME TITLE TEXT-004.
  SELECTION-SCREEN SKIP 2.
  SELECTION-SCREEN PUSHBUTTON 3(45) TEXT-ba5 USER-COMMAND x.
  SELECTION-SCREEN SKIP 2.
  SELECTION-SCREEN PUSHBUTTON 3(45) TEXT-bb3 USER-COMMAND y.
  SELECTION-SCREEN SKIP 2.
SELECTION-SCREEN END OF BLOCK b3.

Fragment of code that should've done the job:

DATA: lt_authorized_users TYPE STANDARD TABLE OF sy-uname,
      ls_user             TYPE sy-uname.

 lt_authorized_users = VALUE #(  ( 'USER 1' )
                                 ( 'USER 2' ).

                              
  LOOP AT lt_authorized_users INTO ls_user.
  ENDLOOP.


AT SELECTION-SCREEN OUTPUT.
  LOOP AT SCREEN.
    IF screen-group1 = 'BLK'.
      IF sy-uname = ls_user.
        screen-active = 1.
      ELSE.
        screen-active = 0.
      ENDIF.
      MODIFY SCREEN.
    ENDIF.
  ENDLOOP.

答案1

得分: 3

你在AT SELECTION-SCREEN OUTPUT中使用LOOP AT SCREEN时走在正确的道路上。但是要让IF screen-group1 = 'BLK'生效,实际上你需要说明哪些选择屏幕元素属于组 "BLK",你可以使用大多数选择屏幕元素都支持的MODIF ID id来实现:

SELECTION-SCREEN PUSHBUTTON 3(45) TEXT-ba5 USER-COMMAND x MODIF ID blk.

然后你的代码中还有另一个问题,不是技术问题,而是非常糟糕的实践:构建自己的授权系统!SAP已经有一个授权系统,它是围绕在tcode PFCG中定义的角色构建的,这些角色包含在tcode SU21中定义的授权对象的授权,然后使用tcode SU01将其分配给用户。你可以使用ABAP指令AUTHORITY-CHECK来检查用户是否被授权执行某项操作。每当涉及用户授权时,你应该建立在这个系统的基础上。自制的授权系统对于维护、管理、故障排除和合规性来说是一场噩梦。只有在SAP标准系统无法满足你的需求时才构建自己的授权系统。

至于BLOCKSKIP,你可能会想它们是否需要一个修饰符ID?不,它们不需要。它们不能有修饰符ID。但也不需要,因为当一个块不包含可见元素时,整个块都会在选择屏幕上消失,不占据任何空间。

英文:

You are on the right track with LOOP AT SCREEN in AT SELECTION-SCREEN OUTPUT. But for IF screen-group1 = 'BLK' to work, you actually have to state which elements of the selection screen belong to the group "BLK". You do that with the addition MODIF ID id that is available for most selection screen elements:

SELECTION-SCREEN PUSHBUTTON 3(45) TEXT-ba5 USER-COMMAND x MODIF ID blk.

And then there is another problem in your code that isn't really a technical problem but really, really bad practice: Building your own authorization system! SAP already has an authorization system. It is built around roles defined in tcode PFCG, which contain authorizations for authorization objects defined in tcode SU21 and get assigned to users with tcode SU01. You can check if a user is authorized to do something with the ABAP instruction AUTHORITY-CHECK. Whenever you deal with user authorizations, you should be building on this system. Homebrewed authorization systems are a nightmare for maintenance, administration, troubleshooting and compliance. Only build them when you have a really good reason why the SAP standard system doesn't work for you.

SELECTION-SCREEN BEGIN OF BLOCK b3  WITH FRAME TITLE TEXT-004.
  SELECTION-SCREEN SKIP 2.
  SELECTION-SCREEN PUSHBUTTON 3(45) TEXT-ba5 USER-COMMAND x MODIF ID blk.
  SELECTION-SCREEN SKIP 2.
  SELECTION-SCREEN PUSHBUTTON 3(45) TEXT-bb3 USER-COMMAND y MODIF ID blk.
  SELECTION-SCREEN SKIP 2.
SELECTION-SCREEN END OF BLOCK b3.

AT SELECTION-SCREEN OUTPUT.
  LOOP AT SCREEN.
    IF screen-group1 = 'BLK'.
      AUTHORITY-CHECK OBJECT Z_SEEBUTTONS
                      ID ACTVT = '03'. "Just an example
      IF sy-subrc <> 0.
        screen-active = 0.
        MODIFY SCREEN.
      ENDIF.
    ENDIF.
  ENDLOOP.

You might wonder what's about the BLOCK and the SKIP. Don't they need a modifier ID, too? No, they don't. They can't have one. But they don't need one either, because when a block contains no visible elements, then the whole block disappears without taking up any space on the selection-screen.

huangapple
  • 本文由 发表于 2023年5月10日 20:22:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/76218370.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定