[29] | 1 | package LATMOS::Accounts::Bases::Sql::User; |
---|
[19] | 2 | |
---|
| 3 | use 5.010000; |
---|
| 4 | use strict; |
---|
| 5 | use warnings; |
---|
| 6 | |
---|
[29] | 7 | use base qw(LATMOS::Accounts::Bases::Sql::objects); |
---|
[19] | 8 | |
---|
| 9 | our $VERSION = (q$Rev$ =~ /^Rev: (\d+) /)[0]; |
---|
| 10 | |
---|
| 11 | =head1 NAME |
---|
| 12 | |
---|
| 13 | LATMOS::Ad - Perl extension for blah blah blah |
---|
| 14 | |
---|
| 15 | =head1 SYNOPSIS |
---|
| 16 | |
---|
| 17 | use LATMOS::Accounts::Bases; |
---|
[20] | 18 | my $base = LATMOS::Accounts::Bases->new('sql'); |
---|
[19] | 19 | ... |
---|
| 20 | |
---|
| 21 | =head1 DESCRIPTION |
---|
| 22 | |
---|
| 23 | Account base access over standard unix file format. |
---|
| 24 | |
---|
| 25 | =head1 FUNCTIONS |
---|
| 26 | |
---|
| 27 | =cut |
---|
| 28 | |
---|
| 29 | =head2 new(%options) |
---|
| 30 | |
---|
| 31 | Create a new LATMOS::Ad object for windows AD $domain. |
---|
| 32 | |
---|
| 33 | domain / server: either the Ad domain or directly the server |
---|
| 34 | |
---|
| 35 | ldap_args is an optionnal list of arguments to pass to L<Net::LDAP>. |
---|
| 36 | |
---|
| 37 | =cut |
---|
| 38 | |
---|
[86] | 39 | sub object_table { 'user' } |
---|
[19] | 40 | |
---|
[29] | 41 | sub key_field { 'login' } |
---|
| 42 | |
---|
[74] | 43 | sub has_extended_attributes { 1 } |
---|
| 44 | |
---|
[92] | 45 | sub _delayed_fields { |
---|
| 46 | my ($self)= @_; |
---|
| 47 | return qw(memberOf); |
---|
| 48 | } |
---|
| 49 | |
---|
[57] | 50 | sub _inline_fields { |
---|
| 51 | my ($self, $for) = @_; |
---|
[95] | 52 | return { |
---|
| 53 | %{ $self->_initial_fields($for) }, |
---|
| 54 | memberOf => 'memberOf', |
---|
| 55 | } |
---|
| 56 | } |
---|
| 57 | |
---|
| 58 | sub _initial_fields { |
---|
| 59 | my ($self, $for) = @_; |
---|
[59] | 60 | my %fields = ( |
---|
[57] | 61 | uidNumber => 'uid', |
---|
| 62 | gidNumber => 'gid', |
---|
[114] | 63 | Expire => 'expire', |
---|
[66] | 64 | (($for !~ /w/) |
---|
| 65 | ? ( |
---|
| 66 | gecos => 'gecos', |
---|
| 67 | sAMAccountName => 'sAMAccountName', |
---|
[68] | 68 | uid => 'login', |
---|
[114] | 69 | accountExpires => 'accountExpires', |
---|
| 70 | shadowExpire => 'shadowExpire', |
---|
[66] | 71 | ) |
---|
[57] | 72 | : ()), |
---|
[59] | 73 | ); |
---|
| 74 | \%fields |
---|
[57] | 75 | } |
---|
| 76 | |
---|
[52] | 77 | sub get_field { |
---|
| 78 | my ($self, $field) = @_; |
---|
| 79 | if ($field eq 'gecos') { |
---|
[67] | 80 | return join(' ', map { $_ || '' } ($self->get_c_field('givenName'), ($self->get_c_field('sn')))); |
---|
[68] | 81 | } elsif ($field =~ /^(sAMAccountName|login)$/) { |
---|
[81] | 82 | return $self->id, |
---|
[89] | 83 | } elsif ($field eq 'memberOf') { |
---|
| 84 | my $sth = $self->db->prepare_cached( |
---|
| 85 | q{ |
---|
| 86 | select groupname from users_groups join "user" |
---|
| 87 | on users_groups.uid = "user".uid join |
---|
| 88 | "group" on "group".gid = users_groups.gid |
---|
| 89 | where "user".login = ? order by groupname |
---|
| 90 | } |
---|
| 91 | ); |
---|
| 92 | $sth->execute($self->id); |
---|
| 93 | my @res; |
---|
| 94 | while (my $res = $sth->fetchrow_hashref) { |
---|
| 95 | push(@res, $res->{groupname}); |
---|
| 96 | } |
---|
| 97 | return \@res; |
---|
[114] | 98 | } elsif ($field eq 'accountExpires') { |
---|
| 99 | my $sth = $self->db->prepare_cached( |
---|
| 100 | sprintf( |
---|
| 101 | q{select extract(epoch from expire) + 11644474161 as expire |
---|
| 102 | from %s where %s = ?}, |
---|
| 103 | $self->db->quote_identifier($self->object_table), |
---|
| 104 | $self->db->quote_identifier($self->key_field), |
---|
| 105 | ) |
---|
| 106 | ); |
---|
| 107 | $sth->execute($self->id); |
---|
| 108 | my $res = $sth->fetchrow_hashref; |
---|
| 109 | $sth->finish; |
---|
| 110 | return $res->{expire} ? sprintf("%.f", $res->{expire} * 1E7) : '9223372036854775807'; |
---|
| 111 | } elsif ($field eq 'shadowExpire') { |
---|
| 112 | my $sth = $self->db->prepare_cached( |
---|
| 113 | sprintf( |
---|
| 114 | q{select justify_hours(expire - '1/1/1970'::timestamp) as expire |
---|
| 115 | from %s where %s = ?}, |
---|
| 116 | $self->db->quote_identifier($self->object_table), |
---|
| 117 | $self->db->quote_identifier($self->key_field), |
---|
| 118 | ) |
---|
| 119 | ); |
---|
| 120 | $sth->execute($self->id); |
---|
| 121 | my $res = $sth->fetchrow_hashref; |
---|
| 122 | $sth->finish; |
---|
| 123 | return -1 unless($res->{expire}); |
---|
| 124 | $res->{expire} =~ /(\d+) days\s*(\w)?/; |
---|
| 125 | return $1 + ($2 ? 1 : 0); |
---|
[52] | 126 | } else { |
---|
| 127 | return $self->SUPER::get_field($field); |
---|
| 128 | } |
---|
| 129 | } |
---|
| 130 | |
---|
[89] | 131 | sub set_fields { |
---|
| 132 | my ($self, %data) = @_; |
---|
| 133 | my %fdata; |
---|
| 134 | foreach my $attr (keys %data) { |
---|
| 135 | $attr =~ /^memberOf$/ and do { |
---|
| 136 | my $uid = $self->get_field('uid') or next; |
---|
| 137 | my %member; |
---|
| 138 | foreach (@{ $self->get_field('memberOf') }) { |
---|
| 139 | $member{$_}{c} = 1; |
---|
| 140 | } |
---|
| 141 | foreach (@{ $data{$attr} || []}) { |
---|
| 142 | $member{$_}{n} = 1; |
---|
| 143 | } |
---|
| 144 | |
---|
| 145 | foreach (keys %member) { |
---|
| 146 | $member{$_}{c} && $member{$_}{n} and next; # no change ! |
---|
| 147 | my $group = $self->base->get_object('group', $_) or next; |
---|
| 148 | my $gid = $group->get_field('gid') or next; |
---|
| 149 | if ($member{$_}{n}) { |
---|
| 150 | my $sth = $self->db->prepare_cached( |
---|
| 151 | q{insert into users_groups (uid, gid) values (?,?)} |
---|
| 152 | ); |
---|
| 153 | $sth->execute($uid, $gid); |
---|
| 154 | } elsif ($member{$_}{c}) { |
---|
| 155 | my $sth = $self->db->prepare_cached( |
---|
| 156 | q{delete from users_groups where uid = ? and gid = ?} |
---|
| 157 | ); |
---|
| 158 | $sth->execute($uid, $gid); |
---|
| 159 | } # else {} # can't happend |
---|
| 160 | } |
---|
| 161 | next; |
---|
| 162 | }; |
---|
| 163 | $fdata{$attr} = $data{$attr}; |
---|
| 164 | } |
---|
| 165 | if (keys %fdata) { |
---|
| 166 | $self->SUPER::set_fields(%fdata); |
---|
| 167 | } |
---|
| 168 | } |
---|
| 169 | |
---|
| 170 | |
---|
[19] | 171 | 1; |
---|
| 172 | |
---|
| 173 | __END__ |
---|
| 174 | |
---|
| 175 | =head1 SEE ALSO |
---|
| 176 | |
---|
| 177 | =head1 AUTHOR |
---|
| 178 | |
---|
| 179 | Olivier Thauvin, E<lt>olivier.thauvin@latmos.ipsl.frE<gt> |
---|
| 180 | |
---|
| 181 | =head1 COPYRIGHT AND LICENSE |
---|
| 182 | |
---|
| 183 | Copyright (C) 2008, 2009 CNRS SA/CETP/LATMOS |
---|
| 184 | |
---|
| 185 | This library is free software; you can redistribute it and/or modify |
---|
| 186 | it under the same terms as Perl itself, either Perl version 5.10.0 or, |
---|
| 187 | at your option, any later version of Perl 5 you may have available. |
---|
| 188 | |
---|
| 189 | |
---|
| 190 | =cut |
---|