-4

i want to get the current user who is logged in and i used this way and i jave injected the SpringsecurityService and i am getting this error like "Caused by No such property: principal for class: grails.plugin.springsecurity.SpringSecurityService Possible solutions: principal".

Organization.controller

SpringSecurityService springSecurityService

 def user = springSecurityService.get(SpringSecurityService.principal.id)
Burt Beckwith
  • 75,342
  • 5
  • 143
  • 156
AB21
  • 353
  • 1
  • 4
  • 15
  • Possible duplicate of [Grails Spring Security (get current user)](https://stackoverflow.com/questions/23279897/grails-spring-security-get-current-user) – Anton Hlinisty Apr 16 '18 at 21:14

1 Answers1

3

Inject springSecurityService and call getCurrentUser()

Example:

class OrganizationController {

    def springSecurityService
    def myAction(){
       User user = springSecurityService.getCurrentUser() //where "User" is is user domain class or you can "def" also
    }
}

}

user will hold your current logged in user object.

Hope this will helps you

Rahul Mahadik
  • 11,668
  • 6
  • 41
  • 54
  • 1
    thnk you.You really helped me today i have another probem too i ll post the link here and please see it – AB21 Apr 16 '18 at 15:56
  • 1
    You can get user in gsp using `${applicationContext.springSecurityService.currentUser}` – Rahul Mahadik Apr 16 '18 at 15:57
  • i have recently created "new TrainUser(date:new Date(),user:user,training:trainingMain,status:true).save(flush: true, failOnError: true)" constructor and after this line of code i want the ID it has been stored. how to do it?? – AB21 Apr 16 '18 at 16:20
  • no its new question stackoverflow is not giving me to post until 90 min – AB21 Apr 16 '18 at 16:38
  • `def user=new TrainUser(date:new Date(),user:user,training:trainingMain,status:true).save(flush: true, failOnError: true)` and get id using `user.id` – Rahul Mahadik Apr 16 '18 at 16:41
  • 1
    Please delete above and these comments if this works. we need to follow stackoveflow policies – Rahul Mahadik Apr 16 '18 at 16:42
  • https://stackoverflow.com/questions/49873717/i-am-going-to-save-an-user-and-organization-object-in-database-but-it-is-not-ins please look this issue – AB21 Apr 17 '18 at 08:52