Bot-ChatBots
view release on metacpan or search on metacpan
lib/Bot/ChatBots/Auth.pod view on Meta::CPAN
=head1 DESCRIPTION
This class provides you a simple authorization mechanism for blocking or
allowing records based on specific internal fields.
Two mechanisms are provided: I<blacklisting> and I<whitelisting>. These
two mechanisms can be applied to either I<users> identifiers or to
I<channel> identifiers.
I<Blacklist>s are hash references whose keys mark elements that are
blocked. I<Whitelist>s are hash references whose keys mark elements that
are allowed. Most probably you will just want to use only one of the two,
because:
=over
*
whatever is blacklisted is blocked, even if it is then whitelisted;
*
if you pass any whitelist (even an empty one), everything not contained in
it as a key is automatically blocked.
=back
It can make sense to use one mechanism with users and the other one with
channels though.
This module logs the operation on the log channel (acquired via
L<Log::Any>) at the INFO level.
t/02-auth.t view on Meta::CPAN
@retval = $auth->process({sender => {id => 'a'}});
ok !scalar(@retval), 'no channel id';
@retval = $auth->process({channel => {id => 'b'}});
ok !scalar(@retval), 'no sender id';
};
subtest blacklist => sub {
@retval = $auth->process({sender => {id => 'y'}});
ok !scalar(@retval), 'user y is blocked (blacklist)';
@retval =
$auth->process({sender => {id => 'z'}, channel => {id => 'x'}});
ok !scalar(@retval), 'user z channel x is blocked (channel blacklist)';
@retval =
$auth->process({sender => {id => 'z'}, channel => {id => 'z'}});
ok scalar(@retval), 'user z channel z goes';
};
subtest whitelist => sub {
$auth->users({whitelist => {z => 1}});
$auth->channels({whitelist => {z => 1}});
@retval = $auth->process({sender => {id => 'y'}});
ok !scalar(@retval), 'user y is blocked (blacklist)';
@retval =
$auth->process({sender => {id => 'z'}, channel => {id => 'x'}});
ok !scalar(@retval), 'user z channel x is blocked (channel blacklist)';
@retval =
$auth->process({sender => {id => 'z'}, channel => {id => 'z'}});
ok scalar(@retval), 'user z channel z goes';
};
done_testing();
( run in 0.457 second using v1.01-cache-2.11-cpan-49f99fa48dc )