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.60';
3 36     36   100 use strict;
  36         39  
  36         742  
4 36     36   99 use warnings;
  36         35  
  36         614  
5              
6             # ABSTRACT: guard for operations with temporarily different effective uid
7              
8              
9 36     36   95 use Params::Validate;
  36         46  
  36         1372  
10 36     36   2661 use Ubic::Result qw(result);
  36         36  
  36         1163  
11 36     36   7987 use Ubic::Credentials;
  36         53  
  36         150  
12 36     36   156 use Carp;
  36         48  
  36         1567  
13 36     36   131 use Scalar::Util qw(weaken);
  36         53  
  36         1137  
14 36     36   127 use Try::Tiny;
  36         39  
  36         6522  
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 92 my $class = shift;
23 76         752 my ($credentials) = validate_pos(@_, { isa => 'Ubic::Credentials' });
24              
25 76 50       197 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         209 my $self = bless {
38             credentials => $credentials,
39             } => $class;
40              
41             try {
42 76     76   2151 $credentials->set_effective;
43             }
44             catch {
45 0     0   0 die result('unknown', "$_");
46 76         582 };
47              
48 76         829 $ag_ref = \$self;
49 76         250 weaken($ag_ref);
50              
51 76         303 return $self;
52             }
53              
54             sub DESTROY {
55 76     76   108 my $self = shift;
56 76         86 local $@;
57              
58 76         379 $self->{credentials}->reset_effective;
59             }
60              
61              
62             1;
63              
64             __END__