Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get id of the current user logged in grails?

Tags:

grails

I want to display the info about the user logged in like his details etc given during sign up ...how to do it ?? As iam new to grails plz help! iam using Spring security plugin

like image 245
ruheen Avatar asked Apr 24 '12 08:04

ruheen


3 Answers

Well you can use the springSecurityService to get some user information in a controller:

    class MyController {
      def springSecurityService

      def myAction() {
        def principal = springSecurityService.principal
        String username = principal.username
        ...
      }
     }

Or in a gsp

    <sec:loggedInUserInfo field="username" />

It is a pretty general question.

like image 59
Kelly Avatar answered Sep 28 '22 07:09

Kelly


Define (in a controller, or service where you need user id):

SpringSecurityService springSecurityService

and try:

springSecurityService.currentUser.id //loads user from db, and returns its id

or:

springSecurityService.principal.id //if you need just id
like image 37
Igor Artamonov Avatar answered Sep 28 '22 09:09

Igor Artamonov


add this @the top of your service or controller class

def securityService

this returns the cuureent user id.

securityService.currentUser.id 
like image 34
abdul Avatar answered Sep 28 '22 09:09

abdul