Last active
October 12, 2017 15:08
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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