Experimenting with KiokuX::User
Jess Robinson [Wed, 2 May 2012 15:46:30 +0000 (15:46 +0000)]
lib/Text/Tradition/User.pm [new file with mode: 0644]
lib/Text/Tradition/UserStore.pm [new file with mode: 0644]
t/text_tradition_user.t [new file with mode: 0644]

diff --git a/lib/Text/Tradition/User.pm b/lib/Text/Tradition/User.pm
new file mode 100644 (file)
index 0000000..5a91101
--- /dev/null
@@ -0,0 +1,17 @@
+package Text::Tradition::User;
+
+use strict;
+use warnings;
+
+use Moose;
+with qw(KiokuX::User);
+
+has 'password'   => (is => 'rw', required => 1);
+has 'traditions' => (is => 'rw', isa => 'ArrayRef[Text::Tradition]', required => 0);
+
+# after add_tradition => sub { 
+#     $tradition->set_user($self) 
+#         unless $tradition->user->username eq $self->name;
+# }
+
+1;
diff --git a/lib/Text/Tradition/UserStore.pm b/lib/Text/Tradition/UserStore.pm
new file mode 100644 (file)
index 0000000..6a10e88
--- /dev/null
@@ -0,0 +1,35 @@
+package Text::Tradition::UserStore;
+
+use strict;
+use warnings;
+
+use Moose;
+use KiokuX::User::Util qw(crypt_password);
+
+use Text::Tradition::User;
+use Text::Tradition::Directory;
+
+has 'directory' => ( is => 'rw', isa => 'KiokuX::Model');
+
+sub add_user {
+    my ($self, $username, $password) = @_;
+
+    my $user = Text::Tradition::User->new(
+        id => $username,
+        password => crypt_password($password),
+    );
+
+    my $scope = $self->directory->new_scope;
+    $self->directory->store($user->kiokudb_object_id, $user);
+
+    return $user;
+}
+
+sub find_user {
+    my ($self, $username) = @_;
+
+    return $self->directory->lookup($self->user_prefix . $username);
+    
+}
+
+1;
diff --git a/t/text_tradition_user.t b/t/text_tradition_user.t
new file mode 100644 (file)
index 0000000..d1e15ae
--- /dev/null
@@ -0,0 +1,22 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+
+use Test::More 'no_plan';
+use KiokuX::Model;
+use File::Temp;
+
+use_ok('Text::Tradition::UserStore');
+
+my $fh = File::Temp->new();
+my $file = $fh->filename;
+$fh->close;
+my $dsn = "dbi:SQLite:dbname=$file";
+my $d = KiokuX::Model->new( 'dsn' => $dsn,'extra_args' => { 'create' => 1 } );
+
+my $user_store = Text::Tradition::UserStore->new(directory => $d);
+
+my $new_user = $user_store->add_user('fred', 'bloggs');
+isa_ok($new_user, 'Text::Tradition::User');
+