Add workshop door opening support
All checks were successful
ci/woodpecker/manual/woodpecker Pipeline was successful

Also changes group access checking
This commit is contained in:
Madis Mägi 2023-08-11 18:16:05 +03:00
parent d37cb302bb
commit cce18b20a7
3 changed files with 28 additions and 8 deletions

View File

@ -5,7 +5,7 @@ from datetime import datetime, timedelta
from functools import wraps from functools import wraps
from pymongo import MongoClient from pymongo import MongoClient
from flask import Blueprint, abort, g, make_response, redirect, render_template, request, jsonify from flask import Blueprint, abort, g, make_response, redirect, render_template, request, jsonify
from common import CustomForm, build_query, flatten, format_name, spam, users from common import CustomForm, build_query, flatten, format_name, spam, users, User
page_api = Blueprint("api", __name__) page_api = Blueprint("api", __name__)
db = MongoClient(const.MONGO_URI).get_default_database() db = MongoClient(const.MONGO_URI).get_default_database()
@ -69,7 +69,13 @@ def view_slack_doorboy():
return "Invalid channel was supplied" return "Invalid channel was supplied"
command = request.form.get("command") command = request.form.get("command")
try: try:
door = {"/open-new-door": "backdoor", "/open-back-door": "backdoor", "/open-front-door": "frontdoor", "/open-ground-door": "grounddoor"}[command] door = {
"/open-new-door": "backdoor",
"/open-back-door": "backdoor",
"/open-front-door": "frontdoor",
"/open-ground-door": "grounddoor",
"/open-workshop-door": "workshopdoor"
}[command]
except KeyError: except KeyError:
return "Invalid command was supplied" return "Invalid command was supplied"
@ -79,7 +85,11 @@ def view_slack_doorboy():
if user.slack_id == request.form.get("user_id"): if user.slack_id == request.form.get("user_id"):
member = user member = user
approved = "k-space:floor" in member.groups if door == "workshopdoor":
access_group = "k-space:workshop"
else:
access_group = "k-space:floor"
approved = access_group in member.groups
db.eventlog.insert_one({ db.eventlog.insert_one({
"method": "slack", "method": "slack",
"approved": approved, "approved": approved,

View File

@ -8,7 +8,7 @@ from wtforms import StringField, IntegerField, SelectField, BooleanField, DateTi
from wtforms.validators import DataRequired from wtforms.validators import DataRequired
import const import const
from common import spam, users_lookup from common import spam, users_lookup, User
from oidc import login_required, read_user from oidc import login_required, read_user
page_doorboy = Blueprint("doorboy", __name__) page_doorboy = Blueprint("doorboy", __name__)
@ -141,8 +141,14 @@ def view_doorboy_hold():
@login_required @login_required
def view_doorboy_open(door): def view_doorboy_open(door):
user = read_user() user = read_user()
if door not in ("grounddoor", "frontdoor", "backdoor"): raise if door not in ("grounddoor", "frontdoor", "backdoor", "workshopdoor"):
approved = user["username"] in users_lookup return "", 400
if door == "workshopdoor":
access_group = "k-space:workshop"
else:
access_group = "k-space:floor"
approved = access_group in users_lookup.get(user["username"], User()).groups
db.eventlog.insert_one({ db.eventlog.insert_one({
"method": "web", "method": "web",
"approved": approved, "approved": approved,
@ -158,7 +164,10 @@ def view_doorboy_open(door):
subject = user["name"] subject = user["name"]
msg = "%s %s door access for %s via https://inventory.k-space.ee/m/doorboy" % (status, door, subject) msg = "%s %s door access for %s via https://inventory.k-space.ee/m/doorboy" % (status, door, subject)
spam(msg) spam(msg)
if approved:
return redirect("/m/doorboy") return redirect("/m/doorboy")
else:
return "", 401
@page_doorboy.route("/m/doorboy/slam", methods=["POST"]) @page_doorboy.route("/m/doorboy/slam", methods=["POST"])
@ -179,6 +188,7 @@ def view_doorboy_slam():
@login_required @login_required
def view_doorboy(): def view_doorboy():
user = read_user() user = read_user()
workshop_access = "k-space:workshop" in users_lookup.get(user["username"], User()).groups
latest_events = db.eventlog.find({"component": "doorboy", "type":"open-door"}).sort([("timestamp", -1)]).limit(10); latest_events = db.eventlog.find({"component": "doorboy", "type":"open-door"}).sort([("timestamp", -1)]).limit(10);
latest_swipes = db.inventory.find({"component": "doorboy", "type":"token"}).sort([("last_seen", -1)]).limit(10); latest_swipes = db.inventory.find({"component": "doorboy", "type":"token"}).sort([("last_seen", -1)]).limit(10);
return render_template("doorboy.html", **locals()) return render_template("doorboy.html", **locals())

View File

@ -8,8 +8,8 @@
<li><a class="waves-effect waves-light btn" href="/m/doorboy/grounddoor/open">Ground door</a> the one on street level facing KBFI</li> <li><a class="waves-effect waves-light btn" href="/m/doorboy/grounddoor/open">Ground door</a> the one on street level facing KBFI</li>
<li><a class="waves-effect waves-light btn" href="/m/doorboy/frontdoor/open">Front door</a> the one from ground door 5 floors upward</li> <li><a class="waves-effect waves-light btn" href="/m/doorboy/frontdoor/open">Front door</a> the one from ground door 5 floors upward</li>
<li><a class="waves-effect waves-light btn" href="/m/doorboy/backdoor/open">Back door</a> on 5th floor on the Pancake cafeteria side. Note: ground door on cafeteria side is open whenever the cafeteria is open. Other times use the ground door listed above.</li> <li><a class="waves-effect waves-light btn" href="/m/doorboy/backdoor/open">Back door</a> on 5th floor on the Pancake cafeteria side. Note: ground door on cafeteria side is open whenever the cafeteria is open. Other times use the ground door listed above.</li>
<li><a class="waves-effect waves-light btn {% if not workshop_access%}disabled{%endif%}" href="/m/doorboy/workshopdoor/open">Workshop door</a> also known as the dirty room</li>
<!-- <!--
<li>Coming soon: <a class="waves-effect waves-light btn" href="/m/doorboy/workshop/open">Workshop door</a> also known as the dirty room</li>
<li>Coming later: <a class="waves-effect waves-light btn" href="/m/doorboy/server/open">Server room door</a></li> <li>Coming later: <a class="waves-effect waves-light btn" href="/m/doorboy/server/open">Server room door</a></li>
--> -->
</ul> </ul>