Skip to content
118 changes: 84 additions & 34 deletions resources/lib/UnityGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,11 @@ private function reenable(bool $send_mail = true): void
}
$this->SQL->addLog("reenabled_pi_group", $this->gid);
if ($send_mail) {
$this->MAILER->sendMail($this->getOwner()->getMail(), "group_reenabled", [
"group_name" => $this->gid,
]);
$this->MAILER->sendMail(
$this->getOwnerMailAndPlusAddressedManagerMails(),
"group_reenabled",
["group_name" => $this->gid],
);
}
$this->setIsDisabled(false);
$owner_uid = $this->getOwner()->uid;
Expand Down Expand Up @@ -176,9 +178,11 @@ public function cancelGroupJoinRequest(UnityUser $user, bool $send_mail = true):
}
$this->SQL->removeRequest($user->uid, $this->gid);
if ($send_mail) {
$this->MAILER->sendMail($this->getOwner()->getMail(), "group_join_request_cancelled", [
"uid" => $user->uid,
]);
$this->MAILER->sendMail(
$this->getOwnerMailAndPlusAddressedManagerMails(),
"group_join_request_cancelled",
["uid" => $user->uid],
);
}
}

Expand All @@ -200,13 +204,17 @@ public function approveUser(UnityUser $new_user, bool $send_mail = true): void
$this->MAILER->sendMail($new_user->getMail(), "group_user_added", [
"group" => $this->gid,
]);
$this->MAILER->sendMail($this->getOwner()->getMail(), "group_user_added_owner", [
"group" => $this->gid,
"user" => $new_user->uid,
"name" => $new_user->getFullname(),
"email" => $new_user->getMail(),
"org" => $new_user->getOrg(),
]);
$this->MAILER->sendMail(
$this->getOwnerMailAndPlusAddressedManagerMails(),
"group_user_added_owner",
[
"group" => $this->gid,
"user" => $new_user->uid,
"name" => $new_user->getFullname(),
"email" => $new_user->getMail(),
"org" => $new_user->getOrg(),
],
);
}
$new_user->updateIsQualified($send_mail); // being in a group makes you qualified
}
Expand All @@ -224,13 +232,17 @@ public function denyUser(UnityUser $new_user, bool $send_mail = true): void
$this->MAILER->sendMail($new_user->getMail(), "group_user_denied", [
"group" => $this->gid,
]);
$this->MAILER->sendMail($this->getOwner()->getMail(), "group_user_denied_owner", [
"group" => $this->gid,
"user" => $new_user->uid,
"name" => $new_user->getFullName(),
"email" => $new_user->getMail(),
"org" => $new_user->getOrg(),
]);
$this->MAILER->sendMail(
$this->getOwnerMailAndPlusAddressedManagerMails(),
"group_user_denied_owner",
[
"group" => $this->gid,
"user" => $new_user->uid,
"name" => $new_user->getFullName(),
"email" => $new_user->getMail(),
"org" => $new_user->getOrg(),
],
);
}
}

Expand All @@ -251,13 +263,17 @@ public function removeUser(UnityUser $new_user, bool $send_mail = true): void
$this->MAILER->sendMail($new_user->getMail(), "group_user_removed", [
"group" => $this->gid,
]);
$this->MAILER->sendMail($this->getOwner()->getMail(), "group_user_removed_owner", [
"group" => $this->gid,
"user" => $new_user->uid,
"name" => $new_user->getFullName(),
"email" => $new_user->getMail(),
"org" => $new_user->getOrg(),
]);
$this->MAILER->sendMail(
$this->getOwnerMailAndPlusAddressedManagerMails(),
"group_user_removed_owner",
[
"group" => $this->gid,
"user" => $new_user->uid,
"name" => $new_user->getFullName(),
"email" => $new_user->getMail(),
"org" => $new_user->getOrg(),
],
);
}
// if user is no longer in any PI group, disqualify them
$new_user->updateIsQualified($send_mail);
Expand All @@ -281,13 +297,17 @@ public function newUserRequest(UnityUser $new_user, bool $send_mail = true): voi
$this->MAILER->sendMail($new_user->getMail(), "group_user_request", [
"group" => $this->gid,
]);
$this->MAILER->sendMail($this->getOwner()->getMail(), "group_user_request_owner", [
"group" => $this->gid,
"user" => $new_user->uid,
"name" => $new_user->getFullname(),
"email" => $new_user->getMail(),
"org" => $new_user->getOrg(),
]);
$this->MAILER->sendMail(
$this->getOwnerMailAndPlusAddressedManagerMails(),
"group_user_request_owner",
[
"group" => $this->gid,
"user" => $new_user->uid,
"name" => $new_user->getFullname(),
"email" => $new_user->getMail(),
"org" => $new_user->getOrg(),
],
);
}
}

