aboutsummaryrefslogtreecommitdiffstats
path: root/lib/Fripost/Schema/Search.pm
blob: a9eb2ea7c2ccc65e0fe1d8262bac53d5755c147b (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
package Fripost::Schema::Search;

use 5.010_000;
use warnings;
use strict;

use Fripost::Schema::Type;
use Fripost::Schema::Utils;

use base qw/Net::LDAP::Search/;
our $VERSION = '0.01';

# Count the entries got out from the query.
sub count { $_[0]->{_res}->count }

# Create a hash out of the LDAP entry. Keys depend on the context
# of the object.
# The value can be an array reference (multi-valued attributes) or
# a scalar (otherwise).
sub entries {
    my $self = shift;

    my $dumpEntry;
    if ( $self->{_type} == MAILBOX ) {
        $dumpEntry = "_userEntry";
    }
    elsif ( $self->{_type} == DOMAIN ) {
        $dumpEntry = "_domainEntry";
    }
    elsif ( $self->{_type} == ALIAS ) {
        $dumpEntry = "_aliasEntry";
    }
    else {
        die "Something weird happened. Please report."
    }

    no strict "refs";
    return (map {&$dumpEntry($_)} $self->{_res}->entries);
}


sub _userEntry {
    my $entry = shift;
    my %user;
    $user{username} = Fripost::Schema::Utils::fromDN ($entry->dn);
    &_get_values( $entry, \%user, 'uid');
    map { &_get_values($entry, \%user, $_) }
        qw /isActive userPassword/;
    return \%user;
}

sub _domainEntry {
    my $entry = shift;
    my %domain;
    &_get_values( $entry, \%domain, 'domain', 'dc');
    map { &_get_values($entry, \%domain, $_) }
        qw /isActive owner/;
    if (defined $domain{owner}) {
        $domain{owner} = [ $domain{owner} ]
            unless (ref $domain{owner}) eq 'ARRAY';
        $domain{owner} = [ map { Fripost::Schema::Utils::fromDN($_) }
                               @{$domain{owner}} ];
    }
    return \%domain;
}

sub _aliasEntry {
    my $entry = shift;
    my %alias;
    my $domain = (split /=/, (split /,/, $entry->dn, 3)[1], 2)[1];

    &_get_values( $entry, \%alias, 'address', 'mailLocalAddress');
    if (defined $alias{address}) {
        $alias{address} = [ $alias{address} ]
            unless (ref $alias{address}) eq 'ARRAY';
        $alias{address} = [ map { $_ . '@' . $domain }
                                @{$alias{address}} ];
    }
    &_get_values( $entry, \%alias, 'goto', 'mailTarget');
    &_get_values( $entry, \%alias, 'isActive');
    return \%alias;
}

sub _get_values {
    my ($entry, $h, $attr, $attr2) = @_;
    $attr2 //= $attr;
    my $values = $entry->get_value ( $attr2, asref => 1 );

    return unless defined $values;
    if ($#$values == 0) {
        $h->{$attr} = $values->[0];
    }
    else {
        $h->{$attr} = $values;
    }
}

sub _get_dn {
    return [split ',', $_[0]->dn()];
}


=head1 NAME

Fripost::Schema::Search - Class for the result of LDAP queries.

=head1 AUTHOR

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

=head1 COPYRIGHT

Copyright 2012 Guilhem Moulin, all rights reserved.

=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 of Search.pm

__END__