From de8df2bf4c8a133b31db9da0e06a5180f09dad31 Mon Sep 17 00:00:00 2001 From: nuxsmin Date: Sat, 24 Feb 2018 22:43:32 +0100 Subject: [PATCH] * [ADD] Eventlog UI with search option --- app/config/actions.xml | 12 ++ .../web/Controllers/ApiTokenController.php | 5 +- .../web/Controllers/EventlogController.php | 111 ++++++++++++++ .../Helpers/Account/AccountActionsHelper.php | 2 +- .../Controllers/Helpers/ItemsGridHelper.php | 135 ++++++++++++++++-- .../views/eventlog/datagrid-rows.inc | 21 +++ .../material-blue/views/eventlog/index.inc | 3 + .../views/grid/datagrid-grid.inc | 111 ++++++++++++++ .../views/grid/datagrid-rows.inc | 21 ++- .../views/grid/datagrid-table-simple.inc | 28 ++++ .../views/grid/datagrid-table.inc | 4 +- .../views/grid/datatabs-grid.inc | 96 +------------ lib/SP/Controller/AccountSearchController.php | 2 +- lib/SP/Controller/Grids/Items.php | 19 ++- lib/SP/Controller/Grids/Notices.php | 2 +- lib/SP/Core/Acl/Acl.php | 44 +++--- lib/SP/Core/Acl/ActionNotFoundException.php | 37 +++++ lib/SP/Core/Acl/Actions.php | 12 +- lib/SP/Core/Acl/ActionsInterface.php | 2 + lib/SP/Html/DataGrid/DataGrid.php | 24 ++++ lib/SP/Html/DataGrid/DataGridActionType.php | 2 +- lib/SP/Html/DataGrid/DataGridBase.php | 77 ++++++++-- lib/SP/Html/DataGrid/DataGridDataBase.php | 26 ++-- .../Html/DataGrid/DataGridDataInterface.php | 8 +- lib/SP/Html/DataGrid/DataGridInterface.php | 6 + .../Html/DataGrid/DataGridPagerInterface.php | 3 + .../EventLog/EventlogRepository.php | 91 ++++++++++++ lib/SP/Services/EventLog/EventlogService.php | 71 +++++++++ public/js/app-actions.js | 60 ++++---- public/js/app-actions.min.js | 44 +++--- 30 files changed, 859 insertions(+), 220 deletions(-) create mode 100644 app/modules/web/Controllers/EventlogController.php create mode 100644 app/modules/web/themes/material-blue/views/eventlog/datagrid-rows.inc create mode 100644 app/modules/web/themes/material-blue/views/eventlog/index.inc create mode 100644 app/modules/web/themes/material-blue/views/grid/datagrid-grid.inc create mode 100644 app/modules/web/themes/material-blue/views/grid/datagrid-table-simple.inc create mode 100644 lib/SP/Core/Acl/ActionNotFoundException.php create mode 100644 lib/SP/Repositories/EventLog/EventlogRepository.php create mode 100644 lib/SP/Services/EventLog/EventlogService.php diff --git a/app/config/actions.xml b/app/config/actions.xml index 831b11dd..cdac373d 100644 --- a/app/config/actions.xml +++ b/app/config/actions.xml @@ -127,6 +127,18 @@ Registro de Eventos eventlog/index + + 905 + EVENTLOG_SEARCH + Buscar Eventos + eventlog/search + + + 906 + EVENTLOG_CLEAR + Limpiar Eventos + eventlog/clear + 100 ACCOUNT_VIEW diff --git a/app/modules/web/Controllers/ApiTokenController.php b/app/modules/web/Controllers/ApiTokenController.php index 73fafab6..cc411d7e 100644 --- a/app/modules/web/Controllers/ApiTokenController.php +++ b/app/modules/web/Controllers/ApiTokenController.php @@ -173,6 +173,8 @@ class ApiTokenController extends ControllerBase implements CrudControllerInterfa * Delete action * * @param $id + * @throws \Psr\Container\ContainerExceptionInterface + * @throws \Psr\Container\NotFoundExceptionInterface */ public function deleteAction($id) { @@ -202,8 +204,6 @@ class ApiTokenController extends ControllerBase implements CrudControllerInterfa /** * Saves create action - * - * @throws \SP\Core\Dic\ContainerException */ public function saveCreateAction() { @@ -239,7 +239,6 @@ class ApiTokenController extends ControllerBase implements CrudControllerInterfa * @param $id * @throws \Psr\Container\ContainerExceptionInterface * @throws \Psr\Container\NotFoundExceptionInterface - * @throws \SP\Core\Dic\ContainerException */ public function saveEditAction($id) { diff --git a/app/modules/web/Controllers/EventlogController.php b/app/modules/web/Controllers/EventlogController.php new file mode 100644 index 00000000..2c5f5c0f --- /dev/null +++ b/app/modules/web/Controllers/EventlogController.php @@ -0,0 +1,111 @@ +. + */ + +namespace SP\Modules\Web\Controllers; + +use SP\Core\Acl\ActionsInterface; +use SP\Http\JsonResponse; +use SP\Modules\Web\Controllers\Helpers\ItemsGridHelper; +use SP\Modules\Web\Controllers\Traits\ItemTrait; +use SP\Modules\Web\Controllers\Traits\JsonTrait; +use SP\Services\EventLog\EventlogService; + +/** + * Class EventlogController + * + * @package SP\Modules\Web\Controllers + */ +class EventlogController extends ControllerBase +{ + use JsonTrait, ItemTrait; + + /** + * @var EventlogService + */ + protected $eventLogService; + + /** + * @throws \SP\Core\Dic\ContainerException + */ + public function indexAction() + { + if (!$this->acl->checkUserAccess(ActionsInterface::EVENTLOG)) { + return; + } + + $this->view->addTemplate('index'); + + $this->view->assign('data', $this->getSearchGrid()); + + $this->view(); + } + + /** + * @return $this + * @throws \SP\Core\Dic\ContainerException + */ + protected function getSearchGrid() + { + $itemsGridHelper = $this->dic->get(ItemsGridHelper::class); + $itemSearchData = $this->getSearchData($this->configData); + + return $itemsGridHelper->updatePager($itemsGridHelper->getEventLogGrid($this->eventLogService->search($itemSearchData)), $itemSearchData); + } + + /** + * @throws \SP\Core\Dic\ContainerException + */ + public function searchAction() + { + if (!$this->acl->checkUserAccess(ActionsInterface::EVENTLOG_SEARCH)) { + return; + } + + $this->view->addTemplate('datagrid-table-simple', 'grid'); + $this->view->assign('data', $this->getSearchGrid()); + + $this->returnJsonResponseData(['html' => $this->render()]); + } + + /** + * clearAction + */ + public function clearAction() + { + try { + $this->eventLogService->clear(); + + $this->returnJsonResponse(JsonResponse::JSON_SUCCESS, __u('Registro de eventos vaciado')); + } catch (\Exception $e) { + processException($e); + + $this->returnJsonResponseException($e); + } + } + + protected function initialize() + { + $this->eventLogService = $this->dic->get(EventlogService::class); + } +} \ No newline at end of file diff --git a/app/modules/web/Controllers/Helpers/Account/AccountActionsHelper.php b/app/modules/web/Controllers/Helpers/Account/AccountActionsHelper.php index 49a59a5d..297d0867 100644 --- a/app/modules/web/Controllers/Helpers/Account/AccountActionsHelper.php +++ b/app/modules/web/Controllers/Helpers/Account/AccountActionsHelper.php @@ -304,7 +304,7 @@ class AccountActionsHelper extends HelperBase { $action = new DataGridAction(); $action->setId(ActionsInterface::ACCOUNT_COPY); - $action->setType(DataGridActionType::NEW_ITEM); + $action->setType(DataGridActionType::MENUBAR_ITEM); $action->setName(__('Copiar Cuenta')); $action->setTitle(__('Copiar Cuenta')); $action->addClass('btn-action'); diff --git a/app/modules/web/Controllers/Helpers/ItemsGridHelper.php b/app/modules/web/Controllers/Helpers/ItemsGridHelper.php index e273aff5..d677a881 100644 --- a/app/modules/web/Controllers/Helpers/ItemsGridHelper.php +++ b/app/modules/web/Controllers/Helpers/ItemsGridHelper.php @@ -30,12 +30,15 @@ use SP\Bootstrap; use SP\Core\Acl\Acl; use SP\Core\Acl\ActionsInterface; use SP\Core\UI\ThemeIconsBase; +use SP\DataModel\ItemSearchData; use SP\Html\Assets\FontIcon; +use SP\Html\DataGrid\DataGrid; use SP\Html\DataGrid\DataGridAction; use SP\Html\DataGrid\DataGridActionSearch; use SP\Html\DataGrid\DataGridActionType; use SP\Html\DataGrid\DataGridData; use SP\Html\DataGrid\DataGridHeader; +use SP\Html\DataGrid\DataGridInterface; use SP\Html\DataGrid\DataGridPager; use SP\Html\DataGrid\DataGridTab; use SP\Repositories\CustomField\CustomFieldDefRepository; @@ -101,7 +104,7 @@ class ItemsGridHelper extends HelperBase // Grid item's actions $GridActionNew = new DataGridAction(); $GridActionNew->setId(ActionsInterface::CATEGORY_CREATE); - $GridActionNew->setType(DataGridActionType::NEW_ITEM); + $GridActionNew->setType(DataGridActionType::MENUBAR_ITEM); $GridActionNew->setName(__('Nueva Categoría')); $GridActionNew->setTitle(__('Nueva Categoría')); $GridActionNew->setIcon($this->icons->getIconAdd()); @@ -207,7 +210,7 @@ class ItemsGridHelper extends HelperBase // Grid item's actions $GridActionNew = new DataGridAction(); $GridActionNew->setId(ActionsInterface::CLIENT_CREATE); - $GridActionNew->setType(DataGridActionType::NEW_ITEM); + $GridActionNew->setType(DataGridActionType::MENUBAR_ITEM); $GridActionNew->setName(__('Nuevo Cliente')); $GridActionNew->setTitle(__('Nuevo Cliente')); $GridActionNew->setIcon($this->icons->getIconAdd()); @@ -291,7 +294,7 @@ class ItemsGridHelper extends HelperBase // Grid item's actions $GridActionNew = new DataGridAction(); $GridActionNew->setId(ActionsInterface::CUSTOMFIELD_CREATE); - $GridActionNew->setType(DataGridActionType::NEW_ITEM); + $GridActionNew->setType(DataGridActionType::MENUBAR_ITEM); $GridActionNew->setName(__('Nuevo Campo')); $GridActionNew->setTitle(__('Nuevo Campo')); $GridActionNew->setIcon($this->icons->getIconAdd()); @@ -597,7 +600,7 @@ class ItemsGridHelper extends HelperBase $GridActionNew = new DataGridAction(); $GridActionNew->setId(ActionsInterface::USER_CREATE); - $GridActionNew->setType(DataGridActionType::NEW_ITEM); + $GridActionNew->setType(DataGridActionType::MENUBAR_ITEM); $GridActionNew->setName(__('Nuevo Usuario')); $GridActionNew->setTitle(__('Nuevo Usuario')); $GridActionNew->setIcon($this->icons->getIconAdd()); @@ -612,7 +615,7 @@ class ItemsGridHelper extends HelperBase ) { $GridActionLdapSync = new DataGridAction(); $GridActionLdapSync->setId(ActionsInterface::LDAP_SYNC); - $GridActionLdapSync->setType(DataGridActionType::NEW_ITEM); + $GridActionLdapSync->setType(DataGridActionType::MENUBAR_ITEM); $GridActionLdapSync->setName(__('Importar usuarios de LDAP')); $GridActionLdapSync->setTitle(__('Importar usuarios de LDAP')); $GridActionLdapSync->setIcon(new FontIcon('get_app')); @@ -717,7 +720,7 @@ class ItemsGridHelper extends HelperBase // Grid item's actions $GridActionNew = new DataGridAction(); $GridActionNew->setId(ActionsInterface::GROUP_CREATE); - $GridActionNew->setType(DataGridActionType::NEW_ITEM); + $GridActionNew->setType(DataGridActionType::MENUBAR_ITEM); $GridActionNew->setName(__('Nuevo Grupo')); $GridActionNew->setTitle(__('Nuevo Grupo')); $GridActionNew->setIcon($this->icons->getIconAdd()); @@ -806,7 +809,7 @@ class ItemsGridHelper extends HelperBase // Grid item's actions $GridActionNew = new DataGridAction(); $GridActionNew->setId(ActionsInterface::PROFILE_CREATE); - $GridActionNew->setType(DataGridActionType::NEW_ITEM); + $GridActionNew->setType(DataGridActionType::MENUBAR_ITEM); $GridActionNew->setName(__('Nuevo Perfil')); $GridActionNew->setTitle(__('Nuevo Perfil')); $GridActionNew->setIcon($this->icons->getIconAdd()); @@ -897,7 +900,7 @@ class ItemsGridHelper extends HelperBase // Grid item's actions $GridActionNew = new DataGridAction(); $GridActionNew->setId(ActionsInterface::APITOKEN_CREATE); - $GridActionNew->setType(DataGridActionType::NEW_ITEM); + $GridActionNew->setType(DataGridActionType::MENUBAR_ITEM); $GridActionNew->setName(__('Nueva Autorización')); $GridActionNew->setTitle(__('Nueva Autorización')); $GridActionNew->setIcon($this->icons->getIconAdd()); @@ -998,7 +1001,7 @@ class ItemsGridHelper extends HelperBase // Grid item's actions $GridActionNew = new DataGridAction(); $GridActionNew->setId(ActionsInterface::PUBLICLINK_CREATE); - $GridActionNew->setType(DataGridActionType::NEW_ITEM); + $GridActionNew->setType(DataGridActionType::MENUBAR_ITEM); $GridActionNew->setName(__('Nuevo Enlace')); $GridActionNew->setTitle(__('Nuevo Enlace')); $GridActionNew->setIcon($this->icons->getIconAdd()); @@ -1086,7 +1089,7 @@ class ItemsGridHelper extends HelperBase // Grid item's actions $GridActionNew = new DataGridAction(); $GridActionNew->setId(ActionsInterface::TAG_CREATE); - $GridActionNew->setType(DataGridActionType::NEW_ITEM); + $GridActionNew->setType(DataGridActionType::MENUBAR_ITEM); $GridActionNew->setName(__('Nueva Etiqueta')); $GridActionNew->setTitle(__('Nueva Etiqueta')); $GridActionNew->setIcon($this->icons->getIconAdd()); @@ -1216,6 +1219,118 @@ class ItemsGridHelper extends HelperBase return $Grid; } + /** + * @param array $data + * @return DataGrid + * @throws \SP\Core\Dic\ContainerException + */ + public function getEventLogGrid(array $data) + { + // Grid Header + $GridHeaders = new DataGridHeader(); + $GridHeaders->addHeader(__('ID')); + $GridHeaders->addHeader(__('Fecha / Hora')); + $GridHeaders->addHeader(__('Nivel')); + $GridHeaders->addHeader(__('Evento')); + $GridHeaders->addHeader(__('Login')); + $GridHeaders->addHeader(__('IP')); + $GridHeaders->addHeader(__('Descripción')); + + $isDemoMode = $this->configData->isDemoEnabled(); + + // Grid Data + $GridData = new DataGridData(); + $GridData->setDataRowSourceId('id'); + $GridData->addDataRowSource('id'); + $GridData->addDataRowSource('date'); + $GridData->addDataRowSource('level'); + $GridData->addDataRowSource('action'); + $GridData->addDataRowSource('login'); + $GridData->addDataRowSource('ipAddress', false, + function ($value) use ($isDemoMode) { + return $isDemoMode ? preg_replace('#\d+#', '*', $value) : $value; + }); + $GridData->addDataRowSource('description', false, + function ($value) use ($isDemoMode) { + if ($isDemoMode) { + $value = preg_replace('/\\d+\\.\\d+\\.\\d+\\.\\d+/', "*.*.*.*", $value); + } + + $text = str_replace(';;', PHP_EOL, utf8_decode($value)); + + if (preg_match('/^SQL.*/m', $text)) { + $text = preg_replace('/([[:alpha:]_]+),/m', '\\1,
', $text); + $text = preg_replace('/(UPDATE|DELETE|TRUNCATE|INSERT|SELECT|WHERE|LEFT|ORDER|LIMIT|FROM)/m', '
\\1', $text); + } + + if (strlen($text) >= 150) { + $text = wordwrap($text, 150, PHP_EOL, true); + } + + return str_replace(PHP_EOL, '
', $text); + }); + $GridData->setData($data); + + // Grid + $Grid = new DataGrid(); + $Grid->setId('tblEventLog'); + $Grid->setDataTableTemplate('datagrid-table-simple', 'grid'); + $Grid->setDataRowTemplate('datagrid-rows', $this->view->getBase()); + $Grid->setDataPagerTemplate('datagrid-nav-full', 'grid'); + $Grid->setHeader($GridHeaders); + $Grid->setData($GridData); + $Grid->setTitle(__('Registro de Eventos')); + $Grid->setTime(round(microtime() - $this->queryTimeStart, 5)); + + // Grid Actions + $GridActionSearch = new DataGridActionSearch(); + $GridActionSearch->setId(ActionsInterface::EVENTLOG_SEARCH); + $GridActionSearch->setType(DataGridActionType::SEARCH_ITEM); + $GridActionSearch->setName('frmSearchEvent'); + $GridActionSearch->setTitle(__('Buscar Evento')); + $GridActionSearch->setOnSubmitFunction('eventlog/search'); + $GridActionSearch->addData('action-route', Acl::getActionRoute(ActionsInterface::EVENTLOG_SEARCH)); + + $Grid->setDataActions($GridActionSearch); + + $GridActionClear = new DataGridAction(); + $GridActionClear->setId(ActionsInterface::EVENTLOG_CLEAR); + $GridActionClear->setType(DataGridActionType::MENUBAR_ITEM); + $GridActionClear->setName(__('Vaciar registro de eventos')); + $GridActionClear->setTitle(__('Vaciar registro de eventos')); + $GridActionClear->setIcon($this->icons->getIconClear()); + $GridActionClear->setOnClickFunction('eventlog/clear'); + $GridActionClear->addData('action-route', Acl::getActionRoute(ActionsInterface::EVENTLOG_CLEAR)); + $GridActionClear->addData('action-next', Acl::getActionRoute(ActionsInterface::EVENTLOG)); + + $Grid->setDataActions($GridActionClear); + + $Grid->setPager($this->getPager($GridActionSearch) + ->setOnClickFunction('eventlog/nav') + ); + + return $Grid; + } + + /** + * Actualizar los datos del paginador + * + * @param DataGridInterface $dataGrid + * @param ItemSearchData $itemSearchData + * @return DataGridInterface + */ + public function updatePager(DataGridInterface $dataGrid, ItemSearchData $itemSearchData) + { + $dataGrid->getPager() + ->setLimitStart($itemSearchData->getLimitStart()) + ->setLimitCount($itemSearchData->getLimitCount()) + ->setFilterOn($itemSearchData->getSeachString() !== ''); + + $dataGrid->updatePager(); + + return $dataGrid; + } + /** * @throws \Psr\Container\ContainerExceptionInterface * @throws \Psr\Container\NotFoundExceptionInterface diff --git a/app/modules/web/themes/material-blue/views/eventlog/datagrid-rows.inc b/app/modules/web/themes/material-blue/views/eventlog/datagrid-rows.inc new file mode 100644 index 00000000..304a3b55 --- /dev/null +++ b/app/modules/web/themes/material-blue/views/eventlog/datagrid-rows.inc @@ -0,0 +1,21 @@ + + + +getData()->getDataCount() > 0): + foreach ($data->getData()->getData() as $dataIndex => $dataItem): + if ($dataIndex === 'count'): continue; endif; ?> + + + getData()->getDataRowSources() as $rowSrc): ?> + {$rowSrc['name']}() : $dataItem->{$rowSrc['name']}; ?> + + + + + + \ No newline at end of file diff --git a/app/modules/web/themes/material-blue/views/eventlog/index.inc b/app/modules/web/themes/material-blue/views/eventlog/index.inc new file mode 100644 index 00000000..2edd0615 --- /dev/null +++ b/app/modules/web/themes/material-blue/views/eventlog/index.inc @@ -0,0 +1,3 @@ +includeTemplate('datagrid-grid', 'grid'); \ No newline at end of file diff --git a/app/modules/web/themes/material-blue/views/grid/datagrid-grid.inc b/app/modules/web/themes/material-blue/views/grid/datagrid-grid.inc new file mode 100644 index 00000000..36608d98 --- /dev/null +++ b/app/modules/web/themes/material-blue/views/grid/datagrid-grid.inc @@ -0,0 +1,111 @@ + + +
+
    + getDataActions() as $action): + if ($action->getType() === \SP\Html\DataGrid\DataGridActionType::MENUBAR_ITEM): + ?> +
  • + + getTitle(); ?> +
  • + getType() === \SP\Html\DataGrid\DataGridActionType::SEARCH_ITEM): ?> + + +
  • + + getIconBack()->getTitle(); ?> +
  • + getDataActionsMenu()) > 0): ?> +
  • + + +
      + getDataActionsMenu() as $action): ?> +
    • + getIcon()->getIcon(); ?> + +
    • + +
    +
  • + +
+
+ +
+ getDataTableTemplate()): ?> + getDataTableTemplate(); ?> + + + +
diff --git a/app/modules/web/themes/material-blue/views/grid/datagrid-rows.inc b/app/modules/web/themes/material-blue/views/grid/datagrid-rows.inc index e7f379b5..266309df 100644 --- a/app/modules/web/themes/material-blue/views/grid/datagrid-rows.inc +++ b/app/modules/web/themes/material-blue/views/grid/datagrid-rows.inc @@ -14,11 +14,10 @@ use SP\Html\Html; if ($dataIndex === 'count'): continue; endif; $numFields = count($data->getData()->getDataRowSources()); ?> - + getData()->getDataRowSources() as $rowSrc): ?> {$rowSrc['name']}() : $dataItem->{$rowSrc['name']}; ?> - + @@ -33,10 +32,10 @@ use SP\Html\Html; - -
- getDataActions()) > 0): - foreach ($data->getDataActions() as $action): + getDataActionsCount() > 0): ?> + +
+ getDataActions() as $action): if (!$action->isSkip()): $filter = $action->getFilterRowSource(); @@ -60,11 +59,9 @@ use SP\Html\Html; class="mdl-tooltip mdl-tooltip--top">getTitle(); ?> - -   - -
- +
+ + \ No newline at end of file diff --git a/app/modules/web/themes/material-blue/views/grid/datagrid-table-simple.inc b/app/modules/web/themes/material-blue/views/grid/datagrid-table-simple.inc new file mode 100644 index 00000000..fa86caf5 --- /dev/null +++ b/app/modules/web/themes/material-blue/views/grid/datagrid-table-simple.inc @@ -0,0 +1,28 @@ + + + + + + getHeader()->getHeaders() as $header): ?> + + + + + + + getDataRowTemplate(); ?> + +
+ + +getDataPagerTemplate(); ?> + + \ No newline at end of file diff --git a/app/modules/web/themes/material-blue/views/grid/datagrid-table.inc b/app/modules/web/themes/material-blue/views/grid/datagrid-table.inc index 1476129d..bc8d71e9 100644 --- a/app/modules/web/themes/material-blue/views/grid/datagrid-table.inc +++ b/app/modules/web/themes/material-blue/views/grid/datagrid-table.inc @@ -13,7 +13,9 @@ -   + getDataActionsCount() > 0): ?> +   + diff --git a/app/modules/web/themes/material-blue/views/grid/datatabs-grid.inc b/app/modules/web/themes/material-blue/views/grid/datatabs-grid.inc index 79cf16b8..2e3c131e 100644 --- a/app/modules/web/themes/material-blue/views/grid/datatabs-grid.inc +++ b/app/modules/web/themes/material-blue/views/grid/datatabs-grid.inc @@ -16,102 +16,8 @@ class="mdl-tabs__panel" data-tab-route="" data-tab-index=""> -
-
    - getDataActions() as $action): - if ($action->getType() === \SP\Html\DataGrid\DataGridActionType::NEW_ITEM): - ?> -
  • - - getTitle(); ?> -
  • - getType() === \SP\Html\DataGrid\DataGridActionType::SEARCH_ITEM): ?> - - -
  • - - getIconBack()->getTitle(); ?> -
  • - getDataActionsMenu()) > 0): ?> -
  • - - -
      - getDataActionsMenu() as $action): ?> -
    • - getIcon()->getIcon(); ?> - -
    • - -
    -
  • - -
-
- -
- -
+