new: [inboxes] Added `severity` level and `message` and removed `description` column

pull/121/head
Sami Mokaddem 2022-10-25 14:38:16 +02:00
parent 0c4a73cc39
commit 00c1ae616f
No known key found for this signature in database
GPG Key ID: 164C473F627A06FA
15 changed files with 137 additions and 42 deletions

View File

@ -0,0 +1,45 @@
<?php
declare(strict_types=1);
use Migrations\AbstractMigration;
final class InboxSeverity extends AbstractMigration
{
/**
* Change Method.
*
* Write your reversible migrations using this method.
*
* More information on writing migrations is available here:
* https://book.cakephp.org/phinx/0/en/migrations.html#the-change-method
*
* Remember to call "create()" or "update()" and NOT "save()" when working
* with the Table class.
*/
public function change(): void
{
$exists = $this->table('inbox')->hasColumn('severity');
if (!$exists) {
$this->table('inbox')
->addColumn('severity', 'integer', [
'null' => false,
'default' => 0,
'signed' => false,
'length' => 10,
])
->renameColumn('comment', 'message')
->removeColumn('description')
->update();
$this->table('outbox')
->addColumn('severity', 'integer', [
'null' => false,
'default' => 0,
'signed' => false,
'length' => 10,
])
->renameColumn('comment', 'message')
->removeColumn('description')
->update();
}
}
}

View File

