Skip to content

Instantly share code, notes, and snippets.

@stevewithington
Last active April 30, 2020 15:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save stevewithington/5979618 to your computer and use it in GitHub Desktop.
Save stevewithington/5979618 to your computer and use it in GitHub Desktop.
Mura CMS : Examples of how to intercept User creation, update and save events to do some processing of the User Bean and then create custom error messages as well.
<cfscript>
public any function onBeforeUserCreate($, event) {
var user = arguments.event.getValue('userbean');
// do some processing of the user 'bean'
// to create an error message, just add any key to the end of the User's getErrors() struct:
user.getErrors().createFailMessage1 = 'UserCreate fail message goes here. Sorry #user.getFName()#.';
}
public any function onBeforeUserUpdate($, event) {
var user = arguments.event.getValue('userbean');
user.getErrors().updateFailMessage1 = 'UserUpdate fail message goes here. Um...#user.getFName()#...whatcha tryin to do?';
// maybe you do some more processing of the data and have another error...
user.getErrors().anotherFailMessage = 'Another fail while attempting to update the user...so here is the message.';
}
public any function onBeforeUserSave($, event) {
var user = arguments.event.getValue('userbean');
// if you need access to the original userbean
//var originalUserBean = arguments.$.getBean('user').loadBy(userid=user.getUserID());
var originalUserBean = arguments.$.getBean('user').loadBy(username=user.getUsername());
// 2 = user, 1 = group
if ( user.getType() == 2 ) {
user.getErrors().saveFailMessage1 = 'UserSave fail message: Hey #user.getFName()#...not gonna happen.';
}
// you could check for errors, and even _replace_ the error message...
// for example, maybe there's a duplicate username
if ( StructKeyExists(user.getErrors(), 'username') {
user.getErrors().username = 'Hey, the username, #user.getUsername()#, is already being used! The email address associated with the account is #originalUserBean.getEmail()#. If that is you, then reset the password by entering the email address in the form located at <a href="./?nocache=1&display=login">HERE</a>.';
}
}
</cfscript>
@maisyweb
Copy link

Hi Steve, Is this code still valid with the latest version of mura? I've tried your onBeforeUserUpdate code and the error just get ignored and the user details save.

@wesme1337
Copy link

Theres a typo on line 20: user.getUseranme() should be user.getUsername()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment