0
votes

I have a basic Grails 2 application which I setup along with the SpringSecurityCore plugin. This seems to be working fine. However, when I try to add additional properties to my User.groovy file by way of an extended class, I cannot seem to reference those properties in my controllers.

To better illustrate this problem, please take a look at my basic class which extends the User.groovy:

UserInfo.groovy:

class UserInfo extends User {

String firstname
String lastname
}

On my page where I want to reference the current user's firstname, I am simply writing a method as follows:

def index() { 


        def uname = springSecurityService.currentUser.username
        def firstname = springSecurityService.currentUser.firstname

        render uname

    }

This works fine for rendering the username, and I believe that this is because the username is referenced in the base User.groovy file:

class User {

transient springSecurityService

String username
String password

However, the above "def index()" method fails when I try to define the firstname as part of the springSecurityService.currentUser.firstname.

If I inject extra properties into the base User.groovy class, then I can reference them by way of springSecurityService.currentUser.[property]

To illustrate this:

class User {

transient springSecurityService

String username
String password
    String firstname
    ...

I am then able to reference the firstname property of my user in the aforementioned index method.

Is it possible to reference the extended properties of my User without injecting values in the base User class? My goal here is to try and keep the User class as clean as possible while still being able to call upon the values referenced in the UserInfo.groovy file.

Thank you in advance for your time.

3

3 Answers

2
votes

If you really need to put some properties of your User information into another domain and want those properties could still be accessed from springSecurityService.currentUser, you have to implement your own UserDetailsService to package the UserInfo properties with User.

0
votes

If you extend the User class, the SpringSecurity plugin does not know anything about your extended class UserInfo.

If you call springSecurityService.currentUser you get an instance of User not of UserInfo - so you don't have the property firstname.

If you don't like to add several new properties to User you could add a reference to the UserInfo class.

class UserInfo {
    static belongsTo = [user: User]

    String firstname
    String lastname
    ...
}

class User {
   ...
   UserInfo userInfo
   ... 
}

You should remember that this reference adds an extra table to your database, which causes an additional join operation if you want to access your user instance.

0
votes

When you initially install the Spring Security plugin and created your User class, it should have added some properties in Config.groovy that tells it which class to use as the default user class. When extending the default User class, you should update the property in Config.groovy to reflect this. The property you are looking for to update (or add if it's not there) is the following:

grails.plugins.springsecurity.userLookup.userDomainClassName = 'your.package.UserInfo'

There are about a handful of properties in this page that relate to User and Role/Authorities that you may want to update if you extend/update any of the defaults.