cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - Your Friends List is a way to easily have access to the community members that you interact with the most! X

Custom Authenticator - get User object in order to set User Extenstions properties

jacekgra
1-Visitor

Custom Authenticator - get User object in order to set User Extenstions properties

Hi, I am working on custom authenticator and I would like to store some auth key (from external system) in given user extension property directly in Authenticator code.

In normal service I can use following code:

User user = (User) EntityUtilities.findEntity("Jacek", RelationshipTypes.ThingworxRelationshipTypes.User);
user.setPropertyValue("lastName", new StringPrimitive("Gralak"));


And this works great. However when I am using the same piece of code after fire of successful login event in authenticator User object is null (EntityUtiilites are not able to retrieve user object):

AuthenticationUtilities.validateEnabledThingworxUser(USERNAME);


this.setCredentials(USERNAME);
AuthenticationUtilities.getSecurityMonitorThing().fireSuccessfulLoginEvent(USERNAME,SharedConstants.EMPTY_STRING);

User user = (User) EntityUtilities.findEntity(USERNAME, RelationshipTypes.ThingworxRelationshipTypes.User);
user.setPropertyValue("lastName", new StringPrimitive("authentiocator"));


Here user object is null.

Any hints?

3 REPLIES 3

I'm running into this same error.  Any luck on figuring it out?

A_Macierzynski
14-Alexandrite
(To:jacekgra)

According to the doc validateEnabledThingworxUser method is deprecated (in 8.1). Maybe you can try to use validateThingworxUser.

I figured this out.  You need to give the User a Context.  Try this:

            // Start Transaction
            TransactionFactory.beginTransactionRequired();
            ThreadLocalContext.setSecurityContext(SecurityContext.createSuperUserContext());

            // Get User
            User user = (User) EntityUtilities.findEntity(userName, ThingworxRelationshipTypes.User);

            // Set User Property

            user.setPropertyValue("lastName", new StringPrimitive("authenticator"));

            // End Transaction

            TransactionFactory.endTransactionRequired();


I'm not sure if this needs to be in a transaction, but this works for me.  You'll need to wrap everything in a try/catch.  I have this code in a method that is called after the fireSuccessfulLoginEvent is fired.  Hope this helps you.


Announcements


Top Tags