#! /usr/bin/perl -T
use strict;
use warnings;
use utf8;

use lib '../include';
use itkaclcommon;

itkaclcommon::init();
itkaclcommon::check_csrf_token();

my $parent = $itkaclcommon::cgi->param('parent');
my $name = $itkaclcommon::cgi->param('name');
my $description = $itkaclcommon::cgi->param('description');

if ($name !~ /^[a-zA-Z0-9-]+$/) {
	die "Illegal characters in name";
}
if (length($name) > 64) {
	die "Name too long";
}
	
$itkaclcommon::dbh->do('INSERT INTO objects (name,description,parent) VALUES (?,?,?)', undef,
    $name, $description, $parent)
	or die "Couldn't insert new object";

# Let sync-itkacl know there's updates
utime(time(), time(), '/etc/itkacl/updated');

#
# Redirect to the newly created node (a little touch: make sure the node
# is opened so the new node is visible in the tree -- this won't catch all
# cases, but at least most of them).
# 
my $ref = $itkaclcommon::dbh->selectrow_hashref('SELECT id FROM objects WHERE name=? AND parent=?',
	undef, $name, $parent);
	
print $itkaclcommon::cgi->redirect("view.pl?entry=$ref->{'id'}&open=$parent");

