kohlehydrat
kohlehydrat

Reputation: 23

Syntax error in BootStrap.groovy

I'm using Grails with the SpringSecurity plugin. I ran s2-quickstart so the user-domain-class was named "User.groovy" and the role-domain-class was named "Role.groovy". Consequently, the mapping class was named "UserRole.groovy". I then modifed BootStrap.groovy to create a sample user, which led to a nasty syntax error "Groovy:unexpected token: UserRole @ line 19, column 2." when calling "UserRole.create".

This is my BootStrap.groovy file:

import com.foo.Role
import com.foo.User
import com.foo.UserRole


class BootStrap {

    def springSecurityService

    def userSecRole = Role.findByAuthority("ROLE_USER") ?: new Role()(authority: "ROLE_USER").save()

    def user = new User(
        username: "user",
        password: springSecurityService.encodePassword("user"),
        enabled: true
        )


    UserRole.create user, userSecRole     // <--- This is where the error happens


    def init = { servletContext ->
    }
    def destroy = {
    }
}

Upvotes: 2

Views: 781

Answers (1)

tim_yates
tim_yates

Reputation: 171144

You've put your code in the main class definition.

That code should be inside the init closure, ie:

import com.foo.Role
import com.foo.User
import com.foo.UserRole

class BootStrap {

    def springSecurityService

    def init = { servletContext ->
      def userSecRole = Role.findByAuthority("ROLE_USER") ?: new Role()(authority: "ROLE_USER").save()

      def user = new User(
          username: "user",
          password: springSecurityService.encodePassword("user"),
          enabled: true
          )

      UserRole.create user, userSecRole     // <--- This is where the error happens
    }
    def destroy = {
    }
}

Upvotes: 2

Related Questions