[Jifty-commit] r4439 - in jifty/trunk: lib/Jifty t/TestApp/lib/TestApp/Model t/TestApp/t

jifty-commit at lists.jifty.org jifty-commit at lists.jifty.org
Thu Nov 15 15:22:58 EST 2007


Author: alexmv
Date: Thu Nov 15 15:22:53 2007
New Revision: 4439

Added:
   jifty/trunk/t/TestApp/lib/TestApp/Model/OtherThingy.pm
   jifty/trunk/t/TestApp/lib/TestApp/Model/Thingy.pm
   jifty/trunk/t/TestApp/t/19-rightsfrom.t
Modified:
   jifty/trunk/   (props changed)
   jifty/trunk/lib/Jifty/ClassLoader.pm
   jifty/trunk/lib/Jifty/Record.pm
   jifty/trunk/lib/Jifty/RightsFrom.pm
   jifty/trunk/t/TestApp/lib/TestApp/Model/User.pm
   jifty/trunk/t/TestApp/t/11-current_user.t
   jifty/trunk/t/TestApp/t/before_access.t

Log:
 r24785 at zoq-fot-pik:  chmrr | 2007-11-15 15:20:31 -0500
  * Finalize triggers if possible (supported in forthcoming
    Class::Trigger release)
  * Override JDBI's default of having refers_to null return undef,
    instead of object with no id
  * RightsFrom 'foo_id' should work like RightsFrom 'foo'


Modified: jifty/trunk/lib/Jifty/ClassLoader.pm
==============================================================================
--- jifty/trunk/lib/Jifty/ClassLoader.pm	(original)
+++ jifty/trunk/lib/Jifty/ClassLoader.pm	Thu Nov 15 15:22:53 2007
@@ -340,6 +340,7 @@
     for my $full ($self->models) {
         $self->_require_model_related_classes($full);
     }
+    $_->finalize_triggers for grep {$_->can('finalize_triggers')} $self->models;
 }
 
 # This class helps Jifty::ClassLoader::require() load each model, the model's

Modified: jifty/trunk/lib/Jifty/Record.pm
==============================================================================
--- jifty/trunk/lib/Jifty/Record.pm	(original)
+++ jifty/trunk/lib/Jifty/Record.pm	Thu Nov 15 15:22:53 2007
@@ -576,6 +576,15 @@
 
 sub _brief_description {'name'}
 
+=head2 null_reference
+
+By default, L<Jifty::DBI::Record> returns C<undef> on non-existant
+related fields; Jifty prefers to get back an object with an undef id.
+
+=cut
+
+sub null_reference { 0 }
+
 =head2 _new_collection_args
 
 Overrides the default arguments which this collection passes to new

Modified: jifty/trunk/lib/Jifty/RightsFrom.pm
==============================================================================
--- jifty/trunk/lib/Jifty/RightsFrom.pm	(original)
+++ jifty/trunk/lib/Jifty/RightsFrom.pm	Thu Nov 15 15:22:53 2007
@@ -75,10 +75,10 @@
 
 sub export_curried_sub {
     my %args = (
-        sub_name     => undef,
-        export_to => undef,
-        as           => undef,
-        args        =>  undef,
+        sub_name   => undef,
+        export_to  => undef,
+        as         => undef,
+        args       => undef,
         @_
     );
     no strict 'refs';
@@ -109,6 +109,7 @@
     $right = 'update' if $right ne 'read';
     my $obj;
 
+    $col_name =~ s/_id$//;
     my $column   = $self->column($col_name);
     my $obj_type = $column->refers_to();
 

Added: jifty/trunk/t/TestApp/lib/TestApp/Model/OtherThingy.pm
==============================================================================
--- (empty file)
+++ jifty/trunk/t/TestApp/lib/TestApp/Model/OtherThingy.pm	Thu Nov 15 15:22:53 2007
@@ -0,0 +1,17 @@
+use strict;
+use warnings;
+
+package TestApp::Model::OtherThingy;
+use Jifty::DBI::Schema;
+
+use TestApp::Record schema {
+
+  column value => type is 'text',  is mandatory;
+  column user_id => refers_to TestApp::Model::User;
+
+};
+
+use Jifty::RightsFrom column => 'user_id';
+
+1;
+

Added: jifty/trunk/t/TestApp/lib/TestApp/Model/Thingy.pm
==============================================================================
--- (empty file)
+++ jifty/trunk/t/TestApp/lib/TestApp/Model/Thingy.pm	Thu Nov 15 15:22:53 2007
@@ -0,0 +1,17 @@
+use strict;
+use warnings;
+
+package TestApp::Model::Thingy;
+use Jifty::DBI::Schema;
+
+use TestApp::Record schema {
+
+  column value => type is 'text',  is mandatory;
+  column user_id => refers_to TestApp::Model::User;
+
+};
+
+use Jifty::RightsFrom column => 'user';
+
+1;
+

Modified: jifty/trunk/t/TestApp/lib/TestApp/Model/User.pm
==============================================================================
--- jifty/trunk/t/TestApp/lib/TestApp/Model/User.pm	(original)
+++ jifty/trunk/t/TestApp/lib/TestApp/Model/User.pm	Thu Nov 15 15:22:53 2007
@@ -50,5 +50,17 @@
     'America/Anchorage'
 }
 
+sub current_user_can {
+    my $self = shift;
+    my $right = shift;
+    my %args = @_;
+
+    return 1 if $self->SUPER::current_user_can($right => %args);
+    
+    return 1 if $self->current_user->id and $self->id and $self->current_user->id == $self->id;
+
+    return 0;
+}
+
 1;
 

