diff --git a/app/config_default.json b/app/config_default.json index 3e6c171..1e68910 100644 --- a/app/config_default.json +++ b/app/config_default.json @@ -19,7 +19,8 @@ ], "PLUGINS": { "slack_notifier": { - "SLACK_CHANNEL": "#prismo-debug", + "SLACK_TOOL_CHANNEL": "#prismo-debug", + "SLACK_DOOR_CHANNEL": "#prismo-door-channel", "SLACK_TOKEN": "xoxb-this-is-not-areal-slack-token" } }, diff --git a/app/models/user.py b/app/models/user.py index b3e3161..fa452f7 100644 --- a/app/models/user.py +++ b/app/models/user.py @@ -75,17 +75,38 @@ def get_permissions(cls, user_key=None): # Fetch user data and permissions user_data = [] # Filter by user_key if provided + # Here we also add "Latest Activity" column, for reporting latest any tool/door use by user if user_key: cursor.execute( - "SELECT users.name, users.key FROM users WHERE users.key = ?", + """ + SELECT users.name, users.key, + (SELECT operation_time + FROM event_logs + WHERE user_key = users.key + ORDER BY operation_time DESC + LIMIT 1) AS latest_activity + FROM users + WHERE users.key = ? + """, (user_key,), ) else: - cursor.execute("SELECT users.name, users.key FROM users") + cursor.execute( + """ + SELECT users.name, users.key, + (SELECT operation_time + FROM event_logs + WHERE user_key = users.key + ORDER BY operation_time DESC + LIMIT 1) AS latest_activity + FROM users + """ + ) for row in cursor.fetchall(): user_name = row[0] user_key = row[1] + latest_activity = row[2] # Get device permissions for the current user device_permissions = [] @@ -118,6 +139,7 @@ def get_permissions(cls, user_key=None): "user_name": user_name, "user_key": user_key, "permissions": device_permissions, + "latest_activity": latest_activity, } user_data.append(user_record) diff --git a/app/plugins/slack_notifier/slack_notifier.py b/app/plugins/slack_notifier/slack_notifier.py index ddcc63f..b9051d4 100644 --- a/app/plugins/slack_notifier/slack_notifier.py +++ b/app/plugins/slack_notifier/slack_notifier.py @@ -47,6 +47,48 @@ def unlock_message_block_constructor(tool, user): }] +def door_message_block_constructor(door, user): + return [ + { + "type": "rich_text", + "elements": [ + { + "type": "rich_text_section", + "elements": [ + { + "type": "emoji", + "name": "door", + "unicode": "1f6aa" + }, + { + "type": "text", + "text": " " + }, + { + "type": "text", + "text": "%s" % user, + "style": { + "bold": True + } + }, + { + "type": "text", + "text": " entered through the " + }, + { + "type": "text", + "text": "%s" % door, + "style": { + "bold": True + } + } + ] + } + ] + } + ] + + class SlackNotifierPlugin: def __init__(self, app_context): # Configure the Slack client with your token @@ -102,19 +144,52 @@ def get_device_name(self, device_id): return None + def get_device_type(self, device_id): + """ + Get device name based on its ID + """ + connection = sqlite3.connect(self.db_uri) + cursor = connection.cursor() + + cursor.execute("SELECT type from devices WHERE id = ?", + (device_id,), + ) + connection.commit() + result = cursor.fetchone() + + connection.close() + if result: + return result[0] + + return None + def access_log_entry_added(self, event): try: if event["operation"] == "unlock": user_name = self.get_user_name(event["user_key"]) device_name = self.get_device_name(event["device_id"]) + device_type = self.get_device_type(event["device_id"]) self.logger.info("Access log entry added") self.logger.info("User name: %s", user_name) - self.logger.info("Device name: %s", device_name) - text_message = "🔓 * %s Tool was unlocked* by %s" % (device_name, user_name) - blocks = unlock_message_block_constructor(device_name, user_name) - self.slack_app.client.chat_postMessage(channel=self.config["SLACK_CHANNEL"], - text=text_message, - blocks=blocks) + if device_type == "tool": + self.logger.info("Device name: %s", device_name) + text_message = "🔓 * %s Tool was unlocked* by %s" % (device_name, user_name) + blocks = unlock_message_block_constructor(device_name, user_name) + self.slack_app.client.chat_postMessage( + channel=self.config["SLACK_TOOL_CHANNEL"], + text=text_message, + blocks=blocks) + elif device_type == "door": + self.logger.info("Door opened: %s", device_name) + text_message = "🔓 * %s Door opened by * by %s" % (device_name, user_name) + blocks = door_message_block_constructor(device_name, user_name) + self.slack_app.client.chat_postMessage( + channel=self.config["SLACK_DOOR_CHANNEL"], + text=text_message, + blocks=blocks) + else: + self.logger.error("Unknown reader type! %s", device_type) + except Exception as e: self.logger.error("Error in SlackNotifierPlugin.access_log_entry_added: %s", e) raise e diff --git a/app/static/js/devices.js b/app/static/js/devices.js index 86cea70..1d9e7af 100644 --- a/app/static/js/devices.js +++ b/app/static/js/devices.js @@ -1,8 +1,8 @@ // Global: Device ID, which is pending for update let deviceIDForUpdate = null; - +let deviceTypeForUpdate = null; function flashFirmware() { - console.log("Flashing device: ", deviceIDForUpdate); + console.log("Flashing device: ", deviceIDForUpdate, deviceTypeForUpdate); const socket = new WebSocket("ws://" + location.host + "/reader_flasher"); const logContainer = document.getElementById("log-container"); @@ -12,7 +12,7 @@ function flashFirmware() { console.log( "WebSocket connection established, send device id for flashing", ); - socket.send(deviceIDForUpdate); + socket.send(JSON.stringify({"device_id":deviceIDForUpdate, "device_type": deviceTypeForUpdate})); }); function log(data) { const obj = JSON.parse(data); @@ -58,7 +58,7 @@ function generateAccordionItems(devices) {