Archived
1
0

Refactor update_group to work as update_user

This commit is contained in:
Mercier Pierre-Olivier 2013-09-02 21:09:51 +02:00
parent a3bd738b0f
commit 114e661761
3 changed files with 92 additions and 35 deletions

View File

@ -56,16 +56,15 @@ sub ldap_connect_anon()
## High end functions ## High end functions
sub add_group($$;$) sub add_group($$$;$)
{ {
my $ldap = shift // ldap_connect();
my $cn = shift; my $cn = shift;
my $year = shift; my $year = shift // get_year();
my $ou = shift // "intra"; # expected roles or intra my $ou = shift // "intra"; # expected roles or intra
my $dn = "cn=$cn,ou=$year,ou=$ou,ou=groups,dc=acu,dc=epita,dc=fr"; my $dn = "cn=$cn,ou=$year,ou=$ou,ou=groups,dc=acu,dc=epita,dc=fr";
my $ldap = ldap_connect();
my $mesg = $ldap->add( $dn, my $mesg = $ldap->add( $dn,
attrs => [ attrs => [
objectclass => "intraGroup", objectclass => "intraGroup",
@ -74,8 +73,6 @@ sub add_group($$;$)
); );
if ($mesg->code != 0) { die $mesg->error; } if ($mesg->code != 0) { die $mesg->error; }
$ldap->unbind or die ("couldn't disconnect correctly");
return $dn; return $dn;
} }
@ -211,6 +208,28 @@ sub get_attribute($$$)
return get_dn($ldap, $dn, $what)->get_value($what); return get_dn($ldap, $dn, $what)->get_value($what);
} }
sub search_dn($$@)
{
my $ldap = shift // ldap_connect();
my $base = shift;
my $filter = shift;
if ($base) {
$base .= ","
}
my $mesg = $ldap->search( # search
base => $base."dc=acu,dc=epita,dc=fr",
filter => $filter,
attrs => [ ],
scope => "sub"
);
if ($mesg->code != 0) { print $mesg->error; return undef; }
if ($mesg->count != 1) { return undef; }
return $mesg->entry(0)->dn;
}
sub update_attribute($$$@) sub update_attribute($$$@)
{ {
my $ldap = shift // ldap_connect(); my $ldap = shift // ldap_connect();

View File

@ -14,32 +14,40 @@ use ACU::Process;
our $ou = "intra"; our $ou = "intra";
my %actions = my %group_actions =
( (
"new" => \&group_new, "new" => \&_new,
"add" => \&group_add, "add" => \&_add,
"delete" => \&group_delete, "delete" => \&_delete,
"flush" => \&group_flush, "flush" => \&_flush,
"remove" => \&group_remove, "remove" => \&_remove,
"update" => \&group_update, "update" => \&_update,
); );
sub group_new($$$) my %user_actions =
(
"add" => \&_add,
"flush" => \&_flush,
"remove" => \&_remove,
"update" => \&_update,
);
sub _new($$$)
{ {
my $ldap = shift; my $ldap = shift;
my $dn = shift; my $dn = shift;
my $args = shift; my $args = shift;
# Add group # Add group
if (LDAP::add_group($args->{param}{cn}, LDAP::get_year) eq $dn) if (LDAP::add_group($ldap, $args->{param}{cn}, $args->{param}{year}) eq $dn)
{ {
if ($args->{param}{type}) { if ($args->{param}{type}) {
group_add $dn, $args group_add $dn, $args;
} }
} }
} }
sub group_add($$$) sub _add($$$)
{ {
my $ldap = shift; my $ldap = shift;
my $dn = shift; my $dn = shift;
@ -49,16 +57,16 @@ sub group_add($$$)
# Add content if any # Add content if any
for (my $i = $args->{unamed}; $i > 0; $i--) { for (my $i = $args->{unamed}; $i > 0; $i--) {
LDAP::add_attribute($dn, $cnt_type, $args->{param}{$i}); LDAP::add_attribute($ldap, $dn, $cnt_type, $args->{param}{$i});
} }
} }
sub group_delete($$$) sub _delete($$$)
{ {
return LDAP::delete_entry($_[0], $_[1]); return LDAP::delete_entry($_[0], $_[1]);
} }
sub group_flush($$) sub _flush($$)
{ {
my $ldap = shift; my $ldap = shift;
my $dn = shift; my $dn = shift;
@ -69,7 +77,7 @@ sub group_flush($$)
return LDAP::flush_attribute($ldap, $dn, $cnt_type); return LDAP::flush_attribute($ldap, $dn, $cnt_type);
} }
sub group_remove($$) sub _remove($$)
{ {
my $ldap = shift; my $ldap = shift;
my $dn = shift; my $dn = shift;
@ -85,7 +93,7 @@ sub group_remove($$)
return LDAP::delete_attributes($ldap, $dn, $cnt_type, @data); return LDAP::delete_attributes($ldap, $dn, $cnt_type, @data);
} }
sub group_update($$) sub _update($$)
{ {
my $ldap = shift; my $ldap = shift;
my $dn = shift; my $dn = shift;
@ -98,45 +106,74 @@ sub group_update($$)
push @data, $i; push @data, $i;
} }
LDAP::update_attribute($ldap, $dn, $cnt_type, @data); return LDAP::update_attribute($ldap, $dn, $cnt_type, @data);
} }
sub group_get_type($) sub _get_type($)
{ {
my $param = shift; my $param = shift;
# Extract data type # Extract data type
if ($param{type} eq "members") { if ($param->{type} eq "members") {
return "memberUid"; return "memberUid";
} }
elsif ($param{type} eq "rights") { elsif ($param->{type} eq "rights") {
return "intraRights" ; return "intraRights" ;
} }
else { else {
die ("Unknown type to add: ".$param{type}); die ("Unknown type to add: ".$param->{type});
} }
} }
sub process sub process_group
{ {
my ($given_args, $args) = @_; my ($given_args, $args) = @_;
my $year = $param{year} // LDAP::get_year; my $year = $args->{param}{year} // LDAP::get_year;
my $dn = "cn=".$param{cn}."ou=$year,ou=$ou,ou=groups,dc=acu,dc=epita,dc=fr"; my $dn = "cn=".$args->{param}{cn}."ou=$year,ou=$ou,ou=groups,dc=acu,dc=epita,dc=fr";
my $action = $param{type} // "update"; my $action = $args->{param}{type} // "update";
# Read action # Read action
if (! exists $actions{$action}) { if (! exists $group_actions{$action}) {
return "Unknown command for update_group: ". $action; return "Unknown command for update_group: ". $action;
} }
my $ldap = LDAP::ldap_connect(); my $ldap = LDAP::ldap_connect();
$actions{$action}($ldap, $dn, $args); $group_actions{$action}($ldap, $dn, $args);
$ldap->unbind or print "couldn't disconnect correctly"; $ldap->unbind or warn "couldn't disconnect correctly";
} }
Process::register("update_group", \&process); 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.");
}

1
process/ldap/update_user.pl Symbolic link
View File

@ -0,0 +1 @@
update_group.pl