Modified: jifty/trunk/t/TestApp/t/11-current_user.t
==============================================================================
--- jifty/trunk/t/TestApp/t/11-current_user.t	(original)
+++ jifty/trunk/t/TestApp/t/11-current_user.t	Thu Nov 15 15:22:53 2007
@@ -23,6 +23,7 @@
 
 # Make it so that all users have full access
 TestApp::Model::User->add_trigger( before_access => sub { 'allow' } );
+TestApp::Model::User->finalize_triggers if TestApp::Model::User->can('finalize_triggers');
 
 # Create two users
 my $o = TestApp::Model::User->new(current_user => $system_user);

Added: jifty/trunk/t/TestApp/t/19-rightsfrom.t
==============================================================================
--- (empty file)
+++ jifty/trunk/t/TestApp/t/19-rightsfrom.t	Thu Nov 15 15:22:53 2007
@@ -0,0 +1,88 @@
+#!/usr/bin/env perl
+use warnings;
+use strict;
+
+=head1 DESCRIPTION
+
+Test the RightsFrom mixin.
+
+=cut
+
+use lib 't/lib';
+use Jifty::SubTest;
+
+use Jifty::Test no_plan => 1;;
+
+use_ok('TestApp::Model::User');
+use_ok('TestApp::Model::Thingy');
+use_ok('TestApp::Model::OtherThingy');
+use_ok('TestApp::CurrentUser');
+
+# Get a system user
+my $system_user = TestApp::CurrentUser->superuser;
+ok($system_user, "Found a system user");
+
+# Create users
+my $one = TestApp::Model::User->new(current_user => $system_user);
+$one->create( name => 'A User', email => 'auser at example.com', 
+            password => 'secret', tasty => 0 );
+ok($one->id, "New user has valid id set");
+is($one->name, "A User", "Has the right name");
+my $two = TestApp::Model::User->new(current_user => $system_user);
+$two->create( name => 'Bob', email => 'bob at example.com', 
+            password => 'secret2', tasty => 0 );
+ok($two->id, "New user has valid id set");
+
+# Create a CurrentUser
+my $one_user = TestApp::CurrentUser->new( id => $one->id );
+ok($one_user->id, "Loaded the current user");
+is($one_user->id, $one->id, "Has the right id");
+is($one_user->user_object->id, $one->id, "User object is right");
+is($one_user->user_object->name, $one->name, "Name is consistent");
+
+my $two_by_one = TestApp::Model::User->new( current_user => $one_user );
+$two_by_one->load( $two->id );
+ok($two_by_one->id, "Has an id");
+is($two_by_one->id, $two->id, "Has the right id");
+ok(!$two_by_one->current_user_can("read"), "Can read the remote user");
+ok(!$two_by_one->name, "Can't read their name");
+
+# And a thingy and otherthingy, one from each user; thingy has
+# rights_from 'user', otherthingy has rights from 'user_id';
+for my $class (qw/TestApp::Model::Thingy TestApp::Model::OtherThingy/) {
+    my $mine = $class->new(current_user => $system_user);
+    $mine->create( user_id => $one->id, value => "Whee" );
+    ok( $mine->id, "New object has a valid id");
+    is( $mine->user_id, $one->id, "Has right user" );
+    my $theirs = $class->new(current_user => $system_user);
+    $theirs->create( user_id => $two->id, value => "Not whee" );
+    ok( $theirs->id, "New object has a valid id");
+    is( $theirs->user_id, $two->id, "Has right user" );
+
+    my $access = $class->new( current_user => $one_user );
+    $access->load( $mine->id );
+    ok( $access->id, "Object has an id" );
+    is( $access->id, $mine->id, "Has the right id" );
+    ok( $access->current_user_can("read"), "I can read it");
+    ok( $access->value, "Has a value" );
+    is( $access->value, "Whee", "Can read the value" );
+    isa_ok( $access->user, "TestApp::Model::User", "Has a user" );
+    ok( $access->user_id, "Can read the user_id" );
+    ok( $access->user->id, "Can read the user->id" );
+    is( $access->user->id, $one->id, "Has the right user" );
+
+    $access->load( $theirs->id );
+    ok( $access->id, "Object has an id" );
+    is( $access->id, $theirs->id, "Has the right id" );
+    ok( !$access->current_user_can("read"), "I can't read it");
+    ok( !$access->value, "Can't read the value" );
+    isa_ok( $access->user, "TestApp::Model::User", "Has a user" );
+    ok( !$access->user_id, "Can't read the user_id" );
+    TODO:
+    {
+        local $TODO = "ACLs should apply to object refs, but can't";
+        # Except the problem is that Jifty current_user_can's often
+        # call their object refs, which would cause recursion.
+        ok( !$access->user->id, "Can't read the user->id" );
+    }
+}

Modified: jifty/trunk/t/TestApp/t/before_access.t
==============================================================================
--- jifty/trunk/t/TestApp/t/before_access.t	(original)
+++ jifty/trunk/t/TestApp/t/before_access.t	Thu Nov 15 15:22:53 2007
@@ -22,6 +22,7 @@
     }
     return 'ignore';
 });
+TestApp::Model::User->finalize_triggers if TestApp::Model::User->can('finalize_triggers');
 
 # Try creating non-bob, which will be denied
 my $o = TestApp::Model::User->new(current_user => $system_user);


More information about the Jifty-commit mailing list