-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added the system messages functionality.
- Loading branch information
1 parent
f0ed0ee
commit ed72ee3
Showing
7 changed files
with
304 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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(); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters