MtPolicyd/Plugin/LdapUserConfig.pm

filter: Statementstmt 88.1% 52 / 59 Branchbran 50.0% 5 / 10 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 7 / 7 Podpod 50.0% 1 / 2 totaltotal 79.5% 66 / 83 CC 8 SCAR 12.9
CC
8
Cov
81%
run
5.5
retrieve_ldap_entry
4.5
BEGIN
1.0
CRAP 3.6
« MtPolicyd/Plugin/Role/ConfigurableFields.pm ↑ Summary MtPolicyd/Plugin/Accounting.pm »
1 package Mail::MtPolicyd::Plugin::LdapUserConfig;
2  
3 2 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 our $VERSION = '2.01'; # VERSION
7 # ABSTRACT: mtpolicyd plugin for retrieving per user configuration from LDAP
8  
9 2 extends 'Mail::MtPolicyd::Plugin';
10  
11  
12 2 ▶use Mail::MtPolicyd::Plugin::Result;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 2 ▶use Net::LDAP::Util qw( escape_filter_value );
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 2 has 'basedn' => ( is => 'rw', isa => 'Str', default => '' );
17  
18 2 has 'filter' => ( is => 'rw', isa => 'Str', required => 1 );
19  
20 2 with 'Mail::MtPolicyd::Plugin::Role::ConfigurableFields' => {
21 'fields' => {
22 'filter' => {
23 isa => 'Str',
24 default => 'sasl_username',
25 value_isa => 'Str',
26 },
27 },
28 };
29  
30  
31 2 has 'config_fields' => ( is => 'rw', isa => 'Str', required => 1 );
32  
33 has '_config_fields' => (
34 is => 'ro', isa => 'ArrayRef[Str]', lazy => 1,
35 default => sub {
36 1 ▶ my $self = shift;
Subroutine __ANON__: called CC=1 SCAR=0.0
37 1 return [ split(/\s*,\s*/, $self->config_fields ) ];
38 },
39 2 );
40  
41 2 has 'connection' => ( is => 'ro', isa => 'Str', default => 'ldap' );
42 2 has 'connection_type' => ( is => 'ro', isa => 'Str', default => 'Ldap' );
43  
44 2 with 'Mail::MtPolicyd::Role::Connection' => {
45 name => 'ldap',
46 type => 'Ldap',
47 };
48  
49 sub retrieve_ldap_entry {
50 1 ▶ my ( $self, $r ) = @_;
Subroutine retrieve_ldap_entry: called CC=4 SCAR=15.1
Pod undocumented
51 1 my $ldap = $self->_ldap_handle;
52  
53 1 my $value = $self->get_filter_value( $r );
54 1 ▶ if( ! defined $value ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $value) 0 1 1
55 0 $self->log( $r, 'filter_field('.$self->filter_field.') is not defined in request. skipping ldap search.');
56 0 return;
57 }
58 1 my $filter = $self->filter;
59 1 my $filter_value = escape_filter_value($value);
60 1 $filter =~ s/%s/$filter_value/g;
61 1 $self->log( $r, 'ldap filter is: '.$filter);
62  
63 1 my $msg;
64 1 eval {
65 1 $msg = $ldap->search(
66 base => $self->basedn,
67 filter => $filter,
68 );
69 };
70 1 ▶ if( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 1 1
71 0 $self->log( $r, 'ldap search failed: '.$@ );
72 0 return;
73 }
74 1 ▶ if( $msg->count != 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($msg->count != 1) 0 1 1
75 0 $self->log( $r, 'ldap search return '.$msg->count.' entries' );
76 0 return;
77 }
78  
79 1 my $entry = $msg->entry( 0 );
80 1 $self->log( $r, 'found in ldap: '.$entry->dn );
81  
82 1 return $entry;
83 }
84  
85 sub run {
86 1 ▶ my ( $self, $r ) = @_;
Subroutine run: called CC=5 SCAR=17.1
Pod documented
87  
88 1 my $entry = $self->retrieve_ldap_entry( $r );
89 1 ▶ if( defined $entry ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $entry) 1 0 1
90 1 foreach my $field ( @{$self->_config_fields} ) {
91 3 my ($value) = $entry->get_value( $field );
92 3 ▶ if( defined $value && $value ne '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value and $value ne '') { } 3 0 3
Condition33%1/3
defined $value and $value ne ""
!ll&&!rl&&r
003
MC/DC0%0/2
defined $value and $value ne ""
defined $value $value ne ""
Truth table33%1/3
A and B
A= defined $valueB= $value ne ""
ABresult
0X0
100
111
93 3 $self->log( $r, 'retrieved ldap attribute: '.$field.'='.$value );
94 3 $r->session->{$field} = $value;
95 } else {
96 0 $self->log( $r, 'LDAP attribute '.$field.' is empty. skipping.' );
97 }
98 }
99 }
100  
101 1 return;
102 }
103  
104 2 __PACKAGE__->meta->make_immutable;
105  
106 2 1;
107  
108 __END__
109  
110 =pod
111  
112 =encoding UTF-8
113  
114 =head1 NAME
115  
116 Mail::MtPolicyd::Plugin::LdapUserConfig - mtpolicyd plugin for retrieving per user configuration from LDAP
117  
118 =head1 VERSION
119  
120 version 2.01
121  
122 =head1 SYNOPSIS
123  
124 ldap_host="localhost"
125 ldap_binddn="cn=readonly,dc=domain,dc=com"
126 ldap_password="secret"
127  
128 <Plugin user_config>
129 module="LdapUserConfig"
130 basedn="ou=users,dc=domain,dc=com"
131 filter="(mail=%s)"
132 filter_field="sasl_username"
133 config_fields="mailMessageLimit,mailSendExternal"
134 </Plugin>
135  
136 =head1 DESCRIPTION
137  
138 This plugin could be used to retrieve session variables/user configuration
139 from a LDAP server.
140  
141 =head1 PARAMETERS
142  
143 The LDAP connection must be configured in the global configuration section
144 of mtpolicyd. See L<mtpolicyd>.
145  
146 =over
147  
148 =item basedn (default: '')
149  
150 The basedn to use for the search.
151  
152 =item filter (required)
153  
154 The filter to use for the search.
155  
156 The pattern %s will be replaced with the content of filter_field.
157  
158 =item filter_field (required)
159  
160 The content of this request field will be used to replace %s in the
161 filter string.
162  
163 =item config_fields (required)
164  
165 A comma seperated list of LDAP attributes to retrieve and
166 copy into the current mtpolicyd session.
167  
168 =back
169  
170 =head1 AUTHOR
171  
172 Markus Benning <ich@markusbenning.de>
173  
174 =head1 COPYRIGHT AND LICENSE
175  
176 This software is Copyright (c) 2014 by Markus Benning <ich@markusbenning.de>.
177  
178 This is free software, licensed under:
179  
180 The GNU General Public License, Version 2, June 1991
181  
182 =cut
« MtPolicyd/Plugin/Role/ConfigurableFields.pm ↑ Summary MtPolicyd/Plugin/Accounting.pm »