Application Development Blog Posts
Learn and share on deeper, cross technology development topics such as integration and connectivity, automation, cloud extensibility, developing at scale, and security.
cancel
Showing results for 
Search instead for 
Did you mean: 
Srdjan
Product and Topic Expert
Product and Topic Expert


A handy script for copying ABAP user to one or more new users. It works just like SU01 User Copy function, only programatically, by calling BAPIs of SU_USER ABAP Function Group, via Remote Function Calls (RFCs). No new ABAP code or dialog access needed in backend system, only the source ABAP User and the list of new usernames, to be created.

Using the transaction SU01 in this example, the source user UNAMEFROM is selected and copied to new user UNAMETO1:



The end result is a new created ABAP User, with Address, Logon Data, Defaults, Parameters, Roles, Profiles and Groups.



We achieve the same, using BAPIs from SU_USER ABAP Function Group and Python script:



Using the script, you can do these repetitive SU01 task automatically, from your notebook. The Python RFC Connector shall be installed on a notebook and the data to be copied you can customise for your project needs.



Here the code (clone from Github). Error checks and optional parameters removed, for the sake of readability.

 

# -*- coding: utf-8 -*-

from pyrfc import *

import datetime

# BAPI calls log

def print_log(bapi_return):

    if len(bapi_return) > 0:

        for line in bapi_return:

            print '%s: %s' % (line['TYPE'], line['MESSAGE'])

# Connect to ABAP system

SAPROUTER = '/H/123.12.123.12/E/yt6ntx/H/123.14.131.111/H/'

EC4 = {

    'user'      : 'abapuser',

    'passwd'    : 'abappass',

    'ashost'    : '10.11.12.13',

    'saprouter' : SAPROUTER,

    'sysnr'     : '00',

    'client'    : '300',

    'trace'     : '3',

    'lang'      : 'EN' }

c = Connection(**EC4)

# The sourse user, to be copied

uname_from = 'UNAMEFROM'

# Defaults if source user validity not maintained (undefined)

valid_from = datetime.date(2015,1,19)

valid_to   = datetime.date(2015,12,31)

# New users' password. For automatic generation check CREATE BAPI

initpwd = 'InitPa$$21'

# Users to be created

users= ['UNAMETO1', 'UNAMETO2']

# Get source user details

r = c.call('BAPI_USER_GET_DETAIL', USERNAME = uname_from, CACHE_RESULTS  = ' ')

# Set new users' defaults

if r['LOGONDATA']['GLTGV'] is None:

    r['LOGONDATA']['GLTGV'] = valid_from

if r['LOGONDATA']['GLTGB'] is None:

    r['LOGONDATA']['GLTGB'] = valid_to

password = {'BAPIPWD' : initpwd}

# Create new users

for uname_to in users:

    print uname_to

    r['ADDRESS']['LASTNAME'] = uname_to

    r['ADDRESS']['FULLNAME'] = uname_to

    x = c.call('BAPI_USER_CREATE1',

        USERNAME    = uname_to,

        LOGONDATA   = r['LOGONDATA'],

        PASSWORD    = password,

        DEFAULTS    = r['DEFAULTS'],

        ADDRESS     = r['ADDRESS'],

        COMPANY     = r['COMPANY'],

        REF_USER    = r['REF_USER'],

        PARAMETER   = r['PARAMETER'],

        GROUPS      = r['GROUPS']

    )

    print_log(x['RETURN'])

    x = c.call('BAPI_USER_PROFILES_ASSIGN',

        USERNAME  = uname_to,

        PROFILES  = r['PROFILES']

    )

    print_log(x['RETURN'])

    x = c.call('BAPI_USER_ACTGROUPS_ASSIGN',

        USERNAME       = uname_to,

        ACTIVITYGROUPS = r['ACTIVITYGROUPS']

    )

    print_log(x['RETURN'])

# Finished

print ("%s copied to %d new users\nBye!") % (uname_from, len(users))

You can do the same from Java, .NET, nodejs, GO, Ruby, using respective RFC connectors (here PyRFC used) and of course in ABAP (clone from Github😞

 

REPORT zsbucopy1.

DATA:

  uname_from        LIKE bapibname-bapibname VALUE 'UNAMEFROM',

  ls_logondata      TYPE bapilogond,

  ls_defaults      TYPE bapidefaul,

  ls_address        TYPE bapiaddr3,

  ls_company        TYPE  bapiuscomp,

  lt_parameter      TYPE STANDARD TABLE OF bapiparam,

  lt_profiles      TYPE STANDARD TABLE OF bapiprof,

  lt_activitygroups TYPE STANDARD TABLE OF bapiagr,

  lt_return        TYPE STANDARD TABLE OF bapiret2,

  lt_parameter1    TYPE STANDARD TABLE OF bapiparam1,

  lt_groups        TYPE STANDARD TABLE OF bapigroups,

  uname_to          LIKE bapibname-bapibname VALUE 'UNAMETO',

  is_password      TYPE bapipwd.

is_password-bapipwd = 'Init2014'.

CALL FUNCTION 'BAPI_USER_GET_DETAIL'

  EXPORTING

    username      = uname_from

    cache_results  = ' '

  IMPORTING

    logondata      = ls_logondata

    defaults      = ls_defaults

    address        = ls_address

    company        = ls_company

  TABLES

    parameter      = lt_parameter

    profiles      = lt_profiles

    activitygroups = lt_activitygroups

    return        = lt_return

    parameter1    = lt_parameter1

    groups        = lt_groups.

MOVE uname_to TO: ls_address-lastname, ls_address-fullname.

CALL FUNCTION 'BAPI_USER_CREATE1'

  EXPORTING

    username  = uname_to

    logondata  = ls_logondata

    password  = is_password

    defaults  = ls_defaults

    address    = ls_address

    company    = ls_company

    ref_user  = uname_from

  TABLES

    parameter  = lt_parameter

    return    = lt_return

    groups    = lt_groups

    parameter1 = lt_parameter1.

CALL FUNCTION 'BAPI_USER_PROFILES_ASSIGN'

  EXPORTING

    username = uname_to

  TABLES

    profiles = lt_profiles

    return  = lt_return.

CALL FUNCTION 'BAPI_USER_ACTGROUPS_ASSIGN'

  EXPORTING

    username      = uname_to

  TABLES

    activitygroups = lt_activitygroups

    return        = lt_return.

 

Hope you enjoy creating new ABAP users programatically.


1 Comment