Lemonldap-NG-Common

 view release on metacpan or  search on metacpan

lib/Lemonldap/NG/Common/Session.pm  view on Meta::CPAN

    # Secured storage for new session: generate a new random ID and calculate
    # the storage ID
    my $securedId = $self->id;
    if ( $self->hashStore ) {
        if ( !$self->id ) {
            my $id = $self->options->{setId} || randomId();
            $securedId = $id;
            $self->storageId( id2storage($securedId) );
            $self->options->{setId} = $options->{setId} = $self->storageId;
            $self->error(undef);
        }
    }

    eval {
        local $SIG{ALRM} = sub { die "TIMEOUT\n" };
        eval {
            alarm $self->timeout;

            # SOAP/REST session module must be directly tied
            if ( $self->storageModule =~
                /^Lemonldap::NG::Common::Apache::Session/ )
            {
                tie %h, $self->storageModule,
                  ( $options->{setId} ? $self->id : $self->storageId ),
                  { %{ $self->options }, %$options, kind => $self->kind };
            }
            else {
                tie %h, 'Lemonldap::NG::Common::Apache::Session',
                  ( $options->{setId} ? $self->id : $self->storageId ),
                  { %{ $self->options }, %$options };
            }
        };
        alarm 0;
        die $@ if $@;

    };
    if ( $@ or not tied(%h) ) {
        my $msg = "Session cannot be tied";
        $msg .= ": $@" if $@;
        $self->error($msg);
        return undef;
    }
    if ( $self->hashStore ) {

        # Before returning the session, set here the real cookie value
        my $status = tied(%h)->{status};
        $h{_session_id} = $securedId;
        tied(%h)->{status} = $status;
    }

    return \%h;
}

sub _save_data {
    my ( $self, $data ) = @_;

    my %saved_data = %$data;
    $self->data( \%saved_data );
}

sub update {
    my ( $self, $infos, $tieOptions ) = @_;

    unless ( ref $infos eq "HASH" ) {
        $self->error("You need to provide a HASHREF");
        return 0;
    }

    my $data = $self->_tie_session(
        { ( $tieOptions ? %$tieOptions : () ), noCache => 1 } );

    if ($data) {
        foreach ( keys %$infos ) {
            if ( defined $infos->{$_} ) {
                $data->{$_} = $infos->{$_};
            }
            else {
                delete $data->{$_};
            }
        }

        $self->_save_data($data);
        $self->id( $data->{_session_id} );
        if ( $self->hashStore and $self->id ) {
            $self->_hashDataSessionId($data);
            $data->{_session_hashed} ||= 1;
        }

        untie(%$data);
        return 1;
    }

    $self->error("No data found in session");
    return 0;
}

sub remove {
    my ( $self, $tieOptions ) = @_;

    my $data = $self->_tie_session($tieOptions);
    unless ($data) {
        $self->error("Unable to delete session: $@");
        return 0;
    }

    # Before saving, hide the real ID and replace it by the storage ID
    $self->_hashDataSessionId($data) if $self->hashStore;

    eval { tied(%$data)->delete(); };

    if ($@) {
        $self->error("Unable to delete session: $@");
        return 0;
    }

    return 1;
}

sub _hashDataSessionId {
    my ( $self, $data, $id ) = @_;
    my $nid = id2storage( $id || $self->id );



( run in 2.296 seconds using v1.01-cache-2.11-cpan-75ffa21a3d4 )