--- a/lib/Ius/Dav/Htpasswd.pm Fri Jul 22 09:38:29 2011 +0200
+++ b/lib/Ius/Dav/Htpasswd.pm Fri Sep 06 13:56:57 2013 +0200
@@ -42,6 +42,29 @@
@EXPORT_OK = qw(readconfig mkpasswd useradd userdel userexpiry usage);
}
+use constant {
+
+ E_USER_NONALNUM => 1,
+ E_EXPIRY_NONNUM => 2,
+ E_EXPIRY_TOOSHORT => 3,
+ E_EXPIRY_TOOLONG => 4,
+
+};
+
+# can't use constants in the declaration in which they are defined
+use constant {
+ ERRSTR => {
+
+ E_USER_NONALNUM, 'Invalid character in username.',
+ E_EXPIRY_NONNUM, 'Expiry does not look like a positive integer.',
+ E_EXPIRY_TOOSHORT, 'Expiry too short.',
+ E_EXPIRY_TOOLONG, 'Expiry too long.'
+
+ }
+};
+
+
+
sub usage {
use Pod::Usage;
@@ -51,9 +74,26 @@
}
+sub new {
+
+ my ($class, $self) = (shift, {});
+ $self->{ERRNO} = undef;
+ bless $self, $class;
+ return $self;
+}
+
+sub conf {
+
+ my $self = shift;
+ return { $self->{CONF}->varlist('.') };
+
+}
+
sub readconfig {
- my $conf = new AppConfig(
+ my $self = shift;
+
+ $self->{CONF} = new AppConfig(
qw(
expiry=i
expiry_min=i
@@ -66,24 +106,42 @@
www_group=s
master_user=s)
) or die 'Failed to read config!';
- $conf->file($_)
+ $self->{CONF}->file($_)
for grep -e, map "$_/ius-dav-htpasswd.conf",
qw(/etc/ius-dav-htpasswd /usr/local/etc/ius-dav-htpasswd ~/.ius-dav-htpasswd .);
- return { $conf->varlist('.') };
+
+ return 1;
+
+}
+
+sub seterrno {
+
+ my ($self, $e) = @_;
+
+ $self->{ERRNO} = $e;
+ return;
+
+}
+
+sub errstr {
+
+ my $self = shift;
+ return ERRSTR->{$self->{ERRNO}};
}
sub validate {
- my ( $conf, $user, $expiry ) = @_;
+ my ( $self, $user, $expiry ) = @_;
+ my $conf = $self->conf;
- return unless $user =~ /^[[:alnum:]_]+$/;
+ return $self->seterrno(E_USER_NONALNUM) unless $user =~ /^[[:alnum:]_]+$/;
if ( defined $expiry ) {
- return unless $expiry =~ /^[0-9]+$/;
- return
- unless $expiry >= $conf->{expiry_min}
- and $expiry <= $conf->{expiry_max};
+
+ return $self->seterrno(E_EXPIRY_NONNUM) unless $expiry =~ /^[0-9]+$/;
+ return $self->seterrno(E_EXPIRY_TOOSHORT) unless $expiry >= $conf->{expiry_min};
+ return $self->seterrno(E_EXPIRY_TOOLONG) unless $expiry <= $conf->{expiry_max};
}
return 1;
@@ -92,7 +150,8 @@
sub useradd {
- my ( $conf, $user, $pass, $expiry ) = @_;
+ my ( $self, $user, $pass, $expiry ) = @_;
+ my $conf = $self->conf;
for (
qw(expiry expiry_min expiry_max dav_base_local htpasswd conf_d www_user www_group)
@@ -103,7 +162,7 @@
}
$expiry = $conf->{expiry} unless defined $expiry and $expiry ne '';
- die 'Invalid input' unless validate $conf, $user, $expiry;
+ die $self->errstr unless $self->validate($user, $expiry);
my $user_dir = "$conf->{dav_base_local}/$user";
mkdir "$user_dir" or die "Can't mkdir '$user_dir': $!";
@@ -165,7 +224,8 @@
sub userdel {
- my ( $conf, $user ) = @_;
+ my ( $self, $user ) = @_;
+ my $conf = $self->conf;
my $rc = 0;
@@ -201,7 +261,8 @@
sub userexpiry {
- my ($conf) = @_;
+ my $self = shift;
+ my $conf = $self->conf;
my $rc = 0;
@@ -218,7 +279,7 @@
for my $u (@users) {
if ( my $e = $htpasswd->fetchInfo($u) ) {
- userdel( $conf, $u )
+ $self->userdel( $u )
and warn "Error(s) occured during 'userdel $conf, $u'\n"
if $now >= $e;
}