our
$VERSION
=
'0.000064'
;
sub
new {
my
$class
=
shift
;
my
%params
=
@_
;
my
$env
=
delete
$params
{env} or croak
"'env' is a required attribute"
;
my
$self
=
$class
->SUPER::new(
$env
);
$self
->{
'config'
} =
delete
$params
{config} or croak
"'config' is a required attribute"
;
return
$self
;
}
sub
schema {
$_
[0]->{config}->schema }
sub
session {
my
$self
=
shift
;
return
$self
->{session}
if
$self
->{session};
my
$schema
=
$self
->schema;
my
$session
;
my
$cookies
=
$self
->cookies;
if
(
my
$id
=
$cookies
->{id}) {
$session
=
$schema
->resultset(
'Session'
)->find({
session_id
=>
$id
});
$session
=
undef
unless
$session
&&
$session
->active;
}
$session
||=
$self
->schema->resultset(
'Session'
)->create(
{
session_id
=> Data::GUID->new->as_string},
);
$self
->{session} =
$session
;
$self
->session_host;
return
$session
;
}
sub
session_host {
my
$self
=
shift
;
return
$self
->{session_host}
if
$self
->{session_host};
my
$session
=
$self
->session or
return
undef
;
my
$schema
=
$self
->schema;
$schema
->txn_begin;
my
$host
=
$schema
->resultset(
'SessionHost'
)->find(
{
session_id
=>
$session
->session_id,
address
=>
$self
->address,
agent
=>
$self
->user_agent,
}
);
$host
//=
$schema
->resultset(
'SessionHost'
)->create({
session_host_id
=> Data::GUID->new->as_string,
session_id
=>
$session
->session_id,
address
=>
$self
->address,
agent
=>
$self
->user_agent,
});
$schema
->txn_commit;
return
$self
->{session_host} =
$host
;
}
sub
user {
my
$self
=
shift
;
return
$self
->schema->resultset(
'User'
)->find({
username
=>
'root'
})
if
$self
->{config}->single_user;
my
$host
=
$self
->session_host or
return
undef
;
return
undef
unless
$host
->user_id;
return
$host
->user;
}
1;