Skip to content

Commit

Permalink
migrate remaining o365 services to use getHierarchicalData (#551)
Browse files Browse the repository at this point in the history
* move o365 services to use getHierarchicalData
  • Loading branch information
mvocu authored Feb 5, 2021
1 parent f7ac43e commit 89a430d
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 100 deletions.
32 changes: 17 additions & 15 deletions gen/o365
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ sub processMembers;

our $SERVICE_NAME = "o365";
our $PROTOCOL_VERSION = "3.0.0";
my $SCRIPT_VERSION = "3.0.0";
my $SCRIPT_VERSION = "3.0.1";

perunServicesInit::init;
my $DIRECTORY = perunServicesInit::getDirectory;
my $data = perunServicesInit::getHierarchicalData;
my $data = perunServicesInit::getHashedHierarchicalData;

our $A_USER_FIRST_NAME; *A_USER_FIRST_NAME = \'urn:perun:user:attribute-def:core:firstName';
our $A_USER_LAST_NAME; *A_USER_LAST_NAME = \'urn:perun:user:attribute-def:core:lastName';
Expand All @@ -35,14 +35,15 @@ our $USER_TYPE = 'member';
our $outputData = {value => []};
our $members = {};

my %facilityAttributes = attributesToHash $data->getAttributes;
our $usageLocation = $facilityAttributes{$A_FACILITY_USAGE_LOCATION};
our $usageLocation = $data->getFacilityAttributeValue( attrName => $A_FACILITY_USAGE_LOCATION );

foreach my $resourceData ($data->getChildElements) {
my @membersData = $resourceData->getChildElements;
my %resourceAttributes = attributesToHash $resourceData->getAttributes;
foreach my $resourceId ($data->getResourceIds()) {

processMembers(\@membersData, $resourceAttributes{$A_RESOURCE_ASSIGNED_LICENSE}, $facilityAttributes{$A_FACILITY_DOMAIN_NAME});
my $assignedLicense = $data->getResourceAttributeValue( resource => $resourceId, attrName => $A_RESOURCE_ASSIGNED_LICENSE );
my $facilityDomain = $data->getFacilityAttributeValue( attrName => $A_FACILITY_DOMAIN_NAME );
my @memberIds = $data->getMemberIdsForResource( resource => $resourceId );

processMembers(\@memberIds, $assignedLicense, $facilityDomain );
}

# format of 1 line in photos.csv file:
Expand Down Expand Up @@ -88,12 +89,13 @@ perunServicesInit::finalize;
##############################################################################

sub processMembers {
my @membersData = @{$_[0]};
#X my @membersData = @{$_[0]};
my $memberIds = $_[0];
my $license = $_[1];
my $domainName = $_[2];

foreach my $memberData (dataToAttributesHashes @membersData) {
my $mail = $memberData->{$A_USER_FACILITY_LOGIN} . '@' . $domainName;
foreach my $memberId (@{$memberIds}) {
my $mail = $data->getUserFacilityAttributeValue( member => $memberId, attrName => $A_USER_FACILITY_LOGIN ) . '@' . $domainName;

if($members->{$mail}) {
push @{$members->{$mail}->{'assignedLicenses'}}, $license;
Expand All @@ -102,14 +104,14 @@ sub processMembers {
my $member = {
accountEnabled => \1,
assignedLicenses => \@licenses,
displayName => $memberData->{$A_USER_DISPLAY_NAME},
givenName => $memberData->{$A_USER_FIRST_NAME},
displayName => $data->getUserAttributeValue( member => $memberId, attrName => $A_USER_DISPLAY_NAME ),
givenName => $data->getUserAttributeValue( member => $memberId, attrName => $A_USER_FIRST_NAME ),
mail => $mail,
surname => $memberData->{$A_USER_LAST_NAME},
surname => $data->getUserAttributeValue( member => $memberId, attrName => $A_USER_LAST_NAME ),
usageLocation => $usageLocation,
userPrincipalName => $mail,
userType => $USER_TYPE,
photo => $memberData->{$A_USER_PHOTO}
photo => $data->getUserAttributeValue( member => $memberId, attrName => $A_USER_PHOTO )
};

$members->{$mail} = $member;
Expand Down
38 changes: 16 additions & 22 deletions gen/o365_contacts_export
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ use perunServicesUtils;

our $SERVICE_NAME = "o365_contacts_export";
our $PROTOCOL_VERSION = "3.0.0";
our $SCRIPT_VERSION = "3.0.1";

perunServicesInit::init;
my $DIRECTORY = perunServicesInit::getDirectory;
my $data = perunServicesInit::getHierarchicalData;
my $data = perunServicesInit::getHashedHierarchicalData;

#Constants
our $A_USER_LOGIN_MU; *A_USER_LOGIN_MU = \'urn:perun:user:attribute-def:def:login-namespace:mu';
Expand All @@ -21,31 +22,24 @@ our $A_FACILITY_O365_ALLOWED_DOMAINS; *A_FACILITY_O365_ALLOWED_DOMAINS
my $file_name = "$DIRECTORY/$::SERVICE_NAME";
open FILE,">$file_name" or die "Cannot open $file_name: $! \n";

my %facilityAttributes = attributesToHash $data->getAttributes;
my %allowedDomains = map { $_ => 1 } @{$facilityAttributes{$A_FACILITY_O365_ALLOWED_DOMAINS}};
my %allowedDomains = map { $_ => 1 } @{$data->getFacilityAttributeValue( attrName => $A_FACILITY_O365_ALLOWED_DOMAINS )};

my $memberDataByLogin;
my @resourcesData = $data->getChildElements;
foreach my $resourceData (@resourcesData) {
my @membersData = $resourceData->getChildElements;

foreach my $memberData (@membersData) {
my %memberAttributes = attributesToHash $memberData->getAttributes;
my $login = $memberAttributes{$A_USER_LOGIN_MU};
my @emailAddresses = @{$memberAttributes{$A_MEMBER_O365_EMAIL_ADDRESSES}};

my @allowedEmails = ();
foreach my $email (@emailAddresses) {
my $emailDomain = $email;
$emailDomain =~ s/^.*@//g;
if($allowedDomains{$emailDomain}) {
push @allowedEmails, $email;
}
foreach my $memberId ($data->getMemberIdsForFacility()) {
my $login = $data->getUserAttributeValue( member => $memberId, attrName => $A_USER_LOGIN_MU );
my @emailAddresses = @{$data->getMemberAttributeValue( member => $memberId, attrName => $A_MEMBER_O365_EMAIL_ADDRESSES )};

my @allowedEmails = ();
foreach my $email (@emailAddresses) {
my $emailDomain = $email;
$emailDomain =~ s/^.*@//g;
if($allowedDomains{$emailDomain}) {
push @allowedEmails, $email;
}

@allowedEmails = uniqList @allowedEmails, @{$memberDataByLogin->{$login}};
$memberDataByLogin->{$login} = \@allowedEmails;
}

@allowedEmails = uniqList @allowedEmails, @{$memberDataByLogin->{$login}};
$memberDataByLogin->{$login} = \@allowedEmails;
}

#print data to file
Expand Down
50 changes: 22 additions & 28 deletions gen/o365_life_cycle_manager_mu
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@ use utf8;

local $::SERVICE_NAME = "o365_life_cycle_manager_mu";
local $::PROTOCOL_VERSION = "1.2.0";
my $SCRIPT_VERSION = "1.2.1";

#get system time in miliseconds before starting getting data
my $systemTimeInMillis = int (gettimeofday * 1000);

perunServicesInit::init;
my $DIRECTORY = perunServicesInit::getDirectory;
my $data = perunServicesInit::getDataWithGroups;
my $data = perunServicesInit::getHashedDataWithGroups;

#forward declaration
sub processUsers;
Expand All @@ -41,8 +42,6 @@ my $o365MailForwardHeader = "o365MailForward";
my $o365DeleteAccountRequestHeader = "o365DeleteAccountRequest";

#Constants
our $A_USER_ID; *A_USER_ID = \'urn:perun:user:attribute-def:core:id';
our $A_MEMBER_ID; *A_MEMBER_ID = \'urn:perun:member:attribute-def:core:id';
our $A_USER_LOGIN_MU; *A_USER_LOGIN_MU = \'urn:perun:user:attribute-def:def:login-namespace:mu';
our $A_USER_PREFERRED_MAIL; *A_USER_PREFERRED_MAIL = \'urn:perun:user:attribute-def:def:preferredMail';
our $A_RESOURCE_AFFILIATIONS; *A_RESOURCE_AFFILIATIONS = \'urn:perun:resource:attribute-def:def:affiliation';
Expand All @@ -68,26 +67,23 @@ my $userStruc = {};
my $file = $DIRECTORY . "/data.json";

#foreach resource in the structure
foreach my $resourceData ($data->getChildElements) {
my %resourceAttributes = attributesToHash $resourceData->getAttributes;
foreach my $resourceId ($data->getResourceIds()) {
#defined affilation for whole resource
my $affiliation = $resourceAttributes{$A_RESOURCE_AFFILIATIONS};
my $affiliation = $data->getResourceAttributeValue( resource => $resourceId, attrName => $A_RESOURCE_AFFILIATIONS );

#for each group assigned to the resource
foreach my $groupData (($resourceData->getChildElements)[0]->getChildElements) {
my $membersElement = ($groupData->getChildElements)[1];
foreach my $groupId ($data->getGroupIdsForResource( resource => $resourceId )) {

#and every member in such group
for my $memberData ($membersElement->getChildElements) {
my %memberAttributes = attributesToHash $memberData->getAttributes;
my $userId = $memberAttributes{$A_USER_ID};
foreach my $memberId ($data->getMemberIdsForResourceAndGroup( resource => $resourceId, group => $groupId )) {
my $userId = $data->getUserIdForMember( member => $memberId );
#skip expired membership in group
my $statusInGroup = $memberAttributes{$A_MEMBER_STATUS_IN_GROUP};
my $statusInGroup = $data->getMemberGroupAttributeValue( member => $memberId, group => $groupId, attrName => $A_MEMBER_STATUS_IN_GROUP );
next if $statusInGroup eq 'EXPIRED';
my $expirationInGroup = $memberAttributes{$A_GROUP_MEMBERSHIP_EXPIRATION};
my $expirationInGroup = $data->getMemberGroupAttributeValue( member => $memberId, group => $groupId, attrName => $A_GROUP_MEMBERSHIP_EXPIRATION );

#process every member|group|resource combination one by one
processUsers $affiliation, $expirationInGroup, $memberData;
processUsers $affiliation, $expirationInGroup, $memberId;
}
}
}
Expand Down Expand Up @@ -138,11 +134,10 @@ perunServicesInit::finalize;
##############################################################################
## creates structure for users.json
sub processUsers {
my ($affiliation, $expirationInGroup, $memberData) = @_;
my ($affiliation, $expirationInGroup, $memberId) = @_;

my %memberAttributes = attributesToHash $memberData->getAttributes;
my $userId = $memberAttributes{$A_USER_ID};
my $login = $memberAttributes{$A_USER_LOGIN_MU};
my $userId = $data->getUserIdForMember( member => $memberId );
my $login = $data->getUserAttributeValue( member => $memberId, attrName => $A_USER_LOGIN_MU );
#skip service accounts (login like 's-*')
if( $login =~ /s-/ ) { return; }

Expand Down Expand Up @@ -177,16 +172,15 @@ sub processUsers {
}
#this is the first time user as occured in any processed group so we need to create a new full record
} else {
my $memberId = $memberAttributes{$A_MEMBER_ID};
my $preferredMail = $memberAttributes{$A_USER_PREFERRED_MAIL};
my $o365AlumniAccountEnabled = $memberAttributes{$A_USER_FAC_ALLUMNI_ACC_ENABLED};
my $o365GracePeriodExtension = $memberAttributes{$A_USER_FAC_GRACE_PERIOD_EXT};
my $eligibleForO365AlumniLicence = $memberAttributes{$A_USER_FAC_ELIGIBLE_FOR_LICENCE};
my $o365PrivilegedLicence = $memberAttributes{$A_USER_FAC_PRIVILEGED_LICENCE};
my $o365UserEmailAddresses = $memberAttributes{$A_USER_O365_USER_EMAILS_MU};
my $o365PrimaryEmailAddress = $memberAttributes{$A_USER_O365_PRIMARY_EMAIL_MU};
my $o365MailForward = $memberAttributes{$A_USER_FAC_O365_MAIL_FORWARD};
my $o365DeleteAccountRequest = $memberAttributes{$A_USER_FAC_DEL_ACC_REQUEST};
my $preferredMail = $data->getUserAttributeValue( member => $memberId, attrName => $A_USER_PREFERRED_MAIL );
my $o365AlumniAccountEnabled = $data->getUserFacilityAttributeValue( member => $memberId, attrName => $A_USER_FAC_ALLUMNI_ACC_ENABLED );
my $o365GracePeriodExtension = $data->getUserFacilityAttributeValue( member => $memberId, attrName => $A_USER_FAC_GRACE_PERIOD_EXT );
my $eligibleForO365AlumniLicence = $data->getUserFacilityAttributeValue( member => $memberId, attrName => $A_USER_FAC_ELIGIBLE_FOR_LICENCE );
my $o365PrivilegedLicence = $data->getUserFacilityAttributeValue( member => $memberId, attrName => $A_USER_FAC_PRIVILEGED_LICENCE );
my $o365UserEmailAddresses = $data->getUserAttributeValue( member => $memberId, attrName => $A_USER_O365_USER_EMAILS_MU );
my $o365PrimaryEmailAddress = $data->getUserAttributeValue( member => $memberId, attrName => $A_USER_O365_PRIMARY_EMAIL_MU );
my $o365MailForward = $data->getUserFacilityAttributeValue( member => $memberId, attrName => $A_USER_FAC_O365_MAIL_FORWARD );
my $o365DeleteAccountRequest = $data->getUserFacilityAttributeValue( member => $memberId, attrName => $A_USER_FAC_DEL_ACC_REQUEST );
$userStruc->{$userId}->{$memberIdHeader} = $memberId;
$userStruc->{$userId}->{$loginHeader} = $login;
$userStruc->{$userId}->{$preferredMailHeader} = $preferredMail;
Expand Down
61 changes: 26 additions & 35 deletions gen/o365_mail_forward_export
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ use Data::Dumper;

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

perunServicesInit::init;
my $DIRECTORY = perunServicesInit::getDirectory;
my $data = perunServicesInit::getHierarchicalData;
my $data = perunServicesInit::getHashedHierarchicalData;

#Constants
our $A_MEMBER_O365_EMAIL_ADDRESSES_MU; *A_MEMBER_O365_EMAIL_ADDRESSES_MU = \'urn:perun:member:attribute-def:def:o365EmailAddresses:mu';
Expand All @@ -25,45 +25,36 @@ our $A_FACILITY_FORWARD_EXPORT_DOMAINS; *A_FACILITY_FORWARD_EXPORT_DOMAINS =
our $DEFAULT_FORWARDING_DOMAIN = '@mo.muni.cz';
our $userStruc = {};

my %facilityAttributes = attributesToHash $data->getAttributes;
my %allowedForwardDomains = map { $_ => 1 } @{$facilityAttributes{$A_FACILITY_FORWARD_EXPORT_DOMAINS}};
my %allowedForwardDomains = map { $_ => 1 } @{$data->getFacilityAttributeValue( attrName => $A_FACILITY_FORWARD_EXPORT_DOMAINS )};

#go through all resources
my @resourcesData = $data->getChildElements;
foreach my $resourceData (@resourcesData) {

my @membersData = $resourceData->getChildElements;
foreach my $memberData (@membersData) {
my %memberAttributes = attributesToHash $memberData->getAttributes;
foreach my $memberId ($data->getMemberIdsForFacility()) {

#skip this user if disabled forward is set to true
next if $memberAttributes{$A_USER_FACILITY_DISABLE_MAIL_FORWARD};
#skip this user if store and forward is set to true
next if $memberAttributes{$A_USER_FACILITY_STORE_AND_FORWARD};
#skip this user if disabled forward is set to true
next if $data->getUserFacilityAttributeValue( member => $memberId, attrName => $A_USER_FACILITY_DISABLE_MAIL_FORWARD );
#skip this user if store and forward is set to true
next if $data->getUserFacilityAttributeValue( member => $memberId, attrName => $A_USER_FACILITY_STORE_AND_FORWARD );

#login in MU is UCO
my $login = $memberAttributes{$A_USER_FACILITY_LOGIN};
#login in MU is UCO
my $login = $data->getUserFacilityAttributeValue( member => $memberId, attrName => $A_USER_FACILITY_LOGIN );

#mailForward is unique for the same login, don't need to be loaded more than once
unless ($userStruc->{$login}) {
my $mailForward = $memberAttributes{$A_USER_FACILITY_MAIL_FORWARD} || $login . $DEFAULT_FORWARDING_DOMAIN;
$userStruc->{$login}->{$A_USER_FACILITY_MAIL_FORWARD} = $mailForward;

}
#mailForward is unique for the same login, don't need to be loaded more than once
unless ($userStruc->{$login}) {
my $mailForward = $data->getUserFacilityAttributeValue( member => $memberId, attrName => $A_USER_FACILITY_MAIL_FORWARD ) || $login . $DEFAULT_FORWARDING_DOMAIN;
$userStruc->{$login}->{$A_USER_FACILITY_MAIL_FORWARD} = $mailForward;
}

#members email addresses - for more members with same login can differ
my @memberEmailAddresses = @{$memberAttributes{$A_MEMBER_O365_EMAIL_ADDRESSES_MU}};
#filter only allowed email addresses
foreach my $mail (@memberEmailAddresses) {
my $domainOfEmail = $mail;
$domainOfEmail =~ s/^.*@//;
#this email is in allowed domain, add it
if($allowedForwardDomains{$domainOfEmail}) {
$userStruc->{$login}->{$A_MEMBER_O365_EMAIL_ADDRESSES_MU}->{$mail} = 1;
}
#members email addresses - for more members with same login can differ
my @memberEmailAddresses = @{$data->getMemberAttributeValue( member => $memberId, attrName => $A_MEMBER_O365_EMAIL_ADDRESSES_MU )};
#filter only allowed email addresses
foreach my $mail (@memberEmailAddresses) {
my $domainOfEmail = $mail;
$domainOfEmail =~ s/^.*@//;
#this email is in allowed domain, add it
if($allowedForwardDomains{$domainOfEmail}) {
$userStruc->{$login}->{$A_MEMBER_O365_EMAIL_ADDRESSES_MU}->{$mail} = 1;
}
}
}
}
}

my $fileName = "$DIRECTORY/$::SERVICE_NAME";
open FILE,">$fileName" or die "Cannot open $fileName: $! \n";
Expand Down

0 comments on commit 89a430d

Please sign in to comment.