How to create a user in login plugin

I've created a custom authentication plugin and login plugin to allow users from my other system automatically login to nuxeo as long as they have an active cookie but we don't have user accounts for all users from my other system.

I am trying to create user accounts in the login plugin during the validateUserIdentity function but I can't figure out exactly how to go about doing this. Does anyone have the code I'm missing?

@Override
public String validatedUserIdentity(UserIdentificationInfo userIdent) 
{
    log.info("validate user id");
    try
    {
        log.info("get user manager");
        UserManager userManager = Framework.getService(UserManager.class);
        log.info("try to get user principal from: " + userIdent.getUserName());
        NuxeoPrincipal principal = userManager.getPrincipal(userIdent.getUserName());
        if ( principal != null )
        {
            log.info("found pricipal, returning username");
            //found an existing user, just return the username?
            return userIdent.getUserName();
        }
        else
        {
            log.info("principal was null, create a new user");
            UserSession user_session = WebEngine.getActiveContext().getUserSession();
            CoreSession core_session = user_session.getCoreSession();

                            //Was attempting to find a way to get a user doc ref but couldn't figure it out

            //TODO get the user doc model and fill it out somehow?      
            DocumentModel user_doc_model = new something();
            userManager.createUser(user);
        }
    }
    catch ( Exception ex )
    {
        log.error("error getting pricipal",ex);
    }
    return null;
}

Any help would be much appreciated!

0 votes

1 answers

1247 views

ANSWER



After playing around some more I answered my own question. The UserManager object comes with a method to get the user document model (should have looked for that first, my mistake). So if you are just using the default user objects you can create users like this:

@Override
public String validatedUserIdentity(UserIdentificationInfo userIdent) 
{
    log.info("Burch: validate user id");
    try
    {
        log.info("get user manager");
        UserManager userManager = Framework.getService(UserManager.class);
        log.info("try to get user principal from: " + userIdent.getUserName());
        NuxeoPrincipal principal = userManager.getPrincipal(userIdent.getUserName());
        if ( principal != null )
        {
            log.info("found pricipal, returning username");
            //found an existing user, just return the username?
            return userIdent.getUserName();
        }
        else
        {
            log.info("principal was null, create a new user");              
            DocumentModel user_doc_model = userManager.getBareUserModel();
            user_doc_model.setProperty("user", "username", userIdent.getUserName());
            user_doc_model.setProperty("user", "email", userIdent.getUserName());
            user_doc_model.setProperty("user", "password", "fakepassword" + new Random().nextInt());
            userManager.createUser(user_doc_model);
            return userIdent.getUserName();
        }
    }
    catch ( Exception ex )
    {
        log.error("error getting pricipal",ex);
    }
    return null;
}

Be aware these users have no access rights currently as is so they get some errors when logging in but they exist!

0 votes



Hi, could you please give me an example how you built the plugin and what files you uploaded it where ? I am struggling to create a login plugin
07/24/2017