From 5dcf3752474a822cf0efdf246ef3862c3c628457 Mon Sep 17 00:00:00 2001 From: Izalia Mae Date: Mon, 4 Mar 2024 01:00:57 -0500 Subject: [PATCH] create admin whitelist page --- relay/frontend/page/admin-instances.haml | 2 +- relay/frontend/page/admin-whitelist.haml | 30 ++++++++++++++++++++++-- relay/frontend/style/whitelist.css | 21 +++++++++++++++++ relay/views/frontend.py | 30 +++++++++++++++++++++++- 4 files changed, 79 insertions(+), 4 deletions(-) diff --git a/relay/frontend/page/admin-instances.haml b/relay/frontend/page/admin-instances.haml index c366acc..ae26d7a 100644 --- a/relay/frontend/page/admin-instances.haml +++ b/relay/frontend/page/admin-instances.haml @@ -25,7 +25,7 @@ %tr %td.instance << Instance %td.software << Software - %td << Joined + %td.date << Joined %td.remove %tbody diff --git a/relay/frontend/page/admin-whitelist.haml b/relay/frontend/page/admin-whitelist.haml index 083c8bb..be31f0d 100644 --- a/relay/frontend/page/admin-whitelist.haml +++ b/relay/frontend/page/admin-whitelist.haml @@ -1,5 +1,31 @@ -extends "base.haml" -set page="Whitelist" -block content - .section - UvU + %details.section + %summary << Add Domain + %form(target="/admin/whitelist", method="POST") + #add-domain + %label(for="domain") << Domain + %input(type="domain", id="domain" name="domain", placeholder="Domain") + + %input(type="submit" value="Add Domain") + + #whitelist.section + %table + %thead + %tr + %td.domain << Domain + %td.date << Added + %td.remove + + %tbody + -for item in whitelist + %tr + %td.domain + =item.domain + + %td.date + =item.created.strftime("%Y-%m-%d") + + %td.remove + %a(href="/admin/whitelist/delete/{{item.domain}}" title="Remove whitlisted domain") << ✖ diff --git a/relay/frontend/style/whitelist.css b/relay/frontend/style/whitelist.css index e69de29..6d3566a 100644 --- a/relay/frontend/style/whitelist.css +++ b/relay/frontend/style/whitelist.css @@ -0,0 +1,21 @@ +form input[type="submit"] { + display: block; + margin: 0 auto; +} + +#whitelist .domain{ + width: 100%; +} + +#whitelist .date { + width: max-content; + text-align: right; +} + +#add-domain { + display: grid; + grid-template-columns: max-content auto; + grid-gap: var(--spacing); + margin-top: var(--spacing); + margin-bottom: var(--spacing); +} diff --git a/relay/views/frontend.py b/relay/views/frontend.py index 089751d..550528c 100644 --- a/relay/views/frontend.py +++ b/relay/views/frontend.py @@ -193,7 +193,7 @@ class AdminWhitelist(View): with self.database.session() as conn: context = { - 'domains': tuple(conn.execute('SELECT * FROM whitelist').all()) + 'whitelist': tuple(conn.execute('SELECT * FROM whitelist').all()) } if error: @@ -206,6 +206,34 @@ class AdminWhitelist(View): return Response.new(data, ctype = 'html') + async def post(self, request: Request) -> Response: + data = await request.post() + + if not data['domain']: + return await self.get(request, error = 'Missing domain') + + with self.database.session(True) as conn: + if (ban := conn.get_domain_whitelist(data['domain'])): + return await self.get(request, message = "Domain already in whitelist") + + conn.put_domain_whitelist(data['domain']) + + return await self.get(request, message = "Added/updated domain ban") + + +@register_route('/admin/whitelist/delete/{domain}') +class AdminWhitlistDelete(View): + async def get(self, request: Request, domain: str) -> Response: + with self.database.session() as conn: + if not (conn.get_domain_whitelist(domain)): + msg = 'Whitelisted domain not found' + return await AdminWhitelist.run("GET", request, message = msg) + + conn.del_domain_whitelist(domain) + + return await AdminWhitelist.run("GET", request, message = 'Removed domain from whitelist') + + @register_route('/admin/domain_bans') class AdminDomainBans(View): async def get(self,