search mm with mm_id if registry_no fails

parent b314efd8
......@@ -29,6 +29,18 @@ return function (Slim\App $app) {
],
]));
};
$container[SchMM\FetchUnitByMmId::class] = function ($c) {
$settings = $c['settings'];
return new SchMM\FetchUnitByMmId(new GuzzleHttp\Client([
'base_uri' => $settings['sch_mm']['api_url'],
'auth' => [
$settings['sch_mm']['api_user'],
$settings['sch_mm']['api_pass'],
],
]));
};
});
};
<?php
/**
* gredu_labs.
*
* @link https://github.com/eellak/gredu_labs for the canonical source repository
*
* @copyright Copyright (c) 2008-2015 Greek Free/Open Source Software Society (https://gfoss.ellak.gr/)
* @license GNU GPLv3 http://www.gnu.org/licenses/gpl-3.0-standalone.html
*/
namespace SchMM;
use GuzzleHttp\ClientInterface;
class FetchUnitByMmId
{
/**
* @var ClientInterface
*/
protected $httpClient;
/**
* Class constructor
*
* @param ClientInterface $httpClient
*/
public function __construct(ClientInterface $httpClient)
{
$this->httpClient = $httpClient;
}
public function __invoke($mmId)
{
$config = $this->httpClient->getConfig();
$baseUri = $config['base_uri'];
$auth = $config['auth'];
$url = $baseUri->withQueryValue($baseUri, 'mm_id', $mmId);
$response = $this->httpClient->request('GET', $url, ['auth' => $auth]);
$responseData = json_decode($response->getBody()->getContents(), true);
if (!isset($responseData['data']) || empty($responseData['data'])) {
return;
}
return $responseData['data'][0];
}
}
......@@ -5,6 +5,7 @@ use GrEduLabs\Schools\Service\AssetServiceInterface;
use GrEduLabs\Schools\Service\LabServiceInterface;
use GrEduLabs\Schools\Service\SchoolServiceInterface;
use SchMM\FetchUnit;
use SchMM\FetchUnitByMmId;
use SchSync\Middleware\CreateLabs;
use SchSync\Middleware\CreateSchool;
use SchSync\Middleware\CreateUser;
......@@ -41,6 +42,7 @@ return function (App $app) {
return new CreateSchool(
$c->get('ldap'),
$c->get(FetchUnit::class),
$c->get(FetchUnitByMmId::class),
$c->get('authentication_service'),
$c->get(SchoolServiceInterface::class),
$c->get(SchoolInputFilter::class),
......
......@@ -36,6 +36,12 @@ class CreateSchool
*/
private $fetchUnit;
/**
*
* @var callable
*/
private $fetchUnitFromMMById;
/**
* @var AuthenticationServiceInterface
*/
......@@ -73,9 +79,16 @@ class CreateSchool
*/
private $logger;
/**
*
* @var \Zend\Ldap\Node
*/
private $unit;
public function __construct(
Ldap $ldap,
callable $fetchUnitFromMM,
callable $fetchUnitFromMMById,
AuthenticationServiceInterface $authService,
SchoolServiceInterface $schoolService,
SchoolInputFilter $schoolInputFilter,
......@@ -86,6 +99,7 @@ class CreateSchool
) {
$this->ldap = $ldap;
$this->fetchUnit = $fetchUnitFromMM;
$this->fetchUnitFromMMById = $fetchUnitFromMMById;
$this->authService = $authService;
$this->schoolService = $schoolService;
$this->schoolInputFilter = $schoolInputFilter;
......@@ -119,13 +133,18 @@ class CreateSchool
try {
if (!$school) {
$unit = call_user_func($this->fetchUnit, $registryNo);
if (null === $unit) {
$mmId = $this->findUnitMmId($identity);
$unit = call_user_func($this->fetchUnitFromMMById, $mmId);
}
if (null === $unit) {
$this->logger->error(sprintf(
'Unit with %s for user %s not found in MM',
$identity->mail,
$registryNo
));
$this->logger->debug('Trace', ['registryNo'=> $registryNo, 'identity' => $identity->toArray()]);
$this->logger->debug('Trace', ['registryNo'=> $registryNo, 'mmId' => $mmId, 'identity' => $identity->toArray()]);
return $this->logoutAndRediret($res, sprintf(
'Το σχολείο με κωδικό %s δεν βρέθηκε στο Μητρώο Μονάδων του ΠΣΔ. <a href="%s" title="SSO logout">SSO Logout</a>',
......@@ -135,7 +154,7 @@ class CreateSchool
}
$data = [
'id' => '',
'registry_no' => $unit['registry_no'],
'registry_no' => $registryNo,
'name' => $unit['name'],
'street_address' => $unit['street_address'],
'postal_code' => $unit['postal_code'],
......@@ -176,19 +195,16 @@ class CreateSchool
private function findUnitRegitryNo(Identity $identity)
{
$filter = Filter::equals('mail', $identity->mail);
$baseDn = Dn::factory($this->ldap->getBaseDn())->prepend(['ou' => 'people']);
$result = $this->ldap->search($filter, $baseDn, Ldap::SEARCH_SCOPE_ONE, ['l']);
$unit = $this->findUnit($identity);
if (1 !== $result->count()) {
return;
}
$result = $result->current();
$unitDn = $result['l'][0];
return $unit->getAttribute('gsnunitcode', 0);
}
$unit = $this->ldap->getNode($unitDn);
private function findUnitMmId(Identity $identity)
{
$unit = $this->findUnit($identity);
return $unit->getAttribute('gsnunitcode', 0);
return $unit->getAttribute('gsnregistrycode', 0);
}
private function logoutAndRediret(Response $res, $message)
......@@ -198,4 +214,23 @@ class CreateSchool
return $res->withRedirect($this->unitNotFoundRedirectUrl);
}
private function findUnit(Identity $identity)
{
if (null === $this->unit) {
$filter = Filter::equals('mail', $identity->mail);
$baseDn = Dn::factory($this->ldap->getBaseDn())->prepend(['ou' => 'people']);
$result = $this->ldap->search($filter, $baseDn, Ldap::SEARCH_SCOPE_ONE, ['l']);
if (1 !== $result->count()) {
return;
}
$result = $result->current();
$unitDn = $result['l'][0];
$this->unit = $this->ldap->getNode($unitDn);
}
return $this->unit;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment