Skip to content

Commit

Permalink
move service url in GrouperHTTPWrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
Ioannis committed Feb 20, 2024
1 parent 88ba44e commit aa6e2df
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 43 deletions.
61 changes: 31 additions & 30 deletions Lib/GrouperApiAccess.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,16 @@ public function __construct(array $cfg)

$this->http = new GrouperHTTPWrapper();

if (empty($cfg['CoGrouperLiteWidget']['id'])) {
if (empty($cfg['CoGrouperLiteWidget']['id'])
|| empty($cfg['CoGrouperLiteWidget']['conn_url'])
|| empty($cfg['CoGrouperLiteWidget']['conn_user'])
|| empty($cfg['CoGrouperLiteWidget']['conn_pass'])
|| empty($cfg['CoGrouperLiteWidget']['conn_ver'])
) {
throw new GrouperLiteWidgetException('No GrouperLite instance captured');
}

$connUrl = $cfg['CoGrouperLiteWidget']['conn_url'];
$connVer = $cfg['CoGrouperLiteWidget']['conn_ver'];

$this->config['fullUrl'] = $connUrl . $this->_urlServlet . $connVer;

$this->http->setServiceUrl($cfg['CoGrouperLiteWidget']['conn_url'], $cfg['CoGrouperLiteWidget']['conn_ver']);
$this->http->setUser($cfg['CoGrouperLiteWidget']['conn_user']);
$this->http->setPassword($cfg['CoGrouperLiteWidget']['conn_pass']);
// Assume json content responses
Expand All @@ -83,7 +84,7 @@ public function __construct(array $cfg)
public function addGrouperGroupMember(array $queryData)
{
$groupName = $queryData['groupName'];
$connectionUrl = "{$this->config['fullUrl']}/groups/$groupName/members";
$actionEndpoint = "/groups/$groupName/members";

//Build request logic
$usersToAdd = [
Expand All @@ -100,7 +101,7 @@ public function addGrouperGroupMember(array $queryData)

try {
$results = $this->http->sendRequest('PUT',
$connectionUrl,
$actionEndpoint,
json_encode($usersToAdd, JSON_THROW_ON_ERROR));
} catch (Exception $e) {
CakeLog::write('error', __METHOD__ . ': An error occurred');
Expand Down Expand Up @@ -151,13 +152,13 @@ public function createGroupWithTemplate(array $queryData)
]
];

$connectionUrl = "{$this->config['fullUrl']}/gshTemplateExec";
$actionEndpoint = "/gshTemplateExec";

$status = true;
$message = '';
try {
$results = $this->http->sendRequest('POST',
$connectionUrl,
$actionEndpoint,
json_encode($groupToSave));
} catch (Exception $e) {
CakeLog::write('error', __METHOD__ . ': An error occurred');
Expand Down Expand Up @@ -230,11 +231,11 @@ public function createUpdateGroup(array $queryData)
]
]
];
$connectionUrl = "{$this->config['fullUrl']}/groups";
$actionEndpoint = "/groups";

try {
$results = $this->http->sendRequest('POST',
$connectionUrl,
$actionEndpoint,
json_encode($groupToSave, JSON_THROW_ON_ERROR));
} catch (Exception $e) {
CakeLog::write('error', __METHOD__ . ': An error occurred');
Expand Down Expand Up @@ -280,11 +281,11 @@ public function deleteGroupWithTemplate(array $queryData)
]
];

$connectionUrl = "{$this->config['fullUrl']}/gshTemplateExec";
$actionEndpoint = "/gshTemplateExec";

try {
$results = $this->http->sendRequest('POST',
$connectionUrl,
$actionEndpoint,
json_encode($groupToDelete, JSON_THROW_ON_ERROR));
} catch (Exception $e) {
CakeLog::write('error', __METHOD__ . ': An error occurred');
Expand Down Expand Up @@ -329,11 +330,11 @@ public function getGrouperGroupInfo(array $queryData)
]
]
];
$connectionUrl = "{$this->config['fullUrl']}/attributeAssignments";
$actionEndpoint = "/attributeAssignments";

try {
$results = $this->http->sendRequest('POST',
$connectionUrl,
$actionEndpoint,
json_encode($stemToFind, JSON_THROW_ON_ERROR));
} catch (Exception $e) {
CakeLog::write('error', __METHOD__ . ': An error occurred');
Expand Down Expand Up @@ -369,11 +370,11 @@ public function getGrouperMemberOfGroups(string $userId)
return [];
}

$connectionUrl = "{$this->config['fullUrl']}/subjects/{$userId}/groups?"
$actionEndpoint = "/subjects/{$userId}/groups?"
. 'wsLiteObjectType=WsRestGetGroupsLiteRequest'
. "&actAsSubjectId={$userId}";
try {
$results = $this->http->sendRequest('GET', $connectionUrl);
$results = $this->http->sendRequest('GET', $actionEndpoint);
} catch (Exception $e) {
CakeLog::write('error', __METHOD__ . ': An error occurred');
throw $e;
Expand Down Expand Up @@ -404,11 +405,11 @@ public function getMembersInGroup(array $queryData) {
]
];

$connectionUrl = "{$this->config['fullUrl']}/groups";
$actionEndpoint = "/groups";

try {
$results = $this->http->sendRequest('POST',
$connectionUrl,
$actionEndpoint,
json_encode($usersToShow, JSON_THROW_ON_ERROR));
} catch (Exception $e) {
CakeLog::write('error', __METHOD__ . ': An error occurred');
Expand Down Expand Up @@ -530,11 +531,11 @@ public function joinGrouperGroup(string $userId, string $groupName)
]
];

$connectionUrl = "{$this->config['fullUrl']}/groups/{$groupName}/members";
$actionEndpoint = "/groups/{$groupName}/members";

try {
$results = $this->http->sendRequest('PUT',
$connectionUrl,
$actionEndpoint,
json_encode($groupCommand, JSON_THROW_ON_ERROR));
} catch (Exception $e) {
CakeLog::write('error', __METHOD__ . ': An error occurred');
Expand Down Expand Up @@ -572,11 +573,11 @@ public function leaveGrouperGroup(string $userId, string $groupName)
]
];

$connectionUrl = "{$this->config['fullUrl']}/groups/{$groupName}/members";
$actionEndpoint = "/groups/{$groupName}/members";

try {
$results = $this->http->sendRequest('PUT',
$connectionUrl,
$actionEndpoint,
json_encode($groupCommand, JSON_THROW_ON_ERROR));
} catch (Exception $e) {
CakeLog::write('error', __METHOD__ . ': An error occurred');
Expand Down Expand Up @@ -605,10 +606,10 @@ public function isMemberOf(string $groupName, string $userId): bool

$groupNameEncoded = $this->urlGrouperEncode($groupName);

$connectionUrl = "{$this->config['fullUrl']}/groups"
$actionEndpoint = "/groups"
. "/{$groupNameEncoded}/members/{$userId}";
try {
$results = $this->http->sendRequest('GET', $connectionUrl);
$results = $this->http->sendRequest('GET', $actionEndpoint);
} catch (Exception $e) {
CakeLog::write('error', __METHOD__ . ': An error occurred');
throw $e;
Expand Down Expand Up @@ -662,7 +663,7 @@ public function removeDuplicates(array $arrOne, array $arrTwo)
public function removeGrouperGroupMember(array $queryData)
{
$groupName = $queryData['groupName'];
$connectionUrl = "{$this->config['fullUrl']}/groups/$groupName/members";
$actionEndpoint = "/groups/$groupName/members";

//Build request logic
$userToDelete = [
Expand All @@ -679,7 +680,7 @@ public function removeGrouperGroupMember(array $queryData)

try {
$results = $this->http->sendRequest('POST',
$connectionUrl,
$actionEndpoint,
json_encode($userToDelete, JSON_THROW_ON_ERROR));
} catch (Exception $e) {
CakeLog::write('error', __METHOD__ . ': An error occurred');
Expand Down Expand Up @@ -740,11 +741,11 @@ private function useMembershipUrl(array $queryData)
}

$this->http->setHeader(array('Content-Type' => 'application/json', 'Accept' => 'application/json'));
$connectionUrl = "{$this->config['fullUrl']}/memberships";
$actionEndpoint = "/memberships";

try {
$results = $this->http->sendRequest('POST',
$connectionUrl,
$actionEndpoint,
json_encode($groupsToShow));
} catch (Exception $e) {
CakeLog::write('error', __METHOD__ . ': An error occurred');
Expand Down
63 changes: 50 additions & 13 deletions Lib/GrouperHTTPWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,22 @@ class GrouperHTTPWrapper extends HttpSocket
*/
private string $_password;


/**
* @var string
*
* Grouper Servlet
*/
private $_urlServlet = '/grouper-ws/servicesRest/';

/**
* @var string
*
* Grouper Service Url
*/
private string $_serviceUrl;


/**
* GrouperHTTPWrapper constructor.
*
Expand Down Expand Up @@ -92,14 +108,14 @@ public function disableSSLVerify(): void
}

/**
* Setter - User account for accessing Grouper Web Services
* Setter - Header settings needed to call Grouper Web Services
*
* @param string $user
* @param array $headerSetting
*/
public function setUser(string $user): void
public function setHeader(array $headerSetting): void
{
if (!empty($user)) {
$this->_user = $user;
if (!empty($headerSetting)) {
$this->_request['header'] = $headerSetting;
}
}

Expand All @@ -116,14 +132,35 @@ public function setPassword(string $password): void
}

/**
* Setter - Header settings needed to call Grouper Web Services
* @param string $domain
* @param string $version
*
* @param array $headerSetting
* @return void
*/
public function setHeader(array $headerSetting): void
public function setServiceUrl(string $domain, string $version): void
{
if (!empty($headerSetting)) {
$this->_request['header'] = $headerSetting;
if(!empty($domain) && !empty($version)) {
$this->_serviceUrl = $domain . $this->_urlServlet . $version;
}
}

/**
* @return string
*/
public function getServiceUrl(): string
{
return $this->_serviceUrl;
}

/**
* Setter - User account for accessing Grouper Web Services
*
* @param string $user
*/
public function setUser(string $user): void
{
if (!empty($user)) {
$this->_user = $user;
}
}

Expand All @@ -136,15 +173,15 @@ public function setHeader(array $headerSetting): void
* @return array array of records | array with error message
* @throws GrouperLiteWidgetException If issue with Grouper WS connection
*/
public function sendRequest(string $method, string $uri, string $body = ''): array {
CakeLog::write('debug', __METHOD__ . '::connection url: ' . $uri);

public function sendRequest(string $method, string $endPoint, string $body = ''): array {
$uri = "{$this->_serviceUrl}{$endPoint}";
$this->_request['method'] = $method;
$this->_request['uri'] = $this->_parseUri($uri);
$this->_request['uri']['user'] = $this->_user;
$this->_request['uri']['pass'] = $this->_password;
$this->_request['body'] = $body;

CakeLog::write('debug', __METHOD__ . '::connection url: ' . $uri);
try {
$apiResults = $this->request($this->_request);
} catch (Exception $e) {
Expand Down

0 comments on commit aa6e2df

Please sign in to comment.