Skip to content

Commit

Permalink
Finish rules + tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ddurieux committed Dec 31, 2024
1 parent 1f4224e commit f1b355d
Show file tree
Hide file tree
Showing 362 changed files with 5,475 additions and 1,566 deletions.
Binary file added assets/img/error_401.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 10 additions & 0 deletions assets/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,13 @@ blockquote {
.treelvl5 {
margin-left: 80px !important;
}

.error401 {
background-image: url('img/error_401.jpg');
background-position: center top;
background-size: 100% auto;
width: 800px;
height: 600px;
margin-left: auto;
margin-right: auto;
}
52 changes: 52 additions & 0 deletions db/migrations/20241204154709_renamefieldruleactions_migration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

declare(strict_types=1);

// phpcs:disable PSR1.Classes.ClassDeclaration.MissingNamespace

use Phinx\Migration\AbstractMigration;
use Phinx\Migration\Manager\Environment;
use Phinx\Config\Config;
use App\v1\Controllers\Toolbox;
use Phinx\Db\Adapter\MysqlAdapter;

final class RenamefieldruleactionsMigration extends AbstractMigration
{
public function change(): void
{

// ruleactions field mapping
$mapping = [
'entities_id' => 'entity',
'locations_id' => 'location',
'_groups_id_assign' => 'techniciangroup',
'_groups_id_requester' => 'requestergroup',
'_users_id_observer' => 'watcher',
'itilcategories_id' => 'category',
// olas_id_ttr =>
// slas_id_ttr =>
// slas_id_tto =>
'_groups_id_observer' => 'watchergroup',
'_users_id_requester' => 'requester',
];

$builderSelect = $this->getQueryBuilder('select');
$statement = $builderSelect
->select('*')
->from('ruleactions')
->execute();
$items = $statement->fetchAll('assoc');
foreach ($items as $item)
{
if (isset($mapping[$item['field']]))
{
$builderUpdate = $this->getQueryBuilder('update');
$builderUpdate
->update('ruleactions')
->set('field', $mapping[$item['field']])
->where(['id' => $item['id']])
->execute();
}
}
}
}
38 changes: 38 additions & 0 deletions docs/swagger.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
openapi: 3.0.3
info:
title: API Fusion Resolve IT
version: '1.0'
servers:
- url: https://recette.fusionresolveit.org/jit
components:
securitySchemes:
tokenauth:
type: http
scheme: bearer
bearerFormat: JWT
cookieauth:
type: apiKey
in: cookie
name: token
paths:
/view/dropdown/rule/criteria:
get:
description: ''
parameters:
- name: q
in: query
description: string to filter, can be empty
required: true
schema:
type: string
- name: itemtype
in: query
description: Itemtype of data to get
required: true
schema:
type: string
responses:
'200':
description: OK
security:
- cookieauth: []
177 changes: 8 additions & 169 deletions public/index.php
Original file line number Diff line number Diff line change
@@ -1,45 +1,21 @@
<?php

use Slim\Factory\AppFactory;
use Psr\Http\Message\ServerRequestInterface as Request;
use Symfony\Component\ErrorHandler\ErrorHandler as SymfonyErrorHandler;
use Illuminate\Database\Capsule\Manager as Capsule;
use Illuminate\Events\Dispatcher;
use Illuminate\Container\Container;
use Slim\Views\Twig;
use Slim\Views\TwigMiddleware;
use Fullpipe\TwigWebpackExtension\WebpackExtension;
use Psr\Http\Server\RequestHandlerInterface as RequestHandler;

require __DIR__ . '/../vendor/autoload.php';

// Load lang
$lang = new \App\Translation();
$translator = $lang->loadLanguage();
$apiversion = 'v1';
$basePath = "/";
$phpunit = false;

$app = AppFactory::create();

// Create Twig
$twig = Twig::create('../src/v1/Views');

$publicPath = __DIR__;

$twig->addExtension(new WebpackExtension(
$publicPath . '/assets/manifest.json',
$publicPath . '/'
));

// Add Twig-View Middleware
$app->add(new TwigMiddleware($twig, $app->getRouteCollector()->getRouteParser(), '', 'view'));

$app->addRoutingMiddleware();
$basePath = "";
if (strstr($_SERVER['REQUEST_URI'], 'index.php'))
// manage the basepath dynamically
if (isset($_SERVER['REQUEST_URI']) && strstr($_SERVER['REQUEST_URI'], 'index.php'))
{
$uri_spl = explode('index.php', $_SERVER['REQUEST_URI']);
$basePath = $uri_spl[0] . "index.php";
}
if (strstr($_SERVER['REQUEST_URI'], '/'))
elseif (isset($_SERVER['REQUEST_URI']) && strstr($_SERVER['REQUEST_URI'], '/'))
{
$uri_spl = explode('/', $_SERVER['REQUEST_URI']);
$paths = [];
Expand All @@ -58,144 +34,7 @@
}
$basePath = '/' . implode('/', $paths);
}
$app->setBasePath($basePath);

