[Bast-commits] r3143 - in trunk/DBIx-Class-DigestColumns: . lib/DBIx/Class t t/lib/DigestTest/Schema

groditi at dev.catalyst.perl.org groditi at dev.catalyst.perl.org
Tue Mar 27 18:46:53 GMT 2007


Author: groditi
Date: 2007-03-27 18:46:52 +0100 (Tue, 27 Mar 2007)
New Revision: 3143

Modified:
   trunk/DBIx-Class-DigestColumns/Changes
   trunk/DBIx-Class-DigestColumns/META.yml
   trunk/DBIx-Class-DigestColumns/README
   trunk/DBIx-Class-DigestColumns/lib/DBIx/Class/DigestColumns.pm
   trunk/DBIx-Class-DigestColumns/t/05digest.t
   trunk/DBIx-Class-DigestColumns/t/lib/DigestTest/Schema/Test.pm
Log:
digest_check_method support and tests

Modified: trunk/DBIx-Class-DigestColumns/Changes
===================================================================
--- trunk/DBIx-Class-DigestColumns/Changes	2007-03-25 19:04:56 UTC (rev 3142)
+++ trunk/DBIx-Class-DigestColumns/Changes	2007-03-27 17:46:52 UTC (rev 3143)
@@ -1,6 +1,7 @@
-0.05000 2007-03-05
+0.05000 2007-03-27
 	- Local copy of args in ->update unnecessary
 	- Added test for Marc's patch
+	- Add digest_check_method functionality
 
 0.04000	2007-03-05
 	- Patch by Marc Mims to fix ->update bug

Modified: trunk/DBIx-Class-DigestColumns/META.yml
===================================================================
--- trunk/DBIx-Class-DigestColumns/META.yml	2007-03-25 19:04:56 UTC (rev 3142)
+++ trunk/DBIx-Class-DigestColumns/META.yml	2007-03-27 17:46:52 UTC (rev 3143)
@@ -1,6 +1,6 @@
 ---
 name: DBIx-Class-DigestColumns
-version: 0.03000
+version: 0.04000
 author:
   - 'Tom Kirkpatrick (tkp) <tkp at cpan.org>'
   - |-
@@ -29,7 +29,7 @@
 provides:
   DBIx::Class::DigestColumns:
     file: lib/DBIx/Class/DigestColumns.pm
-    version: 0.03000
+    version: 0.04000
 generated_by: Module::Build version 0.2805
 meta-spec:
   url: http://module-build.sourceforge.net/META-spec-v1.2.html

Modified: trunk/DBIx-Class-DigestColumns/README
===================================================================
--- trunk/DBIx-Class-DigestColumns/README	2007-03-25 19:04:56 UTC (rev 3142)
+++ trunk/DBIx-Class-DigestColumns/README	2007-03-27 17:46:52 UTC (rev 3143)
@@ -97,8 +97,15 @@
 
     digest_encoding defaults to "hex".
 
+  _get_digest_string $value
+    Handles the actual encoding of column values into digests. When given a
+    $value it will return the digest string for that value. This is the
+    method used by "_digest_column_values" So you can use it to create an
+    identical digest if you need one for comparison (e.g. password
+    authentication).
+
   _digest_column_values
-    Handles the actual encoding of column values into digests.
+    Go through the columns and digest the values that need it.
 
     This method is called by insert and update when automatic digests are
     turned on. If dirty is enabled it will only digest the values of dirtied

Modified: trunk/DBIx-Class-DigestColumns/lib/DBIx/Class/DigestColumns.pm
===================================================================
--- trunk/DBIx-Class-DigestColumns/lib/DBIx/Class/DigestColumns.pm	2007-03-25 19:04:56 UTC (rev 3142)
+++ trunk/DBIx-Class-DigestColumns/lib/DBIx/Class/DigestColumns.pm	2007-03-27 17:46:52 UTC (rev 3143)
@@ -32,6 +32,13 @@
 
   __PACKAGE__->load_components(qw/DigestColumns ... Core/);
 
