Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

DBIC Error: DBIx::Class::PassphraseColumn::new(): new_result needs a hash at constructor

by three18ti (Scribe)
on Jun 03, 2012 at 18:55 UTC ( #974178=perlquestion: print w/ replies, xml ) Need Help??
three18ti has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks,

Continuing with my pursuit to learn DBIx from DBIx::Class Error: syntax error at or near "NOT" [Solved] I am now running into an issue with hashing the passwords.
The full project can be found here: https://github.com/three18ti/dbix-schema-test
I would like to use DBIx::Class::PassphraseColumn to automatically handle hashing of password columns.
I honestly think my problem is with my deployment script, but the DBIx docs aren't exactly explicit on how to use DBIx::Schema to deploy users. This will also likely come up when I create a function to create users.

Here's my deployment script

#/usr/bin/perl use v5.14.2; use strict; use warnings; use Data::Dumper; use DBIx::Class; use My::Schema; use Config::Any; # set config file and load it my $config_file = './config.yml' ; # retreives information from first entry in array. my $cfg = Config::Any->load_files({ files => [$config_file], use_ext => 1 })->[0]->{$config_file}; # load db_config my $db_config = $cfg->{'Database'}; my $db_name = $db_config->{'db_name'}; my $db_driver = $db_config->{'db_driver'}; my $user_name = $db_config->{'user_name'}; my $password = $db_config->{'password'}; my $db_host = $db_config->{'db_host'} || 'localhost'; #create dsn my $dsn = $db_config->{'dsn'} || "dbi:$db_driver:dbname=$db_name;host=$db_host"; $dsn .= ';port=' . $db_config->{'port'} if $db_config->{'port'}; # deploy Schema my $schema = My::Schema->connect($dsn, $user_name, $password); $schema->deploy({ add_drop_table => 1}); # add new row say "[*] Creating artist and insertng albums"; my $new_album = $schema->resultset('Artist')->create( { artist => 'Pink Floyd', albums => [ { title => 'Wish You Were Here', rank => '2', }, { title => 'The Wall', rank => '3', }, { title => 'Dark Side of the Moon', rank => '1',}, ], }, ); $new_album->update; say "[*] Searching Database for Artist"; my $rs = $schema->resultset('Artist'); my $res = $rs->search ({ artist => "Pink Floyd" })->single; say "[*] Printing titles of Albums"; say $_->title foreach $res->albums->all; say "[*] Creating Roles"; $schema->populate('Role', [ [ qw/role rank/, ], [ 'Administrator', '-1',], [ 'Contributor', '2', ], [ 'User', '3', ], ] ); say "[*] Creating Users"; #$schema->create({passphrase => 'plain'}); $schema->populate('User', [ [ qw/ username password user_roles/, ], [ 'test1', 'test1', [ 'Administrator', 'Contributor', 'User',], ], [ 'test2', 'test2', qw/Administrator/, ], [ 'test3', 'test3', qw/Contributor/, ], [ 'test4', 'test4', qw/User/, ], [ 'test5', 'test5', qw/Contributor User/, ], ], );

And though it's not much different than the docs, here's my "My::Schema::Result::User"

package My::Schema::Result::User; use strict; use warnings; use Moose; use MooseX::NonMoose; use namespace::autoclean; #use base qw/DBIx::Class::Core/; extends 'DBIx::Class::Core'; __PACKAGE__->load_components(qw/ InflateColumn::DateTime Ordered TimeS +tamp PassphraseColumn /); __PACKAGE__->position_column('user_id'); __PACKAGE__->table('users'); __PACKAGE__->add_columns( user_id => { accessor => 'userid', data_type => 'integer', size => 16, is_nullable => 0, is_auto_increment => 1, }, username => { accessor => 'username', data_type => 'varchar', size => 256, is_nullable => 0, is_auto_increment => 0, }, # Have the 'password' column use a SHA-1 hash +and # 20-byte salt # with RFC 2307 encoding; Generate the # 'check_password' method password => { data_type => 'varchar', size => 256, is_nullable => 0, is_auto_increment => 0, passphrase => 'rfc2307', passphrase_class => 'SaltedDigest', passphrase_args => { algorithm => 'SHA-1', salt_random => 20. }, passphrase_check_method => 'check_password', }, email_address => { data_type => "varchar", size => 256, is_nullable => 1, }, last_name => { data_type => "varchar", size => 100, is_nullable => 1, }, active => { data_type => "integer", size => 1, is_nullable => 1, }, ); __PACKAGE__->set_primary_key('user_id'); __PACKAGE__->has_many( user_roles => 'My::Schema::Result::UserRole', { "foreign.user_id" => "self.id" }, { cascade_copy => 0, cascade_delete => 0 }, ); # many_to_many(): # args: # 1) Name of relationship, # DBIC will create accessor with this name # 2) Name of has_many() relationship # this many_to_many() is shortcut for # 3) Name of belongs_to() relationship # in model class of has_many() above # You must already have the has_many() # defined to use a many_to_many(). __PACKAGE__->many_to_many(roles => 'user_roles', 'role'); =head2 has_role Check if a user has the specified role =cut use Perl6::Junction qw/any/; sub has_role { my ($self, $role) = @_; # Does this user posses the required role? return any(map { $_->role } $self->roles) eq $role; } __PACKAGE__->meta->make_immutable; 1; __END__

Ultimately, I would like to be able to create and check the password by feeding it plain text. Thanks for your insight.

Edit: Looks like it's not my deploy script, when I remove the password field and the __PACKAGE__->meta->make_immutable; in my My/Schema/Result/User.pm (https://github.com/three18ti/dbix-schema-test/commit/baffac44ee8bcc783a736e265266229e180cc5d4)

The error message changes:

DBIx::Class::Row::new(): new_result needs a hash at /usr/lib/perl5/Cla +ss/MOP/Method.pm line 125

Edit2: so... now I'm really confused. Actually, I think the issue may still be how I'm deploying... but I really don't know.

Edit3: Ok, looks like I'm having two problems.
The first problem is: how do I get a password into and out of the password column,
The second problem is: how do I populate the user roles, as I have tried feeding the roles listed as a hash, an array, or directly, I've even tried just assigning one role to a user, but I keep getting varying error messages:

Error passing as a hash:

perl deploy.pl Ignoring relationship 'user_roles' - related resultsource 'MyApp::Sche +ma::Result::UserRole' is not registered with this schema Ignoring relationship 'role' - related resultsource 'MyApp::Schema::Re +sult::Role' is not registered with this schema Ignoring relationship 'user' - related resultsource 'MyApp::Schema::Re +sult::User' is not registered with this schema NOTICE: drop cascades to constraint album_artistid_fkey on table albu +m NOTICE: CREATE TABLE will create implicit sequence "artist_artistid_s +eq" for serial column "artist.artistid" NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "artist +_pkey" for table "artist" NOTICE: CREATE TABLE will create implicit sequence "roles_role_id_seq +" for serial column "roles.role_id" NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "roles_ +pkey" for table "roles" NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "user_r +ole_pkey" for table "user_role" NOTICE: CREATE TABLE will create implicit sequence "users_user_id_seq +" for serial column "users.user_id" NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "users_ +pkey" for table "users" NOTICE: CREATE TABLE will create implicit sequence "album_albumid_seq +" for serial column "album.albumid" NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "album_ +pkey" for table "album" [*] Creating artist and insertng albums [*] Searching Database for Artist [*] Printing titles of Albums Dark Side of the Moon The Wall Wish You Were Here [*] Creating Roles [*] Creating Users Odd number of elements in anonymous hash at deploy.pl line 77. Odd number of elements in anonymous hash at deploy.pl line 77. Odd number of elements in anonymous hash at deploy.pl line 77. Odd number of elements in anonymous hash at deploy.pl line 77. DBIx::Class::Schema::populate(): HASH(0x382f740) reference found where + bind expected for column 'roles' in populate slice: { roles => { Administrator => undef }, username => "test2" } at deploy.pl line 77

Error attempting to pass an array ref:

perl deploy.pl Ignoring relationship 'user_roles' - related resultsource 'MyApp::Sche +ma::Result::UserRole' is not registered with this schema Ignoring relationship 'role' - related resultsource 'MyApp::Schema::Re +sult::Role' is not registered with this schema Ignoring relationship 'user' - related resultsource 'MyApp::Schema::Re +sult::User' is not registered with this schema NOTICE: drop cascades to constraint album_artistid_fkey on table albu +m NOTICE: CREATE TABLE will create implicit sequence "artist_artistid_s +eq" for serial column "artist.artistid" NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "artist +_pkey" for table "artist" NOTICE: CREATE TABLE will create implicit sequence "roles_role_id_seq +" for serial column "roles.role_id" NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "roles_ +pkey" for table "roles" NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "user_r +ole_pkey" for table "user_role" NOTICE: CREATE TABLE will create implicit sequence "users_user_id_seq +" for serial column "users.user_id" NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "users_ +pkey" for table "users" NOTICE: CREATE TABLE will create implicit sequence "album_albumid_seq +" for serial column "album.albumid" NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "album_ +pkey" for table "album" [*] Creating artist and insertng albums [*] Searching Database for Artist [*] Printing titles of Albums Dark Side of the Moon The Wall Wish You Were Here [*] Creating Roles [*] Creating Users DBIx::Class::Schema::populate(): ARRAY(0x2f066c8) reference found wher +e bind expected for column 'roles' in populate slice: { roles => [ "Administrator" ], username => "test2" } at deploy.pl line 77

now, I've just noticed this part of the notice:

Ignoring relationship 'user_roles' - related resultsource 'MyApp::Sche +ma::Result::UserRole' is not registered with this schema Ignoring relationship 'role' - related resultsource 'MyApp::Schema::Re +sult::Role' is not registered with this schema Ignoring relationship 'user' - related resultsource 'MyApp::Schema::Re +sult::User' is not registered with this schema

any hints on what that might mean as this is likely the cause of my problem.

Edit: Ok, I think the issue might be with my relationships. Can anyone provide some insight as to where I have gone wrong? Like I say, I'm following the Catalyst tutorial to try and build my DB...

Comment on DBIC Error: DBIx::Class::PassphraseColumn::new(): new_result needs a hash at constructor
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://974178]
Approved by rovf
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (8)
As of 2014-08-20 07:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (107 votes), past polls