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.58_01'; # TRIAL
3 39     39   195 use strict;
  39         71  
  39         1298  
4 39     39   204 use warnings;
  39         66  
  39         1340  
5              
6             # ABSTRACT: guard for operations with temporarily different effective uid
7              
8              
9 39     39   207 use Params::Validate;
  39         65  
  39         2833  
10 39     39   4696 use Ubic::Result qw(result);
  39         93  
  39         2076  
11 39     39   13639 use Ubic::Credentials;
  39         119  
  39         237  
12 39     39   271 use Carp;
  39         64  
  39         2858  
13 39     39   220 use Scalar::Util qw(weaken);
  39         67  
  39         2212  
14 39     39   194 use Try::Tiny;
  39         85  
  39         12288  
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 84     84 1 130 my $class = shift;
23 84         1154 my ($credentials) = validate_pos(@_, { isa => 'Ubic::Credentials' });
24              
25 84 50       270 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 84         284 my $self = bless {
38             credentials => $credentials,
39             } => $class;
40              
41             try {
42 84     84   3112 $credentials->set_effective;
43             }
44             catch {
45 0     0   0 die result('unknown', "$_");
46 84         885 };
47              
48 84         1186 $ag_ref = \$self;
49 84         366 weaken($ag_ref);
50              
51 84         434 return $self;
52             }
53              
54             sub DESTROY {
55 84     84   154 my $self = shift;
56 84         163 local $@;
57              
58 84         789 $self->{credentials}->reset_effective;
59             }
60              
61              
62             1;
63              
64             __END__