Skip to content

Commit

Permalink
Added the system messages functionality.
Browse files Browse the repository at this point in the history
  • Loading branch information
ruslanbaidan committed Oct 2, 2024
1 parent f0ed0ee commit ed72ee3
Show file tree
Hide file tree
Showing 7 changed files with 304 additions and 0 deletions.
19 changes: 19 additions & 0 deletions config/module.config.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,19 @@
],
],

'monarc_api_system_messages' => [
'type' => 'segment',
'options' => [
'route' => '/api/system-messages[/:id]',
'constraints' => [
'id' => '[0-9]+',
],
'defaults' => [
'controller' => Controller\ApiSystemMessagesController::class,
],
],
],

'monarc_api_models' => [
'type' => 'segment',
'options' => [
Expand Down Expand Up @@ -1413,6 +1426,7 @@
Controller\ApiAnrInstancesMetadataFieldsController::class => AutowireFactory::class,
Controller\ApiInstanceMetadataController::class => AutowireFactory::class,
Controller\ApiSoaScaleCommentController::class => AutowireFactory::class,
Controller\ApiSystemMessagesController::class => AutowireFactory::class,
Export\Controller\ApiAnrExportController::class => AutowireFactory::class,
Export\Controller\ApiAnrInstancesExportController::class => AutowireFactory::class,
Export\Controller\ApiAnrObjectsExportController::class => AutowireFactory::class,
Expand Down Expand Up @@ -1495,6 +1509,7 @@
Table\SoaTable::class => ClientEntityManagerFactory::class,
Table\SnapshotTable::class => ClientEntityManagerFactory::class,
Table\SoaScaleCommentTable::class => ClientEntityManagerFactory::class,
Table\SystemMessageTable::class => ClientEntityManagerFactory::class,
Table\ThemeTable::class => ClientEntityManagerFactory::class,
Table\ThreatTable::class => ClientEntityManagerFactory::class,
Table\UserTable::class => ClientEntityManagerFactory::class,
Expand Down Expand Up @@ -1566,6 +1581,7 @@
Service\InstanceMetadataService::class => AutowireFactory::class,
Service\SoaService::class => AutowireFactory::class,
Service\SoaScaleCommentService::class => AutowireFactory::class,
Service\SystemMessageService::class => AutowireFactory::class,
Stats\Service\StatsAnrService::class => ReflectionBasedAbstractFactory::class,
Stats\Service\StatsSettingsService::class => AutowireFactory::class,
CronTask\Service\CronTaskService::class => AutowireFactory::class,
Expand Down Expand Up @@ -1802,6 +1818,7 @@
'monarc_api_stats_global/processed',
'monarc_api_stats_global/general_settings',
'monarc_api_stats_global/validate-stats-availability',
'monarc_api_system_messages',
],
// User : RWD access per analysis
Entity\UserRole::USER_FO => [
Expand Down Expand Up @@ -1909,6 +1926,7 @@
'monarc_api_stats_global/processed',
'monarc_api_stats_global/general_settings',
'monarc_api_stats_global/validate-stats-availability',
'monarc_api_system_messages',
],
Entity\UserRole::USER_ROLE_CEO => [
'monarc_api_admin_users_roles',
Expand All @@ -1923,6 +1941,7 @@
'monarc_api_stats_global/anrs_settings',
'monarc_api_stats_global/general_settings',
'monarc_api_stats_global/validate-stats-availability',
'monarc_api_system_messages',
],
],
'activeLanguages' => ['fr'],
Expand Down
26 changes: 26 additions & 0 deletions migrations/db/20230901112005_fix_positions_cleanup_db.php
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,32 @@ public function change()
->removeColumn('updated_at')
->update();

