[Catalyst] Capturing user object during login

Michael Reece mreece at sacbee.com
Thu Apr 14 20:32:27 CEST 2005


I don't know about 'proper' but what I ended up with is:

sub login {
    my ( $c, $user, $password ) = @_;
    return 1 if $c->request->{user};
    my $user_class     = $c->config->{authentication}->{user_class};
    my $user_field     = $c->config->{authentication}->{user_field}
        || 'user';
    my $password_field = $c->config->{authentication}->{password_field}
        || 'password';
    if (my @users = $user_class->search( {
            $user_field => $user,
            $password_field => $password
        } )
    ) { 
        # $c->request->{user} = $user;
        $c->request->{user} = $users[0];
        return 1;
    }
    return 0;
}

sub session_login {
    my ( $c, $user, $password ) = @_;
    return 0 unless $c->login( $user, $password );
    # $c->session->{user} = $user;
    $c->session->{user} = $c->request->{user};
    return 1;
}

In my app, a user has the ability to modify their user record (preferences,
etc), so to make sure any changes are reflected in the session object, I
have the following in my '!end' action (I'm still using Cat4):

    if ( $c->session->{user} ) {
        # make sure user object is current
        $c->session->{user} =
            $c->config->{authentication}->{user_class}->retrieve(
                $c->session->{user}->id
            );
        $c->stash->{logged_in} = $c->session->{user};
    } else {
        $c->stash->{logged_in} = undef;
    }



On 4/14/05 5:10 AM, "Chisel Wright" <chisel at herlpacker.co.uk> wrote:

> On Tue, Apr 12, 2005 at 01:15:33PM -0800, Michael Reece wrote:
>> In Cat4, C::Plugin::Authentication::CDBI sets $c->request->{user} to the
>> username, rather than the user object ( from $user_class->search(..) ).  I
>> found that to be ultimately useless, and changed it put the user object in
>> there instead, so my templates can make use of the user's preferences and
>> other bits from their db record.  Anything in the Authentication, such as
>> that, changing in Cat5?
> 
> Yeah, as part of my journey into the realms of Cat5 I've realised that
> I'd like to return a blob of user info, rather than just the username.
> 
> What's the 'proper' way of doing this?
> 
> At the moment I have a local module Catalyst::Plugin::Login that just
> use base's C::Plugin::Authentication::CDBI, and I've copied the
> session_login() function from CPA::CDBI and doctored the
> 
> $c->stash->{'user'} = $user;
> 
> statement to be set to the result of a App::M::CDBI::Table->search
> query.
> 
> It seems to be doing what I intend, but my instincts are telling me that
> this is wrong.
> 
> Any comments either way?
> 
> Chisel

-- 
michael reece :: web engineer :: mreece at sacbee.com :: (916)321-1249





More information about the Catalyst mailing list