Creating user in java class

Hey guys, how`s everyone?

I need to do the following:

I have service that handles a response from an external validation server, and I need to get this response and create an user in java class. Is that possible?

I have tried to use the class LoginHandler (https://github.com/enonic/xp/blob/master/modules/lib/lib-auth/src/main/java/com/enonic/xp/lib/auth/LoginHandler.java), but I had difficult to call the method initialize from LoginHandler, because I’m not sure how to create a context object. Any idea?

Thanks a lot!

If your class extends the ScriptBean-interface, the initialize-method will be invoked automatically when the application is installed/started, there you get the context. Then use this method to set your services etc that you need to fetch from the context.

1 Like

Thanks for your reply!

I didn’t get the right idea to do that. Did you mean anything like that?

public final class TestScriptBean
    implements ScriptBean
{
	private Supplier<SecurityService> securityService;
	private Supplier<Context> context;

    @Override
    public void initialize( final BeanContext context )
    {
    	System.out.println("TestScriptBean");
    	
        this.securityService = context.getService( SecurityService.class );
        this.context = context.getBinding( Context.class );
    }	
}

I have done more tests and I got your idea. I noted that initialize method is calling when you call java code from JS controller, i.e., when we call newBean method. In my situation, I would like to create a user in java class, I mean, is it possible to create an user in Java file and not by service or controller from enonic? Thanks!

exports.get = handleRequest;

function handleRequest(req){
    
	var bean = __.newBean(foo.TestScriptBean');
	
    return {
        contentType: "application/json",
        body: {
            nameId: bean.getNameId(),
            uid: bean.getUid()
        }
    }
}

Can you explain a bit more the context of what you are trying to do? What will trigger the execution of your Java class, if it is not an HTTP request?

If what you want is to create a user when the application is started, you can create a main.js file and make it call your bean defined in a js library in the app.

http://xp.readthedocs.io/en/stable/developer/ssjs/main.html

Yes, I can…

The trigger execution of my Java class is a HTTP request. I don’t want to create a user when application start. I would like to create a user when a get a response from external validation server.

I have a JAVA class that handles a response from an external validation server and I would like to create a user when handles that response. See code example below. Do you get the idea?

public class ProtectedResource implements JaxRsComponent
{
    @GET
    @Path("/doLogin")
    @Produces(MediaType.APPLICATION_JSON)
    public void doLogin(@Context HttpServletRequest request, @Context HttpServletResponse response) throws Exception{
       //request to external server
    }

    @GET
    @POST
    @Path("/HandleResponse")
    @Produces(MediaType.APPLICATION_JSON)
    public void HandleResponse(@Context HttpServletRequest request, @Context HttpServletResponse response) throws Exception{
    	//handle response from external server (user data)
           //create a user or if user exists, do login
    }
}

Is there a reason why you are not simply implementing an Idprovider app to handle this?

We are doing a SAML 2.0 integration. Is it possible with Idprovider?