| 1 | package SPOPS::Secure::Loopback; | ||
| 2 | |||
| 3 | # $Id: Loopback.pm,v 1.5 2004/06/02 00:48:24 lachoy Exp $ | ||
| 4 | |||
| 5 | use strict; | ||
| 6 | use Log::Log4perl qw( get_logger ); | ||
| 7 | use SPOPS; | ||
| 8 | use SPOPS::Secure qw( :level :scope ); | ||
| 9 | use SPOPS::Secure::Util; | ||
| 10 | |||
| 11 | my $log = get_logger(); | ||
| 12 | |||
| 13 | $SPOPS::Secure::Loopback::VERSION = sprintf("%d.%02d", q$Revision: 1.5 $ =~ /(\d+)\.(\d+)/); | ||
| 14 | |||
| 15 | sub fetch_by_object { | ||
| 16 | my ( $class, $object, $p ) = @_; | ||
| 17 | my @security_objects = $class->_fetch_all_by_object( $object, $p ); | ||
| 18 | my ( @user_match, @group_match, @world_match ); | ||
| 19 | if ( $p->{user} ) { | ||
| 20 | my $user_id = eval { $p->{user}->id }; | ||
| 21 | if ( $user_id ) { | ||
| 22 | @user_match = $class->_filter_by_scope( SEC_SCOPE_USER, $user_id, | ||
| 23 | @security_objects ); | ||
| 24 | } | ||
| 25 | } | ||
| 26 | if ( ref $p->{group} eq 'ARRAY' ) { | ||
| 27 | for ( @{ $p->{group} } ) { | ||
| 28 | my $group_id = eval { $_->id }; | ||
| 29 | if ( $group_id ) { | ||
| 30 | push @group_match, $class->_filter_by_scope( SEC_SCOPE_GROUP, | ||
| 31 | $group_id, @security_objects ); | ||
| 32 | } | ||
| 33 | } | ||
| 34 | } | ||
| 35 | @world_match = $class->_filter_by_scope( SEC_SCOPE_WORLD, undef, @security_objects ); | ||
| 36 | return SPOPS::Secure::Util->parse_objects_into_hashref( | ||
| 37 | [ @user_match, @group_match, @world_match ] ); | ||
| 38 | } | ||
| 39 | |||
| 40 | sub _fetch_all_by_object { | ||
| 41 | my ( $class, $object, $p ) = @_; | ||
| 42 | my ( $find_class, $find_id ) = | ||
| 43 | SPOPS::Secure::Util->find_class_and_oid( $object, $p ); | ||
| 44 | return () unless ( $find_class and $find_id ); | ||
| 45 | my $all_class_security = $class->fetch_group({ | ||
| 46 | where => "class = $find_class" }); | ||
| 47 | my @security_objects = (); | ||
| 48 | for ( @{ $all_class_security } ) { | ||
| 49 | next unless ( $_->{object_id} ); | ||
| 50 | if ( $_->{object_id} eq $find_id ) { | ||
| 51 | push @security_objects, $class->new( $_ ); | ||
| 52 | } | ||
| 53 | } | ||
| 54 | return @security_objects; | ||
| 55 | } | ||
| 56 | |||
| 57 | sub fetch_match { | ||
| 58 | my ( $class, $object, $p ) = @_; | ||
| 59 | return undef unless ( $p->{scope} ); | ||
| 60 | my $is_world = 1 if ( $p->{scope} eq SEC_SCOPE_WORLD ); | ||
| 61 | return undef unless ( $is_world or $p->{scope_id} ); | ||
| 62 | my @security_objects = $class->_fetch_all_by_object( $object, $p ); | ||
| 63 | return ( $class->_filter_by_scope( $p->{scope}, $p->{scope_id}, @security_objects) )[0]; | ||
| 64 | } | ||
| 65 | |||
| 66 | sub _filter_by_scope { | ||
| 67 | my ( $class, $scope, $scope_id, @objects ) = @_; | ||
| 68 | my $is_world = 1 if ( $scope eq SEC_SCOPE_WORLD ); | ||
| 69 | my @matching = (); | ||
| 70 | for ( @objects ) { | ||
| 71 | next unless ( $_->{scope} eq $scope ); | ||
| 72 | if ( $is_world or ( $_->{scope_id} eq $scope_id ) ) { | ||
| 73 | push @matching, $_; | ||
| 74 | } | ||
| 75 | } | ||
| 76 | return @matching; | ||
| 77 | } | ||
| 78 | |||
| 79 | 1; | ||
| 80 | |||
| 81 | __END__ |