#!%PERL% -w
# Copyright (c) vhffs project and its contributors
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without 
# modification, are permitted provided that the following conditions 
# are met:
#
# 1. Redistributions of source code must retain the above copyright 
#   notice, this list of conditions and the following disclaimer.
#2. Redistributions in binary form must reproduce the above copyright
#   notice, this list of conditions and the following disclaimer in 
#   the documentation and/or other materials provided with the 
#   distribution.
#3. Neither the name of vhffs nor the names of its contributors 
#   may be used to endorse or promote products derived from this 
#   software without specific prior written permission.
#
#THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
#"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
#LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 
#FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 
#COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
#INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
#BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
#LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 
#CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
# POSSIBILITY OF SUCH DAMAGE.


use utf8;
use POSIX qw(locale_h);
use HTML::Template;
use HTML::Template::Expr;
use locale;
use Locale::gettext;
use CGI;
use CGI::Session;
use strict;

use lib '%VHFFS_LIB_DIR%';
use Vhffs::User;
use Vhffs::Group;
use Vhffs::Main;
use Vhffs::Panel::Main;
use Vhffs::Panel::Menu;
use Vhffs::Services::Web;
use Vhffs::Acl;
use Vhffs::Constants;

my $panel = new Vhffs::Panel::Main();
exit 0 unless $panel;
my $session = $panel->get_session;
exit 0 unless $session;

my $vhffs = $panel->{vhffs};
my $user = $panel->{user};
my $cgi = $panel->{cgi};
my $oid = $cgi->param('target_oid');
my $templatedir = $vhffs->get_config->get_templatedir;
my $template;

my $object = Vhffs::Object::get_by_oid($vhffs, $oid);
my $access_level = Vhffs::Acl::get_perm( $vhffs , $object , $user );

# Object does not exists
unless( defined $object ) {
    $template = new HTML::Template( filename => $templatedir.'/panel/misc/simplemsg.tmpl' );
    $template->param( MESSAGE => sprintf( gettext('Cannot get informations on object #%d'), $oid) );
} else {
    # Object exists, we need to know if access is granted to the user
    unless( $user->can_view( $object ) ) {
        $template = new HTML::Template( filename => $templatedir.'/panel/misc/simplemsg.tmpl' );
        $template->param( MESSAGE => gettext('You\'re not allowed to view this object\'s ACL') );
    } else {
        # access OK, let's see if some action was requested
        if(defined $cgi->param('update_acl_submit')) {
            my $granted_oid = $cgi->param('granted_oid');
            my $perm = $cgi->param('perm'.$granted_oid);
            my $granted;
            my $ret;
            if(! (defined $granted_oid && defined $perm) ) {
                $panel->add_error( gettext('CGI Error !') );
            } elsif( ! defined( $granted = Vhffs::Object::get_by_oid( $vhffs, $granted_oid ) ) ) {
                $panel->add_error( gettext('Group or user not found') );
            }  elsif( ! $user->can_manageacl( $object ) ) {
                $panel->add_error( gettext('You\'re not allowed to manage this object\'s ACL') );
            } else {
                $ret = Vhffs::Acl::add_update_or_del_acl( $vhffs, $object->get_oid, $granted_oid, $perm );
                $panel->add_error( gettext('Sorry, can\'t add or update ACL') ) if( $ret < 0 );
                $panel->add_info( gettext('ACL added') ) if( $ret == 1);
                $panel->add_info( gettext('ACL updated') ) if( $ret == 2);
                $panel->add_info( gettext('ACL deleted') ) if( $ret == 3);
            }
        }

	$panel->set_title( gettext('ACL Administration') );

	$template = new HTML::Template::Expr( filename => $templatedir.'/panel/acl/view.tmpl' , global_vars => 1 );

	$template->param( TEXT_USER => gettext('User') );
	$template->param( TARGET_OID => $object->get_oid );
        $template->param( TEXT_MODIFY => gettext('Modify') );

        $template->param( TEXT_DEFAULT => gettext('Default') );
        $template->param( TEXT_ACL_UNDEF => gettext('Default') );
        $template->param( TEXT_ACL_DENIED => gettext('Can\'t access') );
        $template->param( TEXT_ACL_VIEW => gettext('Can view this service') );
        $template->param( TEXT_ACL_MODIFY => gettext('Can modify this service') );
        $template->param( TEXT_ACL_MANAGEACL => gettext('Can manage ACL for this service') );
        $template->param( TEXT_ACL_DELETE => gettext('Can destroy this service') );

	$template->param( VALUE_ACL_UNDEF => Vhffs::Constants::ACL_UNDEFINED );
	$template->param( VALUE_ACL_DENIED => Vhffs::Constants::ACL_DENIED );
	$template->param( VALUE_ACL_VIEW => Vhffs::Constants::ACL_VIEW );
	$template->param( VALUE_ACL_MODIFY => Vhffs::Constants::ACL_MODIFY );
	$template->param( VALUE_ACL_MANAGEACL => Vhffs::Constants::ACL_MANAGEACL );
	$template->param( VALUE_ACL_DELETE => Vhffs::Constants::ACL_DELETE );

	my $acls = Vhffs::Acl::get_object_acl( $vhffs, $object );
	my @acldefault;
	my @aclusers;
	foreach my $acl ( @{$acls} )  {
		if( defined $acl->{name} ) {
			$acl->{perm} = Vhffs::Constants::ACL_UNDEFINED unless defined $acl->{perm};
			push( @aclusers, { %$acl } );
		}
		else  {
			push( @acldefault, { 'granted_oid' => $acl->{granted_oid} , 'perm' => $acl->{perm} } );
		}
	}

	$template->param( ACL_DEFAULT => \@acldefault );
	$template->param( ACL_USERS => \@aclusers );
    }
}

$panel->build( $template );
$panel->display;
