1 | package LATMOS::Accounts::Bases::Sql::Group; |
---|
2 | |
---|
3 | use 5.010000; |
---|
4 | use strict; |
---|
5 | use warnings; |
---|
6 | |
---|
7 | use base qw(LATMOS::Accounts::Bases::Sql::objects); |
---|
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; |
---|
18 | my $base = LATMOS::Accounts::Bases->new('sql'); |
---|
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 | |
---|
39 | sub object_table { 'group' } |
---|
40 | |
---|
41 | sub key_field { 'groupname' } |
---|
42 | |
---|
43 | sub has_extended_attributes { 1 } |
---|
44 | |
---|
45 | sub _inline_fields { |
---|
46 | my ($self, $for, $base) = @_; |
---|
47 | |
---|
48 | my %fields = ( |
---|
49 | %{ $self->_initial_fields($for, $base) }, |
---|
50 | memberUID => 'memberUID', |
---|
51 | member => 'member', |
---|
52 | gidNumber => 'gidnumber', |
---|
53 | $for !~ /w/ ? ( |
---|
54 | sAMAccountName => 'groupname', |
---|
55 | ) : (), |
---|
56 | ); |
---|
57 | \%fields; |
---|
58 | } |
---|
59 | |
---|
60 | sub _delayed_fields { |
---|
61 | my ($self)= @_; |
---|
62 | return qw(memberUID member); |
---|
63 | } |
---|
64 | |
---|
65 | sub get_field { |
---|
66 | my ($self, $field) = @_; |
---|
67 | for ($field) { |
---|
68 | /^(member|memberUID)$/ and do { |
---|
69 | my $sth = $self->db->prepare_cached( |
---|
70 | q{ |
---|
71 | select id from user_attributes_groups |
---|
72 | join user_attributes_list on |
---|
73 | user_attributes_groups.attr = user_attributes_list.ikey |
---|
74 | where value = ? and canonical = ? |
---|
75 | } |
---|
76 | ); |
---|
77 | $sth->execute($self->id, 'memberOf'); |
---|
78 | my @res; |
---|
79 | while (my $res = $sth->fetchrow_hashref) { |
---|
80 | push(@res, $res->{id}); |
---|
81 | } |
---|
82 | return \@res; |
---|
83 | }; |
---|
84 | } |
---|
85 | $self->SUPER::get_field($field); |
---|
86 | } |
---|
87 | |
---|
88 | sub set_fields { |
---|
89 | my ($self, %data) = @_; |
---|
90 | my %fdata; |
---|
91 | foreach my $attr (keys %data) { |
---|
92 | $attr =~ /^memberUID$/ and do { |
---|
93 | my $cl = $self->base->_load_obj_class('user') or next; |
---|
94 | my $attrid = $cl->_get_field_name_db('memberOf', $self->base) or next; |
---|
95 | my %member; |
---|
96 | foreach (@{ $self->get_field('memberUID') }) { |
---|
97 | $member{$_}{c} = 1; |
---|
98 | } |
---|
99 | foreach (@{ $data{$attr} || []}) { |
---|
100 | $member{$_}{n} = 1; |
---|
101 | } |
---|
102 | |
---|
103 | foreach (keys %member) { |
---|
104 | $member{$_}{c} && $member{$_}{n} and next; # no change ! |
---|
105 | my $user = $self->base->get_object('user', $_) or next; |
---|
106 | if ($member{$_}{n}) { |
---|
107 | my $sth = $self->db->prepare_cached( |
---|
108 | q{insert into user_attributes_groups (id, attr, value) values (?,?,?)} |
---|
109 | ); |
---|
110 | $sth->execute($_, $attrid, $self->id); |
---|
111 | } elsif ($member{$_}{c}) { |
---|
112 | my $sth = $self->db->prepare_cached( |
---|
113 | q{delete from user_attributes_groups where id = ? and attr = ? and value = ?} |
---|
114 | ); |
---|
115 | $sth->execute($_, $attrid, $self->id); |
---|
116 | } # else {} # can't happend |
---|
117 | } |
---|
118 | next; |
---|
119 | }; |
---|
120 | $fdata{$attr} = $data{$attr}; |
---|
121 | } |
---|
122 | if (keys %fdata) { |
---|
123 | $self->SUPER::set_fields(%fdata); |
---|
124 | } |
---|
125 | } |
---|
126 | |
---|
127 | 1; |
---|
128 | |
---|
129 | __END__ |
---|
130 | |
---|
131 | =head1 SEE ALSO |
---|
132 | |
---|
133 | =head1 AUTHOR |
---|
134 | |
---|
135 | Olivier Thauvin, E<lt>olivier.thauvin@latmos.ipsl.frE<gt> |
---|
136 | |
---|
137 | =head1 COPYRIGHT AND LICENSE |
---|
138 | |
---|
139 | Copyright (C) 2008, 2009 CNRS SA/CETP/LATMOS |
---|
140 | |
---|
141 | This library is free software; you can redistribute it and/or modify |
---|
142 | it under the same terms as Perl itself, either Perl version 5.10.0 or, |
---|
143 | at your option, any later version of Perl 5 you may have available. |
---|
144 | |
---|
145 | |
---|
146 | =cut |
---|