Skip to content

Added slack webhook notification functionality #65

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions defaults/config.ini.default
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,9 @@ sender_name = "Unity Sender"
pi_approve = "piapproval@unitywebportal.test" ; Only PI approval messages will go to this email
pi_approve_name = "Unity PI Approval"

[webhook] ; webhook to send messages to admins
url = "https://hooks.slack.com/services/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"

[page] ; which sql objects to use for the content on these pages
home = "home"
support = "support"
Expand Down
1 change: 1 addition & 0 deletions resources/autoload.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
require_once __DIR__ . "/lib/UnitySSO.php";
require_once __DIR__ . "/lib/UnitySite.php";
require_once __DIR__ . "/lib/UnityConfig.php";
require_once __DIR__ . "/lib/UnityWebhook.php";
require_once __DIR__ . "/lib/UnityRedis.php";

// run init script
Expand Down
15 changes: 12 additions & 3 deletions resources/init.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
UnitySQL,
UnitySSO,
UnityUser,
UnityRedis
UnityRedis,
UnityWebhook
};

//
Expand Down Expand Up @@ -77,6 +78,14 @@
$CONFIG["mail"]["pi_approve_name"]
);

// Creates Webhook service
$WEBHOOK = new UnityWebhook(
__DIR__ . "/mail",
__DIR__ . "/../deployment/mail_overrides",
$CONFIG["webhook"]["url"],
$CONFIG["site"]["url"] . $CONFIG["site"]["prefix"]
);

//
// SSO Init
//
Expand All @@ -86,11 +95,11 @@
// SSO is available
$_SESSION["SSO"] = $SSO;

$USER = new UnityUser($SSO["user"], $LDAP, $SQL, $MAILER, $REDIS);
$USER = new UnityUser($SSO["user"], $LDAP, $SQL, $MAILER, $REDIS, $WEBHOOK);
$_SESSION["is_admin"] = $USER->isAdmin();

if (isset($_SESSION["viewUser"]) && $_SESSION["is_admin"]) {
$USER = new UnityUser($_SESSION["viewUser"], $LDAP, $SQL, $MAILER, $REDIS);
$USER = new UnityUser($_SESSION["viewUser"], $LDAP, $SQL, $MAILER, $REDIS, $WEBHOOK);
}

$_SESSION["user_exists"] = $USER->exists();
Expand Down
23 changes: 19 additions & 4 deletions resources/lib/UnityGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class UnityGroup
private $LDAP;
private $SQL;
private $MAILER;
private $WEBHOOK;
private $REDIS;

/**
Expand All @@ -26,14 +27,15 @@ class UnityGroup
* @param LDAP $LDAP LDAP Connection
* @param SQL $SQL SQL Connection
*/
public function __construct($pi_uid, $LDAP, $SQL, $MAILER, $REDIS)
public function __construct($pi_uid, $LDAP, $SQL, $MAILER, $REDIS, $WEBHOOK)
{
$this->pi_uid = $pi_uid;

$this->LDAP = $LDAP;
$this->SQL = $SQL;
$this->MAILER = $MAILER;
$this->REDIS = $REDIS;
$this->WEBHOOK = $WEBHOOK;
}

public function equals($other_group)
Expand Down Expand Up @@ -85,6 +87,16 @@ public function requestGroup($send_mail = true)
"group_request"
);

$this->WEBHOOK->sendWebhook(
"group_request_admin",
array(
"user" => $this->getOwner()->getUID(),
"org" => $this->getOwner()->getOrg(),
"name" => $this->getOwner()->getFullname(),
"email" => $this->getOwner()->getMail()
)
);

$this->MAILER->sendMail(
"admin",
"group_request_admin",
Expand Down Expand Up @@ -356,7 +368,8 @@ public function getRequests()
$this->LDAP,
$this->SQL,
$this->MAILER,
$this->REDIS
$this->REDIS,
$this->WEBHOOK
);
array_push($out, [$user, $request["timestamp"]]);
}
Expand Down Expand Up @@ -389,7 +402,8 @@ public function getGroupMembers($ignorecache = false)
$this->LDAP,
$this->SQL,
$this->MAILER,
$this->REDIS
$this->REDIS,
$this->WEBHOOK
);
array_push($out, $user_obj);
array_push($cache_arr, $user_obj->getUID());
Expand Down Expand Up @@ -508,7 +522,8 @@ public function getOwner()
$this->LDAP,
$this->SQL,
$this->MAILER,
$this->REDIS
$this->REDIS,
$this->WEBHOOK
);
}

