Skip to content

Commit

Permalink
Merge pull request #731 from zlamalp/vsup
Browse files Browse the repository at this point in the history
feat(vsup_is): resolve both manual and DC2 expirations
  • Loading branch information
zlamalp authored Sep 27, 2022
2 parents 857b12d + f44db7e commit 99a56cd
Showing 1 changed file with 39 additions and 2 deletions.
41 changes: 39 additions & 2 deletions gen/vsup_is
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ use strict;
use warnings;
use perunServicesInit;
use perunServicesUtils;
use Time::Piece;
sub resolveExpiration;

local $::SERVICE_NAME = "vsup_is";
local $::PROTOCOL_VERSION = "3.0.0";
my $SCRIPT_VERSION = "3.0.0";
my $SCRIPT_VERSION = "3.0.1";

perunServicesInit::init;
my $DIRECTORY = perunServicesInit::getDirectory;
Expand All @@ -31,6 +33,7 @@ our $A_BIRTH_NUMBER; *A_BIRTH_NUMBER = \'urn:perun:user:attribute-def:def:birth
our $A_GENDER; *A_GENDER = \'urn:perun:user:attribute-def:def:gender';
our $A_JOB; *A_JOB = \'urn:perun:user:attribute-def:def:job';
our $A_EXPIRATION_DC2; *A_EXPIRATION_DC2 = \'urn:perun:user:attribute-def:def:expirationDc2';
our $A_EXPIRATION_MANUAL; *A_EXPIRATION_MANUAL = \'urn:perun:user:attribute-def:def:expirationManual';

our $A_PHONE; *A_PHONE = \'urn:perun:user:attribute-def:def:phoneDc2';
our $A_EMAIL_PRIV; *A_EMAIL_PRIV = \'urn:perun:user:attribute-def:opt:privateMail';
Expand Down Expand Up @@ -73,6 +76,7 @@ foreach my $resourceId ($data->getResourceIds()) {
my $gender = $data->getUserAttributeValue(member => $memberId, attrName => $A_GENDER);
my $job = $data->getUserAttributeValue(member => $memberId, attrName => $A_JOB);
my $expirationDC2 = $data->getUserAttributeValue(member => $memberId, attrName => $A_EXPIRATION_DC2);
my $expirationManual = $data->getUserAttributeValue(member => $memberId, attrName => $A_EXPIRATION_MANUAL);
my $phone = $data->getUserAttributeValue(member => $memberId, attrName => $A_PHONE);
my $emailPriv = $data->getUserAttributeValue(member => $memberId, attrName => $A_EMAIL_PRIV);
my $emailPrivKOS = $data->getUserAttributeValue(member => $memberId, attrName => $A_EMAIL_PRIV_KOS);
Expand All @@ -94,7 +98,7 @@ foreach my $resourceId ($data->getResourceIds()) {
$users->{$key}->{$A_BIRTH_NUMBER} = $birthNumber || '';
$users->{$key}->{$A_GENDER} = $gender || '';
$users->{$key}->{$A_JOB} = $job || '';
$users->{$key}->{$A_EXPIRATION_DC2} = $expirationDC2 || '';
$users->{$key}->{$A_EXPIRATION_DC2} = resolveExpiration($expirationDC2, $expirationManual);

$users->{$key}->{$A_PHONE} = $phone || '';
$users->{$key}->{$A_EMAIL_PRIV} = $emailPriv || ($emailPrivKOS || '');
Expand Down Expand Up @@ -131,3 +135,36 @@ for my $key (@keys) {
close(FILE);

perunServicesInit::finalize;

#
# Return latest of two expiration dates (DC2 / Manual)
#
sub resolveExpiration() {

# read input
my $expirationDc2 = shift;
my $expirationMan = shift;
# parse to time or undef
my $expirationDc2Time = (defined $expirationDc2 and length($expirationDc2) > 0) ? Time::Piece->strptime($expirationDc2,"%Y-%m-%d") : undef;
my $expirationManTime = (defined $expirationMan and length($expirationMan) > 0) ? Time::Piece->strptime($expirationMan,"%Y-%m-%d") : undef;

my @expirations = ();
if (defined $expirationDc2Time) { push(@expirations, $expirationDc2Time->epoch); }
if (defined $expirationManTime) { push(@expirations, $expirationManTime->epoch); }

# sort all expirations
my @sorted_expirations = sort { $a <=> $b } @expirations;
my $latest_expiration = $sorted_expirations[$#sorted_expirations];

if (defined $latest_expiration) {
if (defined $expirationDc2Time and ($latest_expiration == $expirationDc2Time->epoch)) {
return $expirationDc2;
}
if (defined $expirationManTime and ($latest_expiration == $expirationManTime->epoch)) {
return $expirationMan;
}
}
# expiration not set
return '';

}

0 comments on commit 99a56cd

Please sign in to comment.