| 1 | package Catalyst::Authentication::FromSub::User::Object; | ||
| 2 | our $VERSION = '0.01'; | ||
| 3 | |||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | use base qw/Catalyst::Authentication::User Class::Accessor::Fast/; | ||
| 7 | |||
| 8 | BEGIN { | ||
| 9 | __PACKAGE__->mk_accessors(qw/config _user _roles _storage/); | ||
| 10 | } | ||
| 11 | |||
| 12 | sub new { | ||
| 13 | my ( $class, $opts, $c ) = @_; | ||
| 14 | |||
| 15 | my $self = { | ||
| 16 | _storage => $opts->{storage}, | ||
| 17 | _user => $opts->{user}, | ||
| 18 | _roles => undef, | ||
| 19 | }; | ||
| 20 | |||
| 21 | bless $self, $class; | ||
| 22 | } | ||
| 23 | |||
| 24 | my %features = ( session => 1, roles => { self_check => 0 } ); | ||
| 25 | |||
| 26 | sub supported_features { | ||
| 27 | my $self = shift; | ||
| 28 | return \%features; | ||
| 29 | } | ||
| 30 | |||
| 31 | sub for_session { | ||
| 32 | my $self = shift; | ||
| 33 | |||
| 34 | my $config = $self->_storage->{config}; | ||
| 35 | my $id_field = $config->{id_field} || 'id'; | ||
| 36 | return { $id_field => $self->_user->$id_field }; | ||
| 37 | } | ||
| 38 | |||
| 39 | sub from_session { | ||
| 40 | my ( $self, $frozenuser, $c ) = @_; | ||
| 41 | |||
| 42 | my $config = $self->_storage->{config}; | ||
| 43 | |||
| 44 | my $id; | ||
| 45 | if ( ref($frozenuser) eq 'HASH' ) { | ||
| 46 | $id = $frozenuser->{ $config->{'id_field'} }; | ||
| 47 | } | ||
| 48 | else { | ||
| 49 | $id = $frozenuser; | ||
| 50 | } | ||
| 51 | |||
| 52 | $self->_storage->find_user( { $config->{'id_field'} => $id }, $c ); | ||
| 53 | } | ||
| 54 | |||
| 55 | sub get { | ||
| 56 | my ( $self, $field ) = @_; | ||
| 57 | |||
| 58 | if ( $self->_user->can($field) ) { | ||
| 59 | return $self->_user->$field; | ||
| 60 | } | ||
| 61 | else { | ||
| 62 | return undef; | ||
| 63 | } | ||
| 64 | } | ||
| 65 | |||
| 66 | sub get_object { | ||
| 67 | my ($self) = @_; | ||
| 68 | return $self->_user; | ||
| 69 | } | ||
| 70 | |||
| 71 | sub obj { | ||
| 72 | my $self = shift; | ||
| 73 | return $self->get_object(@_); | ||
| 74 | } | ||
| 75 | |||
| 76 | sub AUTOLOAD { | ||
| 77 | my $self = shift; | ||
| 78 | |||
| 79 | ( my $method ) = ( our $AUTOLOAD =~ /([^:]+)$/ ); | ||
| 80 | |||
| 81 | return if $method eq "DESTROY"; | ||
| 82 | |||
| 83 | confess( | ||
| 84 | "Could not call method $method on class " . blessed( $self->_user ) ) | ||
| 85 | unless $self->_user->can($method); | ||
| 86 | $self->_user->$method; | ||
| 87 | } | ||
| 88 | |||
| 89 | 1; | ||
| 90 | __END__ |