SPOPS/Secure/DBI.pm untested

filter: Statementstmt 0.0% 0 / 102 Branchbran 0.0% 0 / 40 Conditioncond 0.0% 0 / 29 MC/DCmcdc 0.0% 0 / 19 Subroutinesub 0.0% 0 / 10 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 204 CC 21 SCAR 38.2
CC
21
Cov
0%
_build_group_sql
90.0
fetch_by_object
42.0
fetch_match
42.0
CRAP 45.4
1 package SPOPS::Secure::DBI;
2  
3 # $Id: DBI.pm,v 1.9 2004/06/02 00:48:24 lachoy Exp $
4  
5 use strict;
6 use Log::Log4perl qw( get_logger );
7 use Data::Dumper qw( Dumper );
8 use SPOPS;
9 use SPOPS::Secure qw( :level :scope );
10 use SPOPS::Secure::Util;
11  
12 my $log = get_logger();
13  
14 $SPOPS::Secure::DBI::VERSION = sprintf("%d.%02d", q$Revision: 1.9 $ =~ /(\d+)\.(\d+)/);
15  
16 # Pass in:
17 # $class->fetch_by_object( $obj, [ { user => $user_obj,
18 # group => \@( $group_obj, $group_obj, ... ) } ] );
19 #
20 # Returns:
21 # hashref with world, group, user as keys (set to SEC_LEVEL_WORLD, ... ?)
22 # and permissions as values; group set to hashref (gid => security_level)
23 # while world/user are scalars. Note that even if you restrict the results to
24 # a user and/or groups, you will always get a result back for WORLD.
25  
26 sub fetch_by_object {
27 my ( $class, $item, $p ) = @_;
28 my ( $find_class, $find_id ) =
29 SPOPS::Secure::Util->find_class_and_oid( $item, $p );
30 unless ( $find_class and defined $find_id ) { # $find_id could be 0...
31 my $msg = 'Cannot check security';
32 warn " -- Cannot retrieve security since no item passed in to check!\n";
33 SPOPS::Exception->throw( 'No item defined to check security for' );
34 }
35  
36 my $where = 'class = ? AND object_id = ? AND ( scope = ?';
37 my @value = ( $find_class, $find_id, SEC_SCOPE_WORLD );
38  
39 # Setup the group and user search clauses
40  
41 my ( $group_where, $group_value ) = $class->_build_group_sql( $p );
42 if ( $group_where ) { $where .= " OR $group_where " }
43 if ( scalar @{ $group_value } ) { push @value, @{ $group_value } }
44  
45 my ( $user_where, $user_value ) = $class->_build_user_sql( $p );
46 if ( $user_where ) { $where .= " OR $user_where " }
47 if ( scalar @{ $user_value } ) { push @value, @{ $user_value } }
48  
49 $where .= ')';
50 $log->is_debug &&
51 $log->debug( "Security searching clause: $where\nwith values ",
52 join( '//', @value ) );
53  
54 # Fetch the objects
55  
56 my $sec_list = $class->fetch_group({ where => $where,
57 value => \@value });
58 return SPOPS::Secure::Util->parse_objects_into_hashref( $sec_list );
59 }
60  
61  
62 # Setup the SQL for the groups passed in
63  
64 sub _build_group_sql {
65 my ( $class, $p ) = @_;
66  
67 # See if we were actually given any groups or the instruction to
68 # get ALL group security
69  
70 my $num_groups = ( ref $p->{group} eq 'ARRAY' )
71 ? scalar @{ $p->{group} } : 0;
72 unless ( $num_groups or $p->{group} eq 'all' ) {
73 $log->is_debug &&
74 $log->debug( "No groups passed in, returning empty info for group SQL" );
75 return ( undef, [] );
76 }
77  
78 # Include the overall group clause unless we specified we want
79 # 'none' of the groups
80  
81 my $where = ' ( scope = ? ';
82 my @value = ( SEC_SCOPE_GROUP );
83  
84 # Only specify the actual groups we want if $p->{group} is either a
85 # group object or an arrayref of group objects
86  
87 if ( ref $p->{group} ) {
88 my $group_list = ( ref $p->{group} eq 'ARRAY' )
89 ? $p->{group} : [ $p->{group} ];
90 if ( scalar @{ $group_list } ) {
91 $log->is_debug &&
92 $log->debug( scalar @{ $group_list }, " groups found passed in" );
93 $where .= ' AND ( ';
94 foreach my $group ( @{ $group_list } ) {
95 next unless ( $group );
96 $where .= ' scope_id = ? OR ';
97 my $gid = ( ref $group ) ? $group->id : $group;
98 push @value, $gid;
99 }
100 $where =~ s/ OR $/\) /;
101 $where =~ s/AND \(\s*$//;
102 }
103 }
104 $where .= ' ) ' if ( $where );
105 $log->is_debug &&
106 $log->debug( "Group WHERE clause: { $where }" );
107 return ( $where, \@value );
108 }
109  
110  
111 # Setup the SQL for the user passed in
112  
113 sub _build_user_sql {
114 my ( $class, $p ) = @_;
115 my ( $where );
116 my ( @value );
117 return ( $where, \@value ) unless ( $p->{user} );
118  
119 # Note that we can only do one user at a time. The caller of this
120 # routine should ensure that $p->{user} is a single user object or
121 # user_id.
122  
123 my $uid = ( ref $p->{user} ) ? $p->{user}->id : $p->{user};
124 $where = ' ( scope = ? AND scope_id = ? )';
125 push @value, SEC_SCOPE_USER, $uid;
126  
127 $log->is_debug &&
128 $log->debug( "User WHERE clause: { $where }" );
129 return ( $where, \@value );
130 }
131  
132  
133 # Pass in:
134 # $class->fetch_match( $obj, { scope => SCOPE, scope_id => $id } );
135 #
136 # Returns
137 # security object that matches the object, scope and scope_id,
138 # undef if no match
139  
140 sub fetch_match {
141 my ( $class, $item, $p ) = @_;
142 return undef unless ( $p->{scope} );
143 my $is_world = 1 if ( $p->{scope} eq SEC_SCOPE_WORLD );
144 return undef unless ( $is_world or $p->{scope_id} );
145  
146 my ( $find_class, $find_id ) =
147 SPOPS::Secure::Util->find_class_and_oid( $item, $p );
148 $p->{scope_id} ||= '';
149 $log->is_info &&
150 $log->info( "Try to find match for [$find_class] [$find_id] ",
151 "scope [$p->{scope}] [$p->{scope_id}]" );
152  
153 my $where = " class = ? AND object_id = ? AND scope = ? ";
154 my @values = ( $find_class, $find_id, $p->{scope} );
155 unless ( $is_world ) {
156 $where .= " AND scope_id = ? ";
157 push @values, $p->{scope_id};
158 }
159  
160 # Note that we want to keep most of the db settings from SQLInterface
161 # if there's an error, so we just override the user_msg with the
162 # canned error message below.
163  
164 my $row = $class->db_select({ select => [ $class->id_field ],
165 from => [ $class->table_name ],
166 where => $where,
167 value => \@values,
168 return => 'single' });
169 return undef unless ( $row->[0] );
170 return $class->fetch( $row->[0] );
171 }
172  
173  
174 1;
175  
176 __END__