X++ code to copy user roles from one user to another user
Create a class as well as the methods below:
{
UserId fromUser,toUser;
NoYesId roles,groups,options;
SysQueryRun queryrun;
DialogField dlgFromUserId,dlgToUserId,dlgRoles,dlgGroups,dlgOptions;
#define.CurrentVersion(1)
#define.Version1(1)
#localmacro.CurrentList
fromUser,
toUser,
roles,
groups,
options
#endmacro
}
server static RBUserRoleMapping construct()
{
return new RBUserRoleMapping ();
}
public server static void main(Args args)
{
RBUserRoleMapping userRoleMapping = RBUserRoleMapping ::construct();
if (userRoleMapping.prompt())
userRoleMapping.run();
}
protected void new()
{
super();
}
public Object dialog()
{
DialogRunbase dialog = super();
;
//dialog = this.dialogInternal(dialog);
dlgFromUserId = dialog.addField(extendedTypeStr(UserId), "From UserId");
dlgToUserId = dialog.addField(extendedTypeStr(UserId), "To UserId");
dlgRoles = dialog.addField(extendedTypeStr(NoYesId), "Roles");
dlgGroups = dialog.addField(extendedTypeStr(NoYesId), "Groups");
dlgOptions = dialog.addField(extendedTypeStr(NoYesId), "Options");
dlgFromUserId.value(fromUser);
dlgToUserId.value(toUser);
dlgRoles.value(roles);
dlgGroups.value(groups);
dlgOptions.value(options);
return dialog;
}
public boolean getFromDialog()
{
boolean ret;
ret = super();
fromUser = dlgFromUserId.value();
toUser = dlgToUserId.value();
roles = dlgRoles.value();
groups = dlgGroups.value();
options = dlgOptions.value();
return ret;
}
public container pack()
{
return [#CurrentVersion,#CurrentList];
}
public boolean unpack(container packedClass)
{
Version version = RunBase::getVersion(packedClass);
switch (version)
{
case #CurrentVersion:
[version,#CurrentList] = packedClass;
break;
default:
return false;
}
return true;
}
private boolean copyUserRoles()
{
boolean ret = true;
SecurityRole securityRole;
SecurityUserRole securityUserRole;
SecurityUserRole securityUserRoleExist;
SecurityUserRole securityUserRoleInsert;
OMUserRoleOrganization userRoleOrganization, userRoleOrganization_Insert;
List copiedUserRoles = new List(Types::String);
ListEnumerator lEnumerator;
#define.SystemUser('SystemUser')
setPrefix(strFmt("Copy User", fromUser, toUser));
try
{
select firstonly securityRole
where securityRole.AotName == #SystemUser;
delete_from securityUserRole where securityUserRole.User == toUser && securityUserRole.SecurityRole == securityRole.RecId;
while select securityUserRole
where securityUserRole.User == fromUser
notExists join * from securityUserRoleExist
where securityUserRoleExist.SecurityRole == securityUserRole.SecurityRole
&& securityUserRoleExist.User == toUser
{
select firstOnly securityRole
where securityRole.RecId == securityUserRole.SecurityRole;
copiedUserRoles.addStart(securityRole.Name);
securityUserRoleInsert.initValue();
securityUserRoleInsert.SecurityRole = securityUserRole.SecurityRole;
securityUserRoleInsert.User = toUser;
securityUserRoleInsert.insert();
securityUserRoleInsert.clear();
while select userRoleOrganization
where userRoleOrganization.User == fromUser
&& userRoleOrganization.SecurityRole == securityUserRole.SecurityRole
{
userRoleOrganization_Insert.initValue();
userRoleOrganization_Insert.OMHierarchyType = userRoleOrganization.OMHierarchyType;
userRoleOrganization_Insert.OMInternalOrganization = userRoleOrganization.OMInternalOrganization;
userRoleOrganization_Insert.SecurityRole = userRoleOrganization.SecurityRole;
userRoleOrganization_Insert.SecurityRoleAssignmentRule = userRoleOrganization.SecurityRoleAssignmentRule;
userRoleOrganization_Insert.User = toUser;
userRoleOrganization_Insert.insert();
userRoleOrganization_Insert.clear();
}
}
}
catch
{
ret = false;
}
if (ret)
{
lEnumerator = copiedUserRoles.getEnumerator();
if (copiedUserRoles.empty())
info(strFmt("User %1 and %2 have already the same user role",fromUser, toUser));
while (lEnumerator.moveNext())
{
info(strFmt('%1',lEnumerator.current()));
}
}
else
error(strFmt("User Roles aborted please review list"));
return ret;
}
private boolean copyUserGroups()
{
boolean ret = true;
UserGroupList userGroupList;
UserGroupList userGroupListExist;
UserGroupList userGroupListInsert;
List copiedGroups = new List(Types::String);
ListEnumerator lEnumerator;
setPrefix(strFmt("Copy user groups", fromUser, toUser));
try
{
while select userGroupList
where userGroupList.userId == fromUser
notExists join * from userGroupListExist
where userGroupListExist.groupId == userGroupList.groupId
&& userGroupListExist.userId == toUser
{
copiedGroups.addStart(userGroupList.groupId);
userGroupListInsert.initValue();
userGroupListInsert.groupId = userGroupList.groupId;
userGroupListInsert.userId = toUser;
userGroupListInsert.insert();
userGroupListInsert.clear();
}
}
catch
{
ret = false;
}
if (ret)
{
lEnumerator = copiedGroups.getEnumerator();
if (copiedGroups.empty())
info(strFmt("User %1 and %2 have already the same user group",fromUser, toUser));
while (lEnumerator.moveNext())
{
info(strFmt('%1',lEnumerator.current()));
}
}
else
error(strFmt("User group aborted , please review it"));
return ret;
}
private boolean copyUserOptions()
{
boolean ret = true;
UserInfo userInfoSource;
UserInfo userInfoTarget;
SysUserInfo sysUserInfoSource;
SysUserInfo sysUserInfoTarget;
setPrefix(strFmt("Copy user option", fromUser, toUser));
try
{
select filterByGridOnByDefault,statuslineInfo,Id from userInfoSource
where userInfoSource.id == fromUser
join Id,DefaultCountryRegion from sysUserInfoSource
where sysUserInfoSource.Id == userInfoSource.id;
ttsBegin;
select forUpdate userInfoTarget
where userInfoTarget.id == toUser;
userInfoTarget.filterByGridOnByDefault = userInfoSource.filterByGridOnByDefault;
userInfoTarget.statuslineInfo = userInfoSource.statuslineInfo;
userInfoTarget.update();
select forUpdate sysUserInfoTarget
where sysUserInfoTarget.Id == toUser;
sysUserInfoTarget.DefaultCountryRegion = sysUserInfoSource.DefaultCountryRegion;
sysUserInfoTarget.update();
ttsCommit;
}
catch
{
ret = false;
}
if (ret)
{
info(strFmt("User %1 and %2 has the same user option ", fromUser, toUser));
}
else
error(strFmt("User option aborted, please review it"));
return ret;
}
public void run()
{
DialogButton diagBut;
str strMessage = strFmt("Are you sure to copy the roles From UserId : %1 To User Id : %2",fromUser,toUser);
str strTitle = "User Role Mapping";
;
diagBut = Box::yesNoCancel(
strMessage,
DialogButton::No, // Initial focus is on the No button.
strTitle);
if (diagBut == DialogButton::Yes)
{
if (roles)
{
this.copyUserRoles();
}
if (groups)
{
this.copyUserGroups();
}
if (options)
{
this.copyUserOptions();
}
info(strfmt("Successfully copied the user roles from %1 to %2", fromUser,toUser));
}
else
{
print "You cancelled the operation";
}
}
public boolean canGoBatch()
{
return true;
}
No comments:
Post a Comment