Expand Down Expand Up @@ -469,4 +489,34 @@ public function removeMemberUID(string $uid): void
}
parent::removeMemberUID($uid);
}

public function addPlusAddressToMail(string $mail): string
{
$matches = [];
$owner_uid = $this->getOwner()->uid;
_preg_match("/(.*)_[^_]+_[^_]+$/", $owner_uid, $matches);
ensure(count($matches) == 2, "failed to extract org from uid: '$owner_uid'");
$short_name = $matches[1];
$parts = explode("@", $mail, 2);
return sprintf("%s+%s@%s", $parts[0], $short_name, $parts[1]);
}

/** @return string[] */
private function getOwnerMailAndPlusAddressedManagerMails(): array
{
$mails = [$this->getOwner()->getMail()];
foreach ($this->getManagerUIDs() as $manager_uid) {
$manager = new UnityUser(
$manager_uid,
$this->LDAP,
$this->SQL,
$this->MAILER,
$this->WEBHOOK,
);
array_push($mails, $this->addPlusAddressToMail($manager->getMail()));
}
$mails = array_unique($mails);
sort($mails);
return $mails;
}
}
20 changes: 18 additions & 2 deletions resources/lib/utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ function _preg_replace(
/**
* @param 0|256|512|768 $flags
* @param null|array{} $matches
* @param-out array<list<int|string|null>|string|null> $matches
* @param-out string[] $matches
* @throws Exception
*/
function _preg_match(
Expand All @@ -223,10 +223,26 @@ function _preg_match(
int $flags = 0,
int $offset = 0,
): int {
$output = preg_match($pattern, $subject, $matches, $flags, $offset);
$dirty_matches = [];
$output = preg_match($pattern, $subject, $dirty_matches, $flags, $offset);
if ($output === false) {
throw new Exception("preg_match returned false!");
}
$clean_matches = [];
foreach ($dirty_matches as $i => $match) {
if (!is_string($match)) {
throw new Exception(
sprintf(
"preg_match made matches '%s', but match index %s is not a string!",
_json_encode($matches),
$i,
),
);
} else {
array_push($clean_matches, $match);
}
}
$matches = $clean_matches;
return $output;
}

Expand Down
19 changes: 3 additions & 16 deletions workers/unity-course.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,6 @@ function cn2org($cn)
return $matches[1];
}

function insert_plus_address($email, $plus)
{
$parts = explode("@", $email, 2);
return $parts[0] . "+" . $plus . "@" . $parts[1];
}

function strip_org($cn)
{
$matches = [];
_preg_match("/(.*)_[^_]+_[^_]+$/", $cn, $matches);
ensure(count($matches) == 2, "failed to extract org from cn: '$cn'");
return $matches[1];
}

// if array is length 1 then replace it with its one element
function flatten_attributes(array $attributes): array
{
Expand Down Expand Up @@ -57,10 +43,11 @@ function flatten_attributes(array $attributes): array
if (!$org->exists()) {
print "WARNING: creating new org '$org_gid'...\n";
}
$mail = insert_plus_address($manager->getMail(), strip_org($cn));
$course_user->init($givenName, $sn, $mail, $org_gid);

$course_pi_group = $course_user->getPIGroup();
$mail = $course_pi_group->addPlusAddressToMail($manager->getMail());
$course_user->init($givenName, $sn, $mail, $org_gid);

if ($course_pi_group->exists()) {
$course_pi_group_dn = $LDAP->getPIGroupEntry($course_pi_group->gid)->getDN();
_die("course PI group already exists: '$course_pi_group_dn'", 1);
Expand Down