| 1 | package Mail::MtPolicyd::Plugin::SqlUserConfig; | ||||||||||||||||||||||||||||
| 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 the user config of a user | ||||||||||||||||||||||||||||
| 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 | 2 | ▶ | use JSON; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 2 | has 'sql_query' => ( | |||||||||||||||||||||||||||
| 16 | is => 'rw', isa => 'Str', | ||||||||||||||||||||||||||||
| 17 | default => 'SELECT config FROM user_config WHERE address=?', | ||||||||||||||||||||||||||||
| 18 | ); | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | has '_json' => ( | ||||||||||||||||||||||||||||
| 21 | is => 'ro', isa => 'JSON', lazy => 1, | ||||||||||||||||||||||||||||
| 22 | default => sub { | ||||||||||||||||||||||||||||
| 23 | 1 | ▶ | return JSON->new; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 24 | } | ||||||||||||||||||||||||||||
| 25 | 2 | ); | |||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 2 | has 'field' => ( is => 'rw', isa => 'Str', default => 'recipient' ); | |||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 2 | with 'Mail::MtPolicyd::Role::Connection' => { | |||||||||||||||||||||||||||
| 30 | name => 'db', | ||||||||||||||||||||||||||||
| 31 | type => 'Sql', | ||||||||||||||||||||||||||||
| 32 | }; | ||||||||||||||||||||||||||||
| 33 | 2 | with 'Mail::MtPolicyd::Plugin::Role::SqlUtils'; | |||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | sub _get_config { | ||||||||||||||||||||||||||||
| 36 | 1 | ▶ | my ( $self, $r ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_get_config: called CC=4 SCAR=15.8
| |||||||||||||||||||||||||||||
| 37 | 1 | my $key = $r->attr( $self->field ); | |||||||||||||||||||||||||||
| 38 | 1 | ▶ | if( ! defined $key || $key =~ /^\s*$/ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not defined $key or $key =~ /^\s*$/
MC/DC0%0/2
not defined $key or $key =~ /^\s*$/
not defined $key
$key =~ /^\s*$/
Truth table33%1/3
not(A) or B
A= not defined $keyB= $key =~ /^\s*$/
| |||||||||||||||||||||||||||||
| 39 | 0 | die('key field '.$self->field.' not defined or empty in request'); | |||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 1 | my $sth = $self->execute_sql( $self->sql_query, $key ); | |||||||||||||||||||||||||||
| 43 | 1 | my ( $json ) = $sth->fetchrow_array; | |||||||||||||||||||||||||||
| 44 | 1 | ▶ | if( ! defined $json ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 45 | 0 | die( 'no user-config found for '.$key ); | |||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||
| 47 | 1 | return $self->_json->decode( $json ); | |||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | sub run { | ||||||||||||||||||||||||||||
| 51 | 1 | ▶ | my ( $self, $r ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
run: called CC=3 SCAR=11.4
Pod
documented
| |||||||||||||||||||||||||||||
| 52 | 1 | my $config; | |||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 1 | eval { $config = $self->_get_config($r) }; | |||||||||||||||||||||||||||
| 55 | 1 | ▶ | if( $@ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 56 | 0 | $self->log($r, 'unable to retrieve user-config: '.$@); | |||||||||||||||||||||||||||
| 57 | 0 | return; | |||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 1 | foreach my $key ( keys %$config ) { | |||||||||||||||||||||||||||
| 61 | 1 | $r->session->{$key} = $config->{$key}; | |||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 1 | return; | |||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 2 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 2 | 1; | |||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | __END__ | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | =pod | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | =encoding UTF-8 | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | =head1 NAME | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | Mail::MtPolicyd::Plugin::SqlUserConfig - mtpolicyd plugin for retrieving the user config of a user | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | =head1 VERSION | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | version 2.01 | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | This plugin will retrieve a JSON string from an SQL database and will merge the data structure | ||||||||||||||||||||||||||||
| 89 | into the current session. | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | This could be used to retrieve configuration values for users from a database. | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | =head1 PARAMETERS | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | =over | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | =item sql_query (default: SELECT config FROM user_config WHERE address=?) | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | The SQL query to retrieve the JSON configuration string. | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | The content of the first row/column is used. | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | =item field (default: recipient) | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | The request field used in the sql query to retrieve the user configuration. | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | =back | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | =head1 EXAMPLE USER SPECIFIC GREYLISTING | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | Create the following table in the SQL database: | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | CREATE TABLE `user_config` ( | ||||||||||||||||||||||||||||
| 114 | `id` int(11) NOT NULL AUTO_INCREMENT, | ||||||||||||||||||||||||||||
| 115 | `address` varchar(255) DEFAULT NULL, | ||||||||||||||||||||||||||||
| 116 | `config` TEXT NOT NULL, | ||||||||||||||||||||||||||||
| 117 | PRIMARY KEY (`id`), | ||||||||||||||||||||||||||||
| 118 | UNIQUE KEY `address` (`address`) | ||||||||||||||||||||||||||||
| 119 | ) ENGINE=MyISAM DEFAULT CHARSET=latin1 | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | INSERT INTO TABLE `user_config` VALUES( NULL, 'karlson@vomdach.de', '{"greylisting":"on"}' ); | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | In mtpolicyd.conf: | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | db_dsn="dbi:mysql:mail" | ||||||||||||||||||||||||||||
| 126 | db_user=mail | ||||||||||||||||||||||||||||
| 127 | db_password=password | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | <Plugin user-config> | ||||||||||||||||||||||||||||
| 130 | module = "SqlUserConfig" | ||||||||||||||||||||||||||||
| 131 | sql_query = "SELECT config FROM user_config WHERE address=?" | ||||||||||||||||||||||||||||
| 132 | </Plugin> | ||||||||||||||||||||||||||||
| 133 | <Plugin greylist> | ||||||||||||||||||||||||||||
| 134 | enabled = "off" # off by default | ||||||||||||||||||||||||||||
| 135 | uc_enabled = "greylisting" # override with value of key 'greylisting' is set in session | ||||||||||||||||||||||||||||
| 136 | module = "Greylist" | ||||||||||||||||||||||||||||
| 137 | score = -5 | ||||||||||||||||||||||||||||
| 138 | mode = "passive" | ||||||||||||||||||||||||||||
| 139 | </Plugin> | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | Markus Benning <ich@markusbenning.de> | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | This software is Copyright (c) 2014 by Markus Benning <ich@markusbenning.de>. | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | This is free software, licensed under: | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | The GNU General Public License, Version 2, June 1991 | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | =cut | ||||||||||||||||||||||||||||