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).
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!