Catalyst-Plugin-Authentication

 view release on metacpan or  search on metacpan

lib/Catalyst/Plugin/Authentication.pm  view on Meta::CPAN

}

# works like user_exists - except only returns true if user
# exists AND is in the realm requested.
sub user_in_realm {
    my ($c, $realmname) = @_;

    if (defined($c->_user)) {
        return ($c->_user->auth_realm eq $realmname);
    } else {
        my $realm = $c->find_realm_for_persisted_user;
        if ($realm) {
            return ($realm->name eq $realmname);
        } else {
            return undef;
        }
    }
}

sub __old_save_user_in_session {
    my ( $c, $user, $realmname ) = @_;

    $c->session->{__user_realm} = $realmname;

    # we want to ask the store for a user prepared for the session.
    # but older modules split this functionality between the user and the
    # store.  We try the store first.  If not, we use the old method.
    my $realm = $c->get_auth_realm($realmname);
    if ($realm->{'store'}->can('for_session')) {
        $c->session->{__user} = $realm->{'store'}->for_session($c, $user);
    } else {
        $c->session->{__user} = $user->for_session;
    }
}

sub persist_user {
    my $c = shift;

    if ($c->user_exists) {

        ## if we have a valid session handler - we store the
        ## realm in the session.  If not - we have to hope that
        ## the realm can recognize its frozen user somehow.
        if ($c->can('session') &&
            $c->config->{'Plugin::Authentication'}{'use_session'} &&
            $c->session_is_valid) {

            $c->session->{'__user_realm'} = $c->_user->auth_realm;
        }

        my $realm = $c->get_auth_realm($c->_user->auth_realm);

        # used to call $realm->save_user_in_session
        $realm->persist_user($c, $c->user);
    }
}


## this was a short lived method to update user information -
## you should use persist_user instead.
sub update_user_in_session {
    my $c = shift;

    return $c->persist_user;
}

sub logout {
    my $c = shift;

    $c->user(undef);

    my $realm = $c->find_realm_for_persisted_user;
    if ($realm) {
        $realm->remove_persisted_user($c);
    }

    $c->maybe::next::method(@_);
}

sub find_user {
    my ( $c, $userinfo, $realmname ) = @_;

    $realmname ||= 'default';
    my $realm = $c->get_auth_realm($realmname);

    if (!$realm) {
        Catalyst::Exception->throw(
                "find_user called with nonexistant realm: '$realmname'.");
    }
    return $realm->find_user($userinfo, $c);
}

## Consider making this a public method. - would make certain things easier when
## dealing with things pre-auth restore.
sub find_realm_for_persisted_user {
    my $c = shift;

    my $realm;
    if ($c->can('session')
        and $c->config->{'Plugin::Authentication'}{'use_session'}
        and $c->session_is_valid
        and exists($c->session->{'__user_realm'})) {

        $realm = $c->auth_realms->{$c->session->{'__user_realm'}};
        if ($realm->user_is_restorable($c)) {
            return $realm;
        }
    } else {
        ## we have no choice but to ask each realm whether it has a persisted user.
        foreach my $realmname (@{$c->_auth_realm_restore_order}) {
            my $realm = $c->auth_realms->{$realmname}
                || Catalyst::Exception->throw("Could not find authentication realm '$realmname'");
            return $realm
                if $realm->user_is_restorable($c);
        }
    }
    return undef;
}

sub auth_restore_user {
    my ( $c, $frozen_user, $realmname ) = @_;



( run in 1.525 second using v1.01-cache-2.11-cpan-39bf76dae61 )