## OpenCA - CA Command
## (c) 2001 by Michael Bell and OpenCA Group
##
##   File Name: add_right
##       Brief: add a new right
## Description: shows a html-form where you can enter the data
##              of the new right
##  Parameters: none

sub cmdAdd_right {

## the data which is stored in the configurationfile of a script
my $operation;
my $module;
my $role;

## Get required parameters ( return only the value string )
my $sheet      = getRequired ( 'AddRightSheet' );
my $rbac       = getRequired ( 'RBAC_DIR' );
my $operations = getRequired ( 'OPERATIONS_DIR');
my $roles      = getRequired ( 'ROLES_DIR' );
my $modules    = getRequired ( 'MODULES_DIR');
my $uptime     = $tools->getDate();

## load modules
opendir ( DIR, $rbac."/".$modules) or
  configError ( i18nGettext ("Cannot open the directory with the modules! (__DIR__)",
                             "__DIR__",
                             "$rbac/$modules"));
my @values = readdir  DIR ;
closedir ( DIR );
my @hvalues;
foreach my $item (@values) {
        if ( $item !~ /^(\.\.|\.)$/ ) {
                push @hvalues, decodeName ($item);
        }
}
@values = sort @hvalues;
$module = "<select name=module>\n";
for my $h (@values) {
  $module .= "<option value=\"".$h."\">".$h."\n";
}
$module .= "</select>\n";

## load operations
opendir ( DIR, $rbac."/".$operations) or
  configError ( i18nGettext ("Cannot open the directory with the operations! (__DIR__)",
                             "__DIR__",
                             "$rbac/$operations"));
my @values = readdir  DIR ;
closedir ( DIR );
my @hvalues;
foreach my $item (@values) {
        if ( $item !~ /^(\.\.|\.)$/ ) {
                push @hvalues, decodeName ($item);
        }
}
@values = sort @hvalues;
$operation = "<select name=operation>\n";
for my $h (@values) {
  $operation .= "<option value=\"".$h."\">".$h."\n";
}
$operation .= "</select>\n";

## load operator and owner from roles
opendir ( DIR, $rbac."/".$roles) or
  configError ( i18nGettext ("Cannot open the directory with the roles! (__DIR__)",
                             "__DIR__",
                             "$rbac/$roles"));
my @values = readdir  DIR ;
closedir ( DIR );
my @hvalues;
foreach my $item (@values) {
        if ( $item !~ /^(\.\.|\.)$/ ) {
                push @hvalues, decodeName ($item);
        }
}
@values = sort @hvalues;
$operator = "<select name=operator>\n";
$owner    = "<select name=owner>\n";
$operator .= "<option value=\"\">".gettext ("&lt;new user&gt;")."\n";
$owner    .= "<option value=\"\">".gettext ("&lt;CA itself or any role&gt;")."\n";
for my $h (@values) {
  $operator .= "<option value=\"".$h."\">".$h."\n";
  $owner    .= "<option value=\"".$h."\">".$h."\n";
}
$operator .= "</select>\n";
$owner    .= "</select>\n";

my $page;

## Get the base Page ( got in $page variable )
if( not $page = $query->getFile( "$sheet" ) ) {
	configError ( i18nGettext ("File not Found : __SHEET__", "__SHEET__", $sheet));
};

## Substitute the Variables in the $page
$page = $query->subVar( $page, '$uptime', $uptime );
$page = $query->subVar( $page, '$module', $module );
$page = $query->subVar( $page, '$operation', $operation );
$page = $query->subVar( $page, '$operator', $operator );
$page = $query->subVar( $page, '$owner', $owner );

print "$page";

}

1;

