cyberman/lib/cyberman/Helper.pm

45 lines
733 B
Perl
Raw Normal View History

2017-07-17 00:12:38 +02:00
package cyberman::Helper;
use base qw(Exporter);
use Dancer2 appname => "cyberman";
2017-07-17 09:55:50 +02:00
use Math::Random::Secure qw(irand);
2017-07-17 00:12:38 +02:00
use Exporter qw(import);
2017-07-17 09:55:50 +02:00
our @EXPORT = qw(auth_test randstring);
# Helper functions
2017-07-17 00:12:38 +02:00
sub auth_test {
2017-07-17 01:35:13 +02:00
my $id = undef;
if (@_) {
$id = shift;
}
2017-07-17 00:12:38 +02:00
if (!vars->{"auth"}) {
return template 'redir' => {
"redir" => "/index",
};
2017-07-17 01:35:13 +02:00
} elsif ($id && vars->{"auth"} != $id) {
return template 'redir' => {
"redir" => "/index",
};
2017-07-17 00:12:38 +02:00
} else {
return 0; # nothing to be returned, route can continue
}
}
2017-07-17 09:55:50 +02:00
sub randstring {
my $len = shift;
my @chars = (0..9, "a".."z", "A".."Z");
my $ret;
for (1..$len) {
$ret .= $chars[irand(scalar(@chars))];
}
return $ret;
}
2017-07-17 00:12:38 +02:00
1;