<commit_before>package CoGe::Accessory::LogUser;

use strict;
use CGI::Cookie;
use Data::Dumper;
use CoGeX::Result::User;

use Exporter 'import';
our @EXPORT_OK = qw(get_cookie_session);

#FIXME: Should have no knowledge of cookies but only sessions
sub get_user {
    my $self        = shift;
    my %opts        = @_;
    my $cookie_name = $opts{cookie_name};
    my $coge        = $opts{coge};
    my ( $user, $uid, $session );    # = "Public";

    $session = get_cookie_session(cookie_name => $cookie_name);

    if ($session) {
        my ($user_session) = $coge->resultset("UserSession")->find( { session => $session } );
        $user = $user_session->user if $user_session;
    }

    unless ($user) {
        $user = new CoGeX::Result::User;
        $user->user_name("public");
        $user->admin(0);
    }
    return ($user);
}

#FIXME: Move into Web.pm
sub get_cookie_session {
    my %opts        = @_;
    my $cookie_name = $opts{cookie_name};
    my %cookies     = fetch CGI::Cookie;

    #print STDERR "LogUser::get_user cookie=$cookie_name " . (defined $cookies{$cookie_name} ? 'exists' : '!exists') . "\n";
    if ( $cookie_name && ref $cookies{$cookie_name} ) {
        my %session = $cookies{$cookie_name}->value;
        return $session{session};
    }

    return undef;
}

#FIXME: Belongs in Web.pm
sub gen_cookie {
    my $self        = shift;
    my %opts        = @_;
    my $session     = $opts{session} || 0;
    my $exp         = $opts{exp} || '+7d'; # issue 48, this field must have lowercase-only
    my $cookie_name = $opts{cookie_name};
    my %params      = ( -name => $cookie_name, -path => "/" );
#   print STDERR "LogUser::gen_cookie session=$session cookie_name=$cookie_name\n";

    $params{-expires} = $exp if $exp;
    $params{ -values } = { session => $session } if $session;
    my $c = new CGI::Cookie(%params);
    return $c;<commit_msg>Add debug flag and messages<commit_after>package CoGe::Accessory::LogUser;

use strict;
use CGI::Cookie;
use Data::Dumper;
use CoGeX::Result::User;

use Exporter 'import';
our @EXPORT_OK = qw(get_cookie_session);

our $DEBUG = 0;

#FIXME: Should have no knowledge of cookies but only sessions
sub get_user {
    my $self        = shift;
    my %opts        = @_;
    my $cookie_name = $opts{cookie_name};
    my $coge        = $opts{coge};
    my ( $user, $uid, $session );    # = "Public";

    $session = get_cookie_session(cookie_name => $cookie_name);
    print STDERR "LogUser::get_user: session=$session\n" if $DEBUG;

    if ($session) {
        my ($user_session) = $coge->resultset("UserSession")->find( { session => $session } );
        $user = $user_session->user if $user_session;
    }

    unless ($user) {
        $user = new CoGeX::Result::User;
        $user->user_name("public");
        $user->admin(0);
    }
    return ($user);
}

#FIXME: Move into Web.pm
sub get_cookie_session {
    my %opts        = @_;
    my $cookie_name = $opts{cookie_name};
    my %cookies     = fetch CGI::Cookie;

    print STDERR "LogUser::get_cookie_session cookie=$cookie_name " . (defined $cookies{$cookie_name} ? 'exists' : '!exists') . "\n" if $DEBUG;
    if ( $cookie_name && ref $cookies{$cookie_name} ) {
        my %session = $cookies{$cookie_name}->value;
        return $session{session};
    }

    return undef;
}

#FIXME: Belongs in Web.pm
sub gen_cookie {
    my $self        = shift;
    my %opts        = @_;
    my $session     = $opts{session} || 0;
    my $exp         = $opts{exp} || '+7d'; # issue 48, this field must have lowercase-only
    my $cookie_name = $opts{cookie_name};
    my %params      = ( -name => $cookie_name, -path => "/" );
    print STDERR "LogUser::gen_cookie session=$session cookie_name=$cookie_name\n" if $DEBUG;

    $params{-expires} = $exp if $exp;
    $params{ -values } = { session => $session } if $session;
    my $c = new CGI::Cookie(%params);
    return $c;
{'fraction_non_alphanumeric': 0.1583675564681725, 'fraction_numerical': 0.0028234086242299797, 'mean_word_length': 2.897, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2643376', 'n_tokens_mistral': 1350, 'n_tokens_neox': 1323, 'n_words': 330}