// See https://github.com/tuupola/slim-jwt-auth
$container = $app->getContainer();

$container["jwt"] = function ($container)
{
return new StdClass();
};

$secret = sodium_base642bin('TEST', SODIUM_BASE64_VARIANT_ORIGINAL);

$capsule = new Capsule();
$dbConfig = include('../phinx.php');
$myDatabase = $dbConfig['environments'][$dbConfig['environments']['default_environment']];
$configdb = [
'driver' => $myDatabase['adapter'],
'host' => $myDatabase['host'],
'database' => $myDatabase['name'],
'username' => $myDatabase['user'],
'password' => $myDatabase['pass'],
'charset' => $myDatabase['charset'],
'collation' => $myDatabase['collation'],
];
$capsule->addConnection($configdb);
$capsule->setEventDispatcher(new Dispatcher(new Container()));
$capsule->setAsGlobal();
$capsule->bootEloquent();

$ignoreList = [
$basePath . "/ping",
$basePath . "/view/login",
$basePath . "/view/sso",
$basePath . "/view/sso/cb",
$basePath . "/api/v1/fusioninventory",
];
$mailcollectors = \App\Models\Mailcollector::where('is_active', true)->where('is_oauth', true)->get();
foreach ($mailcollectors as $collector)
{
$ignoreList[] = $basePath . "/view/mailcollectors/" . $collector->id . "/oauth/cb";
}

$app->add(new Tuupola\Middleware\JwtAuthentication([
"ignore" => $ignoreList,
"secure" => false,
"secret" => $secret,
"before" => function ($request, $arguments)
{
$myUser = \App\Models\User::find($arguments['decoded']['user_id']);
// $jwtid = $myUser->getPropertyAttribute('userjwtid');
// if (is_null($jwtid) || $jwtid != $arguments['decoded']['jti'])
// {
// throw new Exception('jti changed, ask for a new token ' . $myUser['jwtid'] . ' != ' .
// $arguments['decoded']['jti'], 401);
// }
$GLOBALS['user_id'] = $arguments['decoded']['user_id'];
$GLOBALS['username'] = $myUser->completename;
$GLOBALS['profile_id'] = $arguments['decoded']['profile_id'];
$GLOBALS['entity_id'] = $arguments['decoded']['entity_id'];
$GLOBALS['entity_treepath'] = $arguments['decoded']['entity_treepath'];
$GLOBALS['entity_recursive'] = $arguments['decoded']['entity_recursive'];
// Load permissions
// $GLOBALS['permissions'] = \App\v1\Controllers\Config\Role::generatePermission(
// $arguments['decoded']['role_id']
// );
},
"error" => function ($response, $arguments)
{
global $basePath;

$GLOBALS['user_id'] = null;
// for web, redirect to login page
header('Location: ' . $basePath . '/view/login');
exit();

// for API
throw new Exception($arguments["message"], 401);
}
]));

// Init session
$app->add(
new \Slim\Middleware\Session([
'name' => 'fusionresolveit_session',
'autorefresh' => true,
'lifetime' => '1 hour',
])
);

// Define routes
\App\Route::setRoutes($app);

// Define Custom Error Handler
$customErrorHandler = function (
Request $request,
Throwable $exception,
bool $displayErrorDetails,
bool $logErrors,
bool $logErrorDetails
) use ($app)
{
global $basePath, $publicPath;

if ($exception->getCode() == 401)
{
$view = Twig::create('../src/v1/Views');
$view->addExtension(new WebpackExtension(
$publicPath . '/assets/manifest.json',
$publicPath . '/'
));

$response = $app->getResponseFactory()->createResponse()->withStatus($exception->getCode());
$viewData = [
'rootpath' => $basePath,
'message' => $exception->getMessage(),
];
return $view->render($response, 'error401.html.twig', $viewData);
} else {
echo $exception->getMessage();
}
};

$errorMiddleware = $app->addErrorMiddleware(true, true, true);
$errorMiddleware->setDefaultErrorHandler($customErrorHandler);

// get php errors (warning...)
SymfonyErrorHandler::register();

// Middleware to replace auto path for vendor.(js|css) by real path
$app->add(function (Request $request, RequestHandler $handler) use ($app, $basePath)
{
$response = $handler->handle($request);
$body = (string) $response->getBody();

$body = str_replace('auto/vendor.', $basePath . '/assets/vendor.', $body);
$response->getBody()->write($body);

return $response;
});

// Run app
$app = (new App\App())->get();
$app->run();
Loading

0 comments on commit f1b355d

Please sign in to comment.