Is it possible to create bonita user using script task?

1
0
-1

I tried to create a user in bonita portal using a script task.

use the following code and failed without success.

// First of all, let's log in on the engine: org.bonitasoft.engine.api.APIClient apiClient = new APIClient() apiClient.login("install", "install") // create new user, with username john and password bpm IdentityAPI identityAPI = apiClient.getIdentityAPI() final User user = identityAPI.createUser("john", "bpm") System.out.println("New user created: " + user)

Is it possible to create bonita user using script task? If possible what am I got wrong here?

Comments

Submitted by kasfer on Tue, 11/02/2021 - 06:07

Tried this script too..

// Let's set the connection settings to use HTTP on the already running Bonita runtime:
Map settings = new HashMap();
settings.put("server.url", "http://localhost:3381");
settings.put("application.name", "bonita");
APITypeManager.setAPITypeAndParams(ApiAccessType.LOCAL, settings);

final LoginAPI loginAPI = TenantAPIAccessor.getLoginAPI();
APISession session = loginAPI.login("install", "install");
ProcessAPI processAPI = TenantAPIAccessor.getProcessAPI(session);

APIClient apiClient = new APIClient()
// create new user, with username john and password bpm
IdentityAPI identityAPI = apiClient.getIdentityAPI()
// create complex user
UserCreator creator = new UserCreator("john", "bpm")
creator.setFirstName("Johnny").setLastName("B. Good")
ContactDataCreator proContactDataCreator = new ContactDataCreator().setAddress("32 rueGustave Eiffel").setCity("Grenoble").setPhoneNumber("555 14 12 541")
creator.setProfessionalContactData(proContactDataCreator)
final User user2 = identityAPI.createUser(creator)

this is the exception

Caused by: org.bonitasoft.engine.core.operation.exception.SOperationExecutionException: org.bonitasoft.engine.expression.exception.SExpressionEvaluationException: Groovy script throws an exception of type class org.bonitasoft.engine.platform.LoginException with message = org.bonitasoft.engine.transaction.STransactionCreationException: We do not support nested calls to the transaction service. Current state is: 0.
Expression : SExpressionImpl [name=createNewUserScript(), content=import org.bonitasoft.engine.api.APIClient

1 answer

1
0
-1
This one is the BEST answer!

Hi !

When you are executing a script in a process operation or a connector, you are already executing code server side as a technical use, so should not try to call the Login service.

This script should just work fine:

 
// Use the apiAccessor from a Groovy script to retrieve the Bonita apis
def identityAPI = apiAccessor.getIdentityAPI()
// create complex user
UserCreator creator = new UserCreator("john", "bpm")
creator.setFirstName("Johnny").setLastName("B. Good")
ContactDataCreator proContactDataCreator = new ContactDataCreator().setAddress("32 rueGustave Eiffel").setCity("Grenoble").setPhoneNumber("555 14 12 541")
creator.setProfessionalContactData(proContactDataCreator)
final User user2 = identityAPI.createUser(creator)
 

HTH
Romain

Comments

Submitted by kasfer on Tue, 11/02/2021 - 11:45

@romain

Thanks for the quick reply.

But it also has the following error.

"depends on apiAccessor is neither defined in the script nor in dependencies."

Submitted by kasfer on Tue, 11/02/2021 - 12:20

@Romain

It works, Your answer is correct. The second time The issue is with the underlying script file.

great relief.

Thanks .

Notifications