aboutsummaryrefslogtreecommitdiffstats
path: root/lib/Fripost/Schema/Pending.pm
blob: 868f5910334449ca145241091deb9463871fb9ad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package Fripost::Schema::Pending;

=head1 NAME

Pending.pm - Manage pending entries

=head1 DESCRIPTION

This module abstracts the LDAP schema definition and provides methods to
unlock, list or delete pending entries.

=cut

use 5.010_000;
use strict;
use warnings;
use utf8;

use parent 'Fripost::Schema::Auth';
use Fripost::Schema::Util qw/ldap_error dn2mail ldap_and_filter/;
use POSIX 'strftime';


=head1 METHODS

=over 4

=item B<unlock> (I<name>, I<token>, I<OPTIONS>)

Unlock the pending I<name>, locked with I<token>. I<Token> may be left
undefined if I<name> is marked as pending, but is not locked.

=over 4

=item B<-dry-run> => 0|1

Merely simulate the unlock. I<token> is still checked to be a valid code
when defined.

=back

Errors can be caught with options B<-die> and B<-error>; See
B<Fripost::Schema::Util> for details.

=cut


sub unlock {
    my $self = shift;
    my $name = shift;
    my $token = shift;
    my %options = @_;

    # Nothing to do after an error.
    return if $options{'-error'} && ${$options{'-error'}};

    my $dn = $self->mail2dn( $name );
    my %delete = (objectClass => 'FripostPendingEntry');
    if ($token) {
        my $mesg = $self->ldap->compare( $dn
                                       , attr => 'fripostPendingToken'
                                       , value => $token );
        my $catch = { Net::LDAP::Constant::LDAP_COMPARE_TRUE => 0
                    , Net::LDAP::Constant::LDAP_COMPARE_FALSE =>
                        "Wrong unlock code for ‘".$name."’"
                    };
        ldap_error($mesg, %options, -die => $catch) // return;
        return 1 if $options{'-dry-run'};
        $delete{fripostPendingToken} = [];
    }

    my $mesg = $self->ldap->modify( $dn, delete => \%delete );
    ldap_error($mesg, %options);
}


=item B<find> (I<OPTIONS>)

List all pending entries.

=over 4

=item B<-delete> => 0|1

When set, delete all found entries.

=item B<-quiet> => 0|1

When set, do not print the list of found entries.

=item B<-max-age>

When set, limit found entries to those that were created before the
given date, in second since epoch.

=back

Errors can be caught with options B<-die> and B<-error>; See
B<Fripost::Schema::Util> for details.

=cut


sub find {
    my $self = shift;
    my %options = @_;

    # Nothing to do after an error.
    return if $options{'-error'} && ${$options{'-error'}};

    my @filter = 'objectClass=FripostPendingEntry';
    if ($options{'-max-age'}) {
        my $now = int(strftime "%s", gmtime);
        my $maxdate = Net::LDAP::Util::escape_filter_value(
                          strftime ("%Y%m%d%H%M%SZ",
                                    localtime($now - $options{'-max-age'}))
                      );
        push @filter, "createTimestamp<=$maxdate"
    }

    my $base = Fripost::Schema::Util::canonical_dn(@{$self->suffix});
    my $found = $self->ldap->search (
                    base => $base,
                    scope => 'subtree',
                    deref => 'never',
                    filter => ldap_and_filter (@filter),
                    attrs => [ '1.1' ],
                    (!$options{'-delete'} && $options{'-quiet'} ? () :
                      callback =>
                      sub {
                          my ($mesg, $obj) = @_;
                          if (defined $obj and $obj->isa('Net::LDAP::Entry')) {
                              print STDERR "Deleting DN ".$obj->dn."\n"
                                  unless $options{'-quiet'};
                              if ($options{'-delete'}) {
                                  $obj->delete;
                                  my $mesg = $obj->update($self->ldap);
                                  ldap_error($mesg, %options) // return;
                              }
                              $mesg->pop_entry;
                          }
                      })
                );
    ldap_error($found, %options);
}

=back

=head1 AUTHOR

Guilhem Moulin C<< <guilhem at fripost.org> >>

=head1 COPYRIGHT

Copyright 2012,2013 Guilhem Moulin.

=head1 LICENSE

This program is free software; you can redistribute it and/or modify it
under the same terms as perl itself.

=cut

1;

__END__