Skip to content

Commit

Permalink
Allow the service catalog to be used from the central interface
Browse files Browse the repository at this point in the history
  • Loading branch information
AdrienClairembault committed Dec 23, 2024
1 parent 5ed9f50 commit 0a523fc
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 9 deletions.
3 changes: 2 additions & 1 deletion src/Glpi/Controller/Form/RendererController.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
use Glpi\Form\AccessControl\FormAccessControlManager;
use Glpi\Form\AccessControl\FormAccessParameters;
use Glpi\Form\Form;
use Glpi\Form\ServiceCatalog\ServiceCatalog;
use Glpi\Http\Firewall;
use Glpi\Security\Attribute\SecurityStrategy;
use Session;
Expand All @@ -65,7 +66,7 @@ public function __invoke(Request $request): Response

return $this->render('pages/form_renderer.html.twig', [
'title' => $form->fields['name'],
'menu' => ['admin', Form::getType()],
'menu' => ['helpdesk', ServiceCatalog::getType()],
'form' => $form,
'unauthenticated_user' => !Session::isAuthenticated(),

Expand Down
11 changes: 8 additions & 3 deletions src/Glpi/Controller/ServiceCatalog/IndexController.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
use Glpi\Controller\AbstractController;
use Glpi\Form\AccessControl\FormAccessParameters;
use Glpi\Form\ServiceCatalog\ItemRequest;
use Glpi\Form\ServiceCatalog\ServiceCatalogCompositeInterface;
use Glpi\Form\ServiceCatalog\ServiceCatalog;
use Glpi\Form\ServiceCatalog\ServiceCatalogManager;
use Glpi\Http\Firewall;
use Glpi\Security\Attribute\SecurityStrategy;
Expand All @@ -48,15 +48,17 @@

final class IndexController extends AbstractController
{
private string $interface;
private ServiceCatalogManager $service_catalog_manager;

public function __construct()
{
// TODO: replace by autowiring once dependency injection is fully implemented.
$this->service_catalog_manager = new ServiceCatalogManager();
$this->interface = Session::getCurrentInterface();
}

#[SecurityStrategy(Firewall::STRATEGY_HELPDESK_ACCESS)]
#[SecurityStrategy(Firewall::STRATEGY_AUTHENTICATED)]
#[Route(
"/ServiceCatalog",
name: "glpi_service_catalog",
Expand All @@ -76,7 +78,10 @@ public function __invoke(Request $request): Response

return $this->render('pages/self-service/service_catalog.html.twig', [
'title' => __("New ticket"),
'menu' => ["create_ticket"],
'menu' => $this->interface == "central"
? ["helpdesk", ServiceCatalog::class]
: ["create_ticket"]
,
'items' => $items,
]);
}
Expand Down
22 changes: 20 additions & 2 deletions src/Glpi/Form/ServiceCatalog/ServiceCatalog.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
use Glpi\Application\View\TemplateRenderer;
use Glpi\Form\Form;
use Override;
use Session;
use Ticket;

final class ServiceCatalog extends CommonGLPI
{
Expand All @@ -48,9 +50,10 @@ public static function getTypeName($nb = 0)
return __("Service catalog");
}

public static function getIcon()
// TODO: Should be #[Override] but getIcon() is defined by CommonDBTM instead of CommonGLPI.
public static function getIcon(): string
{
return "ti ti-notes";
return "ti ti-library";
}

#[Override]
Expand Down Expand Up @@ -83,4 +86,19 @@ public static function displayTabContentForItem(

return true;
}

#[Override]
public static function getSearchURL($full = true): string
{
/** @var array $CFG_GLPI */
global $CFG_GLPI;

return $full ? $CFG_GLPI['root_doc'] . '/ServiceCatalog' : '/ServiceCatalog';
}

#[Override]
public static function canView(): bool
{
return Session::haveRight(Ticket::$rightname, CREATE);
}
}
7 changes: 4 additions & 3 deletions src/Html.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
use Glpi\Exception\Http\BadRequestHttpException;
use Glpi\Exception\Http\NotFoundHttpException;
use Glpi\Exception\RedirectException;
use Glpi\Form\ServiceCatalog\ServiceCatalog;
use Glpi\Plugin\Hooks;
use Glpi\Toolbox\FrontEnd;
use Glpi\Toolbox\URL;
Expand Down Expand Up @@ -1298,7 +1299,7 @@ public static function getMenuInfos()
'helpdesk' => [
'title' => __('Assistance'),
'types' => [
'Ticket', 'Problem', 'Change',
'Ticket', ServiceCatalog::class, 'Problem', 'Change',
'Planning', 'Stat', 'TicketRecurrent', 'RecurrentChange'
],
'icon' => 'ti ti-headset'
Expand Down Expand Up @@ -1502,7 +1503,7 @@ public static function generateHelpMenu()

if (Session::haveRight("ticket", CREATE)) {
$menu['create_ticket'] = [
'default' => '/ServiceCatalog',
'default' => ServiceCatalog::getSearchURL(false),
'title' => __('Create a ticket'),
'icon' => 'ti ti-plus',
];
Expand All @@ -1527,7 +1528,7 @@ public static function generateHelpMenu()
];

if (Session::haveRight("ticket", CREATE)) {
$menu['tickets']['content']['ticket']['links']['add'] = '/ServiceCatalog';
$menu['tickets']['content']['ticket']['links']['add'] = ServiceCatalog::getSearchURL(false);
}
}

Expand Down
34 changes: 34 additions & 0 deletions tests/cypress/e2e/form/service_catalog/service_catalog_page.cy.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,4 +141,38 @@ describe('Service catalog page', () => {
cy.get('@child_category').click();
cy.findByRole('region', {'name': form_name}).should('exist');
});

it('can use the service catalog on the central interface', () => {
cy.changeProfile('Super-Admin');

// Create a simple form
const form_name = `Test form for service_catalog_page.cy.js ${(new Date()).getTime()}`;
createActiveForm(form_name);
cy.get('@form_id').visitFormTab('Form');
cy.findByRole('button', {'name': 'Add a new question'}).click();
cy.focused().type('Question 1');
cy.findByRole('button', {'name': 'Save'}).click();
cy.findByRole('alert')
.should('contain.text', 'Item successfully updated')
;

// Go to service catalog
cy.visit('/ServiceCatalog');
cy.validateBreadcrumbs(['Home', 'Assistance', 'Service catalog']);
cy.validateMenuIsActive('Service catalog');

// Go to our form
cy.findByRole('region', {'name': form_name}).as('form');
cy.get('@form').click();
cy.url().should('include', '/Form/Render');
cy.validateBreadcrumbs(['Home', 'Assistance', 'Service catalog']);
cy.validateMenuIsActive('Service catalog');

// Submit the form
cy.findByRole('textbox', {'name': 'Question 1'}).type('Answer 1');
cy.findByRole('button', {'name': 'Send form'}).click();
cy.findByRole('alert')
.should('contain.text', 'Item successfully created')
;
});
});
2 changes: 2 additions & 0 deletions tests/cypress/support/commands.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,7 @@ declare namespace Cypress {
checkAndCloseAlert(text: string): Chainable<any>
getCsrfToken(): Chainable<any>
changeEntity(entity: string|number, is_recursive: boolean): Chainable<any>
validateBreadcrumbs(breadcrumbs: array): Chainable<any>
validateMenuIsActive(name: string): Chainable<any>
}
}
18 changes: 18 additions & 0 deletions tests/cypress/support/commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -481,3 +481,21 @@ Cypress.Commands.add('checkAndCloseAlert', (text) => {
cy.get('@alert').should('contain.text', text);
cy.get('@alert').findByRole('button', {name: 'Close'}).click();
});

Cypress.Commands.add('validateBreadcrumbs', (breadcrumbs) => {
cy.findByRole('banner').findAllByRole('list').eq(0).as('breadcrumbs');
breadcrumbs.forEach((expected_breadcrumb, i) => {
cy.get('@breadcrumbs')
.findAllByRole('link')
.eq(i)
.should('contains.text', expected_breadcrumb)
;
});
});

Cypress.Commands.add('validateMenuIsActive', (name) => {
cy.findByRole('complementary')
.findByRole('link', {'name': name})
.should('have.class', 'active')
;
});

0 comments on commit 0a523fc

Please sign in to comment.