$this->execute(
'CREATE TABLE IF NOT EXISTS `system_messages` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`user_id` int(11) unsigned NOT NULL,
`title` varchar(255) NOT NULL,
`description` TEXT,
`status` smallint(3) unsigned NOT NULL DEFAULT 1,
`creator` varchar(255) NOT NULL,
`created_at` datetime DEFAULT CURRENT_TIMESTAMP,
`updater` varchar(255) DEFAULT NULL,
`updated_at` datetime DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
CONSTRAINT `system_messages_user_id_id_fk1` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
);'
);
$usersQuery = $this->query('SELECT id FROM users WHERE status = 1;');
foreach ($usersQuery->fetchAll() as $userData) {
$this->table('system_messages')->insert([
'user_id' => $userData['id'],
'title' => 'Monarc version is updated to v2.13.1',
'description' => 'Please, read the release notes available https://www.monarc.lu/news. In case if you encounter any issues with your analysis, please let us know by sending us an email to [email protected].',
'status' => 1,
'creator' => 'System',
])->saveData();
}

/* TODO: Should be added to the next release migration, to perform this release in a safe mode.
$this->table('anr_instance_metadata_fields')->removeColumn('label_translation_key')->update();
$this->table('instances_metadata')->removeColumn('comment_translation_key')->update();
Expand Down
41 changes: 41 additions & 0 deletions src/Controller/ApiSystemMessagesController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php declare(strict_types=1);
/**
* @link https://github.com/monarc-project for the canonical source repository
* @copyright Copyright (c) 2016-2024 Luxembourg House of Cybersecurity LHC.lu - Licensed under GNU Affero GPL v3
* @license MONARC is licensed under GNU Affero General Public License version 3
*/

namespace Monarc\FrontOffice\Controller;

use Laminas\Mvc\Controller\AbstractRestfulController;
use Monarc\Core\Controller\Handler\ControllerRequestResponseHandlerTrait;
use Monarc\FrontOffice\Service\SystemMessageService;

class ApiSystemMessagesController extends AbstractRestfulController
{
use ControllerRequestResponseHandlerTrait;

public function __construct(private SystemMessageService $systemMessageService)
{
}

public function getList()
{
$messages = $this->systemMessageService->getListOfActiveMessages();

return $this->getPreparedJsonResponse([
'count' => \count($messages),
'messages' => $messages,
]);
}

/**
* @param int $id
*/
public function delete($id)
{
$this->systemMessageService->inactivateMessage((int)$id);

return $this->getSuccessfulJsonResponse();
}
}
115 changes: 115 additions & 0 deletions src/Entity/SystemMessage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
<?php declare(strict_types=1);
/**
* @link https://github.com/monarc-project for the canonical source repository
* @copyright Copyright (c) 2016-2024 Luxembourg House of Cybersecurity LHC.lu - Licensed under GNU Affero GPL v3
* @license MONARC is licensed under GNU Affero General Public License version 3
*/

namespace Monarc\FrontOffice\Entity;

use Doctrine\ORM\Mapping as ORM;
use Monarc\Core\Entity\Traits\CreateEntityTrait;
use Monarc\Core\Entity\Traits\UpdateEntityTrait;

/**
* @ORM\Table(name="system_messages")
* @ORM\Entity
*/
class SystemMessage
{
use CreateEntityTrait, UpdateEntityTrait;

public const STATUS_INACTIVE = 0;
public const STATUS_ACTIVE = 1;

/**
* @var int
*
* @ORM\Column(name="id", type="integer", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
protected $id;

/**
* @var User
*
* @ORM\ManyToOne(targetEntity="User")
* @ORM\JoinColumns({@ORM\JoinColumn(name="user_id", referencedColumnName="id", nullable=false)})
*/
protected $user;

/**
* @var string
*
* @ORM\Column(name="title", type="text", nullable=false)
*/
protected $title;

/**
* @var ?string
*
* @ORM\Column(name="description", type="text", nullable=true)
*/
protected $description;

/**
* @var int
*
* @ORM\Column(name="status", type="smallint", options={"unsigned":true, "default":1})
*/
protected $status = self::STATUS_ACTIVE;

public function getId()
{
return $this->id;
}

public function getUser(): User
{
return $this->user;
}

public function setUser(User $user): self
{
$this->user = $user;

return $this;
}

public function getTitle(): string
{
return $this->title;
}

public function setTitle(string $title): self
{
$this->title = $title;

return $this;
}

public function getDescription(): string
{
return (string)$this->description;
}

public function setDescription(string $description): self
{
$this->description = $description;

return $this;
}

public function getStatus(): int
{
return $this->status;
}

public function setStatus(int $status): self
{
$this->status = $status;

return $this;
}
}
51 changes: 51 additions & 0 deletions src/Service/SystemMessageService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php declare(strict_types=1);
/**
* @link https://github.com/monarc-project for the canonical source repository
* @copyright Copyright (c) 2016-2024 Luxembourg House of Cybersecurity LHC.lu - Licensed under GNU Affero GPL v3
* @license MONARC is licensed under GNU Affero General Public License version 3
*/

