Added pages to view the list of potential jobs

TODO: add ability to run a job & view queued jobs
This commit is contained in:
2023-05-31 16:23:18 +01:00
parent 9b2da13e3a
commit 119e932154
9 changed files with 353 additions and 1 deletions

View File

@@ -0,0 +1,102 @@
<?php
/*
* AVSDev UF Worker (https://avsdev.uk)
*
* @link https://git.avsdev.uk/avsdev/sprinkle-worker
* @license https://git.avsdev.uk/avsdev/sprinkle-worker/blob/master/LICENSE.md (LGPL-3.0 License)
*/
namespace UserFrosting\Sprinkle\Worker\Controller;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use UserFrosting\Sprinkle\Core\Controller\SimpleController;
use UserFrosting\Support\Exception\ForbiddenException;
use UserFrosting\Support\Exception\NotFoundException;
/**
* Controller class for job-related requests, (listing jobs).
*
* @author Craig Williams (https://avsdev.uk)
*/
class JobController extends SimpleController
{
/**
* Returns a list of Jobs.
*
* Generates a list of jobs from the JobInspector. No sorting or pagination functionality.
* This page requires authentication.
*
* Request type: GET
*
* @param Request $request
* @param Response $response
* @param array $args
*
* @throws ForbiddenException If user is not authorized to access page
*/
public function getList(Request $request, Response $response, $args)
{
// GET parameters
$params = $request->getQueryParams();
/** @var \UserFrosting\Sprinkle\Account\Authorize\AuthorizationManager */
$authorizer = $this->ci->authorizer;
/** @var \UserFrosting\Sprinkle\Account\Database\Models\Interfaces\UserInterface $currentUser */
$currentUser = $this->ci->currentUser;
// Access-controlled page
if (!$authorizer->checkAccess($currentUser, 'uri_jobs')) {
throw new ForbiddenException();
}
/** @var \UserFrosting\Sprinkle\Jobs\JobInspector $jobInspector */
$jobInspector = $this->ci->jobInspector;
$jobs = $jobInspector->getAvailableJobDefinitions();
// Be careful how you consume this data - it has not been escaped and contains untrusted user-supplied content.
// For example, if you plan to insert it into an HTML DOM, you must escape it on the client side (or use client-side templating).
return $response->withJson($jobs, 200, JSON_PRETTY_PRINT);
}
/**
* Renders the job listing page.
*
* This page renders a table of jobs, with dropdown menus for admin actions for each job.
* Actions typically include: run job
* This page requires authentication.
*
* Request type: GET
*
* @param Request $request
* @param Response $response
* @param array $args
*
* @throws ForbiddenException If user is not authorized to access page
*/
public function pageList(Request $request, Response $response, $args)
{
/** @var \UserFrosting\Sprinkle\Account\Authorize\AuthorizationManager */
$authorizer = $this->ci->authorizer;
/** @var \UserFrosting\Sprinkle\Account\Database\Models\Interfaces\UserInterface $currentUser */
$currentUser = $this->ci->currentUser;
// Access-controlled page
if (!$authorizer->checkAccess($currentUser, 'uri_jobs')) {
throw new ForbiddenException();
}
/** @var \UserFrosting\Sprinkle\Jobs\JobInspector $jobInspector */
$jobInspector = $this->ci->jobInspector;
$jobs = $jobInspector->getAvailableJobDefinitions();
return $this->ci->view->render($response, 'pages/jobs.html.twig', [
'jobs' => $jobs
]);
}
}

View File

@@ -0,0 +1,95 @@
<?php
/*
* AVSDev UF Worker (https://avsdev.uk)
*
* @link https://git.avsdev.uk/avsdev/sprinkle-worker
* @license https://git.avsdev.uk/avsdev/sprinkle-worker/blob/master/LICENSE.md (LGPL-3.0 License)
*/
namespace UserFrosting\Sprinkle\Worker\Database\Seeds;
use UserFrosting\Sprinkle\Account\Database\Models\Permission;
use UserFrosting\Sprinkle\Account\Database\Models\Role;
use UserFrosting\Sprinkle\Core\Database\Seeder\BaseSeed;
use UserFrosting\Sprinkle\Core\Facades\Seeder;
/**
* Seeder for the dashboard permissions.
*/
class WorkerPermissions extends BaseSeed
{
/**
* {@inheritdoc}
*/
public function run()
{
// We require the default roles
Seeder::execute('DefaultRoles');
// Get and save permissions
$permissions = $this->getPermissions();
$this->savePermissions($permissions);
// Add default mappings to permissions
$this->syncPermissionsRole($permissions);
}
/**
* @return array Permissions to seed
*/
protected function getPermissions()
{
return [
'uri_job' => new Permission([
'slug' => 'uri_job',
'name' => 'View job',
'conditions' => 'always()',
'description' => 'View the job page of any job.',
]),
'uri_jobs' => new Permission([
'slug' => 'uri_jobs',
'name' => 'Jobs management page',
'conditions' => 'always()',
'description' => 'View a page containing a table of jobs.',
]),
];
}
/**
* Save permissions.
*
* @param array $permissions
*/
protected function savePermissions(array &$permissions)
{
foreach ($permissions as $slug => $permission) {
// Trying to find if the permission already exist
$existingPermission = Permission::where(['slug' => $permission->slug, 'conditions' => $permission->conditions])->first();
// Don't save if already exist, use existing permission reference
// otherwise to re-sync permissions and roles
if ($existingPermission == null) {
$permission->save();
} else {
$permissions[$slug] = $existingPermission;
}
}
}
/**
* Sync permissions with default roles.
*
* @param array $permissions
*/
protected function syncPermissionsRole(array $permissions)
{
$roleSiteAdmin = Role::where('slug', 'site-admin')->first();
if ($roleSiteAdmin) {
$roleSiteAdmin->permissions()->syncWithoutDetaching([
$permissions['uri_jobs']->id,
$permissions['uri_job']->id,
]);
}
}
}

View File

@@ -11,8 +11,10 @@ namespace UserFrosting\Sprinkle\Worker\ServicesProvider;
use Illuminate\Container\Container;
use Psr\Container\ContainerInterface;
use Twig\Extra\Intl\IntlExtension;
use UserFrosting\Sprinkle\Worker\Worker\Worker;
use UserFrosting\Sprinkle\Core\ServicesProvider\BaseServicesProvider;
use UserFrosting\Sprinkle\Worker\Jobs\JobInspector;
/**
* Worker services provider.
@@ -49,5 +51,20 @@ class ServicesProvider
$container['jobInspector'] = function ($c) {
return new JobInspector($c);
};
/*
* Extends the 'view' service with the HasRole extension for Twig.
*
* @return \Slim\Views\Twig
*/
$container->extend('view', function ($view, $c) {
$twig = $view->getEnvironment();
if (!$twig->hasExtension('Twig\Extra\Intl\IntlExtension')) {
$twig->addExtension(new IntlExtension());
}
return $view;
});
}
}