Skip to content

Commit

Permalink
Add admin UI for listing API keys
Browse files Browse the repository at this point in the history
🐣 This also adds a means for invalidation the cache.
  • Loading branch information
mmulich committed Feb 8, 2016
1 parent dad07e5 commit 715fc8c
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 1 deletion.
10 changes: 9 additions & 1 deletion cnxpublishing/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ def declare_api_routes(config):
add_route('moderate', '/moderations/{id}')
add_route('moderation-rss', '/feeds/moderations.rss')

# API Key routes
add_route('api-keys', '/api-keys')
add_route('api-key', '/api-keys/{id}')


def declare_browsable_routes(config):
"""Declaration of routes that can be browsed by users."""
Expand All @@ -71,6 +75,7 @@ def declare_browsable_routes(config):
add_route = config.add_route
add_route('admin-index', '/a/')
add_route('admin-moderation', '/a/moderation/')
add_route('admin-api-keys', '/a/api-keys/')


def declare_routes(config):
Expand Down Expand Up @@ -147,7 +152,10 @@ class RootFactory(object):
)),
(security.Allow, 'g:reviewers', ('preview',)),
(security.Allow, 'g:moderators', ('preview', 'moderate',)),
(security.Allow, 'g:administrators', ('preview', 'moderate',)),
(security.Allow, 'g:administrators',
('preview',
'moderate',
'administer')),
security.DENY_ALL,
)

Expand Down
18 changes: 18 additions & 0 deletions cnxpublishing/templates/api-keys.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{% extends "base.html" %}
{% block content %}
<h1>API Keys</h1>
<table>
<tr>
<th>Key</th>
<th>Name</th>
<th>Groups</th>
</tr>
{% for info in api_keys %}
<tr id="{{ info.id ~ '-row' }}">
<td>{{ info.key }}</td>
<td>{{ info.name }}</td>
<td>{{ ', '.join(info.groups) }}</td>
</tr>
{% endfor %}
</table>
{% endblock %}
42 changes: 42 additions & 0 deletions cnxpublishing/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -604,6 +604,32 @@ def post_moderation(request):

return httpexceptions.HTTPAccepted()

# ############ #
# API Keys #
# ############ #

@view_config(route_name='api-keys', request_method='GET',
accept="application/json",
renderer='json', permission='administer')
def get_api_keys(request):
"""Return the list of API keys."""
settings = request.registry.settings

with psycopg2.connect(settings[config.CONNECTION_STRING]) as db_conn:
with db_conn.cursor() as cursor:
cursor.execute("""\
SELECT row_to_json(combined_rows) FROM (
SELECT id, key, name, groups FROM api_keys
) AS combined_rows""")
api_keys = [x[0] for x in cursor.fetchall()]

return api_keys

# TODO Add CRUD views for API Keys...

# ################### #
# Admin Interface #
# ################### #

@view_config(route_name='admin-index', request_method='GET',
renderer="cnxpublishing:templates/index.html",
Expand All @@ -614,6 +640,9 @@ def admin_index(request): # pragma: no cover
{'name': 'Moderation List',
'uri': request.route_url('admin-moderation'),
},
{'name': 'API Keys',
'uri': request.route_url('admin-api-keys'),
},
],
}

Expand All @@ -626,3 +655,16 @@ def admin_index(request): # pragma: no cover
permission='view')
def admin_moderations(request): # pragma: no cover
return {'moderations': get_moderation(request)}


@view_config(route_name='admin-api-keys', request_method='GET',
renderer="cnxpublishing:templates/api-keys.html",
permission='administer')
def admin_api_keys(request): # pragma: no cover
# Easter Egg that will invalidate the cache, just hit this page.
# FIXME Move this logic into the C[R]UD views...
from .authnz import lookup_api_key_info
from .main import cache
cache.invalidate(lookup_api_key_info)

return {'api_keys': get_api_keys(request)}

0 comments on commit 715fc8c

Please sign in to comment.