Question

Hi I'm having a hard time trying to add the first user in my grails bootstrap. I've tried many different ways but all have failed since the Audit Trail plugin wants a user to stamp the createdBy and EditedBy fields but the first one doesn't exit yet.

Here is my config for Audit Trails, essentially it's the same as default except I'm using User.username over the User.id:

grails {
    plugin {
        audittrail { 
            createdBy.field = "createdBy"
            createdBy.type   = "java.lang.String" //Long is the default
            editedBy.field = "editedBy"
            editedBy.type   = "java.lang.String" //Long is the default
            createdDate.field = "createdDate"
            editedDate.field = "editedDate"

            currentUserClosure = {ctx->
                def authPrincipal = ctx.springSecurityService.principal
                if(authPrincipal && authPrincipal != "anonymousUser"){
                    return authPrincipal.username
                } else {
                    return null //fall back
                }
            }
        }
    }
}

Here is what my User class looks like, ie. I just add the annotation for the plugin to work:

@gorm.AuditStamp
class User {
  //... basic Spring Security Generated User File with minor changes
}

Here is the contents of my bootstrap file:

def adminUser = new User(
    username: 'admin',
    enabled: true,
    password: 'password',
    firstName: 'ADMIN',
    lastName: 'ADMIN'
).save(flush: true)

// The below will work if I take off the annotation on User class.
SpringSecurityUtils.doWithAuth('admin') {
    def adminRole = new Role(authority: 'ROLE_ADMIN').save(flush: true)
    UserRole.create adminUser, adminRole, true
}

So now how can I add that first user? I've tried:

- Using annonymousUser
    - Failed, plugin prevents it
- Hardcoding the createdBy and editedby to "admin"
    - Failed get's overridden 
- Use executeUpdate to insert the user directly in the DB
    - Failed: in bootstrap
- Delay the save of the first user until the doWithAuth
    - Failed: couldn't find the user

Any help would be great! Thanks!

Was it helpful?

Solution

I had the same problem and was searching for a solution. I managed to get it working by doing the below:

grails {
plugin {
audittrail {
createdBy.field = "createdBy"
createdBy.type   = "java.lang.String" //Long is the default
editedBy.field = "modifiedBy"
editedBy.type   = "java.lang.String" //Long is the default
createdDate.field = "createdDate"
editedDate.field = "modifiedDate"

//custom closure to return the current user who is logged in
currentUserClosure = {ctx->
//ctx is the applicationContext
def userName = ctx.springSecurityService.principal?.username
return userName != null ? userName : "System"
}
}
}
}

In the Bootstrap.groovy file, just do a save like:

def adminUser = new User(username: 'admin', enabled: true, password: 'password',
firstName: 'ADMIN', lastName: 'ADMIN').save(flush: true)
def adminRole = new Role(authority: 'ROLE_ADMIN').save(flush: true)
UserRole.create adminUser, adminRole, true

The newly created first user will be stamped with user "System" (createdBy and modifiedBy columns).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top