| 1 | package Catalyst::Plugin::Authentication::Store::DBIC; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | our $VERSION = '0.11'; | ||
| 7 | |||
| 8 | use Catalyst::Plugin::Authentication::Store::DBIC::Backend; | ||
| 9 | use Catalyst::Utils (); | ||
| 10 | |||
| 11 | sub setup { | ||
| 12 | my $c = shift; | ||
| 13 | |||
| 14 | $c->log->warn('Authentication::Store::DBIC is deprecated!') | ||
| 15 | unless $c->config->{authentication}{dbic}{no_deprecation_warning}; | ||
| 16 | |||
| 17 | # default values | ||
| 18 | $c->config->{authentication}{dbic}{user_field} ||= 'user'; | ||
| 19 | $c->config->{authentication}{dbic}{password_field} ||= 'password'; | ||
| 20 | $c->config->{authentication}{dbic}{catalyst_user_class} ||= | ||
| 21 | 'Catalyst::Plugin::Authentication::Store::DBIC::User'; | ||
| 22 | |||
| 23 | $c->default_auth_store( | ||
| 24 | Catalyst::Plugin::Authentication::Store::DBIC::Backend->new( { | ||
| 25 | auth => $c->config->{authentication}{dbic}, | ||
| 26 | authz => $c->config->{authorization}{dbic} | ||
| 27 | } ) | ||
| 28 | ); | ||
| 29 | |||
| 30 | $c->NEXT::setup(@_); | ||
| 31 | } | ||
| 32 | |||
| 33 | sub setup_finished { | ||
| 34 | my $c = shift; | ||
| 35 | |||
| 36 | return $c->NEXT::setup_finished unless @_; | ||
| 37 | |||
| 38 | my $config = $c->default_auth_store; | ||
| 39 | if (my $user_class = $config->{auth}{user_class}) { | ||
| 40 | $config->{auth}{user_class} = _get_instance( $c, $user_class ); | ||
| 41 | |||
| 42 | if ($config->{auth}{user_class}->isa('Class::DBI') and | ||
| 43 | $config->{auth}{catalyst_user_class} eq 'Catalyst::Plugin::Authentication::Store::DBIC::User') | ||
| 44 | { | ||
| 45 | my $uc = 'Catalyst::Plugin::Authentication::Store::DBIC::User::CDBI'; | ||
| 46 | eval "require $uc"; | ||
| 47 | die $@ if $@; | ||
| 48 | $config->{auth}{catalyst_user_class} = $uc; | ||
| 49 | } | ||
| 50 | |||
| 51 | } | ||
| 52 | else { | ||
| 53 | Catalyst::Exception->throw( message => "You must provide a user_class" ); | ||
| 54 | } | ||
| 55 | |||
| 56 | if (my $role_class = $config->{authz}{role_class}) { | ||
| 57 | $config->{authz}{role_class} = _get_instance( $c, $role_class ); | ||
| 58 | } | ||
| 59 | |||
| 60 | if (my $user_role_class = $config->{authz}{user_role_class}) { | ||
| 61 | $config->{authz}{user_role_class} = _get_instance( $c, $user_role_class ); | ||
| 62 | } | ||
| 63 | |||
| 64 | $c->NEXT::setup_finished(@_); | ||
| 65 | } | ||
| 66 | |||
| 67 | sub _get_instance { | ||
| 68 | my( $c, $class ) = @_; | ||
| 69 | |||
| 70 | # first see if there's a component already loaded. this means the user | ||
| 71 | # specified the full component name (MyApp::Model::Foo::Bar) | ||
| 72 | my $comp; | ||
| 73 | if( $comp = $c->components->{ $class } ) { | ||
| 74 | return $comp if ref $comp; | ||
| 75 | } | ||
| 76 | |||
| 77 | # second check to see if model() or comp() gives us something. this means | ||
| 78 | # the user specified the part after MyApp::Model only | ||
| 79 | my $model = $c->model($class) || $c->comp($class); | ||
| 80 | |||
| 81 | return $model if ref $model; | ||
| 82 | |||
| 83 | # now we're on to class names only | ||
| 84 | |||
| 85 | # if a component wasn't found, perhaps it's not been loaded yet. | ||
| 86 | if( !$comp ) { | ||
| 87 | eval { Catalyst::Utils::ensure_class_loaded( $class ); }; | ||
| 88 | } | ||
| 89 | |||
| 90 | # if the class existed, check to see if it's a dbic class and return a | ||
| 91 | # resultset instance | ||
| 92 | if( $comp || !$@ ){ | ||
| 93 | if( $class->can('resultset_instance') ) { | ||
| 94 | return $class->resultset_instance; | ||
| 95 | } | ||
| 96 | return $class; | ||
| 97 | } | ||
| 98 | |||
| 99 | # last case where the model gave us a non-ref which could be an old dbic | ||
| 100 | # class-data style setup | ||
| 101 | if( $model->can('resultset_instance') ) { | ||
| 102 | return $model->resultset_instance; | ||
| 103 | } | ||
| 104 | |||
| 105 | return $model; | ||
| 106 | } | ||
| 107 | |||
| 108 | sub user_object { | ||
| 109 | my $c = shift; | ||
| 110 | |||
| 111 | return ( $c->user_exists ) ? $c->user->obj : undef; | ||
| 112 | } | ||
| 113 | |||
| 114 | 1; | ||
| 115 | __END__ |