1 | package LATMOS::Accounts::Bases::Ad::Group; |
---|
2 | |
---|
3 | use 5.010000; |
---|
4 | use strict; |
---|
5 | use warnings; |
---|
6 | |
---|
7 | use base qw(LATMOS::Accounts::Bases::Ad::objects); |
---|
8 | use Net::LDAP; |
---|
9 | use Net::LDAP::Entry; |
---|
10 | use Net::LDAP::Control::Paged; |
---|
11 | use Net::LDAP::Constant qw( LDAP_CONTROL_PAGED ); |
---|
12 | use Net::LDAP::Util qw( escape_filter_value ); |
---|
13 | |
---|
14 | our $VERSION = (q$Rev$ =~ /^Rev: (\d+) /)[0]; |
---|
15 | |
---|
16 | =head1 NAME |
---|
17 | |
---|
18 | LATMOS::Ad - Perl extension for blah blah blah |
---|
19 | |
---|
20 | =head1 SYNOPSIS |
---|
21 | |
---|
22 | use LATMOS::Ad; |
---|
23 | blah blah blah |
---|
24 | |
---|
25 | =head1 DESCRIPTION |
---|
26 | |
---|
27 | Stub documentation for LATMOS::Ad, created by h2xs. It looks like the |
---|
28 | author of the extension was negligent enough to leave the stub |
---|
29 | unedited. |
---|
30 | |
---|
31 | Blah blah blah. |
---|
32 | |
---|
33 | =head1 FUNCTIONS |
---|
34 | |
---|
35 | =cut |
---|
36 | |
---|
37 | sub _class_filter { '(ObjectClass=group)' } |
---|
38 | |
---|
39 | sub _key_attr { 'cn' } |
---|
40 | |
---|
41 | sub _canonical_fields { |
---|
42 | my ($self, $base, $mode) = @_; |
---|
43 | ( |
---|
44 | qw(gidNumber description sAMAccountName member), |
---|
45 | ($mode !~ /w/ |
---|
46 | ? qw(cn dn) |
---|
47 | : () |
---|
48 | ) |
---|
49 | ) |
---|
50 | } |
---|
51 | |
---|
52 | sub _create { |
---|
53 | my ($class, $base, $id, %data) = @_; |
---|
54 | |
---|
55 | my $entry = Net::LDAP::Entry->new(); |
---|
56 | |
---|
57 | $entry->dn(join(',', |
---|
58 | sprintf('cn=%s', escape_filter_value($id)), |
---|
59 | $base->object_base_dn($class->type), |
---|
60 | )); |
---|
61 | $entry->replace(objectClass => [ qw(top group)],); |
---|
62 | foreach (keys %data) { |
---|
63 | $class->_populate_entry($entry, $_, $data{$_}); |
---|
64 | } |
---|
65 | my $msg = $base->ldap->add($entry); |
---|
66 | return $msg->code ? 0 : 1; |
---|
67 | } |
---|
68 | |
---|
69 | sub get_field { |
---|
70 | my ($self, $field) = @_; |
---|
71 | |
---|
72 | $field eq 'member' and do { |
---|
73 | my @res; |
---|
74 | $self->base->_unlimited_search( |
---|
75 | base => $self->base->object_base_dn('user'), |
---|
76 | filter => sprintf( |
---|
77 | '(&(objectClass=user)(memberOf=%s))', |
---|
78 | escape_filter_value($self->{entry}->dn), |
---|
79 | ), |
---|
80 | callback => sub { |
---|
81 | my ($mesg, $entry) = @_; |
---|
82 | ref $entry eq 'Net::LDAP::Entry' or return; |
---|
83 | push(@res, $entry->get_value('cn')); |
---|
84 | }, |
---|
85 | ); |
---|
86 | return \@res |
---|
87 | }; |
---|
88 | $self->SUPER::get_field($field); |
---|
89 | } |
---|
90 | |
---|
91 | sub set_fields { |
---|
92 | my ($self, %data) = @_; |
---|
93 | my %ndata; |
---|
94 | while (my ($f, $val) = each(%data)) { |
---|
95 | $f eq 'member' and do { |
---|
96 | my %users; |
---|
97 | $users{$_}{e} = 1 foreach (@{ $self->get_field('member') || []}); |
---|
98 | $users{$_}{n} = 1 foreach (@{ $val || []}); |
---|
99 | foreach (keys %users) { |
---|
100 | $users{$_}{e} && $users{$_}{n} and next; |
---|
101 | my $user = $self->base->get_object('user', $_) or next; |
---|
102 | if ($users{$_}{e}) { |
---|
103 | $self->{entry}->del(member => $user->get_field('dn')); |
---|
104 | } elsif ($users{$_}{n}) { |
---|
105 | $self->{entry}->add(member => $user->get_field('dn')); |
---|
106 | } # else {} # can't happen |
---|
107 | my $mesg = $self->{entry}->update($self->base->ldap); |
---|
108 | if ($mesg->code) { |
---|
109 | warn $mesg->error; |
---|
110 | } |
---|
111 | } |
---|
112 | next; |
---|
113 | }; |
---|
114 | $ndata{$f} = $val; |
---|
115 | } |
---|
116 | $self->SUPER::set_fields(%ndata); |
---|
117 | } |
---|
118 | |
---|
119 | 1; |
---|
120 | |
---|
121 | __END__ |
---|
122 | |
---|
123 | =head1 SEE ALSO |
---|
124 | |
---|
125 | =head1 AUTHOR |
---|
126 | |
---|
127 | Olivier Thauvin, E<lt>olivier.thauvin@aerov.jussieu.frE<gt> |
---|
128 | |
---|
129 | =head1 COPYRIGHT AND LICENSE |
---|
130 | |
---|
131 | Copyright (C) 2008 CNRS SA/CETP/LATMOS |
---|
132 | |
---|
133 | This library is free software; you can redistribute it and/or modify |
---|
134 | it under the same terms as Perl itself, either Perl version 5.10.0 or, |
---|
135 | at your option, any later version of Perl 5 you may have available. |
---|
136 | |
---|
137 | |
---|
138 | =cut |
---|