From afeb06006dc259f57c6ff8b5eb3663f3fe574a2d Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Tue, 10 Mar 2020 04:30:19 +0000 Subject: [PATCH] Add View Audit Logs to view/edit user. #330 --- config/acl.yaml | 2 + config/routes/user.yaml | 10 +++ config/services.yaml | 10 +-- src/Controller/UserController.php | 47 ++++++++++++ templates/user/form.html.twig | 120 +++++++++++++++--------------- templates/user/log.html.twig | 79 ++++++++++++++++++++ 6 files changed, 204 insertions(+), 64 deletions(-) create mode 100644 templates/user/log.html.twig diff --git a/config/acl.yaml b/config/acl.yaml index c6ccc243..1ecbfcdb 100644 --- a/config/acl.yaml +++ b/config/acl.yaml @@ -21,6 +21,8 @@ access_keys: label: Super Admin Role - id: user.profile label: User Profile + - id: user.logs + label: User Logs - id: role label: Role Access acls: diff --git a/config/routes/user.yaml b/config/routes/user.yaml index 5b8dedbe..171d4f93 100644 --- a/config/routes/user.yaml +++ b/config/routes/user.yaml @@ -41,3 +41,13 @@ user_profile_submit: path: /profile controller: App\Controller\UserController::profileSubmit methods: [POST] + +user_view_logs_form: + path: /users/{id}/logs + controller: App\Controller\UserController::viewLogsForm + methods: [GET] + +user_view_logs: + path: /user/{id}/logs + controller: App\Controller\UserController::getLogs + methods: [POST] diff --git a/config/services.yaml b/config/services.yaml index 3e7e3604..ba4ed655 100644 --- a/config/services.yaml +++ b/config/services.yaml @@ -23,12 +23,10 @@ services: # The best practice is to be explicit about your dependencies anyway. # influxdb - influxdb_client: - class: InfluxDB\Client + InfluxDB\Client: arguments: ['%env(INFLUXDB_HOST)%', '%env(INFLUXDB_PORT)%'] - influxdb_database: - class: InfluxDB\Database - arguments: ['%env(INFLUXDB_DB)%', '@influxdb_client'] + InfluxDB\Database: + arguments: ['%env(INFLUXDB_DB)%', "@InfluxDB\\Client"] # makes classes in src/ available to be used as services # this creates a service per class whose id is the fully-qualified class name @@ -270,7 +268,7 @@ services: App\EventListener\EntityListener: arguments: $token_storage: "@security.token_storage" - $log_db: '@influxdb_database' + $log_db: "@InfluxDB\\Database" $entities: ['App\Entity\User', 'App\Entity\Role', 'App\Entity\Partner'] tags: - name: 'doctrine.event_listener' diff --git a/src/Controller/UserController.php b/src/Controller/UserController.php index d9488e81..5a6af055 100644 --- a/src/Controller/UserController.php +++ b/src/Controller/UserController.php @@ -15,6 +15,8 @@ use Symfony\Bundle\FrameworkBundle\Controller\Controller; use Catalyst\MenuBundle\Annotation\Menu; +use InfluxDB\Client; + class UserController extends Controller { /** @@ -484,4 +486,49 @@ class UserController extends Controller ]); } } + + /** + * @Menu(selected="user_list") + */ + public function viewLogsForm($id) + { + $this->denyAccessUnlessGranted('user.logs', null, 'No access.'); + + $params['id'] = $id; + + // response + return $this->render('user/log.html.twig', $params); + } + + public function getLogs(Client $client, $id) + { + error_log('in getLogs'); + + // fetch database + // TODO: find way to replace hardcoded db name + $database = $client->selectDB('logging_db'); + + // query will return a resultset object + $result = $database->query('SELECT * FROM entity_log'); + + // get the points from the resultset, which is an array + $points = $result->getPoints(); + + error_log('getLogs count points ' . count($points)); + + // array has format + /* + $rows = []; + foreach ($points as $point) + { + $row['time'] = $point['time']; + error_log($point['entity_type']); + + error_log($point['action']); + } */ + + return $this->json([ + 'data' => $points, + ]); + } } diff --git a/templates/user/form.html.twig b/templates/user/form.html.twig index efa35dc4..dba00ef0 100644 --- a/templates/user/form.html.twig +++ b/templates/user/form.html.twig @@ -57,6 +57,9 @@
Unique alias for this user +
+ View Audit Logs +
@@ -182,72 +185,73 @@ {% endblock %} {% block scripts %} - + // remove all error classes + function removeErrors() { + $(".form-control-danger").removeClass('form-control-danger'); + $("[data-field]").removeClass('has-danger'); + $(".form-control-feedback[data-field]").addClass('hide'); + } + }); + + {% endblock %} diff --git a/templates/user/log.html.twig b/templates/user/log.html.twig new file mode 100644 index 00000000..bd8f3655 --- /dev/null +++ b/templates/user/log.html.twig @@ -0,0 +1,79 @@ +{% extends 'base.html.twig' %} + +{% block body %} + +
+
+
+

Audit Logs

+
+
+
+ +
+ +
+
+
+
+ +
+ +
+
+
+
+
+{% endblock %} + +{% block scripts %} + +{% endblock %} +