summaryrefslogtreecommitdiff
path: root/libpiny/lib/Piny/User.pm
blob: 90742e76027a11f865ef2710193ce30d6c8b7c24 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
# Copyright © 2010 Julian Blake Kongslie <jblake@omgwallhack.org>
# Licensed under the BSD 3-clause license.

use strict;
use warnings;

package Piny::User;

use Moose;
use Moose::Util::TypeConstraints;
use MooseX::StrictConstructor;

use Piny::Config;
use Piny::Email;
use Piny::Group;

# Types

subtype 'Username'
  => as 'Str'
  => where { $_ =~ /^(?!(git|iki)-)[a-zA-Z][a-zA-Z0-9_.-]{0,30}$/ }
  => message { if ( /^((?:git|iki)-|[^a-zA-Z])/ ) { "Usernames are not allowed to begin with $1" } elsif ( /([^a-zA-Z0-9_.-])/ ) { "Usernames are not allowed to contain $1" } elsif ( /[a-zA-Z0-9_.-]{32,}/ ) { "Usernames are not allowed to be more than 31 bytes" } else { "Invalid username" } }
  ;

# Attributes

has 'uid' =>
  ( is          => 'ro'
  , isa         => 'Int'
  , lazy_build  => 1
  );

has 'name' =>
  ( is          => 'ro'
  , isa         => 'Username'
  , lazy_build  => 1
  );

has 'pwent' =>
  ( is          => 'ro'
  , isa         => 'ArrayRef'
  , lazy_build  => 1
  , init_arg    => undef
  );

has 'password_hash' =>
  ( is          => 'ro'
  , isa         => 'Str'
  , lazy_build  => 1
  , init_arg    => undef
  );

has 'home' =>
  ( is          => 'ro'
  , isa         => 'Path'
  , lazy_build  => 1
  , init_arg    => undef
  );

has 'config' =>
  ( is          => 'ro'
  , isa         => 'Piny::Config'
  , lazy_build  => 1
  , init_arg    => undef
  );

has 'email' =>
  ( is          => 'ro'
  , isa         => 'Piny::Email'
  , lazy_build  => 1
  , init_arg    => undef
  );

has 'groups' =>
  ( is          => 'ro'
  , isa         => 'ArrayRef[Piny::Group]'
  , lazy_build  => 1
  , init_arg    => undef
  );

# Public methods

sub add_group {
  my ( $s, @groups ) = @_;

  foreach my $group ( @groups ) {
    $group->add_member( $s );
  };
};

sub remove_group {
  my ( $s, @groups ) = @_;

  foreach my $group ( @groups ) {
    $group->remove_member( $s );
  };
};

sub has_group {
  my ( $s, $group ) = @_;

  foreach my $owngroup ( @{$s->groups( )} ) {
    return 1 if $owngroup->gid( ) == $group->gid( );
  };

  return;
};

# Class methods

sub all_users {
  my ( $class ) = @_;

  my @ret;

  endpwent( );

  while ( my @info = getpwent( ) ) {
    eval {
      my $user = $class->new( uid => $info[2] );
      # Some forced early evaluation, so error checking happens now.
      $user->name( );
      $user->email( );
      push( @ret, $user );
    };
  };

  endpwent( );

  return @ret;
};

# 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( uid => $_[0] );
    } else {
      return $class->$orig( name => $_[0] );
    };
  } else {
    return $class->$orig( @_ );
  };
};

sub BUILD {
  my ( $s ) = @_;

  if ( not ( $s->has_uid( ) or $s->has_name( ) ) ) {
    die "You must provide either UID or name!";
  };

  if ( $s->has_uid( ) and $s->has_name( ) ) {
    die "You must not provide both UID and name!";
  };
};

sub _build_uid {
  my ( $s ) = @_;

  return $s->pwent( )->[2];
};

sub _build_name {
  my ( $s ) = @_;

  return $s->pwent( )->[0];
};

sub _build_pwent {
  my ( $s ) = @_;

  if ( $s->has_uid( ) ) {
    my @res = getpwuid( $s->uid( ) );
    die "getpwuid( " . $s->uid( ) . " ) failed: $!" unless @res;
    return \@res;
  } elsif ( $s->has_name( ) ) {
    my @res = getpwnam( $s->name( ) );
    die "getpwnam( " . $s->name( ) . " ) failed: $!" unless @res;
    return \@res;
  } else {
    die "Not enough information provided to lookup user!";
  };
};

sub _build_password_hash {
  my ( $s ) = @_;

  return $s->pwent( )->[1];
};

sub _build_home {
  my ( $s ) = @_;

  return $s->pwent( )->[7];
};

sub _build_config {
  my ( $s ) = @_;

  return Piny::Config->new( confpath => $s->home . "/.gitconfig" );
};

sub _build_email {
  my ( $s ) = @_;

  if ( not defined $s->config->user_email ) {
    die "You must provide a user.email attribute in your .gitconfig!\nPlease run pinyconfig --user user.email your\@email.com";
  };

  return Piny::Email->new( address => $s->config->user_email );
};

sub _build_groups {
  my ( $s ) = @_;

  my @res;
  my @ent;

  endgrent( );

  while ( @ent = getgrent( ) ) {
    next if ( $ent[3] eq "" );
    foreach my $member ( split( / /, $ent[3] ) ) {
      if ( $member eq $s->name( ) ) {
        push @res, Piny::Group->new( gid => $ent[2] );
        last;
      };
    };
  };

  endgrent( );

  return \@res;
};

# Moose boilerplate

__PACKAGE__->meta->make_immutable;

1;