aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorGuilhem Moulin <guilhem.moulin@fripost.org>2012-06-08 08:07:07 +0200
committerGuilhem Moulin <guilhem.moulin@fripost.org>2012-06-08 08:07:07 +0200
commit05d7924b1c44e85a379b3ff5cca7b512383df769 (patch)
tree7b480cb29372e94034bed1da91099f146aaeeded /lib
parent93d4b1701fbef7e2173324e27adc751062ca360f (diff)
Ensure that no cycle is created when add an alias.
Diffstat (limited to 'lib')
-rw-r--r--lib/Fripost/Commands/add_alias.pm5
-rwxr-xr-xlib/Fripost/Tests.pm80
2 files changed, 85 insertions, 0 deletions
diff --git a/lib/Fripost/Commands/add_alias.pm b/lib/Fripost/Commands/add_alias.pm
index 7155368..8581a9f 100644
--- a/lib/Fripost/Commands/add_alias.pm
+++ b/lib/Fripost/Commands/add_alias.pm
@@ -19,6 +19,7 @@ use IO::Prompter;
use Fripost::Prompt;
use Fripost::Schema;
use Fripost::Email;
+use Fripost::Tests;
our $VERSION = '0.01';
@@ -32,6 +33,10 @@ sub main {
my @addr = @_;
@addr || push @addr, (split /, */, prompt "Alias from address(es): ");
+ my $graph = build_alias_graph ($ldap->alias->search()->entries);
+ my @path = search_path ($graph, $goto, $addr[0]);
+ die "Error: Cannot create cycle " . join (' -> ', @path) . ' -> ' . $goto . "\n"
+ if @path;
# Show goto adress
say "Goto adress: $goto";
diff --git a/lib/Fripost/Tests.pm b/lib/Fripost/Tests.pm
new file mode 100755
index 0000000..cfdfa47
--- /dev/null
+++ b/lib/Fripost/Tests.pm
@@ -0,0 +1,80 @@
+package Fripost::Tests;
+
+use 5.010_000;
+use strict;
+use warnings;
+use utf8;
+
+=head1 NAME
+
+Tests.pm
+
+=cut
+
+our @EXPORT = qw/build_alias_graph search_path/;
+our @ISA = qw(Exporter);
+
+use FindBin qw($Bin);
+use lib "$Bin/lib";
+
+use Fripost::Schema;
+
+sub build_alias_graph {
+ my $graph;
+ foreach (@_) {
+ my $to = $_->{goto};
+ foreach my $from (@{$_->{address}}) {
+ push @{$graph->{$from}}, $to;
+ }
+ }
+
+ return $graph;
+}
+
+
+sub search_path {
+ my ($graph, $from, $to) = @_;
+
+ my @stack;
+ push @stack, [$from];
+
+ while (@stack) {
+ my $path = pop @stack;
+ my $last = @{$path}[$#$path];
+ return @$path if $last eq $to;
+
+ foreach (@{$graph->{$last}}) {
+ push @stack, [@$path,$_];
+ }
+ }
+}
+
+
+
+
+=head1 AUTHOR
+
+Stefan Kangas C<< <skangas at skangas.se> >>
+
+Guilhem Moulin C<< <guilhem at fripost.org> >>
+
+=head1 COPYRIGHT
+
+Copyright 2010,2011 Stefan Kangas.
+
+Copyright 2012 Guilhem Moulin.
+
+=head1 LICENSE
+
+This program is free software; you can redistribute it and/or modify it
+under the same terms as perl itself.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+
+=cut
+
+1; # End of Tests.pm
+
+__END__