added /delete_session

This commit is contained in:
iou1name 2019-09-26 18:44:00 -04:00
parent b24061fb05
commit b174c1d2e8
2 changed files with 41 additions and 16 deletions

View File

@ -336,6 +336,26 @@ async def delete_key(request):
raise web.HTTPFound(location=index_url) raise web.HTTPFound(location=index_url)
@routes.post(config.url_prefix + '/delete_session', name='delete_session')
@auth.auth_required
async def delete_session(request):
"""Allows a user to delete a session ."""
data = await request.post()
async with request.app['pool'].acquire() as conn:
for key, value in data.items():
session_id = key.replace('session-', '', 1)
if not session_id:
continue
if value != 'on':
continue
await conn.execute(
"DELETE FROM user_session "
"WHERE id = $1 AND user_id = $2",
session_id, request['session']['id'])
index_url = request.app.router['index'].url_for()
raise web.HTTPFound(location=index_url)
async def init_app(): async def init_app():
"""Initializes the application.""" """Initializes the application."""
app = web.Application() app = web.Application()

View File

@ -107,9 +107,11 @@
<h2>Active Sessions</h2> <h2>Active Sessions</h2>
<article style="display: none;"> <article style="display: none;">
<hr> <hr>
<form action="{{ request.app.router['delete_session'].url_for() }}" method="POST" enctype="application/x-www-form-urlencoded">
<table id="active_sessions"> <table id="active_sessions">
<thead> <thead>
<tr> <tr>
<th>Session ID</th>
<th>IP Address</th> <th>IP Address</th>
<th>Delete</th> <th>Delete</th>
</tr> </tr>
@ -117,12 +119,15 @@
<tbody> <tbody>
{% for session in active_sessions %} {% for session in active_sessions %}
<tr> <tr>
<td><code>{{ session['id'][:5] }}...{{ session['id'][-5:] }}</code></td>
<td>{{ session['ip_address'] }}</td> <td>{{ session['ip_address'] }}</td>
<td><input aria-label="Delete {{ session['id'][:5] }}" id="session-{{ session['id'][:5] }}" name="session-{{ session['id'][:5] }}" type="checkbox"></td> <td><input aria-label="Delete {{ session['id'][:5] }}...{{ session['id'][-5:] }}" id="session-{{ session['id'] }}" name="session-{{ session['id'] }}" type="checkbox"></td>
</tr> </tr>
{% endfor %} {% endfor %}
</tbody> </tbody>
</table> </table>
<input type="submit" value="Delete">
</form>
</article> </article>
</section> </section>
</main> </main>