Thursday, March 23, 2006

Authentication and DBIC

因为正在搞的项目使用了多个数据源(说过好多次了。:) 所以只能在运行时指定 Catalyst::Plugin::Authentication::Store::DBIC 所采用的表所在的数据源。将这个 config 放到 Root.pm 下总是会报错。怎么改也改不好,大致原因是 Catalyst 一运行就载入所有的插件 Plugins, 而载入插件时插件就会读取它自己的 config ,所以在 __PACKAGE__->setup; 后修改插件的 config 是不起作用的。而我们获取表所在的数据源需要知道 $c->req->address 类似东西,所以一定会是在 setup 以后。
最后迫不得已到 Catalyst mailing list 发了个 [Catalyst] Catalyst::Plugin::Authentication::Store::DBIC and config in auto
邮件组里的人倒没给我解决,可能是时差关系。因为这是 $work, 所以就只好自己看源码了。最后的报错总是 Can't call method "search" on an undefined value at,研究了个把小时,总算搞定了。

写一个 Module 专门用于 Authentication:
package MyApp::Model::UserAuth;

use strict;
use warnings;
use base 'Catalyst::Model';

sub search {
my $self = shift;
my $user = MyApp->config->{dbic}{geo}->resultset('User')->search(@_);
return $user;
}

1;
这里的 search 方法是专门给 DBIC::User 用的。因为这是一个 Model, 所以可以得到存到 config 里的数据源所在的表的。最后设置 yml 文件。
authentication:
dbic:
user_class: "MyApp::Model::UserAuth"
user_field: "username"
password_field: "password"

:) I like $$$$$.

0 Comments:

Post a Comment

Links to this post:

Create a Link

<< Home