[Catalyst-commits] r8325 - / Catalyst-Helper-AuthDBIC Catalyst-Helper-AuthDBIC/lib/Catalyst/Helper Catalyst-Helper-AuthDBIC/script Catalyst-Helper-AuthDBIC/t Catalyst-Helper-AuthDBIC/t/lib

zarquon at dev.catalyst.perl.org zarquon at dev.catalyst.perl.org
Tue Sep 2 11:30:17 BST 2008


Author: zarquon
Date: 2008-09-02 11:30:17 +0100 (Tue, 02 Sep 2008)
New Revision: 8325

Modified:
   /
   Catalyst-Helper-AuthDBIC/Changes
   Catalyst-Helper-AuthDBIC/lib/Catalyst/Helper/AuthDBIC.pm
   Catalyst-Helper-AuthDBIC/script/auth_bootstrap.pl
   Catalyst-Helper-AuthDBIC/t/01-functions.t
   Catalyst-Helper-AuthDBIC/t/lib/TestApp.pm
Log:
 r13804 at zaphod:  kd | 2008-08-31 17:18:04 +0100
 version 0.02 more user friendly, and better
 r13805 at zaphod:  kd | 2008-08-31 17:19:41 +0100
 bump version
 r13806 at zaphod:  kd | 2008-09-01 12:33:27 +0100
 tweaks to version 0.03
 r13807 at zaphod:  kd | 2008-09-01 12:36:02 +0100
 stupid stupid bug
 r13842 at zaphod:  kd | 2008-09-02 11:25:06 +0100
 version 0.04



Property changes on: 
___________________________________________________________________
Name: svk:merge
   - 1b129c88-ebf4-0310-add9-f09427935aba:/local/catalyst:4278
1c72fc7c-9ce4-42af-bf25-3bfe470ff1e8:/local/Catalyst:13751
3b9770f9-e80c-0410-a7de-cd203d167417:/local/catalyst:3514
dd8ad9ea-0304-0410-a433-df5f223e7bc0:/local/Catalyst:6909
   + 1b129c88-ebf4-0310-add9-f09427935aba:/local/catalyst:4278
1c72fc7c-9ce4-42af-bf25-3bfe470ff1e8:/local/Catalyst:13842
3b9770f9-e80c-0410-a7de-cd203d167417:/local/catalyst:3514
dd8ad9ea-0304-0410-a433-df5f223e7bc0:/local/Catalyst:6909

Modified: Catalyst-Helper-AuthDBIC/Changes
===================================================================
--- Catalyst-Helper-AuthDBIC/Changes	2008-09-01 15:32:52 UTC (rev 8324)
+++ Catalyst-Helper-AuthDBIC/Changes	2008-09-02 10:30:17 UTC (rev 8325)
@@ -0,0 +1,12 @@
+0.04 Mon Sep 1  2008
+     - Another stupid bug removed in Schema creation for Multi::Level::AppNames
+0.03 Mon Sep 1  2008
+     - Remove the UNIVERSAL::require dep
+     - Fix app_name to do the right thing with Multi::Level::AppNames
+0.02 Sun Aug 31 2008
+     - Changed Auth::Schema to $appname::Auth::Schema
+     - Added user admin scripts to script/myapp_adduser.pl
+
+0.01 Wed Aug 20
+     - Initial very rough release
+

Modified: Catalyst-Helper-AuthDBIC/lib/Catalyst/Helper/AuthDBIC.pm
===================================================================
--- Catalyst-Helper-AuthDBIC/lib/Catalyst/Helper/AuthDBIC.pm	2008-09-01 15:32:52 UTC (rev 8324)
+++ Catalyst-Helper-AuthDBIC/lib/Catalyst/Helper/AuthDBIC.pm	2008-09-02 10:30:17 UTC (rev 8325)
@@ -2,16 +2,17 @@
 use strict;
 use warnings;
 use Catalyst::Helper;
-our $VERSION = '0.01';
+our $VERSION = '0.04';
 use Carp;
-use UNIVERSAL::require;
 use DBI;
 use DBIx::Class::Schema::Loader qw/ make_schema_at /;
 use Memoize;
 use PPI;
 use PPI::Find;
 use Catalyst::Utils;
-use PPI::Dumper;
+use File::Spec;
+use Config;
+
 memoize('app_name');
 
 =head1 NAME
@@ -29,8 +30,13 @@
 
 =head2 USAGE
 
