diff --git a/examples/db.py b/examples/db.py new file mode 100644 index 0000000..3c8b1f9 --- /dev/null +++ b/examples/db.py @@ -0,0 +1,35 @@ +import sqlalchemy as sa + + +metadata = sa.Metadata() + + +users = sa.Table( + 'users', metadata, + sa.Column('id', sa.Integer, nullable=False), + sa.Column('login', sa.String(256), nullable=False), + sa.Column('passwd', sa.String(256), nullable=False), + sa.Column('salt', sa.String(256), nullable=False), + sa.Column('is_superuser', sa.Boolean, nullable=False, + server_default='FALSE'), + sa.Column('disabled', sa.Boolean, nullable=False, + server_default='FALSE'), + + # indices + sa.PrimaryKeyConstraint('id', name='user_pkey'), + sa.UniqueConstraint('login', name='user_login_key'), +) + + +permissions = sa.Table( + 'permissions', metadata, + sa.Column('id', sa.Integer, nullable=False), + sa.Column('user_id', sa.Integer, nullable=False), + sa.Column('perm_name', sa.String(64), nullable=False), + + # indices + sa.PrimaryKeyConstraint('id', name='permission_pkey'), + sa.ForeignKeyConstraint(['user_id'], [users.c.id], + name='user_permission_fkey', + ondelete='CASCADE'), +) diff --git a/examples/dict_auth.py b/examples/db_auth.py similarity index 73% rename from examples/dict_auth.py rename to examples/db_auth.py index b28dacd..14b18e1 100644 --- a/examples/dict_auth.py +++ b/examples/db_auth.py @@ -3,9 +3,9 @@ import asyncio from aiohttp_security.authorization import AbstractAuthorizationPolicy -class DictionaryAuthorizationPolicy(AbstractAuthorizationPolicy): - def __init__(self, data): - self.data = data +class DBAuthorizationPolicy(AbstractAuthorizationPolicy): + def __init__(self, db_pool): + self.db_pool = db_pool @asyncio.coroutine def permits(self, identity, permission, context=None): @@ -18,4 +18,6 @@ class DictionaryAuthorizationPolicy(AbstractAuthorizationPolicy): @asyncio.coroutine def authorized_user_id(self, identity): + with (yield from self.db_pool) as conn: + conn return identity if identity in self.data else None diff --git a/requirements-dev.txt b/requirements-dev.txt index 9deb34e..71211ee 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -7,3 +7,4 @@ sphinx alabaster>=0.6.2 pep257 aiohttp_session +aiopg[sa]