search mm with mm_id if registry_no fails

parent b314efd8
...@@ -29,6 +29,18 @@ return function (Slim\App $app) { ...@@ -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; ...@@ -5,6 +5,7 @@ use GrEduLabs\Schools\Service\AssetServiceInterface;
use GrEduLabs\Schools\Service\LabServiceInterface; use GrEduLabs\Schools\Service\LabServiceInterface;
use GrEduLabs\Schools\Service\SchoolServiceInterface; use GrEduLabs\Schools\Service\SchoolServiceInterface;
use SchMM\FetchUnit; use SchMM\FetchUnit;
use SchMM\FetchUnitByMmId;
use SchSync\Middleware\CreateLabs; use SchSync\Middleware\CreateLabs;
use SchSync\Middleware\CreateSchool; use SchSync\Middleware\CreateSchool;
use SchSync\Middleware\CreateUser; use SchSync\Middleware\CreateUser;
...@@ -41,6 +42,7 @@ return function (App $app) { ...@@ -41,6 +42,7 @@ return function (App $app) {
return new CreateSchool( return new CreateSchool(
$c->get('ldap'), $c->get('ldap'),
$c->get(FetchUnit::class), $c->get(FetchUnit::class),
$c->get(FetchUnitByMmId::class),
$c->get('authentication_service'), $c->get('authentication_service'),
$c->get(SchoolServiceInterface::class), $c->get(SchoolServiceInterface::class),
$c->get(SchoolInputFilter::class), $c->get(SchoolInputFilter::class),
......
...@@ -36,6 +36,12 @@ class CreateSchool ...@@ -36,6 +36,12 @@ class CreateSchool
*/ */
private $fetchUnit; private $fetchUnit;
/**
*
* @var callable
*/
private $fetchUnitFromMMById;
/** /**
* @var AuthenticationServiceInterface * @var AuthenticationServiceInterface
*/ */
...@@ -73,9 +79,16 @@ class CreateSchool ...@@ -73,9 +79,16 @@ class CreateSchool
*/ */
private $logger; private $logger;
/**
*
* @var \Zend\Ldap\Node
*/
private $unit;
public function __construct( public function __construct(
Ldap $ldap, Ldap $ldap,
callable $fetchUnitFromMM, callable $fetchUnitFromMM,
callable $fetchUnitFromMMById,
AuthenticationServiceInterface $authService, AuthenticationServiceInterface $authService,
SchoolServiceInterface $schoolService, SchoolServiceInterface $schoolService,
SchoolInputFilter $schoolInputFilter, SchoolInputFilter $schoolInputFilter,
...@@ -86,6 +99,7 @@ class CreateSchool ...@@ -86,6 +99,7 @@ class CreateSchool
) { ) {
$this->ldap = $ldap; $this->ldap = $ldap;
$this->fetchUnit = $fetchUnitFromMM; $this->fetchUnit = $fetchUnitFromMM;
$this->fetchUnitFromMMById = $fetchUnitFromMMById;
$this->authService = $authService; $this->authService = $authService;
$this->schoolService = $schoolService; $this->schoolService = $schoolService;
$this->schoolInputFilter = $schoolInputFilter; $this->schoolInputFilter = $schoolInputFilter;
...@@ -119,13 +133,18 @@ class CreateSchool ...@@ -119,13 +133,18 @@ class CreateSchool
try { try {
if (!$school) { if (!$school) {
$unit = call_user_func($this->fetchUnit, $registryNo); $unit = call_user_func($this->fetchUnit, $registryNo);
if (null === $unit) {
$mmId = $this->findUnitMmId($identity);
$unit = call_user_func($this->fetchUnitFromMMById, $mmId);
}
if (null === $unit) { if (null === $unit) {
$this->logger->error(sprintf( $this->logger->error(sprintf(
'Unit with %s for user %s not found in MM', 'Unit with %s for user %s not found in MM',
$identity->mail, $identity->mail,
$registryNo $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( return $this->logoutAndRediret($res, sprintf(
'Το σχολείο με κωδικό %s δεν βρέθηκε στο Μητρώο Μονάδων του ΠΣΔ. <a href="%s" title="SSO logout">SSO Logout</a>', 'Το σχολείο με κωδικό %s δεν βρέθηκε στο Μητρώο Μονάδων του ΠΣΔ. <a href="%s" title="SSO logout">SSO Logout</a>',
...@@ -135,7 +154,7 @@ class CreateSchool ...@@ -135,7 +154,7 @@ class CreateSchool
} }
$data = [ $data = [
'id' => '', 'id' => '',
'registry_no' => $unit['registry_no'], 'registry_no' => $registryNo,
'name' => $unit['name'], 'name' => $unit['name'],
'street_address' => $unit['street_address'], 'street_address' => $unit['street_address'],
'postal_code' => $unit['postal_code'], 'postal_code' => $unit['postal_code'],
...@@ -176,19 +195,16 @@ class CreateSchool ...@@ -176,19 +195,16 @@ class CreateSchool
private function findUnitRegitryNo(Identity $identity) private function findUnitRegitryNo(Identity $identity)
{ {
$filter = Filter::equals('mail', $identity->mail); $unit = $this->findUnit($identity);
$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 $unit->getAttribute('gsnunitcode', 0);
return; }
}
$result = $result->current();
$unitDn = $result['l'][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) private function logoutAndRediret(Response $res, $message)
...@@ -198,4 +214,23 @@ class CreateSchool ...@@ -198,4 +214,23 @@ class CreateSchool
return $res->withRedirect($this->unitNotFoundRedirectUrl); 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