diff options
author | Julian Blake Kongslie <jblake@omgwallhack.org> | 2010-03-18 00:11:52 -0700 |
---|---|---|
committer | Julian Blake Kongslie <jblake@omgwallhack.org> | 2010-03-18 00:11:52 -0700 |
commit | b09b9989b43f6b015f0fac9670c1180e97b67972 (patch) | |
tree | 92887ebbd8ecf4d37cc929a735e2c77ee163f692 /usr/src/libpiny/lib/Piny/Group.pm | |
parent | 5e09783cda2e4cd08daf2b2024af1f8f3d9b7a98 (diff) | |
download | piny-code-b09b9989b43f6b015f0fac9670c1180e97b67972.tar.gz piny-code-b09b9989b43f6b015f0fac9670c1180e97b67972.zip |
Switched to Module::Build instead of hardcoding paths.
Diffstat (limited to 'usr/src/libpiny/lib/Piny/Group.pm')
-rw-r--r-- | usr/src/libpiny/lib/Piny/Group.pm | 108 |
1 files changed, 108 insertions, 0 deletions
diff --git a/usr/src/libpiny/lib/Piny/Group.pm b/usr/src/libpiny/lib/Piny/Group.pm new file mode 100644 index 0000000..4b957a7 --- /dev/null +++ b/usr/src/libpiny/lib/Piny/Group.pm @@ -0,0 +1,108 @@ +# Copyright © 2010 Julian Blake Kongslie <jblake@omgwallhack.org> +# Licensed under the BSD 3-clause license. + +package Piny::Group; + +use Moose; + +use Piny::User; + +# Attributes + +has 'gid' => + ( is => 'ro' + , isa => 'Int' + , lazy_build => 1 + ); + +has 'groupname' => + ( is => 'ro' + , isa => 'Str' + , lazy_build => 1 + ); + +has 'grent' => + ( is => 'ro' + , isa => 'ArrayRef' + , lazy_build => 1 + , init_arg => undef + ); + +has 'members' => + ( is => 'ro' + , isa => 'ArrayRef[Piny::User]' + , lazy_build => 1 + , init_arg => undef + ); + +# Builder methods + +# If constructed with just one argument, then +# * If that argument is numeric, treat it as a UID. +# * Otherwise, treat it as a username. +around BUILDARGS => sub { + my ( $orig, $class ) = ( shift, shift ); + + if ( @_ == 1 && ! ref $_[0] ) { + if ( $_[0] =~ m/^\d+$/ ) { + return $class->$orig( gid => $_[0] ); + } else { + return $class->$orig( groupname => $_[0] ); + }; + } else { + return $class->$orig( @_ ); + }; +}; + +sub BUILD { + my ( $s ) = @_; + + if ( not ( $s->has_gid( ) or $s->has_groupname( ) ) ) { + die "You must provide either GID or groupname!"; + }; + + if ( $s->has_gid( ) and $s->has_groupname( ) ) { + die "You must not provide both GID and groupname!"; + }; +}; + +sub _build_gid { + my ( $s ) = @_; + + return $s->grent( )->[2]; +}; + +sub _build_groupname { + my ( $s ) = @_; + + return $s->grent( )->[0]; +}; + +sub _build_grent { + my ( $s ) = @_; + + if ( $s->has_gid( ) ) { + my @res = getgrgid( $s->gid( ) ); + die "getgrgid( " . $s->gid( ) . " ) failed: $!" unless @res; + return \@res; + } elsif ( $s->has_groupname( ) ) { + my @res = getgrnam( $s->groupname( ) ); + die "getgrnam( " . $s->groupname( ) . " ) failed: $!" unless @res; + return \@res; + } else { + die "Not enough information provided to lookup group!"; + }; +}; + +sub _build_members { + my ( $s ) = @_; + + return [ ] if ( $s->grent( )->[3] eq "" ); + return [ map { Piny::User->new( username => $_ ) } split( /:/, $s->grent( )->[3] ) ]; +}; + +# Moose boilerplate + +__PACKAGE__->meta->make_immutable; + +1; |