+  #automatically generate a method "check_password" in result class
+  __PACKAGE__->add_columns(
+    'password' => {
+      data_type => 'char',
+      size      => 32,
+      digest_check_method => 'check_password',
+  }
   __PACKAGE__->digestcolumns(
       columns   => [qw/ password /],
       algorithm => 'MD5',
@@ -67,6 +74,19 @@
 
   __PACKAGE__->digest_algorithm('SHA-1');
 
+=head1 Options added to add_column
+
+=head2 digest_check_method => $method_name
+
+By using the digest_check_method attribute when you declare a column you
+can create a check method for that column. The check method accepts a 
+plain text string, performs the correct digest on it and returns a boolean
+value indicating whether this method matches the currently_stored value.
+
+  $row->password('new_password');
+  $row->check_password('new_password'); #returns true
+  $row->check_password('new_password'); #returns false
+
 =head1 METHODS
 
 =head2 digestcolumns
@@ -84,6 +104,40 @@
 
 =cut
 
+sub register_column {
+    my ($self, $column, $info, @rest) = @_;
+    $self->next::method($column, $info, @rest);
+    
+    return unless defined $info->{'digest_check_method'};
+
+    my $method_name = $info->{'digest_check_method'};
+    my $result_class = $self->result_class;
+    #don't overwrite another method
+    $self->throw_exception("Can't create digest_check_method ${method_name}. ".
+			   "{$method_name} already exists.") 
+	if $self->can($method_name) || $result_class->can($method_name);;
+    my $class_method_name = $result_class."::".$method_name;
+
+    {
+	no strict 'refs';
+	*$class_method_name = sub{
+	    my ($self, $value) = @_;
+	    my $col_value = $self->get_column($column);
+	    #make sure we DTRT if column is dirty
+	    $col_value = $self->_get_digest_string($col_value)
+		if $self->is_column_changed($column);
+	    return $col_value eq $self->_get_digest_string($value);
+	};
+    }
+}
+
+=head2 register_column
+
+Override the original register_column to handle the creation of
+check methods.
+
+=cut
+
 sub digestcolumns {
     my $self = shift;
     my %args = @_;

Modified: trunk/DBIx-Class-DigestColumns/t/05digest.t
===================================================================
--- trunk/DBIx-Class-DigestColumns/t/05digest.t	2007-03-25 19:04:56 UTC (rev 3142)
+++ trunk/DBIx-Class-DigestColumns/t/05digest.t	2007-03-27 17:46:52 UTC (rev 3143)
@@ -1,10 +1,11 @@
 use strict;
 use warnings;
 use Test::More;
+use Class::ISA;
 
 BEGIN {
     plan eval "require Digest"
-        ? ( tests => 6 )
+        ? ( tests => 9 )	
         : ( skip_all => 'needs Digest for testing' );
 }
 
@@ -16,15 +17,21 @@
 
 
 $row = $schema->resultset('Test')->create({ password => 'testvalue' });
+my $rs = $schema->resultset('Test');
 is $row->password, 'e9de89b0a5e9ad6efd5e5ab543ec617c', 'got hex MD5 from Digest (default) on insert';
 
 $row->update({password => 'secret!'});
 is $row->password, 'dd945ab221b14e3be0d31fd4026f27eb', 'update with args';
 
 $row->password('testvalue2');
+#check again here to make sure we work even if column is dirty
+ok $row->check_password('testvalue2'), 'digest_check_method works';
 $row->update;
 is $row->password, '91c04cd25d9cc5fc44b7e4086e022d4d', 'got hex MD5 from Digest (default) on update';
 
+ok $row->can('check_password'), 'digest_check_method created successfully';
+ok $row->check_password('testvalue2'), 'digest_check_method works';
+
 eval { DigestTest::Schema::Test->digest_encoding('Dummy') };
 like $@, qr/is not a supported encoding scheme/, 'Unsupported encoding scheme';
 

Modified: trunk/DBIx-Class-DigestColumns/t/lib/DigestTest/Schema/Test.pm
===================================================================
--- trunk/DBIx-Class-DigestColumns/t/lib/DigestTest/Schema/Test.pm	2007-03-25 19:04:56 UTC (rev 3142)
+++ trunk/DBIx-Class-DigestColumns/t/lib/DigestTest/Schema/Test.pm	2007-03-27 17:46:52 UTC (rev 3143)
@@ -14,6 +14,7 @@
   'password' => {
     data_type => 'varchar',
     size      => 100,
+    digest_check_method => 'check_password',
   }
 );
 




More information about the Bast-commits mailing list