-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #655 from metodej/PT-237
feat: new script oidc_with_groups
- Loading branch information
Showing
2 changed files
with
124 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
#!/bin/bash | ||
SERVICE_NAME="oidc_with_groups" | ||
|
||
. generic_send |