Expand Down
22 changes: 13 additions & 9 deletions resources/lib/UnityLDAP.php
Original file line number Diff line number Diff line change
Expand Up @@ -234,15 +234,15 @@ public function getUnassignedID($uid)
//
// Functions that return user/group objects
//
public function getAllUsers($UnitySQL, $UnityMailer, $UnityRedis, $ignorecache = false)
public function getAllUsers($UnitySQL, $UnityMailer, $UnityRedis, $UnityWebhook, $ignorecache = false)
{
$out = array();

if (!$ignorecache) {
$users = $UnityRedis->getCache("sorted_users", "");
if (!is_null($users)) {
foreach ($users as $user) {
array_push($out, new UnityUser($user, $this, $UnitySQL, $UnityMailer, $UnityRedis));
array_push($out, new UnityUser($user, $this, $UnitySQL, $UnityMailer, $UnityRedis, $UnityWebhook));
}

return $out;
Expand All @@ -252,21 +252,23 @@ public function getAllUsers($UnitySQL, $UnityMailer, $UnityRedis, $ignorecache =
$users = $this->userOU->getChildren(true);

foreach ($users as $user) {
array_push($out, new UnityUser($user->getAttribute("cn")[0], $this, $UnitySQL, $UnityMailer, $UnityRedis));
$params = array($user->getAttribute("cn")[0], $this, $UnitySQL, $UnityMailer, $UnityRedis, $UnityWebhook);
array_push($out, new UnityUser(...$params));
}

return $out;
}

public function getAllPIGroups($UnitySQL, $UnityMailer, $UnityRedis, $ignorecache = false)
public function getAllPIGroups($UnitySQL, $UnityMailer, $UnityRedis, $UnityWebhook, $ignorecache = false)
{
$out = array();

if (!$ignorecache) {
$groups = $UnityRedis->getCache("sorted_groups", "");
if (!is_null($groups)) {
foreach ($groups as $group) {
array_push($out, new UnityGroup($group, $this, $UnitySQL, $UnityMailer, $UnityRedis));
$params = array($group, $this, $UnitySQL, $UnityMailer, $UnityRedis, $UnityWebhook);
array_push($out, new UnityGroup(...$params));
}

return $out;
Expand All @@ -281,22 +283,23 @@ public function getAllPIGroups($UnitySQL, $UnityMailer, $UnityRedis, $ignorecach
$this,
$UnitySQL,
$UnityMailer,
$UnityRedis
$UnityRedis,
$UnityWebhook
));
}

return $out;
}

public function getAllOrgGroups($UnitySQL, $UnityMailer, $UnityRedis, $ignorecache = false)
public function getAllOrgGroups($UnitySQL, $UnityMailer, $UnityRedis, $UnityWebhook, $ignorecache = false)
{
$out = array();

if (!$ignorecache) {
$orgs = $UnityRedis->getCache("sorted_orgs", "");
if (!is_null($orgs)) {
foreach ($orgs as $org) {
array_push($out, new UnityOrg($org, $this, $UnitySQL, $UnityMailer, $UnityRedis));
array_push($out, new UnityOrg($org, $this, $UnitySQL, $UnityMailer, $UnityRedis, $UnityWebhook));
}

return $out;
Expand All @@ -311,7 +314,8 @@ public function getAllOrgGroups($UnitySQL, $UnityMailer, $UnityRedis, $ignorecac
$this,
$UnitySQL,
$UnityMailer,
$UnityRedis
$UnityRedis,
$UnityWebhook
));
}

Expand Down
6 changes: 4 additions & 2 deletions resources/lib/UnityOrg.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,16 @@ class UnityOrg
private $SQL;
private $LDAP;
private $REDIS;
private $WEBHOOK;

public function __construct($orgid, $LDAP, $SQL, $MAILER, $REDIS)
public function __construct($orgid, $LDAP, $SQL, $MAILER, $REDIS, $WEBHOOK)
{
$this->orgid = $orgid;

$this->LDAP = $LDAP;
$this->SQL = $SQL;
$this->MAILER = $MAILER;
$this->WEBHOOK = $WEBHOOK;
$this->REDIS = $REDIS;
}

Expand Down Expand Up @@ -82,7 +84,7 @@ public function getOrgMembers($ignorecache = false)
$out = array();
$cache_arr = array();
foreach ($members as $member) {
$user_obj = new UnityUser($member, $this->LDAP, $this->SQL, $this->MAILER, $this->REDIS);
$user_obj = new UnityUser($member, $this->LDAP, $this->SQL, $this->MAILER, $this->REDIS, $this->WEBHOOK);
array_push($out, $user_obj);
array_push($cache_arr, $user_obj->getUID());
}
Expand Down
13 changes: 9 additions & 4 deletions resources/lib/UnityUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,17 @@ class UnityUser
private $SQL;
private $MAILER;
private $REDIS;
private $WEBHOOK;

public function __construct($uid, $LDAP, $SQL, $MAILER, $REDIS)
public function __construct($uid, $LDAP, $SQL, $MAILER, $REDIS, $WEBHOOK)
{
$this->uid = $uid;

$this->LDAP = $LDAP;
$this->SQL = $SQL;
$this->MAILER = $MAILER;
$this->REDIS = $REDIS;
$this->WEBHOOK = $WEBHOOK;
}

public function equals($other_user)
Expand Down Expand Up @@ -514,7 +516,8 @@ public function getPIGroup()
$this->LDAP,
$this->SQL,
$this->MAILER,
$this->REDIS
$this->REDIS,
$this->WEBHOOK
);
}

Expand All @@ -525,7 +528,8 @@ public function getOrgGroup()
$this->LDAP,
$this->SQL,
$this->MAILER,
$this->REDIS
$this->REDIS,
$this->WEBHOOK
);
}

