OpenInteract2/CreateSecurity.pm

filter: Statementstmt 19.1% 26 / 136 Branchbran 0.0% 0 / 56 Conditioncond 0.0% 0 / 19 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 42.8% 6 / 14 Podpod 20.0% 1 / 5 totaltotal 13.6% 33 / 242 CC 37 SCAR 47.4
CC
37
Cov
12%
validate
272.0
run
110.0
new
20.0
CRAP 114.3
« OpenInteract2/Auth/User.pm ↑ Summary OpenInteract2/Cache.pm »
1 package OpenInteract2::CreateSecurity;
2  
3 # $Id: CreateSecurity.pm,v 1.3 2005/03/18 04:09:48 lachoy Exp $
4  
5 87 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 87 ▶use base qw( Class::Accessor::Fast );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 87 ▶use Log::Log4perl qw( get_logger );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 87 ▶use OpenInteract2::Constants qw( :log );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 87 ▶use OpenInteract2::Context qw( CTX );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 87 ▶use SPOPS::Secure qw( :level :scope );
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 87 $OpenInteract2::CreateSecurity::VERSION = sprintf("%d.%02d", q$Revision: 1.3 $ =~ /(\d+)\.(\d+)/);
13  
14 87 my @FIELDS = qw(
15 website_dir scope scope_id level
16 action action_class spops spops_class where iterator
17 num_processed num_failed start_time end_time elapsed_time
18 );
19 87 __PACKAGE__->mk_accessors( @FIELDS );
20  
21 87 my %VALID_SCOPE = (
22 world => SEC_SCOPE_WORLD,
23 user => SEC_SCOPE_USER,
24 group => SEC_SCOPE_GROUP,
25 );
26  
27 87 my %VALID_LEVEL = (
28 none => SEC_LEVEL_NONE,
29 read => SEC_LEVEL_READ,
30 write => SEC_LEVEL_WRITE,
31 );
32  
33 87 my %DEFAULTS = (
34 iterator => 'yes',
35 );
36  
37 87 my ( $log );
38  
39 0 ▶sub new {
Subroutine new: not called CC=4 SCAR=30.0
Pod documented
40 0 my ( $class, $params ) = @_;
41 0 my $self = bless( {}, $class );
42 0 for ( @FIELDS ) {
43 0 ▶ my $value = $params->{ $_ } || $DEFAULTS{ $_ };
Condition0%0/3
$params->{$_} || $DEFAULTS{$_}
l!l&&r!l&&!r
000
MC/DC0%0/2
$params->{$_} || $DEFAULTS{$_}
$params->{$_} $DEFAULTS{$_}
Truth table0%0/3
A || B
A= $params->{$_}B= $DEFAULTS{$_}
ABresult
1X1
011
000
44 0 ▶ $self->$_( $value ) if ( $value );
Branch: 1 branch
BranchTrueFalseTotal
if $value 0 0 0
45 }
46 0 return $self;
47 }
48  
49 0 ▶sub errors {
Subroutine errors: not called CC=2 SCAR=17.9
Pod undocumented
50 0 my ( $self ) = @_;
51 0 ▶ $self->{errors} ||= {};
Condition0%0/2
$self->{"errors"} ||= {}
l!l
00
MC/DC0%0/1
$self->{"errors"} ||= {}
$self->{"errors"}
Truth table0%0/2
A
A= $self->{"errors"}
Aresult
11
00
52 0 return values %{ $self->{errors} };
53 }
54  
55 0 ▶sub errors_with_params {
Subroutine errors_with_params: not called CC=2 SCAR=17.9
Pod undocumented
56 0 my ( $self ) = @_;
57 0 ▶ $self->{errors} ||= {};
Condition0%0/2
$self->{"errors"} ||= {}
l!l
00
MC/DC0%0/1
$self->{"errors"} ||= {}
$self->{"errors"}
Truth table0%0/2
A
A= $self->{"errors"}
Aresult
11
00
58 0 return $self->{errors};
59 }
60  
61 0 ▶sub validate {
Subroutine validate: not called CC=16 SCAR=56.1
Pod undocumented
62 0 my ( $self ) = @_;
63  
64 0 ▶ unless ( CTX ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (CTX()) 0 0 0
65 0 ▶ unless ( -d $self->website_dir ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $self->website_dir) 0 0 0
66 0 my $msg =
67 "Parameter 'website_dir' must be set to website dir to " .
68 "create a Context object.";
69 0 $self->{errors} = { website_dir => $msg };
70 0 return 0;
71 }
72 0 eval {
73 0 OpenInteract2::Context->create({
74 website_dir => $self->website_dir
75 });
76 };
77 0 ▶ if ( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
78 0 my $msg = "Failed to create Context object: $@";
79 0 $self->{errors} = { website_dir => $msg };
80 0 return 0;
81 }
82 }
83  
84 0 my %errors = ();
85  
86 0 my $scope = lc $self->scope;
87 0 my $scope_id = $self->scope_id;
88 0 my $level = lc $self->level;
89  
90 0 ▶ unless ( $scope =~ /^(world|group|user)$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($scope =~ /^(world|group|user)$/) 0 0 0
91 0 $errors{scope} = "'scope' must be set to 'world', 'group', or 'user'";
92 }
93 0 ▶ if ( $scope ne 'world' and ! $scope_id ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($scope ne "world" and not $scope_id) 0 0 0
Condition0%0/3
$scope ne "world" and not $scope_id
!ll&&!rl&&r
000
MC/DC0%0/2
$scope ne "world" and not $scope_id
$scope ne "world" not $scope_id
Truth table0%0/3
A and not(B)
A= $scope ne "world"B= not $scope_id
ABresult
0X0
100
111
94 0 $errors{scope_id} = "'scope_id' must be set if the --scope is set " .
95 "to 'group' or 'user'";
96 }
97 0 ▶ unless ( $level =~ /^(none|read|write)$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($level =~ /^(none|read|write)$/) 0 0 0
98 0 $errors{level} = "'level' must be set to 'none', 'read' or 'write'";
99 }
100  
101 0 ▶ if ( my $spops_key = $self->spops ) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $spops_key = $self->spops) { } 0 0 0
elsif (my $action_name = $self->action) { } 0 0 0
102 0 my $spops_class = eval { CTX->lookup_object( $spops_key ) };
103 0 ▶ if ( $spops_class ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($spops_class) { } 0 0 0
104 0 ▶ if ( ! $spops_class->can( 'fetch' ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $spops_class->can('fetch')) { } 0 0 0
elsif (not $spops_class->isa('SPOPS::Secure')) { } 0 0 0
105 $errors{spops} =
106 0 "Class '$spops_class' is not valid. Are you " .
107 "sure it is defined in your OpenInteract setup?";
108 }
109 elsif ( ! $spops_class->isa( 'SPOPS::Secure' ) ) {
110 $errors{spops} =
111 0 "Class '$spops_class' is not using security.";
112 }
113 else {
114 0 $self->spops_class( $spops_class );
115 }
116 }
117 else {
118 $errors{spops} =
119 0 "Cannot find SPOPS class mapped to key '$spops_key'";
120 }
121 }
122  
123 elsif ( my $action_name = $self->action ) {
124 0 my $info = eval { CTX->lookup_action_info( $action_name ) };
125 0 ▶ if ( $info->{class} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($info->{'class'}) { } 0 0 0
126 0 ▶ if ( ! UNIVERSAL::isa( $info->{class}, 'OpenInteract2::Action' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not UNIVERSAL::isa($info->{'class'}, 'OpenInteract2::Action')) { } 0 0 0
127 $errors{action} =
128 0 "Class '$info->{class}' does not seem to be a " .
129 "subclass of 'OpenInteract2::Action'.";
130 }
131 else {
132 0 $self->action_class( $info->{class} );
133 }
134  
135 }
136 else {
137 $errors{action} =
138 0 "Cannot find action mapped to name '$action_name'";
139 }
140 }
141  
142 else {
143 $errors{class} =
144 0 "Neither action name nor SPOPS key set. One must be set to " .
145 "the class for which you want to set security (e.g., " .
146 "'OpenInteract2::Action::News', 'OpenInteract2::News')";
147 }
148  
149 0 $self->scope( $VALID_SCOPE{ lc $scope } );
150 0 $self->level( $VALID_LEVEL{ lc $level } );
151  
152 0 $self->{errors} = \%errors;
153  
154 0 ▶ if ( scalar keys %errors == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar keys %errors == 0) 0 0 0
155 0 $self->{_validated}++;
156 0 return 1;
157 }
158 0 return 0;
159 }
160  
161  
162 0 ▶sub run {
Subroutine run: not called CC=10 SCAR=47.0
Pod undocumented
163 0 my ( $self ) = @_;
164  
165 0 $self->start_time( time );
166 0 ▶ return unless ( $self->{_validated} or $self->validate );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"_validated"} or $self->validate 0 0 0
Condition0%0/3
$self->{"_validated"} or $self->validate
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"_validated"} or $self->validate
$self->{"_validated"} $self->validate
Truth table0%0/3
A or B
A= $self->{"_validated"}B= $self->validate
ABresult
1X1
011
000
167  
168 0 ▶ $log ||= get_logger( LOG_OI );
Condition0%0/3
$log ||= get_logger("OI2")
l!l&&r!l&&!r
000
MC/DC0%0/2
$log ||= get_logger("OI2")
$log get_logger("OI2")
Truth table0%0/3
A ||= B
A= $logB= get_logger("OI2")
ABresult
1X1
011
000
169  
170 0 my $scope_id = $self->_resolve_scope_id;
171 0 my $scope = $self->scope;
172 0 my $level = $self->level;
173  
174 0 my ( $count, $failure ) = ( 0, 0 );
175  
176 0 ▶ if ( my $spops_class = $self->spops_class ) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $spops_class = $self->spops_class) { } 0 0 0
elsif (my $action_class = $self->action_class) { } 0 0 0
177 0 my ( $object_store );
178 0 ▶ if ( $self->iterator eq 'yes' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->iterator eq 'yes') { } 0 0 0
179 0 $object_store = $spops_class->fetch_iterator({
180 skip_security => 1,
181 column_group => '_id_field',
182 where => $self->where,
183 });
184 }
185 else {
186 0 $object_store = $spops_class->fetch_group({
187 skip_security => 1,
188 column_group => '_id_field',
189 where => $self->where,
190 });
191 }
192 0 ▶ while ( my $object = $self->_get_from_store( $object_store ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $object = $self->_get_from_store($object_store)) 0 0 0
193 0 my $ok = _create_or_update_security(
194 $scope, $scope_id, $level, $spops_class, $object->id
195 );
196 0 $count++;
197 0 ▶ $failure++ unless ( $ok );
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 0 0 0
198 }
199 }
200  
201 elsif ( my $action_class = $self->action_class ) {
202 0 my $ok = _create_or_update_security(
203 $scope, $scope_id, $level, $action_class, '0',
204 );
205 0 $count++;
206 0 ▶ $failure++ unless ( $ok );
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 0 0 0
207 }
208  
209 else {
210 # ... die here? this should have already been caught in validate()
211 }
212  
213 0 $self->num_processed( $count );
214 0 $self->num_failed( $failure );
215 0 $self->end_time( time );
216 0 $self->elapsed_time( $self->end_time - $self->start_time );
217 0 return $count;
218 }
219  
220 0 ▶sub _create_or_update_security {
Subroutine _create_or_update_security: not called CC=3 SCAR=24.8
221 0 my ( $scope, $scope_id, $level, $class, $object_id ) = @_;
222 0 my $item = OpenInteract2::Security->fetch_match( undef, {
223 class => $class,
224 object_id => $object_id,
225 scope => $scope,
226 scope_id => $scope_id,
227 });
228 0 ▶ if ( $item ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($item) { } 0 0 0
229 0 $item->{security_level} = $level;
230 }
231 else {
232 0 $item = OpenInteract2::Security->new({
233 class => $class,
234 object_id => $object_id,
235 scope => $scope,
236 scope_id => $scope_id,
237 security_level => $level
238 });
239 }
240 0 eval { $item->save() };
241 0 ▶ if ( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 0 0
242 0 $log->info( "FAIL: $object_id ($@)" );
243 0 return 0;
244 }
245 else {
246 0 $log->info( "OK: $object_id" );
247 0 return 1;
248 }
249 }
250  
251 # Use either an iterator or a list
252  
253 0 ▶sub _get_from_store {
Subroutine _get_from_store: not called CC=4 SCAR=30.0
254 0 my ( $self, $store ) = @_;
255 0 ▶ return undef unless ( $store );
Branch: 1 branch
BranchTrueFalseTotal
unless $store 0 0 0
256 0 ▶ if ( ref $store eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $store eq "ARRAY") 0 0 0
257 0 ▶ return undef unless ( scalar @{ $store } );
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @{$store;} 0 0 0
258 0 return shift @{ $store };
259 }
260 0 return $store->get_next;
261 }
262  
263 0 ▶sub _resolve_scope_id {
Subroutine _resolve_scope_id: not called CC=3 SCAR=24.8
264 0 my ( $self ) = @_;
265 0 my $scope_id = $self->scope_id;
266 0 my $default_objects = CTX->lookup_default_object_id;
267 0 ▶ if ( $scope_id and $default_objects->{ $scope_id } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($scope_id and $default_objects->{$scope_id}) 0 0 0
Condition0%0/3
$scope_id and $default_objects->{$scope_id}
!ll&&!rl&&r
000
MC/DC0%0/2
$scope_id and $default_objects->{$scope_id}
$scope_id $default_objects->{$scope_id}
Truth table0%0/3
A and B
A= $scope_idB= $default_objects->{$scope_id}
ABresult
0X0
100
111
268 0 $scope_id = $default_objects->{ $scope_id };
269 }
270 0 return $scope_id;
271 }
272  
273 87 1;
274  
275 __END__
« OpenInteract2/Auth/User.pm ↑ Summary OpenInteract2/Cache.pm »