RAP: Break out of context
Why?
If you are developing a SAP RAP app and need some individual coding, you are within the context of the app. In terms of authorizations this basically means that you have access to the current entity without the need of authority checks and just need authority checks (privileged access) if you are selecting, writing data or do other stuff outside the entity. So if you want to do some SAP standard stuff, you will not be able to do so, unless you leave the context.
How?
Background Processing Framework
The tool helping us doing so is the Background Processing Framework (bgPF). I won't explain it in detail, because SAP does it here already ;). Of course I will tell you here, what you need to do, to break the boundaries. The bgPF contains of two interfaces and two major methods. The first ones are the one, you can ignore, because they will stay in context and only let you process the stuff in background (obviously). Those are the interface if_bgmc_op_single and the method set_operation. So take care to use the ones we need: if_bgmc_op_single_tx_uncontr and set_operation_tx_uncontrolled.
Example
Lets say you want to create transport requests and allow your users to do it on their own without needing authorization of a developer. Than you can create a simple app containing a field for the description and call the logic in an uncontrolled transaction.
First you need the class implementing the interface for the bgPF and implement the execute method containing the logic to create the transport request (or whatever you need to do):
CLASS zcl_bgpf_impl Definition.
PUBLIC Section.
INTERFACES:
if_bgmc_op_single_tx_uncontr.
METHODS:
constructor
IMPORTING
iv_description TYPE c LENGTH 50. (will adjust the type, I do not have it in mind right now).
PRIVATE Section.
DATA:
mv_description TYPE c LENGHT 50.
ENDCLASS.
CLASS zcl_bgpf_impl Implementation.
METHOD constructor.
mv_description = iv_description.
ENDMETHOD.
METHOD if_bgmc_op_single_tx_uncontr~execute.
DATA(lo_workbench_request) = xco_cp_cts=>transports->workbench( '<yoursystemid>' )->create_request( mv_description ).
DATA(lo_task) = lo_workbench_request->create_task( ).
ENDMETHOD.
ENDCLASS.
This is already the implementation part. Of course you need to call the bgPF method properly. This can be done as followed (only the necessary part is shown). The description of course needs to be properly determined by the RAP app and passed to the constructor.
"Create instance of the class to process
DATA(lo_operation) = NEW zcl_bgpf_impl(
iv_description = lv_description
).
"Create bgPF classes
DATA(lo_process_factory) = cl_bgmc_process_factory=>get_default( ).
DATA(lo_process) = lo_process_factory->create( ).
"Pass instance to bgPF
lo_process->set_name( 'handle user role' )->set_operation_tx_uncontrolled( lo_operation ).
"Execute class
lo_process->save_for_execution( ).
And that's it already. Just a few rows of code and you are able to do whatever you want ignoring your apps' context :D