From cdedf0083077ae2b0bd2038e8fc1d270880f05cd Mon Sep 17 00:00:00 2001 From: Metodej Klang Date: Tue, 23 Nov 2021 15:05:30 +0100 Subject: [PATCH] feat: new script oidc_with_groups - added gen and send scripts for new service oidc_with_groups --- gen/oidc_with_groups | 120 ++++++++++++++++++++++++++++++++++++++++++ send/oidc_with_groups | 4 ++ 2 files changed, 124 insertions(+) create mode 100755 gen/oidc_with_groups create mode 100644 send/oidc_with_groups diff --git a/gen/oidc_with_groups b/gen/oidc_with_groups new file mode 100755 index 00000000..aa11e4b5 --- /dev/null +++ b/gen/oidc_with_groups @@ -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; +} diff --git a/send/oidc_with_groups b/send/oidc_with_groups new file mode 100644 index 00000000..086f0139 --- /dev/null +++ b/send/oidc_with_groups @@ -0,0 +1,4 @@ +#!/bin/bash +SERVICE_NAME="oidc_with_groups" + +. generic_send