Catalyst::Authentication::Store::MoCo fixed
→GitHub - taiyoh/catalyst-authentication-store-moco: based on Catalyst: :Class
今更気づいたんだけど、MoCoだとmany-to-manyって一発でいかない…?
代替策として、has_manyの2段構成で対処。
具体的には、
http://cpansearch.perl.org/src/JAYK/Catalyst-Authentication-Store-DBIx-Class-0.1082/t/lib/SetupDB.pm
これと同じDB構成だとすると(というか、テストはまんまこれ使ってる…)、
use strict; use warnings; package TestApp::Schema; use base 'DBIx::MoCo'; __PACKAGE__->db_object('TestApp::Schema::DataBase'); package TestApp::Schema::DataBase; use base qw/DBIx::MoCo::DataBase/; our $db_file = $ENV{TESTAPP_DB_FILE} || ''; __PACKAGE__->dsn("dbi:SQLite:$db_file"); __PACKAGE__->username(''); __PACKAGE__->password(''); package TestApp::Schema::Role; use base 'TestApp::Schema'; __PACKAGE__->table('role'); __PACKAGE__->primary_keys('id'); package TestApp::Schema::User; use base 'TestApp::Schema'; __PACKAGE__->table('user'); __PACKAGE__->primary_keys('id'); __PACKAGE__->has_many( 'roles' => 'TestApp::Schema::UserRole', { key => { 'id' => 'user' }} ); package TestApp::Schema::UserRole; use base 'TestApp::Schema'; __PACKAGE__->table('user_role'); __PACKAGE__->primary_keys('id'); __PACKAGE__->has_many( 'roles' => 'TestApp::Schema::Role', { key => { 'roleid' => 'id' }} );
という感じで、同じキーを使ってアクセスするようにしてます。
# Catalyst::Authentication::Store::MoCo::User sub roles { my ( $self ) = shift; ## this used to load @wantedroles - but that doesn't seem to be used by the roles plugin, so I dropped it. ## shortcut if we have already retrieved them if (ref $self->_roles eq 'ARRAY') { return(@{$self->_roles}); } my @roles = (); if (exists($self->config->{'role_column'})) { my $role_data = $self->get($self->config->{'role_column'}); if ($role_data) { @roles = split /[\s,\|]+/, $self->get($self->config->{'role_column'}); } $self->_roles(\@roles); } elsif (exists($self->config->{'role_relation'})) { my $relation = $self->config->{'role_relation'}; my $rel = $self->_user->$relation; my $rf = $self->config->{'role_field'}; my @roles = $rel->map( sub { $_->$relation->first } ); if ($roles[0]->has_column($rf)) { $self->_roles([ map { $_->{$rf} } @roles ]); } else { Catalyst::Exception->throw("role table does not have a column called " . $self->config->{'role_field'}); } } else { Catalyst::Exception->throw("user->roles accessed, but no role configuration found"); } return @{$self->_roles}; }
ということで、Catalyst::Authentication::Store::DBIx::Classのテストをコピってきて、単体では通る状態のをコミットしました。