When developing a Grails 3.0 plugin:
- How should a domain be defined so that it can be extended by the application using the plugin?
- How does the plugin reference instances of the extended class?
As an example, a security plugin could have classes such as:
User.groovy
package com.example.plugins.security
class User {
String email
String hash
Boolean enabled = true
}
SecurityService.groovy
package com.example.plugins.security
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder
class SecurityService {
def authenticate(String email, String password) {
def user = User.findByEmail(email) //instance of BookstoreUser???
def encoder = new BCryptPasswordEncoder()
return user && encoder.matches(password, user.hash) ? user : null
}
}
The application would have a domain such as:
grails-app/domain/com/example/bookstore/BookstoreUser.groovy
package org.example.bookstore
import org.bson.types.ObjectId
import org.example.plugins.security.User
class BookstoreUser extends User {
ObjectId id
String firstName
String lastName
static mapWith = "mongo"
}
The rest of the code is at:
https://github.com/center-key/bookstore
The same way you would define any other domain class. Declare the Groovy source file under
grails-app/domain/
.The code that you show in your question would work fine.
If an application writes a class like
BookstoreUser
that extends yourUser
class, an instance ofBookstoreUser
can be passed into yourdisableUser
method and that method will behave as you probably expect it should. The method will set theenabled
property tofalse
and will then save the updated instance.