Skip to content

Commit

Permalink
Merge pull request #417 from ineilson/multiple-apiauth
Browse files Browse the repository at this point in the history
Same API identifier at multiple sites to master.
  • Loading branch information
gregcorbett authored Jan 31, 2023
2 parents fc2d676 + 91e8f9a commit 665a831
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 25 deletions.
6 changes: 3 additions & 3 deletions htdocs/PI/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -398,10 +398,10 @@ function authByIdentifier($forceStrictForHosts = false) {
// Check if it is registered API Authentication credential.

$authEntServ = \Factory::getAPIAuthenticationService();
$authEnt = $authEntServ->getAPIAuthentication($this->identifier);
$authEnts = $authEntServ->getAPIAuthentication($this->identifier);

if (!is_null($authEnt)) {
$authEntServ->updateLastUseTime($authEnt);
if (count($authEnts) > 0) {
$authEntServ->updateLastUseTime($authEnts);
$authenticated = true;
}

Expand Down
2 changes: 1 addition & 1 deletion htdocs/web_portal/static_html/goc5_logo.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<!-- <img src="img/Logo-1.6.png" class="logo_image" height="39" style="vertical-align: middle;"/>-->
<h3 class="Logo_Text Small_Bottom_Margin Standard_Padding"
style="vertical-align: middle; margin-left: 0.2em;">
GOCDB 5.10.2
GOCDB 5.10.3
</h3>

</a>
Expand Down
36 changes: 20 additions & 16 deletions lib/Gocdb_Services/APIAuthenticationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ function __construct() {
* Returns the APIAuthentication entity associated with the given identifier.
*
* @param string $ident Identifier (e.g. X.509 DN as string)
* @return \APIAuthentication APIAuthentication associated with this identifier
* @return \APIAuthentication[] APIAuthentication associated with this identifier
*/
public function getAPIAuthentication($ident) {

Expand All @@ -48,12 +48,13 @@ public function getAPIAuthentication($ident) {
$dql = "SELECT a FROM APIAuthentication a " .
"WHERE (a.identifier = :ident)" ;

/* @var $qry \Doctine\DBAL\query */
$qry = $this->em->createQuery($dql);
$qry->setParameter('ident', $ident);

$apiAuth = $qry->getOneOrNullResult();
$apiAuths = $qry->getResult();

return $apiAuth;
return $apiAuths;
}

/**
Expand Down Expand Up @@ -188,17 +189,19 @@ public function editAPIAuthentication(\APIAuthentication $authEntity, \User $use
/**
* Set the last use time field to the current UTC time
*
* @param \APIAuthentication $authEntity entity to update
* @param \APIAuthentication[] $authEntities entity to update
* @throws \Exception if the update fails
*/
public function updateLastUseTime(\APIAuthentication $authEntity) {

public function updateLastUseTime(array $authEntities)
{
$this->em->getConnection()->beginTransaction();

try {
$authEntity->setLastUseTime();

$this->em->persist($authEntity);
/* @var \APIAuthentication $authEntity */
foreach ($authEntities as $authEntity) {
$authEntity->setLastUseTime();
$this->em->persist($authEntity);
}

$this->em->flush();
$this->em->getConnection()->commit();
Expand All @@ -220,14 +223,15 @@ public function updateLastUseTime(\APIAuthentication $authEntity) {
*/
public function uniqueAPIAuthEnt(\Site $site, $identifier, $type) {

$authEnt = $this->getAPIAuthentication($identifier, $type);
$authEntities = $this->getAPIAuthentication($identifier, $type);

if (!is_null($authEnt) &&
$authEnt->getParentSite()->getId() == $site->getId()) {
throw new \Exception(
"An authentication object of type \"$type\" and with identifier " .
"\"$identifier\" already exists for " . $site->getName()
);
foreach ($authEntities as $authEnt) {
if ($authEnt->getParentSite()->getId() == $site->getId()) {
throw new \Exception(
"An authentication object of type \"$type\" and with identifier " .
"\"$identifier\" already exists for " . $site->getName()
);
}
}
}
/**
Expand Down
17 changes: 12 additions & 5 deletions tests/unit/lib/Gocdb_Services/APIAuthenticationServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -170,10 +170,11 @@ public function testGetAPIAuthentication()

$ident = '/CN=A Dummy Subject';
$type = 'X.509';
// Start with no APIAuthentication entities to be found
$this->assertNull(
// Start with no APIAuthentication entities to be found
$this->assertCount(
0,
$authEntServ->getAPIAuthentication($ident),
"Non-null value returned when searching for APIAuthentication entity " .
"Non-zero count returned when searching for APIAuthentication entity " .
"for id:{$ident} when expected none."
);

Expand All @@ -194,9 +195,15 @@ public function testGetAPIAuthentication()

$authEntMatched = $authEntServ->getAPIAuthentication($ident);

$this->assertCount(
1,
$authEntMatched,
"Failed to return single APIAuthentication entity searching for id:{$ident}."
);

$this->assertTrue(
$authEnt === $authEntMatched,
"Failed to return APIAuthentication entity for id:{$ident}."
$authEnt === $authEntMatched[0],
"Failed to return matching APIAuthentication entity searching for for id:{$ident}."
);
}
}

0 comments on commit 665a831

Please sign in to comment.