1package SPOPS::Secure::Util; 2 3# $Id: Util.pm,v 1.6 2004/06/02 00:48:24 lachoy Exp $ 4 5use strict; 6use Data::Dumper qw( Dumper ); 7use Log::Log4perl qw( get_logger ); 8use SPOPS::Secure qw( :level :scope ); 9 10my $log = get_logger(); 11 12# Setup a hashref where w/u => security_level and g points to a 13# hashref where the key is the group_id value is the security level. 14 15sub parse_objects_into_hashref { 16 my ( $class, $security_objects ) = @_; 17 18 my %items = ( SEC_SCOPE_WORLD() => undef, 19 SEC_SCOPE_USER() => undef, 20 SEC_SCOPE_GROUP() => {} ); 21 unless ( ref $security_objects eq 'ARRAY' 22 and scalar @{ $security_objects } > 0 ) { 23 return undef; 24 } 25 26ITEM: 27 foreach my $sec ( @{ $security_objects } ) { 28 if ( $sec->{scope} eq SEC_SCOPE_WORLD || $sec->{scope} eq SEC_SCOPE_USER ) { 29 $items{ $sec->{scope} } = $sec->{security_level}; 30 $log->is_debug && 31 $log->debug( "Assign [$sec->{security_level}] to [$sec->{scope}]" ); 32 } 33 elsif ( $sec->{scope} eq SEC_SCOPE_GROUP ) { 34 $items{ $sec->{scope} }->{ $sec->{scope_id} } = $sec->{security_level}; 35 $log->is_debug && 36 $log->debug( "Assign [$sec->{security_level}] to ", 37 "[$sec->{scope}][$sec->{scope_id}]" ); 38 } 39 } 40 $log->is_info && 41 $log->info( "All security parsed: ", Dumper( \%items ) );; 42 return \%items; 43} 44 45sub find_class_and_oid { 46 my ( $class, $item, $p ) = @_; 47 48 # First assume it's a class we're passed in to check 49 50 my $obj_class = $p->{class} || $item; 51 my $oid = $p->{object_id} || $p->{oid} || '0'; 52 53 # If this is an object, modify lines accordingly 54 55 if ( ref $item and UNIVERSAL::can( $item, 'id' ) ) { 56 $oid = eval { $item->id } || '0'; 57 $obj_class = ref $item; 58 } 59 return ( $obj_class, $oid ); 60} 61 62 631; 64 65__END__ 66 67=head1 NAME 68 69SPOPS::Secure::Util - Common utilities for SPOPS::Secure and subclasses 70 71=head1 SYNOPSIS 72 73 my $levels = SPOPS::Secure::Util->parse_object_into_hashref( \@security_objects ); 74 print "Given security from objects:\n", 75 "USER: $levels->{ SEC_SCOPE_USER() }\n", 76 "WORLD: $levels->{ SEC_SCOPE_WORLD() }\n"; 77 "GROUP [ID/LEVEL]: "; 78 print join( ' ', map { "[$_/$levels->{ SEC_SCOPE_GROUP() }{ $_ }" } 79 keys %{ $levels->{ SEC_SCOPE_GROUP() } } ); 80 81 # Not sure if $item is class or object? 82 83 sub somesub { 84 my ( $item, $params ) = @_; 85 my ( $object_class, $object_id ) = 86 SPOPS::Secure::Util->find_class_and_oid( $item, $params ); 87 } 88 89=head1 DESCRIPTION 90 91Common utility methods for security tasks. 92 93=head1 METHODS 94 95All methods are class methods. 96 97B<parse_objects_into_hashref( \@security_objects )> 98 99Places the relevant information from C<\@security_objects> into a 100hashref for easy analysis. If no objects are in C<\@security_objects> 101it returns undef. Otherwise the returned hashref should have as the 102three keys the constants C<SEC_SCOPE_WORLD>, C<SEC_SCOPE_GROUP> and 103C<SEC_SCOPE_USER>. 104 105The values of C<SEC_SCOPE_WORLD> and C<SEC_SCOPE_USER> are a single 106value corresponding to one of the C<SEC_LEVEL_*> constants. The value 107of C<SEC_LEVEL_GROUP> is another hashref with the keys as the group 108IDs each of which has a single value corresponding to one of the 109C<SEC_LEVEL_*> constants. 110 111B<find_class_and_oid( [$class|$object], \%params )> 112 113Useful when a method can be called as a class or object 114method and the class/ID to be analyzed can be either in the object 115calling or in the class and the parameters. 116 117Returns a two-argument list. The first is the object class, the second 118is the object ID. 119 120If the first argument is an object and it has a method C<id()>, we 121assign the result of calling it to the object ID; for the object class 122we call C<ref> on the object. 123 124Otherwise we look in C<\%params> for a parameter 'class'. If it is not 125found we use the first argument. For the object ID we 126look in C<\%params> for a parameter 'object_id' or 'oid'. If neither 127are found we assign '0' to the object ID. For example: 128 129 my $class = 'My::Object'; my ( $object_class, $object_id ) = 130 SPOPS::Secure::Util->find_class_and_oid( $class, { object_id => 5 } ); 131 # $object_class = 'My::Object'; $object_id = 5 132 133 my $object = My::OtherObject->new({ id => 99 }); 134 my ( $object_class, $object_id ) = 135 SPOPS::Secure::Util->find_class_and_oid( $object ); 136 # $object_class = 'My::OtherObject'; $object_id = 99 137 138=head1 BUGS 139 140None known. 141 142=head1 TO DO 143 144Nothing known. 145 146=head1 SEE ALSO 147 148=head1 COPYRIGHT 149 150Copyright (c) 2002-2004 intes.net, inc.. All rights reserved. 151 152This library is free software; you can redistribute it and/or modify 153it under the same terms as Perl itself. 154 155=head1 AUTHORS 156 157Chris Winters E<lt>chris@cwinters.comE<gt> 158