Archived
1
0
This repository has been archived on 2021-10-08. You can view files and clone it, but cannot push or open issues or pull requests.
ACU/process/ldap/update_group.pl
Mercier Pierre-Olivier 9c27bf131e Some fixes in LDAP
2013-09-03 07:20:58 +02:00

236 lines
5.5 KiB
Perl

#! /usr/bin/env perl
use v5.10.1;
use strict;
use warnings;
use Pod::Usage;
BEGIN {
push @INC, "../../";
}
use ACU::Log;
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,
);
my $_get_type;
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 = $_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 = $_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 = $_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 = $_get_type->($args->{param});
return user_update($ldap, $dn, $args) if ($cnt_type eq "userInfos");
my @data;
for (my $i = $args->{unamed}; $i > 0; $i--) {
push @data, $i;
}
return LDAP::update_attribute($ldap, $dn, $cnt_type, @data);
}
sub user_update($$)
{
my $ldap = shift;
my $dn = shift;
my $args = shift;
LDAP::update_attribute($ldap, $dn, "cn", $args->{param}{cn}) if ($args->{param}{cn});
LDAP::update_attribute($ldap, $dn, "cn", $args->{param}{firstname}." ".$args->{param}{lastname}) if ($args->{param}{firstname} && $args->{param}{lastname});
LDAP::update_attribute($ldap, $dn, "l", $args->{param}{l}) if ($args->{param}{l});
LDAP::update_attribute($ldap, $dn, "mail", $args->{param}{mail}) if ($args->{param}{mail});
LDAP::update_attribute($ldap, $dn, "postalAddress", $args->{param}{postalAddress}) if ($args->{param}{postalAddress});
LDAP::update_attribute($ldap, $dn, "postalCode", $args->{param}{postalCode}) if ($args->{param}{postalCode});
LDAP::update_attribute($ldap, $dn, "sn", $args->{param}{sn}) if ($args->{param}{sn});
LDAP::update_attribute($ldap, $dn, "telephoneNumber", $args->{param}{telephoneNumber}) if ($args->{param}{telephoneNumber});
LDAP::update_attribute($ldap, $dn, "sshPublicKey", $args->{param}{sshPublicKey}) if ($args->{param}{sshPublicKey});
LDAP::update_attribute($ldap, $dn, "strongAuthKey", $args->{param}{strongAuthKey}) if ($args->{param}{strongAuthKey});
LDAP::update_attribute($ldap, $dn, "c", $args->{param}{c}) if ($args->{param}{c});
LDAP::update_attribute($ldap, $dn, "title", $args->{param}{title}) if ($args->{param}{title});
LDAP::update_attribute($ldap, $dn, "intraRight", $args->{param}{intraRight}) if ($args->{param}{intraRight});
LDAP::update_attribute($ldap, $dn, "intraTheme", $args->{param}{intraTheme}) if ($args->{param}{intraTheme});
LDAP::update_attribute($ldap, $dn, "birthdate", $args->{param}{birthdate}) if ($args->{param}{birthdate});
}
sub group_get_type($)
{
my $param = shift;
my $type = $param->{type} // "members";
# Extract data type
if ($type eq "members") {
return "memberUid";
}
elsif ($type eq "rights") {
return "intraRights" ;
}
else {
die ("Unknown type to add: ".$type);
}
}
sub user_get_type($)
{
my $param = shift;
my $type = $param->{type} // "userInfos";
# Extract data type
if ($type eq "rights") {
return "intraRights" ;
}
elsif ($type eq "sshkeys") {
return "sshPublicKey" ;
}
elsif ($type eq "userInfos") {
return "userInfos" ;
}
else {
die ("Unknown type to add: ".$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}{action} // "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";
return "Ok";
}
sub process_user
{
my ($given_args, $args) = @_;
my $action = $args->{param}{action} // "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";
return "Ok";
}
if ($0 =~ /^update_group/) {
$_get_type = \&group_get_type;
Process::register("update_group", \&process_group);
}
elsif ($0 =~ /^update_user/) {
$_get_type = \&user_get_type;
Process::register("update_user", \&process_user);
}
else {
die ("Bad filename.");
}