Skip to content

Commit

Permalink
Merge pull request #950 from LibreCat/fix_cache_no_config
Browse files Browse the repository at this point in the history
remove configuration option permissions.cache.expires_in
  • Loading branch information
nicolasfranck committed May 27, 2021
2 parents 3963a55 + fa4b2c8 commit 00a1fc8
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 31 deletions.
48 changes: 27 additions & 21 deletions lib/LibreCat/App/Catalogue/Controller/Permission.pm
Original file line number Diff line number Diff line change
Expand Up @@ -9,38 +9,50 @@ use LibreCat::Access;
use Carp;
use Dancer qw(:syntax);
use Exporter qw/import/;
use CHI;

use Moo;

sub cache {
state $cache = CHI->new(
driver => "Memory",
datastore => +{},
expires_in => h->config->{permissions}->{cache}->{expires_in} // 5,
);
var("cache") or
var cache => +{};
}

sub cache_get {

my($self, $key) = @_;
my $cache = $self->cache();
exists($cache->{$key}) ? $cache->{$key} : undef;

}

sub cache_set {

my($self, $key, $val) = @_;
my $cache = cache();
$cache->{$key} = $val;

}

sub get_cached_publication {
my $id = $_[0];
my($self, $id) = @_;

my $pub = cache()->get( "RECORD_${id}" );
my $pub = $self->cache_get( "PUBLICATION_${id}" );
my $set_cache = !$pub;
$pub //= h->publication->get($id);

cache()->set( "RECORD_${id}", $pub) if $set_cache;
$self->cache_set( "PUBLICATION_${id}", $pub) if $set_cache;

$pub;
}

sub get_cached_user {
my $user_id = $_[0];
my($self, $user_id) = @_;

my $user = cache()->get( "USER_${user_id}" );
my $user = $self->cache_get( "USER_${user_id}" );
my $set_cache = !$user;
$user //= h->get_person( $user_id );

cache()->set("USER_${user_id}", $user) if $set_cache;
$self->cache_set("USER_${user_id}", $user) if $set_cache;

$user;
}
Expand All @@ -63,11 +75,11 @@ sub _can_do_action {

return 0 unless defined($user_id) && defined($role);

my $pub = $opts->{live} ? h->publication->get($id) : get_cached_publication($id);
my $pub = $self->get_cached_publication($id);

is_hash_ref($pub) or return 0;

my $user = $opts->{live} ? h->get_person( $user_id ) : get_cached_user($user_id);
my $user = $self->get_cached_user($user_id);

# do not touch deleted records
return 0 if $pub->{status} && $pub->{status} eq 'deleted';
Expand Down Expand Up @@ -107,7 +119,6 @@ Publication identifier
* user_id
* role
* [live=1]
=back
Expand All @@ -130,7 +141,6 @@ Publication identifier
* user_id
* role
* [live=1]
=back
Expand All @@ -153,7 +163,6 @@ Publication identifier
* user_id
* role
* [live=1]
=back
Expand All @@ -176,7 +185,6 @@ Publication identifier
* user_id
* role
* [live=1]
=back
Expand All @@ -199,7 +207,6 @@ Publication identifier
* user_id
* role
* [live=1]
=back
Expand All @@ -226,7 +233,6 @@ Hash reference containing:
* role (string)
* file_id (string)
* ip (string)
* [live=1]
=back
Expand All @@ -238,7 +244,7 @@ sub can_download {
is_string($id) or return (0, "");
is_hash_ref($opts) or return (0, "");

my $pub = $opts->{live} ? h->publication->get($id) : get_cached_publication($id);
my $pub = $self->get_cached_publication($id);

is_hash_ref($pub) or return (0,"");

Expand Down
3 changes: 1 addition & 2 deletions lib/LibreCat/App/Catalogue/Route/file.pm
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,7 @@ sub _handle_download {
file_id => $file_id,
user_id => session->{user_id},
role => session->{role},
ip => request->address,
live => 1
ip => request->address
}
);

Expand Down
16 changes: 8 additions & 8 deletions lib/LibreCat/App/Catalogue/Route/publication.pm
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ Checks if the user has permission the see/edit this record.
unless (
p->can_edit(
$rec->{_id},
{user_id => session("user_id"), role => session("role"), live=>1}
{user_id => session("user_id"), role => session("role")}
)
)
{
Expand Down Expand Up @@ -234,7 +234,7 @@ Checks if the user has the rights to edit this record.
unless (
p->can_return(
$rec->{_id},
{user_id => session("user_id"), role => session("role"), live=>1}
{user_id => session("user_id"), role => session("role")}
)
)
{
Expand Down Expand Up @@ -272,7 +272,7 @@ Deletes record with id. For admins only.
unless (
p->can_delete(
$rec->{_id},
{user_id => session("user_id"), role => session("role"), live=>1}
{user_id => session("user_id"), role => session("role")}
)
)
{
Expand Down Expand Up @@ -426,7 +426,7 @@ Publishes private records, returns to the list.
unless (
p->can_make_public(
$rec->{_id},
{user_id => session("user_id"), role => session("role"), live=>1}
{user_id => session("user_id"), role => session("role")}
)
)
{
Expand Down Expand Up @@ -675,7 +675,7 @@ If record does not exist, then this route does not match
$finalSubmit eq "recPublish" &&
$p->can_make_public(
$id,
{ user_id => session("user_id"), role => session("role"), live => 1 }
{ user_id => session("user_id"), role => session("role")}
)
){
# ok
Expand All @@ -684,23 +684,23 @@ If record does not exist, then this route does not match
$finalSubmit eq "recReturn" &&
$p->can_return(
$id,
{ user_id => session("user_id"), role => session("role"), live => 1 }
{ user_id => session("user_id"), role => session("role")}
)
){
# ok
}
elsif(
$finalSubmit eq "recSubmit" && $p->can_submit(
$id,
{ user_id => session("user_id"), role => session("role"), live => 1 }
{ user_id => session("user_id"), role => session("role")}
)
){
# ok
}
elsif(
$p->can_edit(
$id,
{ user_id => session("user_id"), role => session("role"), live => 1 }
{ user_id => session("user_id"), role => session("role")}
)
){
# ok
Expand Down

0 comments on commit 00a1fc8

Please sign in to comment.