Quantcast

Show user detail after login success

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Show user detail after login success

didinj
Hi,

I'm using spring security core plugin with little modification to domain class User.
class User {

    transient springSecurityService

    String username
    String password
    String fullname
    String specialist
    String city
    String country
    byte[] avatar
    boolean enabled
    boolean accountExpired
    boolean accountLocked
    boolean passwordExpired

    static constraints = {
        username(blank: false, unique: true, email:true)
        password(blank: false, password:true)
        fullname(blank:true) 
        specialist()
        city()
        country()
        avatar(blank:true, maxSize:102400)
    }

    static mapping = {
        password column: '`password`'
    }

    Set<Role> getAuthorities() {
        UserRole.findAllByUser(this).collect { it.role } as Set
    }

    def beforeInsert() {
        encodePassword()
    }

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

    protected void encodePassword() {
        password = springSecurityService.encodePassword(password)
    }
}

Then I do grails generate-all for this domain.
Everything working fine, I can create user and login. My question is how to show current user detail describe in User domain  after user login?

Should I redirect to /views/user/show after login? where I put redirect code in LoginController?

Thanks,
Didin
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Show user detail after login success

bobbywarner
You configure which URL Spring Security redirects to after login by adding a line like this in Config.groovy:

grails.plugins.springsecurity.successHandler.defaultTargetUrl = '/whatEverYouWant'

To get the logged in user on whatever controller you redirect to, you can use code like this:

User.get(springSecurityService.principal.id)


Hope that helps!
Bobby
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Show user detail after login success

didinj
Thanks bob for reply,

I have done to add url in config.groovy and redirect works.
grails.plugins.springsecurity.successHandler.defaultTargetUrl = '/user/show'

I'm trying to put user id at user controller:
def springSecurityService

def show() {
        
        def userInstance = User.get(params.id)
        if (!userInstance) {
			flash.message = message(code: 'default.not.found.message', args: [message(code: 'user.label', default: 'User'), params.id])
            redirect(action: "list")
            return
        }

        [userInstance: userInstance]
    }

But that's returned error :
No such property: id for class: java.lang.String. Stacktrace follows:
Message: No such property: id for class: java.lang.String
    Line | Method
->>   24 | save      in lindproject.UserController
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
|   1110 | runWorker in java.util.concurrent.ThreadPoolExecutor
|    603 | run . . . in java.util.concurrent.ThreadPoolExecutor$Worker
^    722 | run       in java.lang.Thread
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Show user detail after login success

didinj
Finally I fix by my self and works!

Here'se the final controller code:
def springSecurityService

def show() {
        def user = springSecurityService.getPrincipal() 
        def userid = user?.getId()
        def userInstance = User.get(userid)
        if (!userInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'user.label', default: 'User'), params.id])
            redirect(action: "list")
            return
        }

        [userInstance: userInstance]
    }

Is there any simpler or more secure code this?
Loading...