英文:
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标准系统无法满足你的需求时才构建自己的授权系统。
至于BLOCK
和SKIP
,你可能会想它们是否需要一个修饰符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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论