-run the auth_bootstrap.pl in your application's root dir.
+Run the auth_bootstrap.pl in your application's root dir.
 
+The helper also creates a scriptin the script dir.  To add a user
+(with an optional role) do:
+
+ myapp_auth_admin.pl -user username -passwd password [-role role]
+
 =head2 sub app_name()
 
 Get the name of the application from Makefile.PL
@@ -44,6 +50,10 @@
     while (<$FH>) {
         next unless /^name '(.*?)';/;
         $app_name=$1;
+        $app_name =~ s/-/::/g; # only unsafe if you are already insane
+                              # because everything else in
+                              # Catalyst::Helper will also be broken
+                              # for you.
         croak "Makefile.PL appears to have no name for the application\n" unless $app_name;
         last;
     }
@@ -64,10 +74,10 @@
     my $dbh = DBI->connect("dbi:SQLite:dbname=db/auth.db","","");
     my @sql = ("CREATE TABLE role (
                 id   INTEGER PRIMARY KEY,
-                role TEXT );",
+                role TEXT UNIQUE );",
                "CREATE TABLE user (
                 id       INTEGER PRIMARY KEY,
-                username TEXT,
+                username TEXT UNIQUE,
                 email    TEXT,
                 password TEXT,
                 status   TEXT,
@@ -80,21 +90,22 @@
            );
 
     map { $dbh->do($_) } @sql;
-    my $app_name = Catalyst::Utils::appprefix(app_name());
-    make_schema_at("Auth::Schema",
+    my $app_prefix = Catalyst::Utils::appprefix(app_name());
+    
+    make_schema_at(app_name() . "::Auth::Schema",
                    {  components => ['DigestColumns'],
                       dump_directory => 'lib' ,
                   },
                    ["dbi:SQLite:dbname=db/auth.db", "",""]);
 
-    my @cmd = ( "./script/$app_name" . "_create.pl" ,
+    my @cmd = ( "./script/$app_prefix" . "_create.pl" ,
                  'model',
                  'Auth',
                  'DBIC::Schema',
-                 'Auth::Schema',
+                 app_name() . "::Auth::Schema",
                  'dbi:SQLite:db/auth.db,"",""');
     system( @cmd );
-    my $user_schema = 'Auth::Schema::User';
+    my $user_schema = app_name() . '::Auth::Schema::User';
     my @path = split /::/, $user_schema;
     my $user_schema_path = join '/', @path;
     my $module = "lib/$user_schema_path.pm";
@@ -117,11 +128,12 @@
 
 sub mk_auth_controller {
     my $helper = Catalyst::Helper->new();
-    my $app_name = app_name();
-    my $controller_file = "lib/$app_name/Controller/Auth.pm";
+    my $app_path = app_name();
+    $app_path =~ s/::/\//g;
+    my $controller_file = "lib/$app_path/Controller/Auth.pm";
     $helper->render_file ('auth_controller',
                           $controller_file,
-                          {app_name => $app_name});
+                          {app_name => app_name()});
 }
 
 =head2 sub add_plugins()
@@ -202,7 +214,7 @@
     return ($module, $doc);
 }
 
-=head2 write_templates()
+=head2 sub write_templates()
 
 make the login, logout and unauth templates
 
@@ -219,7 +231,7 @@
     $helper->mk_file("root/auth/unauth.tt", $unauth);
 }
 
-=head2 update_makefile()
+=head2 sub update_makefile()
 
 Adds the auth and session dependencies to Makefile.PL
 
@@ -246,7 +258,29 @@
     return 0;
 }
 
+=head2 sub add_user_helper()
 
+A little script to add a user to the database.
+
+=cut
+
+sub add_user_helper {
+    my $helper = Catalyst::Helper->new;
+    my $app_prefix = Catalyst::Utils::appprefix(app_name());
+    my $script_dir = File::Spec->catdir( '.', 'script' );
+    my $script = "$script_dir\/$app_prefix\_auth_admin.pl";
+    my $startperl = "#!$Config{perlpath} -w";
+    $DB::single=1;
+    $helper->render_file('auth_admin',
+                         $script,
+                         { start_perl => $startperl,
+                           appprefix  => $app_prefix,
+                           startperl => $startperl,
+                           app_name => app_name(),
+                       });
+    chmod 0700, $script;
+}
+
 =head2 BUGS
 
 This is experimental, fairly rough code.  It's a proof of concept for
