File Coverage

lib/Ubic/AccessGuard.pm
Criterion Covered Total %
statement 36 41 87.8
branch 1 4 25.0
condition n/a
subroutine 11 12 91.6
pod 1 1 100.0
total 49 58 84.4


line stmt bran cond sub pod time code
1             package Ubic::AccessGuard;
2             $Ubic::AccessGuard::VERSION = '1.59';
3 37     37   120 use strict;
  37         63  
  37         884  
4 37     37   114 use warnings;
  37         42  
  37         767  
5              
6             # ABSTRACT: guard for operations with temporarily different effective uid
7              
8              
9 37     37   126 use Params::Validate;
  37         41  
  37         1660  
10 37     37   2802 use Ubic::Result qw(result);
  37         44  
  37         1389  
11 37     37   8749 use Ubic::Credentials;
  37         75  
  37         188  
12 37     37   170 use Carp;
  37         46  
  37         1816  
13 37     37   149 use Scalar::Util qw(weaken);
  37         44  
  37         1376  
14 37     37   139 use Try::Tiny;
  37         41  
  37         7620  
15              
16             # AccessGuard is actually a singleton - there can't be two different guards, since process can't have two euids.
17             # So we keep weakref to any created AccessGuard.
18             my $ag_ref;
19              
20              
21             sub new {
22 76     76 1 106 my $class = shift;
23 76         749 my ($credentials) = validate_pos(@_, { isa => 'Ubic::Credentials' });
24              
25 76 50       218 if ($ag_ref) {
26             # oops, another AccessGuard already exists
27 0         0 my $ag = $$ag_ref;
28 0 0       0 if ($ag->{credentials}->eq($credentials)) {
29             # new guard is the same as old guard
30 0         0 return $ag;
31             }
32             else {
33 0         0 croak "Can't create AccessGuard for ".$credentials->as_string.", there is already another AccessGuard for ".$ag->{credentials}->as_string;
34             }
35             }
36              
37 76         225 my $self = bless {
38             credentials => $credentials,
39             } => $class;
40              
41             try {
42 76     76   2247 $credentials->set_effective;
43             }
44             catch {
45 0     0   0 die result('unknown', "$_");
46 76         658 };
47              
48 76         880 $ag_ref = \$self;
49 76         283 weaken($ag_ref);
50              
51 76         331 return $self;
52             }
53              
54             sub DESTROY {
55 76     76   118 my $self = shift;
56 76         98 local $@;
57              
58 76         453 $self->{credentials}->reset_effective;
59             }
60              
61              
62             1;
63              
64             __END__