Skip to content

Commit

Permalink
Merge pull request #655 from metodej/PT-237
Browse files Browse the repository at this point in the history
feat: new script oidc_with_groups
  • Loading branch information
metodej authored Nov 30, 2021
2 parents 8b306f9 + cdedf00 commit 997b63f
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 0 deletions.
120 changes: 120 additions & 0 deletions gen/oidc_with_groups
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
#!/usr/bin/perl

use strict;
use warnings;
use File::Basename;
use perunServicesInit;
use Exporter 'import';

our $JSON_FORMAT = "json";
our @EXPORT = qw($JSON_FORMAT);

sub process_user;

local $::SERVICE_NAME = basename($0);
local $::PROTOCOL_VERSION = "3.0.0";
my $SCRIPT_VERSION = "3.0.0";

perunServicesInit::init;
my $DIRECTORY = perunServicesInit::getDirectory;
my $data = perunServicesInit::getHashedDataWithGroups;
my $agent = perunServicesInit->getAgent;
my $attributesAgent = $agent->getAttributesAgent;
my $servicesAgent = $agent->getServicesAgent;
my $service = $servicesAgent->getServiceByName( name => $::SERVICE_NAME);

our $A_R_UNIX_NAME; *A_R_UNIX_NAME = \'urn:perun:resource:attribute-def:def:unixGroupName-namespace:einfra';
our $A_G_UNIX_NAME; *A_G_UNIX_NAME = \'urn:perun:group:attribute-def:def:unixGroupName-namespace:einfra';

my @requiredAttributesDefinitions = $attributesAgent->getRequiredAttributesDefinition(service => $service->getId);
my @userRequiredAttributes = ();
my @userFacilityRequiredAttributes = ();
foreach my $attrDef (@requiredAttributesDefinitions) {
# if attribute's namespace starts with "urn:perun:user:"
my $o = index $attrDef->getNamespace, "urn:perun:user:";
if ($o == 0) {
push @userRequiredAttributes, $attrDef;
next;
}
$o = index $attrDef->getNamespace, "urn:perun:user_facility:";
if ($o == 0) {
push @userFacilityRequiredAttributes, $attrDef;
next;
}
}

####### prepare data ######################
my %usersIds = ();
my %groupData = ();
foreach my $resourceId ($data->getResourceIds()) {

my $resourceName = $data->getResourceAttributeValue(resource => $resourceId, attrName => $A_R_UNIX_NAME);

my @users;
# process members on resource
foreach my $memberId ($data->getMemberIdsForResource(resource => $resourceId)) {
my $userId = $data->getUserIdForMember(member => $memberId);
if (exists($usersIds{$userId}) && $usersIds{$userId} eq $resourceName) {
next;
} else {
$usersIds{$userId} = $resourceName;
}
my $userData = process_user($memberId);
push @users, $userData;
}
push(@{$groupData{$resourceName}}, @users);

# process members on groups of resource
foreach my $groupId ($data->getGroupIdsForResource(resource => $resourceId)) {
my $groupName = $data->getGroupAttributeValue(group => $groupId, attrName => $A_G_UNIX_NAME);
my @usersFromGroup;

foreach my $memberId ($data->getMemberIdsForResourceAndGroup(resource => $resourceId, group => $groupId)) {
my $userId = $data->getUserIdForMember(member => $memberId);
if (exists($usersIds{$userId}) && $usersIds{$userId} eq $groupName) {
next;
} else {
$usersIds{$userId} = $groupName;
}
my $userData = process_user($memberId);
push @usersFromGroup, $userData;
}
push(@{$groupData{$groupName}}, @usersFromGroup);
}
}

####### output file ######################
my $fileName = "$DIRECTORY/$::SERVICE_NAME";
open FILE, ">$fileName" or die "Cannot open $fileName: $! \n";
print FILE JSON::XS->new->utf8->pretty->canonical->encode(\%groupData);
close FILE or die "Cannot close $fileName: $! \n";

perunServicesInit::finalize;
# END of main script

sub process_user($) {
my $memberId = shift;

my $userData = ();
foreach my $userAttribute (@userRequiredAttributes) {
my $attrValue = $data->getUserAttributeValue(member => $memberId, attrName => $userAttribute->getName);
# In case there is an undefined boolean attribute, we have to change it to false
if ($userAttribute->getType eq "boolean" && !defined $attrValue) {
$userData->{$userAttribute->getName} = \0;
} else {
$userData->{$userAttribute->getName} = $attrValue;
}
}

foreach my $userFacilityAttribute (@userFacilityRequiredAttributes) {
my $attrValue = $data->getUserFacilityAttributeValue(member => $memberId, attrName => $userFacilityAttribute->getName);
# In case there is an undefined boolean attribute, we have to change it to false
if ($userFacilityAttribute->getType eq "boolean" && !defined $attrValue) {
$userData->{$userFacilityAttribute->getName} = \0;
} else {
$userData->{$userFacilityAttribute->getName} = $attrValue;
}
}

return $userData;
}
4 changes: 4 additions & 0 deletions send/oidc_with_groups
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/bash
SERVICE_NAME="oidc_with_groups"

. generic_send

0 comments on commit 997b63f

Please sign in to comment.