180 lines
3.2 KiB
Perl
180 lines
3.2 KiB
Perl
#! /usr/bin/env perl
|
|
|
|
use v5.10.1;
|
|
use strict;
|
|
use warnings;
|
|
use Pod::Usage;
|
|
|
|
BEGIN {
|
|
push @INC, "../../";
|
|
}
|
|
|
|
use ACU::LDAP;
|
|
use ACU::Process;
|
|
|
|
our $ou = "intra";
|
|
|
|
my %group_actions =
|
|
(
|
|
"new" => \&_new,
|
|
"add" => \&_add,
|
|
"delete" => \&_delete,
|
|
"flush" => \&_flush,
|
|
"remove" => \&_remove,
|
|
"update" => \&_update,
|
|
);
|
|
|
|
my %user_actions =
|
|
(
|
|
"add" => \&_add,
|
|
"flush" => \&_flush,
|
|
"remove" => \&_remove,
|
|
"update" => \&_update,
|
|
);
|
|
|
|
sub _new($$$)
|
|
{
|
|
my $ldap = shift;
|
|
my $dn = shift;
|
|
my $args = shift;
|
|
|
|
# Add group
|
|
if (LDAP::add_group($ldap, $args->{param}{cn}, $args->{param}{year}) eq $dn)
|
|
{
|
|
if ($args->{param}{type}) {
|
|
group_add $dn, $args;
|
|
}
|
|
}
|
|
}
|
|
|
|
sub _add($$$)
|
|
{
|
|
my $ldap = shift;
|
|
my $dn = shift;
|
|
my $args = shift;
|
|
|
|
my $cnt_type = group_get_type $args->{param};
|
|
|
|
# Add content if any
|
|
for (my $i = $args->{unamed}; $i > 0; $i--) {
|
|
LDAP::add_attribute($ldap, $dn, $cnt_type, $args->{param}{$i});
|
|
}
|
|
}
|
|
|
|
sub _delete($$$)
|
|
{
|
|
return LDAP::delete_entry($_[0], $_[1]);
|
|
}
|
|
|
|
sub _flush($$)
|
|
{
|
|
my $ldap = shift;
|
|
my $dn = shift;
|
|
my $args = shift;
|
|
|
|
my $cnt_type = group_get_type $args->{param};
|
|
|
|
return LDAP::flush_attribute($ldap, $dn, $cnt_type);
|
|
}
|
|
|
|
sub _remove($$)
|
|
{
|
|
my $ldap = shift;
|
|
my $dn = shift;
|
|
my $args = shift;
|
|
|
|
my $cnt_type = group_get_type $args->{param};
|
|
|
|
my @data;
|
|
for (my $i = $args->{unamed}; $i > 0; $i--) {
|
|
push @data, $i;
|
|
}
|
|
|
|
return LDAP::delete_attributes($ldap, $dn, $cnt_type, @data);
|
|
}
|
|
|
|
sub _update($$)
|
|
{
|
|
my $ldap = shift;
|
|
my $dn = shift;
|
|
my $args = shift;
|
|
|
|
my $cnt_type = group_get_type $args->{param};
|
|
|
|
my @data;
|
|
for (my $i = $args->{unamed}; $i > 0; $i--) {
|
|
push @data, $i;
|
|
}
|
|
|
|
return LDAP::update_attribute($ldap, $dn, $cnt_type, @data);
|
|
}
|
|
|
|
|
|
sub _get_type($)
|
|
{
|
|
my $param = shift;
|
|
|
|
# Extract data type
|
|
if ($param->{type} eq "members") {
|
|
return "memberUid";
|
|
}
|
|
elsif ($param->{type} eq "rights") {
|
|
return "intraRights" ;
|
|
}
|
|
else {
|
|
die ("Unknown type to add: ".$param->{type});
|
|
}
|
|
}
|
|
|
|
|
|
sub process_group
|
|
{
|
|
my ($given_args, $args) = @_;
|
|
|
|
my $year = $args->{param}{year} // LDAP::get_year;
|
|
my $dn = "cn=".$args->{param}{cn}."ou=$year,ou=$ou,ou=groups,dc=acu,dc=epita,dc=fr";
|
|
my $action = $args->{param}{type} // "update";
|
|
|
|
# Read action
|
|
if (! exists $group_actions{$action}) {
|
|
return "Unknown command for update_group: ". $action;
|
|
}
|
|
|
|
my $ldap = LDAP::ldap_connect();
|
|
|
|
$group_actions{$action}($ldap, $dn, $args);
|
|
|
|
$ldap->unbind or warn "couldn't disconnect correctly";
|
|
}
|
|
|
|
sub process_user
|
|
{
|
|
my ($given_args, $args) = @_;
|
|
|
|
my $action = $args->{param}{type} // "update";
|
|
|
|
# Read action
|
|
if (! exists $user_actions{$action}) {
|
|
return "Unknown command for update_user: ". $action;
|
|
}
|
|
|
|
my $ldap = LDAP::ldap_connect();
|
|
|
|
my $dn = LDAP::search_dn($ldap, "ou=users", "uid=".$args->{param}{uid});
|
|
|
|
$user_actions{$action}($ldap, $dn, $args);
|
|
|
|
$ldap->unbind or warn "couldn't disconnect correctly";
|
|
}
|
|
|
|
|
|
if ($0 =~ /^update_group/) {
|
|
Process::register("update_group", \&process_group);
|
|
}
|
|
elsif ($0 =~ /^update_user/) {
|
|
Process::register("update_user", \&process_user);
|
|
}
|
|
else {
|
|
die ("Bad filename.");
|
|
}
|