namespace Monarc\FrontOffice\Service;

use Monarc\Core\Service\ConnectedUserService;
use Monarc\FrontOffice\Entity\SystemMessage;
use Monarc\FrontOffice\Entity\User;
use Monarc\FrontOffice\Table\SystemMessageTable;

class SystemMessageService
{
private User $connectedUser;

public function __construct(
private SystemMessageTable $systemMessageTable,
ConnectedUserService $connectedUserService
) {
/** @var User $connectedUser */
$connectedUser = $connectedUserService->getConnectedUser();
$this->connectedUser = $connectedUser;
}

public function getListOfActiveMessages(): array
{
$result = [];
foreach ($this->systemMessageTable->findAllActiveByUser($this->connectedUser) as $systemMessage) {
$result[] = [
'id' => $systemMessage->getId(),
'title' => $systemMessage->getTitle(),
'description' => $systemMessage->getDescription(),
];
}

return $result;
}

public function inactivateMessage(int $id): void
{
$systemMessage = $this->systemMessageTable->findByIdAndUser($id, $this->connectedUser);
if ($systemMessage->getStatus() === SystemMessage::STATUS_ACTIVE) {
$this->systemMessageTable->save(
$systemMessage->setStatus(SystemMessage::STATUS_INACTIVE)->setUpdater($this->connectedUser->getEmail())
);
}
}
}
51 changes: 51 additions & 0 deletions src/Table/SystemMessageTable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php declare(strict_types=1);
/**
* @link https://github.com/monarc-project for the canonical source repository
* @copyright Copyright (c) 2016-2024 Luxembourg House of Cybersecurity LHC.lu - Licensed under GNU Affero GPL v3
* @license MONARC is licensed under GNU Affero General Public License version 3
*/

namespace Monarc\FrontOffice\Table;

use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityNotFoundException;
use Monarc\Core\Table\AbstractTable;
use Monarc\FrontOffice\Entity\SystemMessage;
use Monarc\FrontOffice\Entity\User;

class SystemMessageTable extends AbstractTable
{
public function __construct(EntityManager $entityManager, string $entityName = SystemMessage::class)
{
parent::__construct($entityManager, $entityName);
}

/**
* @return SystemMessage[]
*/
public function findAllActiveByUser(User $user): array
{
return $this->getRepository()->createQueryBuilder('sm')
->where('sm.user = :user')
->andWhere('sm.status = ' . SystemMessage::STATUS_ACTIVE)
->setParameter('user', $user)
->getQuery()
->getResult();
}

public function findByIdAndUser(int $id, User $user): SystemMessage
{
$systemMessage = $this->getRepository()->createQueryBuilder('sm')
->where('sm.id = :id')
->andWhere('sm.user = :user')
->setParameter('id', $id)
->setParameter('user', $user)
->getQuery()
->getOneOrNullResult();
if ($systemMessage === null) {
throw EntityNotFoundException::fromClassNameAndIdentifier(SystemMessage::class, [$id]);
}

return $systemMessage;
}
}
1 change: 1 addition & 0 deletions view/layout/layout.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@
->appendFile('js/ClientSoaService.js')
->appendFile('js/StatsService.js')
->appendFile('js/ClientSettingsCtrl.js')
->appendFile('js/SystemMessageService.js')

// ANR, but client-only
->appendFile('js/anr/AnrKbMgmtCtrl.js')
Expand Down

0 comments on commit ed72ee3

Please sign in to comment.