@ -19,9 +19,12 @@ class GenericInboxProcessor
protected $validator; protected $validator;
protected $processingTemplate = '/genericTemplates/confirm'; protected $processingTemplate = '/genericTemplates/confirm';
protected $processingTemplatesDirectory = ROOT . '/libraries/default/InboxProcessors/templates'; protected $processingTemplatesDirectory = ROOT . '/libraries/default/InboxProcessors/templates';
protected $defaultSeverity;
protected $severity;
public function __construct($registerActions=false) { public function __construct($registerActions=false) {
$this->Inbox = TableRegistry::getTableLocator()->get('Inbox'); $this->Inbox = TableRegistry::getTableLocator()->get('Inbox');
$this->defaultSeverity = $this->Inbox::SEVERITY_INFO;
if ($registerActions) { if ($registerActions) {
$this->registerActionInProcessor(); $this->registerActionInProcessor();
} }
@ -54,6 +57,10 @@ class GenericInboxProcessor
{ {
return $this->description ?? ''; return $this->description ?? '';
} }
public function getSeverity()
{
return $this->severity ?? $this->defaultSeverity;
}
protected function getProcessingTemplatePath() protected function getProcessingTemplatePath()
{ {
@ -192,7 +199,7 @@ class GenericInboxProcessor
{ {
$requestData['scope'] = $this->scope; $requestData['scope'] = $this->scope;
$requestData['action'] = $this->action; $requestData['action'] = $this->action;
$requestData['description'] = $this->description; $requestData['severity'] = $this->getSeverity();
$request = $this->generateRequest($requestData); $request = $this->generateRequest($requestData);
$savedRequest = $this->Inbox->createEntry($request); $savedRequest = $this->Inbox->createEntry($request);
return $this->genActionResult( return $this->genActionResult(

View File

@ -193,7 +193,6 @@ class GenericOutboxProcessor
$user_id = Router::getRequest()->getSession()->read('Auth.id'); $user_id = Router::getRequest()->getSession()->read('Auth.id');
$requestData['scope'] = $this->scope; $requestData['scope'] = $this->scope;
$requestData['action'] = $this->action; $requestData['action'] = $this->action;
$requestData['description'] = $this->description;
$requestData['user_id'] = $user_id; $requestData['user_id'] = $user_id;
$request = $this->generateRequest($requestData); $request = $this->generateRequest($requestData);
$savedRequest = $this->Outbox->createEntry($request); $savedRequest = $this->Outbox->createEntry($request);

View File

@ -16,8 +16,8 @@ use Cake\Http\Exception\ForbiddenException;
class InboxController extends AppController class InboxController extends AppController
{ {
public $filterFields = ['scope', 'action', 'title', 'origin', 'comment']; public $filterFields = ['scope', 'action', 'title', 'origin', 'message'];
public $quickFilterFields = ['scope', 'action', ['title' => true], ['comment' => true]]; public $quickFilterFields = ['scope', 'action', ['title' => true], ['message' => true]];
public $containFields = ['Users']; public $containFields = ['Users'];
public $paginate = [ public $paginate = [

View File

@ -16,8 +16,8 @@ use Cake\Http\Exception\ForbiddenException;
class OutboxController extends AppController class OutboxController extends AppController
{ {
public $filterFields = ['scope', 'action', 'title', 'comment']; public $filterFields = ['scope', 'action', 'title', 'message'];
public $quickFilterFields = ['scope', 'action', ['title' => true], ['comment' => true]]; public $quickFilterFields = ['scope', 'action', ['title' => true], ['message' => true]];
public $containFields = ['Users']; public $containFields = ['Users'];
public function beforeFilter(EventInterface $event) public function beforeFilter(EventInterface $event)

View File

@ -2,7 +2,9 @@
namespace App\Model\Entity; namespace App\Model\Entity;
use App\Model\Table\AppTable;
use Cake\ORM\Entity; use Cake\ORM\Entity;
use Cake\ORM\TableRegistry;
class AppModel extends Entity class AppModel extends Entity
{ {
@ -33,6 +35,11 @@ class AppModel extends Entity
return $this->_accessibleOnNew ?? []; return $this->_accessibleOnNew ?? [];
} }
public function table(): AppTable
{
return TableRegistry::get($this->getSource());
}
public function rearrangeForAPI(): void public function rearrangeForAPI(): void
{ {
} }

View File

@ -7,7 +7,7 @@ use Cake\ORM\Entity;
class Inbox extends AppModel class Inbox extends AppModel
{ {
protected $_virtual = ['local_tool_connector_name']; protected $_virtual = ['local_tool_connector_name', 'severity_variant'];
protected function _getLocalToolConnectorName() protected function _getLocalToolConnectorName()
{ {
@ -17,4 +17,9 @@ class Inbox extends AppModel
} }
return $localConnectorName; return $localConnectorName;
} }
protected function _getSeverityVariant(): string
{
return $this->table()->severityVariant[$this->severity];
}
} }

View File

@ -7,5 +7,10 @@ use Cake\ORM\Entity;
class Outbox extends AppModel class Outbox extends AppModel
{ {
protected $_virtual = ['severity_variant'];
protected function _getSeverityVariant(): string
{
return $this->table()->severityVariant[$this->severity];
}
} }

View File

@ -8,6 +8,7 @@ use Cake\ORM\Table;
use Cake\ORM\RulesChecker; use Cake\ORM\RulesChecker;
use Cake\Validation\Validator; use Cake\Validation\Validator;
use Cake\Http\Exception\NotFoundException; use Cake\Http\Exception\NotFoundException;
use Cake\ORM\ResultSet;
use App\Utility\UI\Notification; use App\Utility\UI\Notification;
@ -15,6 +16,17 @@ Type::map('json', 'Cake\Database\Type\JsonType');
class InboxTable extends AppTable class InboxTable extends AppTable
{ {
public const SEVERITY_PRIMARY = 0,
SEVERITY_INFO = 1,
SEVERITY_WARNING = 2,
SEVERITY_DANGER = 3;
public $severityVariant = [
self::SEVERITY_PRIMARY => 'primary',
self::SEVERITY_INFO => 'info',
self::SEVERITY_WARNING => 'warning',
self::SEVERITY_DANGER => 'danger',
];
public function initialize(array $config): void public function initialize(array $config): void
{ {
@ -97,8 +109,8 @@ class InboxTable extends AppTable
$allNotifications = []; $allNotifications = [];
$inboxNotifications = $this->getNotificationsForUser($user); $inboxNotifications = $this->getNotificationsForUser($user);
foreach ($inboxNotifications as $notification) { foreach ($inboxNotifications as $notification) {
$title = __('New message'); $title = $notification->title;
$details = $notification->title; $details = $notification->message;
$router = [ $router = [
'controller' => 'inbox', 'controller' => 'inbox',
'action' => 'process', 'action' => 'process',
@ -109,7 +121,7 @@ class InboxTable extends AppTable
'icon' => 'envelope', 'icon' => 'envelope',
'details' => $details, 'details' => $details,
'datetime' => $notification->created, 'datetime' => $notification->created,
'variant' => 'warning', 'variant' => $notification->severity_variant,
'_useModal' => true, '_useModal' => true,
'_sidebarId' => 'inbox', '_sidebarId' => 'inbox',
]))->get(); ]))->get();
@ -117,18 +129,14 @@ class InboxTable extends AppTable
return $allNotifications; return $allNotifications;
} }
public function getNotificationsForUser(\App\Model\Entity\User $user): array public function getNotificationsForUser(\App\Model\Entity\User $user): ResultSet
{ {
$query = $this->find(); $query = $this->find();
$conditions = []; $conditions = [
if ($user['role']['perm_admin']) { 'Inbox.user_id' => $user->id
// Admin will not see notifications if it doesn't belong to them. They can see process the message from the inbox ];
$conditions['Inbox.user_id IS'] = null;
} else {
$conditions['Inbox.user_id'] = $user->id;
}
$query->where($conditions); $query->where($conditions);
$notifications = $query->all()->toArray(); $notifications = $query->all();
return $notifications; return $notifications;
} }
} }

View File

@ -8,11 +8,13 @@ use Cake\ORM\Table;
use Cake\ORM\RulesChecker; use Cake\ORM\RulesChecker;
use Cake\Validation\Validator; use Cake\Validation\Validator;
use Cake\Http\Exception\NotFoundException; use Cake\Http\Exception\NotFoundException;
use Cake\ORM\TableRegistry;
Type::map('json', 'Cake\Database\Type\JsonType'); Type::map('json', 'Cake\Database\Type\JsonType');
class OutboxTable extends AppTable class OutboxTable extends AppTable
{ {
public $severityVariant;
public function initialize(array $config): void public function initialize(array $config): void
{ {
@ -22,6 +24,9 @@ class OutboxTable extends AppTable
$this->belongsTo('Users'); $this->belongsTo('Users');
$this->addBehavior('AuditLog'); $this->addBehavior('AuditLog');
$this->setDisplayField('title'); $this->setDisplayField('title');
$this->Inbox = TableRegistry::getTableLocator()->get('Inbox');
$this->severityVariant = $this->Inbox->severityVariant;
} }
protected function _initializeSchema(TableSchemaInterface $schema): TableSchemaInterface protected function _initializeSchema(TableSchemaInterface $schema): TableSchemaInterface

View File

@ -25,7 +25,7 @@ class Notification
foreach ($options as $key => $value) { foreach ($options as $key => $value) {
$this->{$key} = $value; $this->{$key} = $value;
} }
$this->validate(); $this->_validate();
} }
public function get(): array public function get(): array
@ -36,7 +36,7 @@ class Notification
return null; return null;
} }
private function validate() protected function _validate()
{ {
$validator = new Validator(); $validator = new Validator();

View File

@ -53,6 +53,18 @@ echo $this->element('genericElements/IndexTable/index_table', [
'data_path' => 'created', 'data_path' => 'created',
'element' => 'datetime' 'element' => 'datetime'
], ],
[
'name' => 'severity',
'sort' => 'severity',
'data_path' => 'severity',
'element' => 'function',
'function' => function ($entry, $context) {
return $context->Bootstrap->badge([
'text' => $entry->severity_variant,
'variant' => $entry->severity_variant,
]);
}
],
[ [
'name' => 'scope', 'name' => 'scope',
'sort' => 'scope', 'sort' => 'scope',
@ -80,14 +92,9 @@ echo $this->element('genericElements/IndexTable/index_table', [
'element' => 'user' 'element' => 'user'
], ],
[ [
'name' => 'description', 'name' => 'message',
'sort' => 'description', 'sort' => 'message',
'data_path' => 'description', 'data_path' => 'message',
],
[
'name' => 'comment',
'sort' => 'comment',
'data_path' => 'comment',
], ],
], ],
'title' => __('Inbox'), 'title' => __('Inbox'),

View File

@ -33,8 +33,8 @@ echo $this->element(
'path' => 'user_id', 'path' => 'user_id',
], ],
[ [
'key' => 'description', 'key' => 'message',
'path' => 'description', 'path' => 'message',
], ],
[ [
'key' => 'comment', 'key' => 'comment',

View File

@ -49,6 +49,18 @@ echo $this->element('genericElements/IndexTable/index_table', [
'data_path' => 'created', 'data_path' => 'created',
'element' => 'datetime' 'element' => 'datetime'
], ],
[
'name' => 'severity',
'sort' => 'severity',
'data_path' => 'severity',
'element' => 'function',
'function' => function ($entry, $context) {
return $context->Bootstrap->badge([
'text' => $entry->severity_variant,
'variant' => $entry->severity_variant,
]);
}
],
[ [
'name' => 'scope', 'name' => 'scope',
'sort' => 'scope', 'sort' => 'scope',
@ -71,14 +83,9 @@ echo $this->element('genericElements/IndexTable/index_table', [
'element' => 'user' 'element' => 'user'
], ],
[ [
'name' => 'description', 'name' => 'message',
'sort' => 'description', 'sort' => 'message',
'data_path' => 'description', 'data_path' => 'message',
],
[
'name' => 'comment',
'sort' => 'comment',
'data_path' => 'comment',
], ],
], ],
'title' => __('Outbox'), 'title' => __('Outbox'),

View File

@ -29,8 +29,8 @@ echo $this->element(
'path' => 'user_id', 'path' => 'user_id',
], ],
[ [
'key' => 'description', 'key' => 'message',
'path' => 'description', 'path' => 'message',
], ],
[ [
'key' => 'comment', 'key' => 'comment',