Avoid circular refs with target_model for login actions
[catagits/Reaction.git] / lib / Reaction / InterfaceModel / Action / User / Login.pm
1 package Reaction::InterfaceModel::Action::User::Login;
2
3 use Reaction::Class;
4 use aliased 'Reaction::InterfaceModel::Action';
5 use Reaction::Types::Core qw(SimpleStr Password);
6
7 use namespace::clean -except => [ qw(meta) ];
8 extends Action;
9
10 # Avoid circular ref with target_model for Auth controller login actions.
11 sub BUILD { Scalar::Util::weaken($_[0]->{target_model}) }
12
13 has 'username' => (isa => SimpleStr, is => 'rw', lazy_fail => 1);
14 has 'password' => (isa => Password,  is => 'rw', lazy_fail => 1);
15
16 around error_for_attribute => sub {
17   my $super = shift;
18   my ($self, $attr) = @_;
19   my $result = $super->(@_);
20   my $predicate = $attr->get_predicate_method;
21   if (defined $result && $self->$predicate) {
22     return 'Invalid username or password';
23   }
24   return;
25 };
26 sub do_apply {
27   my $self = shift;
28   my $target = $self->target_model;
29   return $target->login($self->username, $self->password);
30 };
31 __PACKAGE__->meta->make_immutable;
32
33
34 1;
35
36 =head1 NAME
37
38 Reaction::InterfaceModel::Action::User::Login
39
40 =head1 DESCRIPTION
41
42 =head2 username
43
44 =head2 password
45
46 =head1 AUTHORS
47
48 See L<Reaction::Class> for authors.
49
50 =head1 LICENSE
51
52 See L<Reaction::Class> for the license.
53
54 =cut