Restore doorboy slack endpoint
All checks were successful
ci/woodpecker/manual/woodpecker Pipeline was successful

This commit is contained in:
Madis Mägi 2023-08-10 19:03:33 +03:00
parent 2cfccc22a0
commit f53d670189
2 changed files with 41 additions and 1 deletions

View File

@ -1,6 +1,7 @@
import os
import re
import const
from datetime import datetime, timedelta
from functools import wraps
from pymongo import MongoClient
from flask import Blueprint, abort, g, make_response, redirect, render_template, request, jsonify
@ -59,3 +60,40 @@ def get_group_cards():
print(f"found tokens are: {fl}")
return jsonify(fl)
@page_api.route("/api/slack/doorboy", methods=['POST'])
def view_slack_doorboy():
print(request.data)
if request.form.get("token") != "FSh3r8UE1vFHP4GrAn8SgZUY":
return "Invalid token was supplied"
if request.form.get("channel_id") not in ("C01CWPF5H8W", "CDL9H8Q9W"):
return "Invalid channel was supplied"
command = request.form.get("command")
try:
door = {"/open-new-door": "back", "/open-back-door": "back", "/open-front-door": "front", "/open-ground-door": "ground"}[command]
except KeyError:
return "Invalid command was supplied"
member = None
print(users)
for user in users:
if user.slack_id == request.form.get("user_id"):
member = user
approved = "k-space:floor" in member.groups
db.eventlog.insert_one({
"method": "slack",
"approved": approved,
"duration": 5,
"component": "doorboy",
"type": "open-door",
"door": door,
"member_id": member.username,
"member": member.display_name,
"timestamp": datetime.utcnow(),
})
status = "Permitted" if approved else "Denied"
subject = member.display_name
msg = "%s %s door access for %s via Slack bot" % (status, door, subject)
spam(msg)
return "Opening %s for %s" % (door, member.display_name)

View File

@ -22,6 +22,7 @@ db = MongoClient(const.MONGO_URI).get_default_database()
class User:
username: str = None
display_name: str = None
slack_id: str = None
groups: List[str] = field(default_factory=list)
def __getitem__(self, item):
@ -35,10 +36,11 @@ def get_users():
for item in ret["items"]:
username = item['metadata']['name']
display_name = item.get("spec", {}).get("customProfile", {}).get("name", None)
slack_id = item.get("status", {}).get("slackId", None)
groups = []
for group in item.get("status", {}).get("groups", []):
groups.append(f"{group['prefix']}:{group['name']}")
yield User(username, display_name, groups)
yield User(username, display_name, slack_id, groups)
users = list(get_users())
users_lookup = {u.username : u for u in users}