Expand All @@ -547,7 +551,8 @@ public function getGroups($ignorecache = false)
$this->LDAP,
$this->SQL,
$this->MAILER,
$this->REDIS
$this->REDIS,
$this->WEBHOOK
);
array_push($out, $group_obj);
}
Expand Down
69 changes: 69 additions & 0 deletions resources/lib/UnityWebhook.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

namespace UnityWebPortal\lib;

class UnityWebhook
{
private $template_dir;
private $override_template_dir;
private $url;
private $MSG_LINKREF;

public function __construct(
$template_dir,
$override_template_dir,
$url,
$msg_linkref
) {
$this->template_dir = $template_dir;
$this->override_template_dir = $override_template_dir;
$this->url = $url;
$this->MSG_LINKREF = $msg_linkref;
}

public function htmlToMarkdown($html)
{
// Define regex patterns for each markdown format
$bold = '/<(b|strong)\b[^>]*>(.*?)<\/(b|strong)>/s';
$italic = '/<i\b[^>]*>(.*?)<\/i>/s';
$strikethrough = '/<del\b[^>]*>(.*?)<\/del>/s';
$link = '/<a\b[^>]*href=["\']?([^"\'\s]*)[^>]*>(.*?)<\/a>/s';

// Replace each HTML tag with its corresponding markdown format
$md = preg_replace($bold, '*$2*', $html);
$md = preg_replace($italic, '_$1_', $md);
$md = preg_replace($strikethrough, '~$1~', $md);
$md = preg_replace($link, '$2: $1', $md);

// Replace any remaining HTML tags with an empty string
$md = strip_tags($md);

return $md;
}

public function sendWebhook($template = null, $data = null)
{
$template_filename = $template . ".php";
if (file_exists($this->override_template_dir . "/" . $template_filename)) {
$template_path = $this->override_template_dir . "/" . $template_filename;
} else {
$template_path = $this->template_dir . "/" . $template_filename;
}

ob_start();
include $template_path;
$mes_html = ob_get_clean();

$message = $this->htmlToMarkdown($mes_html);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $this->url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(array('text' => $message)));
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
}
2 changes: 1 addition & 1 deletion webroot/admin/ajax/get_group_members.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
die("PI UID not set");
}

$group = new UnityGroup($_GET["pi_uid"], $LDAP, $SQL, $MAILER, $REDIS);
$group = new UnityGroup($_GET["pi_uid"], $LDAP, $SQL, $MAILER, $REDIS, $WEBHOOK);
$members = $group->getGroupMembers();
$requests = $group->getRequests();

Expand Down
Loading