diff --git a/config/app.config.php b/config/app.config.php
index 2a1bc34a4a4137716a2389692d9f08b5b19b579c..998db5689d7859c20af98fc9fabfe0ee3d7a66b9 100644
--- a/config/app.config.php
+++ b/config/app.config.php
@@ -13,10 +13,10 @@ return [
'module/assets_manager/bootstrap.php',
'module/authentication/bootstrap.php',
'module/authorization/bootstrap.php',
- // 'module/sch_ldap/bootstrap.php',
+ 'module/sch_ldap/bootstrap.php',
'module/sch_sso/bootstrap.php',
'module/sch_mm/bootstrap.php',
- // 'module/sch_auto_create/bootstrap.php',
+ 'module/sch_inventory/bootstrap.php',
'module/schools/bootstrap.php',
'module/application/bootstrap.php',
],
diff --git a/config/settings/acl.global.php b/config/settings/acl.global.php
index 2eacdbd0046de0c435a31d298336ef5037d95164..6d41f58718f85b134898b9a041fe1cbd6aace98b 100644
--- a/config/settings/acl.global.php
+++ b/config/settings/acl.global.php
@@ -11,9 +11,10 @@ return [
'acl' => [
'default_role' => 'guest',
'roles' => [
- 'guest' => [],
- 'user' => [],
- 'admin' => ['user'],
+ 'guest' => [],
+ 'user' => [],
+ 'school' => ['user'],
+ 'admin' => ['user'],
],
'resoures' => [],
'guards' => [
diff --git a/config/settings/inventory.local.php.dist b/config/settings/inventory.local.php.dist
new file mode 100644
index 0000000000000000000000000000000000000000..2a0605732cd99e3d304e9122bce76143544a5e65
--- /dev/null
+++ b/config/settings/inventory.local.php.dist
@@ -0,0 +1,5 @@
+<?php return [
+ 'inventory' => [
+ 'base_uri' => '',
+ ],
+];
\ No newline at end of file
diff --git a/config/settings/schools.global.php b/config/settings/schools.global.php
index 2b9fd9243f07e3da3e91a156447f3b2cb89d0fd8..b975d0bc8946e95ea2d4fc06de6fa51e12a81f3c 100644
--- a/config/settings/schools.global.php
+++ b/config/settings/schools.global.php
@@ -11,10 +11,10 @@ return [
'acl' => [
'guards' => [
'routes' => [
- ['/school', ['user'], ['get']],
- ['/school/labs', ['user'], ['get']],
- ['/school/staff', ['user'], ['get']],
- ['/school/assets', ['user'], ['get']],
+ ['/school', ['school'], ['get']],
+ ['/school/labs', ['school'], ['get']],
+ ['/school/staff', ['school'], ['get']],
+ ['/school/assets', ['school'], ['get']],
],
],
],
diff --git a/module/authorization/bootstrap.php b/module/authorization/bootstrap.php
index 5827a0ffb379018959e19bb82b4b402944f7b829..f59a1baaaf7f4665ce640bea29f28e4979b7d0c3 100644
--- a/module/authorization/bootstrap.php
+++ b/module/authorization/bootstrap.php
@@ -22,6 +22,10 @@ return function (Slim\App $app) {
return new GrEduLabs\Authorization\Acl($settings['acl'], $c);
};
+ $container['acl'] = $container->protect(function () use ($container) {
+ return $container[GrEduLabs\Authorization\Acl::class];
+ });
+
$container['current_role'] = $container->protect(function () use ($container) {
$settings = $container['settings'];
$defaultRole = $settings['acl']['default_role'];
@@ -40,14 +44,17 @@ return function (Slim\App $app) {
return new GrEduLabs\Authorization\RouteGuard($c[GrEduLabs\Authorization\Acl::class], $role);
};
- $container[GrEduLabs\Authorization\RoleListener::class] = function ($c) {
- return new GrEduLabs\Authorization\RoleListener($c['authentication_storage']);
+ $container[GrEduLabs\Authorization\Listener\RoleProvider::class] = function ($c) {
+ return new GrEduLabs\Authorization\Listener\RoleProvider(
+ $c['authentication_storage'],
+ $c[GrEduLabs\Authorization\Acl::class]
+ );
};
$events = $container['events'];
$events('on', 'authenticate.success', function ($stop, $identity) use ($container) {
- $listener = $container[GrEduLabs\Authorization\RoleListener::class];
+ $listener = $container[GrEduLabs\Authorization\Listener\RoleProvider::class];
$listener($stop, $identity);
});
diff --git a/module/authorization/src/Listener/RoleProvider.php b/module/authorization/src/Listener/RoleProvider.php
new file mode 100644
index 0000000000000000000000000000000000000000..6ab209e8303b4a17add01a13858c0f8b1c47ad99
--- /dev/null
+++ b/module/authorization/src/Listener/RoleProvider.php
@@ -0,0 +1,39 @@
+<?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 GrEduLabs\Authorization\Listener;
+
+use GrEduLabs\Authorization\RoleAwareInterface;
+use RedBeanPHP\R;
+use Zend\Authentication\Storage\StorageInterface;
+use Zend\Permissions\Acl\AclInterface;
+
+class RoleProvider
+{
+ private $session;
+
+ private $acl;
+
+ public function __construct(StorageInterface $session, AclInterface $acl)
+ {
+ $this->session = $session;
+ $this->acl = $acl;
+ }
+
+ public function __invoke(callable $stop, RoleAwareInterface $identity)
+ {
+ $user = R::findOne('user', 'mail = ?', [$identity->mail]);
+ $role = ($user && isset($user->role)) ? $user->role : 'user';
+ $validRoles = $this->acl->getRoles();
+ $role = (in_array($role, $validRoles)) ? $role : 'user';
+ $identity->setRole($role);
+ $this->session->write($identity);
+ }
+}
diff --git a/module/authorization/src/RoleListener.php b/module/authorization/src/RoleListener.php
deleted file mode 100644
index 9718c2c53664bdb3bfdd7dfc711b3f280075e94e..0000000000000000000000000000000000000000
--- a/module/authorization/src/RoleListener.php
+++ /dev/null
@@ -1,29 +0,0 @@
-<?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 GrEduLabs\Authorization;
-
-use Zend\Authentication\Storage\StorageInterface;
-
-class RoleListener
-{
- private $session;
-
- public function __construct(StorageInterface $session)
- {
- $this->session = $session;
- }
-
- public function __invoke(callable $stop, RoleAwareInterface $identity)
- {
- $identity->setRole('user');
- $this->session->write($identity);
- }
-}
diff --git a/module/sch_auto_create/bootstrap.php b/module/sch_auto_create/bootstrap.php
deleted file mode 100644
index fef0c87cd573f61b2ae45f9f119d799aa8d5a968..0000000000000000000000000000000000000000
--- a/module/sch_auto_create/bootstrap.php
+++ /dev/null
@@ -1,28 +0,0 @@
-<?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
- */
-
-return function (Slim\App $app) {
-
- $container = $app->getContainer();
-
- $container['autoloader']->addPsr4('SchAutoCreate\\', __DIR__ . '/src');
-
- $events = $container['events'];
-
- $container[SchAutoCreate\Listener\User::class] = function ($c) {
- return new SchAutoCreate\Listener\User($c['logger']);
- };
-
- $events('on', 'authenticate.success', function ($stop, $identity) use ($container) {
- $listener = $container[SchAutoCreate\Listener\User::class];
-
- return $listener($stop, $identity);
- });
-};
diff --git a/module/sch_inventory/bootstrap.php b/module/sch_inventory/bootstrap.php
new file mode 100644
index 0000000000000000000000000000000000000000..c685c96cb63edf9faac48d525359240086a79f06
--- /dev/null
+++ b/module/sch_inventory/bootstrap.php
@@ -0,0 +1,24 @@
+<?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
+ */
+
+return function (Slim\App $app) {
+
+ $container = $app->getContainer();
+
+ $container['autoloader']->addPsr4('SchInventory\\', __DIR__ . '/src/');
+
+ $container['SchInventory\\Service'] = function ($c) {
+ $settings = $c['settings'];
+
+ return new SchInventory\GuzzleHttpService(
+ new GuzzleHttp\Client($settings['inventory'])
+ );
+ };
+};
diff --git a/module/sch_inventory/src/Equipment.php b/module/sch_inventory/src/Equipment.php
new file mode 100644
index 0000000000000000000000000000000000000000..38ca95c42cb942b899e130977d848f84c12d50fc
--- /dev/null
+++ b/module/sch_inventory/src/Equipment.php
@@ -0,0 +1,56 @@
+<?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 SchInventory;
+
+class Equipment
+{
+ protected $id;
+
+ protected $category;
+
+ protected $description;
+
+ protected $location;
+
+ protected $manufacturer;
+
+ protected $propertyNumber;
+
+ public function __construct($id, $category, $description, $location, $manufacturer, $propertyNumber)
+ {
+ $this->id = $id;
+ $this->category = $category;
+ $this->description = $description;
+ $this->location = $location;
+ $this->manufacturer = $manufacturer;
+ $this->propertyNumber = $propertyNumber;
+ }
+
+ public function __get($name)
+ {
+ if (property_exists($this, $name)) {
+ return $this->{$name};
+ }
+
+ return;
+ }
+
+ public function toArray()
+ {
+ return [
+ 'id' => $this->id,
+ 'category' => $this->category,
+ 'description' => $this->description,
+ 'location' => $this->location,
+ 'manufacturer' => $this->manufacturer,
+ 'propertyNumber' => $this->propertyNumber,
+ ];
+ }
+}
diff --git a/module/sch_inventory/src/EquipmentCollection.php b/module/sch_inventory/src/EquipmentCollection.php
new file mode 100644
index 0000000000000000000000000000000000000000..3f3bd8b4afa9cd3803fecb25238df354535090eb
--- /dev/null
+++ b/module/sch_inventory/src/EquipmentCollection.php
@@ -0,0 +1,66 @@
+<?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 SchInventory;
+
+use CallbackFilterIterator;
+use InvalidArgumentException;
+use Traversable;
+
+class EquipmentCollection extends ImmutableArrayObject
+{
+ /**
+ * Collection constructor
+ *
+ * @param array|Traversable
+ */
+ public function __construct($equipmentObjects)
+ {
+ if ($equipmentObjects instanceof Traversable) {
+ $equipmentObjects = iterator_to_array($equipmentObjects);
+ }
+
+ $previousHandler = set_error_handler(['self', 'handleErrors']);
+ parent::__construct(array_map(function (Equipment $equipment) {
+ return $equipment;
+ }, $equipmentObjects));
+ set_error_handler($previousHandler);
+ }
+
+ /**
+ * Returns a new Equipment collection with equimpment matching the given location
+ *
+ * @param string $location
+ * @retun EquipmentCollection
+ */
+ public function withLocation($location)
+ {
+ return new self(new CallbackFilterIterator($this->getIterator(), function (Equipment $equipment) use ($location) {
+ return $equipment->location === $location;
+ }));
+ }
+
+ /**
+ * Returns a new Equipment collection with equimpment matching the given category
+ *
+ * @param string $category
+ * @retun EquipmentCollection
+ */
+ public function withCategory($category)
+ {
+ return new self(new CallbackFilterIterator($this->getIterator(), function (Equipment $equipment) use ($category) {
+ return $equipment->category === $category;
+ }));
+ }
+
+ private static function handleErrors($severity, $message, $file, $line)
+ {
+ throw new InvalidArgumentException($message);
+ }
+}
diff --git a/module/sch_inventory/src/GuzzleHttpService.php b/module/sch_inventory/src/GuzzleHttpService.php
new file mode 100644
index 0000000000000000000000000000000000000000..99d8300830c7f894fb7b4a489c0cb927323b5ce0
--- /dev/null
+++ b/module/sch_inventory/src/GuzzleHttpService.php
@@ -0,0 +1,76 @@
+<?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 SchInventory;
+
+use GuzzleHttp\ClientInterface;
+
+/**
+ * Inventory service implementation using GuzzleHttp library
+ */
+class GuzzleHttpService implements ServiceInterface
+{
+ /**
+ * @var ClientInterface
+ */
+ protected $httpClient;
+
+ /**
+ * Class constructor
+ *
+ * @param ClientInterface $httpClient
+ */
+ public function __construct(ClientInterface $httpClient)
+ {
+ $this->httpClient = $httpClient;
+ }
+
+ /**
+ * Retrieves all equipment data for unit
+ *
+ * @param mixed $unit
+ * @return EquipmentCollection
+ */
+ public function getUnitEquipment($unit)
+ {
+ $response = $this->httpClient->request('GET', $this->createBaseUri($unit));
+
+ $responseData = json_decode($response->getBody()->getContents(), true);
+
+ return new EquipmentCollection(
+ array_map([$this, 'hydrateEquipment'], $responseData['flat_results'])
+ );
+ }
+
+ /**
+ * Creates the uri with the unit query parameter
+ *
+ * @param mixed $unit
+ * @return Psr\Http\Message\UriInterface
+ */
+ private function createBaseUri($unit)
+ {
+ $config = $this->httpClient->getConfig();
+ $baseUri = $config['base_uri'];
+
+ return $baseUri->withQueryValue($baseUri, 'unit', $unit);
+ }
+
+ private function hydrateEquipment(array $data)
+ {
+ return new Equipment(
+ (isset($data['id']) ? $data['id'] : null),
+ (isset($data['item_template.category.name']) ? $data['item_template.category.name'] : null),
+ (isset($data['item_template.description']) ? $data['item_template.description'] : null),
+ (isset($data['location.name']) ? $data['location.name'] : null),
+ (isset($data['item_template.manufacturer.name']) ? $data['item_template.manufacturer.name'] : null),
+ (isset($data['property_number']) ? $data['property_number'] : null)
+ );
+ }
+}
diff --git a/module/sch_inventory/src/ImmutableArrayObject.php b/module/sch_inventory/src/ImmutableArrayObject.php
new file mode 100644
index 0000000000000000000000000000000000000000..6dd35a083099dd764bb40806dfb1a6d861b2b011
--- /dev/null
+++ b/module/sch_inventory/src/ImmutableArrayObject.php
@@ -0,0 +1,37 @@
+<?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 SchInventory;
+
+use ArrayObject;
+use LogicException;
+
+class ImmutableArrayObject extends ArrayObject
+{
+
+ public function append($value)
+ {
+ throw new LogicException('Attempting to write to an immutable array');
+ }
+
+ public function exchangeArray($input)
+ {
+ throw new LogicException('Attempting to write to an immutable array');
+ }
+
+ public function offsetSet($index, $newval)
+ {
+ throw new LogicException('Attempting to write to an immutable array');
+ }
+
+ public function offsetUnset($index)
+ {
+ throw new LogicException('Attempting to write to an immutable array');
+ }
+}
diff --git a/module/sch_inventory/src/ServiceInterface.php b/module/sch_inventory/src/ServiceInterface.php
new file mode 100644
index 0000000000000000000000000000000000000000..1ba323b7d20c14d1b2b25dd7351f9aabe49e4371
--- /dev/null
+++ b/module/sch_inventory/src/ServiceInterface.php
@@ -0,0 +1,24 @@
+<?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 SchInventory;
+
+/**
+ * Inventory service interface
+ */
+interface ServiceInterface
+{
+ /**
+ * Retrieves all equipment data for unit
+ *
+ * @param mixed $unit
+ * @return EquipmentCollection
+ */
+ public function getUnitEquipment($unit);
+}
diff --git a/module/sch_sso/bootstrap.php b/module/sch_sso/bootstrap.php
index 12dadb04f5040031d188c887175e175b59ede842..fb477e9a5190c0e582ee88438942bafcf82599f2 100644
--- a/module/sch_sso/bootstrap.php
+++ b/module/sch_sso/bootstrap.php
@@ -73,6 +73,11 @@ return function (Slim\App $app) {
);
};
+ $container[SchSSO\Listener\User::class] = function ($c) {
+ return new SchSSO\Listener\User($c['logger']);
+ };
+
+
$container[SchSSO\Action\Login::class] = function ($c) {
$authService = $c['authentication_service'];
$authService->setAdapter($c[SchSSO\Adapter\Cas::class]);
@@ -114,6 +119,13 @@ return function (Slim\App $app) {
}
});
+
+ $events('on', 'authenticate.success', function ($stop, $identity) use ($container) {
+ $listener = $container[SchSSO\Listener\User::class];
+
+ return $listener($stop, $identity);
+ }, 1000);
+
$app->get('/user/login/sso', SchSSO\Action\Login::class)->setName('user.login.sso');
$app->get('/user/logout/sso', SchSSO\Action\Logout::class)->setName('user.logout.sso');
};
diff --git a/module/sch_auto_create/src/Listener/User.php b/module/sch_sso/src/Listener/User.php
similarity index 86%
rename from module/sch_auto_create/src/Listener/User.php
rename to module/sch_sso/src/Listener/User.php
index abd637a673430a350e251a535e813502c4c193b9..9ab2f9b26a3c715766e8597cccc27bb1a05fc132 100644
--- a/module/sch_auto_create/src/Listener/User.php
+++ b/module/sch_sso/src/Listener/User.php
@@ -8,7 +8,7 @@
* @license GNU GPLv3 http://www.gnu.org/licenses/gpl-3.0-standalone.html
*/
-namespace SchAutoCreate\Listener;
+namespace SchSSO\Listener;
use Psr\Log\LoggerInterface;
use RedBeanPHP\R;
@@ -34,7 +34,8 @@ class User
$user->authentication_source = $identity->authenticationSource;
$user->password = '';
$user->created = time();
- $this->logger->info(sprintf('User %s added to database after login', $identity->mail));
+ $user->role = 'school';
+ $this->logger->info(sprintf('User %s imported from sso.sch.gr to database', $identity->mail));
}
$user->last_login = time();
R::store($user);