43d192720c58bcb59684587b584a14655864c378
[catagits/Reaction.git] / lib / Reaction / InterfaceModel / Action / User / ResetPassword.pm
1 package Reaction::InterfaceModel::Action::User::ResetPassword;
2
3 use Reaction::Class;
4 use aliased 'Reaction::InterfaceModel::Action::User::SetPassword';
5 use Reaction::Types::Core qw(NonEmptySimpleStr);
6 use namespace::clean -except => [ qw(meta) ];
7
8 extends SetPassword;
9
10 has confirmation_code =>
11     (isa => NonEmptySimpleStr, is => 'rw', lazy_fail => 1);
12
13 has 'user' => (
14     is => 'rw', metaclass => 'Reaction::Meta::Attribute',
15     predicate => 'has_user',
16 );
17
18 around can_apply => sub {
19     my $super = shift;
20     my ($self) = @_;
21     return 0 unless $self->verify_confirmation_code;
22     return $super->(@_);
23 };
24
25 sub verify_confirmation_code {
26   my $self = shift;
27   my $supplied_code = $self->confirmation_code;
28   my $user = $self->target_model->find_by_confirmation_code($supplied_code)
29     if $self->has_confirmation_code;
30   if ( defined $user ) {
31     $self->user($user);
32     return 1;
33   } else {
34     return 0;
35   }
36 }
37
38 around error_for_attribute => sub {
39   my $super = shift;
40   my ($self, $attr) = @_;
41   if ($attr->name eq 'confirmation_code') {
42     return 'Confirmation code incorrect' unless $self->has_user;
43   }
44   return $super->(@_);
45 };
46
47 sub do_apply {
48   my $self = shift;
49   return $self->user->reset_password($self->new_password);
50 }
51
52 __PACKAGE__->meta->make_immutable;
53
54
55 1;
56
57 =head1 NAME
58
59 Reaction::InterfaceModel::Action::User::ResetPassword
60
61 =head1 DESCRIPTION
62
63 =head2 error_for_attribute
64
65 =head2 confirmation_code
66
67 =head2 verify_confirmation_code
68
69 =head1 SEE ALSO
70
71 L<Reaction::InterfaceModel::Action::DBIC::User::ResetPassword>
72
73 L<Reaction::InterfaceModel::Action::User::Role::ConfirmationCodeSupport>
74
75 =head1 AUTHORS
76
77 See L<Reaction::Class> for authors.
78
79 =head1 LICENSE
80
81 See L<Reaction::Class> for the license.
82
83 =cut