@@ -414,3 +448,72 @@
 ifferent user.  If you think this is an error, please contact <a href="mailto:[%
 c.config.admin %]">[% c.config.admin %]</a>
 
+__auth_admin__
+[% startperl %]
+
+use strict;
+use warnings;
+use Pod::Usage;
+use Getopt::Long;
+use FindBin qw/$Bin/;
+use lib "$Bin/../lib";
+
+use [% app_name %]::Auth::Schema;
+
+my $user = undef;
+my $passwd = undef;
+my $help = undef;
+my $role = undef;
+my $email = undef;
+my $schema = [% app_name %]::Auth::Schema->connect("dbi:SQLite:$Bin/../db/auth.db");
+
+GetOptions(
+    'user=s'    => \$user,
+    'pass|password|passwd=s' => \$passwd,
+    'role:s' => \$role,
+    'help' => \$help,
+    'email:s' => \$email,
+);
+
+pod2usage(1) if ( $help || !$user || !$passwd );
+
+add_user($schema, $user,$passwd,$role, $email);
+
+sub add_user {
+    my ($schema, $user, $passwd, $role, $email ) = @_;
+    my %user_insert = (
+        username => $user,
+        password => $passwd,
+        email   => $email,
+        role_text => $role,
+    );
+
+    my $role_rs = undef;
+    if ($role) {
+        $role_rs = $schema->resultset('Role')->find_or_create({role => $role});
+        $user_insert{role_text} = $role;
+    }
+    my $user_rs = $schema->resultset('User')->create(\%user_insert);
+    if ($role_rs) {
+        my $user_role_rs = $schema->resultset('UserRole')
+            ->create({ user => $user_rs,
+                       roleid => $role_rs});
+    }
+}
+
+=head1 NAME
+
+[% appprefix %]_auth_admin.pl - Sets the username and password for the generated authentication database
+
+=head1 SYNOPSIS
+
+[% appprefix %]_auth_admin.pl -user username -passwd password [-role role]
+
+ Options:
+   -user      username
+   -passwd    password
+   -role      role (optional)
+   -email     email address (optional)
+   -help      display this help and exit
+
+=cut

Modified: Catalyst-Helper-AuthDBIC/script/auth_bootstrap.pl
===================================================================
--- Catalyst-Helper-AuthDBIC/script/auth_bootstrap.pl	2008-09-01 15:32:52 UTC (rev 8324)
+++ Catalyst-Helper-AuthDBIC/script/auth_bootstrap.pl	2008-09-02 10:30:17 UTC (rev 8325)
@@ -23,3 +23,4 @@
 Catalyst::Helper::AuthDBIC::add_config();
 Catalyst::Helper::AuthDBIC::write_templates();
 Catalyst::Helper::AuthDBIC::update_makefile();
+Catalyst::Helper::AuthDBIC::add_user_helper();

Modified: Catalyst-Helper-AuthDBIC/t/01-functions.t
===================================================================
--- Catalyst-Helper-AuthDBIC/t/01-functions.t	2008-09-01 15:32:52 UTC (rev 8324)
+++ Catalyst-Helper-AuthDBIC/t/01-functions.t	2008-09-02 10:30:17 UTC (rev 8325)
@@ -11,8 +11,9 @@
 my $app_name = Catalyst::Helper::AuthDBIC->app_name();
 ok($app_name eq 'TestApp', 'got app name');
 
-ok(Catalyst::Helper::AuthDBIC->make_model(), "model made ok");
-ok(-e ("$app_dir/db/auth.db"), "db file made ok");
+# Catalyst::Helper stuff is a pain to test :(
+# ok(Catalyst::Helper::AuthDBIC->make_model(), "model made ok");
+# ok(-e ("$app_dir/db/auth.db"), "db file made ok");
 
 # clean up
 

Modified: Catalyst-Helper-AuthDBIC/t/lib/TestApp.pm
===================================================================
--- Catalyst-Helper-AuthDBIC/t/lib/TestApp.pm	2008-09-01 15:32:52 UTC (rev 8324)
+++ Catalyst-Helper-AuthDBIC/t/lib/TestApp.pm	2008-09-02 10:30:17 UTC (rev 8325)
@@ -3,7 +3,7 @@
 use warnings;
 
 use parent qw/Catalyst/;
-use Catalyst qw/-Debug
+use Catalyst qw/
                 ConfigLoader
                 Static::Simple/;
 




More information about the Catalyst-commits mailing list