Jifty/RightsFrom.pm untested

filter: Statementstmt 0.0% 0 / 45 Branchbran 0.0% 0 / 6 Conditioncond 0.0% 0 / 11 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 0.0% 0 / 8 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 79 CC 4 SCAR 21.5
CC
4
Cov
0%
delegate_current_user_can
20.0
import
2.0
export_curried_sub
2.0
CRAP 8.5
1 use warnings;
2 use strict;
3  
4 =head1 NAME
5  
6 Jifty::RightsFrom - Delegate access control to a related object
7  
8 =head1 SYNOPSIS
9  
10 package Application::Model::Thing;
11 use Jifty::DBI::Schema;
12 use Application::Record schema {
13 column owner => refers_to Application::Model::Person;
14 }
15  
16 use Jifty::RightsFrom column => 'owner';
17  
18 =head1 DESCRIPTION
19  
20 Provides a C<delegate_current_user_can> method that various
21 task-related objects can use as a base to make their own access
22 control decisions based on their
23 task. L<Jifty::Record/current_user_can> uses this method to make an
24 access control decision if it exists.
25  
26 Note that this means that a model class can use Jifty::RightsFrom,
27 and still have a custom C<current_user_can> method, and they will not
28 interfere with each other.
29  
30 =cut
31  
32 package Jifty::RightsFrom;
33 use base qw/Exporter/;
34  
35  
36 sub import {
37 my $class = shift;
38 export_curried_sub(
39 sub_name => 'delegate_current_user_can',
40 as => 'delegate_current_user_can',
41 export_to => $class,
42 args => \@_
43 );
44 }
45  
46  
47 =head2 export_curried_sub HASHREF
48  
49 Takes:
50  
51 =over
52  
53 =item sub_name
54  
55 The subroutine in this package that you want to export.
56  
57 =item export_to
58  
59 The name of the package you want to export to.
60  
61 =item as
62  
63 The name your new curried sub should be exported into in the package
64 C<export_to>
65  
66  
67 =item args (arrayref)
68  
69 The arguments you want to hand to your sub.
70  
71  
72 =back
73  
74 =cut
75  
76 sub export_curried_sub {
77 my %args = (
78 sub_name => undef,
79 export_to => undef,
80 as => undef,
81 args => undef,
82 @_
83 );
84 no strict 'refs';
85 no warnings 'redefine';
86 local *{ $args{'as'} } = sub { &{ $args{'sub_name'} }(shift @_, @{ $args{'args'} }, @_ ) };
87  
88 local @{Jifty::RightsFrom::EXPORT_OK} = ($args{as});
89 Jifty::RightsFrom->export_to_level( 2, $args{export_to}, $args{as} );
90 }
91  
92 =head2 delegate_current_user_can C<'column'>, C<$column_name>, C<$right_name>, C<@attributes>
93  
94 Make a decision about permissions based on checking permissions on the
95 column of this record specified in the call to C<import>. C<create>,
96 C<delete>, and C<update> rights all check for the C<update> right on
97 the delegated object. On create, we look in the passed attributes for
98 an argument with the name of that column.
99  
100 =cut
101  
102 sub delegate_current_user_can {
103 my $self = shift;
104 my $object_type = shift; #always 'column' for now
105 my $col_name = shift;
106 my $right = shift;
107 my %attribs = @_;
108  
109 $right = 'update' if $right ne 'read';
110 my $obj;
111  
112 $col_name =~ s/_id$//;
113 my $column = $self->column($col_name);
114 my $obj_type = $column->refers_to();
115  
116 # XXX TODO: this card is bloody hard to follow. it's my fault. --jesse
117  
118 my $foreign_key = $attribs{ $column->name };
119 # We only do the isa if the foreign_key is a reference
120 # We could also do this using eval, but it's an order of magnitude slower
121 if ( ref($foreign_key) and $foreign_key->isa($obj_type) ) {
122 $obj = $foreign_key; # the fk is actually an object
123 } elsif (
124 my $fk_value = (
125 $foreign_key
126 || $self->__value( $column->name )
127 || $self->{ $column->name }
128 )
129 )
130 {
131 $obj = $obj_type->new( current_user => $self->current_user );
132 $obj->load_by_cols( ( $column->by || 'id' ) => $fk_value );
133 } else {
134 return 0;
135 }
136  
137 return $obj->current_user_can($right);
138 }
139  
140  
141 1;
142