Grails - MongoDB and plain dirty checking

I am using MongoDB and Spring Security Core and UI in my application. Almost everything works fine except for this bit:

def beforeUpdate() { if (isDirty('password')) { encodePassword() } } 

which is part of the user domain class. I read that dirty checking was not yet supported by the MongoDB plugin. So I tried to implement my own:

 if ( User.collection.findOne(id:id).password != password ) { encodePassword() } 

But it does not work. I get the classic Cannot get property 'password' on null object.

Does anyone know how to reference an instance from a domain class definition? I also open up any best idea for doing a dirty check.

+4
source share
4 answers

Maybe findOne returns null? You tried:

 def existing = User.collection.findOne(id:id)?.password if ( !existing || existing != password ) 
+2
source

I just got into the same problem until dynamic methods work. This needs to be done:

 def mongo def beforeUpdate() { def persisted = mongo.getDB("test").user.findOne(id).password def encodedNew = springSecurityService.encodePassword(password) if(persisted != encodedNew) password = encodedNew //if (isDirty('password')) { // encodePassword() //} } 
0
source
 User.collection.findOne(_id:id).password 
0
source

I also struggled with this problem - here is my solution:

 def beforeUpdate() { User.withNewSession { def user = User.findByUsername(this.username) if ( !user?.password || user?.password != password) { encodePassword() } } } 

Please let me know if there is a more efficient way.

0
source

Source: https://habr.com/ru/post/1411545/


All Articles