#!/usr/bin/perl

use strict;
use Getopt::Long qw(:config no_ignore_case);
use Text::ASCIITable;
use Perun::Agent;
use Perun::Common qw(printMessage getSortingFunction tableContentToPrint);

sub help {
	return qq{
	Prints list of User Roles
	---------------------------------
	Available options:
	--userId      | -u userId
	--batch       | -b batch
	--help        | -h prints this help
	};
}

our $batch;
my $userId;
GetOptions("help|h" => sub {
		print help;
		exit 0;
	},
	"userId|u=i"    => \$userId,
	"batch|b"       => \$batch) || die help;

my $agent = Perun::Agent->new();
my $authzResolverAgent = $agent->getAuthzResolverAgent;

my $role = $authzResolverAgent->getUserRoleNames(user => $userId);
unless ($role) {
	printMessage "No Role found", $batch;
	exit 0;
}

#output
my @rows = ();
my @roles = @$role;
foreach my $rol (@roles) {
	my @row = ($rol);
	push(@rows, \@row);
}

my @columnsNames = ('Role Name');
print tableContentToPrint(\@columnsNames, \@rows, $batch);
