Tuesday, 11 March 2008

Allowing or denying self registration with grails-acegi plugin

The grails-acegi-0.2 plugin is great - it adds login, user management, and user registration capabilities to your application in seconds. However, I'm building a web application where I want to be able to let the deployment team decide whether users can register themselves or alternatively, have an administrator create users. I want provide these settings without the deployment unit (the WAR file) having to be modified.

I currently have a properties file for each server (DEV, STAGE, PROD) which defines properties specific to the environment - such as the email server and username/password etc. In this properties file, I also have application settings which are relevant to that environment. So, this is a good place to define a property:

myapp.registration.enabled=false


Grails has an excellent configuration strategy, and in my applications Config.groovy all I have to do is specify the external config locations:

grails.config.locations = ["file:${System.getProperty('myapp.properties')}"]


I've referenced a system property, so anyone deploying my application just needs to use the -D jvm option - when running with grails all we have to do is:

grails -Dmyapp.properties=/server/env/prod/server.properties


Now I just need to modify the RegistrationController.groovy to check this property:

def index = {
//if logon user.
if(authenticateService.userDomain()!=null){
log.info("${authenticateService.userDomain()} user hit the register page")
redirect(action:"show")
return
}

if(!(grailsApplication.config.jtchat.registration.enabled=='false')) {
def person = new Person()
person.properties = params
return ['person': person]
} else {
redirect(uri:'/')
}
}


And that's it. An easy way to provide settings to an application without modifying the WAR. Grails rocks!

0 comments:

Post a Comment