Skip to content

Instantly share code, notes, and snippets.

@ilmoralito
Last active October 12, 2017 15:08
Show Gist options
  • Save ilmoralito/9688023 to your computer and use it in GitHub Desktop.
Save ilmoralito/9688023 to your computer and use it in GitHub Desktop.
Update current logged user password, using grails spring securit core plugin, i got help in this mail list http://grails.1312388.n4.nabble.com/update-current-user-password-in-spring-security-core-plugin-td4655284.html
package com.testing
import grails.plugin.springsecurity.annotation.Secured
class UserController {
def springSecurityService
@Secured(['ROLE_ADMIN'])
def profile(profileCommand cmd) {
if (request.method == "POST") {
if (cmd.hasErrors()) {
flash.message = "Upps something when wrong"
return
}
cmd.updatePassword()
flash.message = "Password updated :)"
}
}
}
//validations in command object
class profileCommand {
def springSecurityService
def passwordEncoder
String password
String newPassword
String repeatPassword
static constraints = {
password blank: false, validator: { val, obj ->
def currentUser = obj.springSecurityService.currentUser
def currentUserPassword = currentUser.password
return obj.passwordEncoder.isPasswordValid(currentUserPassword, val, null)
}
newPassword validator: { val, obj ->
val == obj.repeatPassword
}
}
User updatePassword() {
User currentUser = springSecurityService.currentUser
currentUser.password = newPassword
currentUser.save(flush: true)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment