From: Matt S Trout Date: Sun, 15 Jul 2012 15:25:01 +0000 (+0000) Subject: UserStore and User classes X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=5b26866ea3dc1b51e669b9e213d002c05de81e14;p=scpubgit%2FApp-EzPz.git UserStore and User classes --- diff --git a/bin/ezpz-admin-repl b/bin/ezpz-admin-repl new file mode 100755 index 0000000..0ba5445 --- /dev/null +++ b/bin/ezpz-admin-repl @@ -0,0 +1,23 @@ +#!/usr/bin/env perl + +use File::Which; +use App::EzPz::UserStore; +use strictures 1; + +die "Usage: ezpz-admin-repl ezmlm-bindir htpasswd-file" + unless @ARGV == 2; + +{ + package Eval::WithLexicals::Scratchpad; + + use vars qw($users); + + $users = App::EzPz::UserStore->new( + ezmlm_bindir => $ARGV[0], + htpasswd_file => $ARGV[1], + ); +} + +do +which('tinyrepl'); + +exit 0; diff --git a/lib/App/EzPz/User.pm b/lib/App/EzPz/User.pm new file mode 100644 index 0000000..e3d843b --- /dev/null +++ b/lib/App/EzPz/User.pm @@ -0,0 +1,21 @@ +package App::EzPz::User; + +use Authen::Htpasswd::User; +use Moo; + +has _htpasswd_user => ( + is => 'ro', init_arg => 'htpasswd_user', required => 1, + handles => [ qw(username password check_password) ], +); + +around BUILDARGS => sub { + my ($orig, $self) = (shift, shift); + my $args = $self->$orig(@_); + $args->{htpasswd_user} ||= + Authen::Htpasswd::User->new( + delete @{$args}{qw(username password)} + ); + return $args +}; + +1; diff --git a/lib/App/EzPz/UserStore.pm b/lib/App/EzPz/UserStore.pm new file mode 100644 index 0000000..8b0a557 --- /dev/null +++ b/lib/App/EzPz/UserStore.pm @@ -0,0 +1,64 @@ +package App::EzPz::UserStore; + +use App::EzPz::User; +use Scalar::Util 'blessed'; +use Authen::Htpasswd; +use Moo; + +has ezmlm_bindir => (is => 'ro', required => 1); + +has htpasswd_file => (is => 'ro', required => 1); + +has _htpasswd => (is => 'lazy'); + +sub _build__htpasswd { + my ($self) = @_; + return Authen::Htpasswd->new($self->htpasswd_file); +} + +sub all { + my ($self) = @_; + return map $self->_inflate_user($_), $self->_htpasswd->all_users; +} + +sub get { + my ($self, $name) = @_; + if (my $htp_user = $self->_htpasswd->lookup_user($name)) { + return $self->_inflate_user($htp_user); + } else { + return undef; + } +} + +sub add { + my ($self, $user) = @_; + unless (blessed($user)) { + $user = App::EzPz::User->new($user); + } + my $htp_file = $self->_htpasswd; + my $htp_user = $user->_htpasswd_user; + $htp_file->add_user($htp_user); + $htp_user->file($htp_file); + return $user; +} + +sub remove { + my ($self, $user) = @_; + $self->_htpasswd->remove_user($user->_htpasswd_user); + return $user; +} + +sub _inflate_user { + my ($self, $htp_user) = @_; + return App::EzPz::User->new( + htpasswd_user => $htp_user, + ); +} + +sub check_password { + my ($self, $name, $password) = @_; + return unless my $user = $self->get($name); + return $user->check_password($password); +} + +1;