Event Listener : UserAuthenticatedEvent()

Hello,

I am trying to use the UserAuthenticatedEvent() to send a message to a specific location in xWiki whenever a new user logs in. In this example, I want to send the message “TEST” to the site called Logs (see the attached picture for clarity).

image

However, while the code does not produce any errors, it doesn’t perform the expected action when I log off and log back on. I used the code from the “Writing an Event Listener” guide on XWiki.org, which works perfectly for document modifications.

Here the code I tried:

{{groovy}}
import org.xwiki.observation.*
import org.xwiki.observation.event.*
import org.xwiki.observation.EventListener;
import org.xwiki.bridge.event.*
import org.xwiki.model.reference.*
import org.xwiki.model.*
import com.xpn.xwiki.web.*
import com.xpn.xwiki.*
import org.xwiki.security.authentication.*
import org.xwiki.component.annotation.Component
 
@Component
class UserAuthenticatedListener implements EventListener {
 
    String getName() {
        return "UserAuthenticatedListener"
    }
 
    List<Event> getEvents() {
        return [new UserAuthenticatedEvent()]
    }
 
    void onEvent(Event event, Object source, Object context) {
        // Reference to the log document
        def logReference = new EntityReference("Logs", EntityType.DOCUMENT, new EntityReference("Main", EntityType.SPACE))
 
        // Retrieve the XWiki context
        XWikiContext xcontext = (XWikiContext) context
 
        // Construct the message to log
        def message = "* Test message at ${new Date()}"
 
        // Get the document and append the message
        def document = xwiki.getDocument(logReference, context)
        document.setContent("TEST")
 
        // Save the document changes
        xwiki.saveDocument(document, "Logging event", true, context)
    }
}
{{/groovy}}

I hope someone here can help me resolve this issue.

Thank you!

Hi @w0lf020,

If you would try your code in Java, you will notice that there is no default constructor for the UserAuthenticatedEvent class, so you will need to pass the user reference as parameter.
Also, you don’t get any feedback (erors) because your component is not registered.
I suggest to either implement this as Java Component or as a Wiki Component.

Hope it helps,
Alex

Note that you can pass null to catch any user authentication.

Hello :slight_smile: ,
thank you for the answer, but I am still in the fog at the moment on how exactly it works. In the Wiki Component API (XWiki.org) there is a simple example made for the EventListener but my question is. how can I implement this Java Code: xwiki-platform/xwiki-platform-core/xwiki-platform-security/xwiki-platform-security-authentication/xwiki-platform-security-authentication-api/src/main/java/org/xwiki/security/authentication/UserAuthenticatedEvent.java at master · xwiki/xwiki-platform (github.com) ?

I would be grateful if someone can give me an example to make my life easier :slight_smile:

You can use any example from the existing code. Here’'s one event listener that handles the likes: xwiki-platform/xwiki-platform-core/xwiki-platform-like/xwiki-platform-like-notifications/src/main/java/org/xwiki/like/internal/LikeEventListener.java at master · xwiki/xwiki-platform · GitHub.
Instead of new LikeEvent() you will use new UserAuthenticatedEvent(null)
Also, you have to register you component in the components.txt file.
Please follow the documentation https://extensions.xwiki.org/xwiki/bin/view/Extension/Component%20Module.

Thank you so much, I will take a look :slight_smile: