Doing custom stuff when a user logs in

By 18 June 2014 Mendix 4 Comments

If you want do some custom checks when a user logs in, it is possible to override the Mendix login mechanism.

With this, you can do a lot of useful things. For example:

  • Implement a custom user blocking mechanism, e.g. block all users that are linked to a certain company.
  • Only allow users to log in from certain IP addresses
  • Dynamically assign user roles when logging in
  • Etc. etc. (leave your comment below for more ideas!)

To override the standard login, we need to do some Java coding. Create a new Java action ReplaceLoginAction, with no parameters. Call this Java action on startup of the application, by adding a startup microflow, and adding a call to this Java action there. Add the following Java code to the Java action:

You need to added 2 Java class files to your module’s Javasource folder:

LoginActionListener.java:

And CustomLoginAction.java:

This executeAction() method will be called when a login-attempt is made, and will do the standard checks that Mendix does when logging in, plus call a microflow LoginAllowed in which you can do your own custom stuff. This can, for example, check whether the application is open for non-admin users, like this:

Microflow LoginAllowed

Microflow LoginAllowed

This microflow will be called by the CustomLoginAction in system context, which allows you to do almost anything! Just be careful for security and performance issues if you do too much here 🙂

4 Comments

  • […] Note: You could also implement a more sophisticated blocking mechanism yourself, by added your own attribute(s) to Administration.Account, an then do a check on these attribute when the user logs in, as explained in my previous post. […]

  • Michael says:

    Hi Bart,

    Trying to implement this function but i receive an error on the first line.
    LoginActionListener loginActionListener = new LoginActionListener();

    Any ideas?

  • Olivier says:

    Hi Bart,

    Thanks for the write up and explanation, great help!
    However, with Mendix 6 this no longer works as it uses Deprecated libraries. Could you update this post?

    thanks!

Leave a Reply

Your email address will not be published.