a4da2f1195c11af17561d5a281752560f55fd9c5
[scpubgit/App-EzPz.git] / lib / App / EzPz / User.pm
1 package App::EzPz::User;
2
3 use Module::Runtime qw(use_module);
4 use Moo;
5
6 has _htpasswd_user => (
7   is => 'ro', init_arg => 'htpasswd_user', required => 1,
8   handles => [ qw(username password check_password) ],
9 );
10
11 has ezmlm_bindir => (is => 'rwp');
12
13 around BUILDARGS => sub {
14   my ($orig, $self) = (shift, shift);
15   my $args = $self->$orig(@_);
16   $args->{htpasswd_user} ||=
17     use_module('Authen::Htpasswd::User')->new(
18       delete @{$args}{qw(username password)}
19     );
20   return $args
21 };
22
23 sub list_names {
24   my ($self) = @_;
25   if (my $unsplit = ($self->_htpasswd_user->extra_info||[])->[0]) {
26     return split /\s*,\s*/, $unsplit;
27   } else {
28     return ();
29   }
30 }
31
32 sub set_list_names {
33   my ($self, @names) = @_;
34   my @extra = @{$self->_htpasswd_user->extra_info||[]};
35   $extra[0] = join(', ', @names);
36   $self->_htpasswd_user->extra_info(@extra);
37   return;
38 }
39
40 sub add_list_name {
41   my ($self, $name) = @_;
42   my %names; @names{my @names = $self->list_names} = ();
43   $self->set_list_names(@names, $name) unless exists $names{$name};
44   return $name;
45 }
46
47 sub remove_list_name {
48   my ($self, $name) = @_;
49   my %names; @names{my @names = $self->list_names} = ();
50   if (exists $names{$name}) {
51     $self->set_list_names(grep !($_ eq $name), $self->list_names)
52   }
53   return $name;
54 }
55
56 1;